diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-01-13 18:24:06 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-01-13 18:25:23 -0800 |
commit | 503e6779973e95417de1729c726e1fbedd291947 (patch) | |
tree | 55e96ea3c958789625cc98e9b94a703fa8b5057f /src | |
parent | cca4d2b10054906d1603c6a4b7fba91bd495fb50 (diff) | |
download | binaryen-503e6779973e95417de1729c726e1fbedd291947.tar.gz binaryen-503e6779973e95417de1729c726e1fbedd291947.tar.bz2 binaryen-503e6779973e95417de1729c726e1fbedd291947.zip |
functions must be parsed after all their targets are known
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm-binary.h | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 908121543..09291157e 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -871,6 +871,8 @@ public: readDataSegments(); readFunctionTable(); readEnd(); + + processFunctions(); } uint8_t getInt8() { @@ -1046,15 +1048,12 @@ public: addLocals(f32); addLocals(f64); } - size_t pre = pos; size_t size = getInt16(); - { - nextLabel = 0; - assert(breakStack.empty()); - readExpression(func->body); - assert(breakStack.empty()); - } - assert(pos = pre + size); + // we can't read the function yet - it might call other functions that are defined later, + // and we do depend on the function type, as well as the mappedFunctions table. + functions.emplace_back(func, pos, size); + pos += size; + func->body = nullptr; // will be filled later. but we do have the name and the type already. wasm.addFunction(func); } if (export_) { @@ -1066,6 +1065,26 @@ public: } } + struct FunctionData { + Function* func; + size_t pos, size; + FunctionData(Function* func, size_t pos, size_t size) : func(func), pos(pos), size(size) {} + }; + + std::vector<FunctionData> functions; + + void processFunctions() { + for (auto& func : functions) { + Function* curr = func.func; + pos = func.pos; + nextLabel = 0; + assert(breakStack.empty()); + readExpression(curr->body); + assert(breakStack.empty()); + assert(pos == func.pos + func.size); + } + } + void readDataSegments() { if (debug) std::cerr << "== readDataSegments" << std::endl; verifyInt8(BinaryConsts::DataSegments); |