From 649bbfc66fff32bf2bb34eec2106e28a4adef585 Mon Sep 17 00:00:00 2001 From: "Alon Zakai (kripken)" Date: Sun, 1 Jan 2017 16:10:38 -0800 Subject: handle a module which has no globals at all --- src/wasm/wasm-binary.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 55f85b4b5..26da1487f 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -906,7 +906,7 @@ void WasmBinaryBuilder::read() { // imports can read global imports, so we run getGlobalName and create the mapping // but after we read globals, we need to add the internal globals too, so do that here mappedGlobals.clear(); // wipe the mapping - getGlobalName(0); // force rebuild + getGlobalName(-1); // force rebuild break; } case BinaryConsts::Section::Data: readDataSegments(); break; @@ -1358,6 +1358,7 @@ Name WasmBinaryBuilder::getGlobalName(Index index) { mappedGlobals[index] = wasm.globals[i]->name; } } + if (index == Index(-1)) return Name("null"); // just a force-rebuild assert(mappedGlobals.count(index)); return mappedGlobals[index]; } -- cgit v1.2.3 From 9be46c626ce03d2967c817a3f8f2ffce0bc06a26 Mon Sep 17 00:00:00 2001 From: "Alon Zakai (kripken)" Date: Sun, 1 Jan 2017 16:21:19 -0800 Subject: handle a binary that breaks to return --- src/wasm-binary.h | 7 ++++++- src/wasm/wasm-binary.cpp | 27 ++++++++++++++++++++++----- test/break-to-return.wasm | Bin 0 -> 51 bytes test/break-to-return.wasm.fromBinary | 16 ++++++++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 test/break-to-return.wasm create mode 100644 test/break-to-return.wasm.fromBinary (limited to 'src') diff --git a/src/wasm-binary.h b/src/wasm-binary.h index bc4a225df..3f5b799fe 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -695,8 +695,13 @@ public: Expression* readExpression(); void readGlobals(); - struct BreakTarget { Name name; int arity;}; + struct BreakTarget { + Name name; + int arity; + BreakTarget(Name name, int arity) : name(name), arity(arity) {} + }; std::vector breakStack; + bool breaksToReturn; // whether a break is done to the function scope, which is in effect a return std::vector expressionStack; diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 26da1487f..65bf0a268 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -879,6 +879,9 @@ void WasmBinaryWriter::visitDrop(Drop *curr) { o << int8_t(BinaryConsts::Drop); } +// reader + +static Name RETURN_BREAK("binaryen|break-to-return"); void WasmBinaryBuilder::read() { @@ -1244,15 +1247,22 @@ void WasmBinaryBuilder::readFunctions() { // process the function body if (debug) std::cerr << "processing function: " << i << std::endl; nextLabel = 0; + breaksToReturn = false; // process body assert(breakStack.empty()); + breakStack.emplace_back(RETURN_BREAK, func->result != none); // the break target for the function scope assert(expressionStack.empty()); assert(depth == 0); func->body = getMaybeBlock(func->result); assert(depth == 0); - assert(breakStack.empty()); + assert(breakStack.size() == 1); + breakStack.pop_back(); assert(expressionStack.empty()); assert(pos == endOfFunction); + if (breaksToReturn) { + // we broke to return, so we need an outer block to break to + func->body = Builder(wasm).blockifyWithName(func->body, RETURN_BREAK); + } } currFunction = nullptr; functions.push_back(func); @@ -1609,10 +1619,17 @@ void WasmBinaryBuilder::visitLoop(Loop *curr) { } WasmBinaryBuilder::BreakTarget WasmBinaryBuilder::getBreakTarget(int32_t offset) { - if (debug) std::cerr << "getBreakTarget "<