Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensible name section #933

Merged
merged 4 commits into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ enum EncodedType {

namespace UserSections {
extern const char* Name;

enum Subsection {
NameFunction = 1,
NameLocal = 2,
};
}

enum ASTNodes {
Expand Down Expand Up @@ -546,6 +551,8 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
void writeResizableLimits(Address initial, Address maximum, bool hasMaximum);
int32_t startSection(BinaryConsts::Section code);
void finishSection(int32_t start);
int32_t startSubsection(BinaryConsts::UserSections::Subsection code);
void finishSubsection(int32_t start);
void writeStart();
void writeMemory();
void writeTypes();
Expand Down Expand Up @@ -722,7 +729,7 @@ class WasmBinaryBuilder {

void readFunctionTableDeclaration();
void readTableElements();
void readNames();
void readNames(size_t);

// AST reading
int depth = 0; // only for debugging
Expand Down
62 changes: 43 additions & 19 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ void WasmBinaryWriter::finishSection(int32_t start) {
o.writeAt(start, U32LEB(size));
}

int32_t WasmBinaryWriter::startSubsection(BinaryConsts::UserSections::Subsection code) {
o << U32LEB(code);
return writeU32LEBPlaceholder(); // section size to be filled in later
}

void WasmBinaryWriter::finishSubsection(int32_t start) {
int32_t size = o.size() - start - 5; // section size does not include the 5 bytes of the size field itself
o.writeAt(start, U32LEB(size));
}

void WasmBinaryWriter::writeStart() {
if (!wasm->start.is()) return;
if (debug) std::cerr << "== writeStart" << std::endl;
Expand Down Expand Up @@ -386,21 +396,24 @@ void WasmBinaryWriter::writeNames() {
if (debug) std::cerr << "== writeNames" << std::endl;
auto start = startSection(BinaryConsts::Section::User);
writeInlineString(BinaryConsts::UserSections::Name);
auto substart = startSubsection(BinaryConsts::UserSections::Subsection::NameFunction);
o << U32LEB(mappedFunctions.size());
Index emitted = 0;
for (auto& import : wasm->imports) {
if (import->kind == ExternalKind::Function) {
o << U32LEB(emitted);
writeInlineString(import->name.str);
o << U32LEB(0); // TODO: locals
emitted++;
}
}
for (auto& curr : wasm->functions) {
o << U32LEB(emitted);
writeInlineString(curr->name.str);
o << U32LEB(0); // TODO: locals
emitted++;
}
assert(emitted == mappedFunctions.size());
finishSubsection(substart);
/* TODO: locals */
finishSection(start);
}

Expand Down Expand Up @@ -968,7 +981,7 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
auto oldPos = pos;
Name sectionName = getInlineString();
if (sectionName.equals(BinaryConsts::UserSections::Name)) {
readNames();
readNames(payloadLen - (pos - oldPos));
} else {
// an unfamiliar custom section
wasm.userSections.resize(wasm.userSections.size() + 1);
Expand Down Expand Up @@ -1509,25 +1522,36 @@ void WasmBinaryBuilder::readTableElements() {
}
}

void WasmBinaryBuilder::readNames() {
void WasmBinaryBuilder::readNames(size_t payloadLen) {
if (debug) std::cerr << "== readNames" << std::endl;
auto num = getU32LEB();
if (num == 0) return;
for (auto& import : wasm.imports) {
if (import->kind == ExternalKind::Function) {
getInlineString(); // TODO: use this
auto numLocals = getU32LEB();
WASM_UNUSED(numLocals);
assert(numLocals == 0); // TODO
if (--num == 0) return;
auto sectionPos = pos;
while (pos < sectionPos + payloadLen) {
auto nameType = getU32LEB();
auto subsectionSize = getU32LEB();
auto subsectionPos = pos;
if (nameType != BinaryConsts::UserSections::Subsection::NameFunction) {
// TODO: locals
std::cerr << "unknown name subsection at " << pos << std::endl;
pos = subsectionPos + subsectionSize;
continue;
}
auto num = getU32LEB();
uint32_t importedFunctions = 0;
for (auto& import : wasm.imports) {
if (import->kind != ExternalKind::Function) continue;
importedFunctions++;
}
for (size_t i = 0; i < num; i++) {
auto index = getU32LEB();
if (index < importedFunctions) {
getInlineString(); // TODO: use this
} else if (index - importedFunctions < functions.size()) {
functions[index - importedFunctions]->name = getInlineString();
}
}
assert(pos == subsectionPos + subsectionSize);
}
for (size_t i = 0; i < num; i++) {
functions[i]->name = getInlineString();
auto numLocals = getU32LEB();
WASM_UNUSED(numLocals);
assert(numLocals == 0); // TODO
}
assert(pos == sectionPos + payloadLen);
}

BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) {
Expand Down
2 changes: 1 addition & 1 deletion test/example/c-api-unused-mem.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
(call $main)
)
)
207
213
(module
(type $0 (func))
(type $1 (func))
Expand Down