diff options
author | Alon Zakai <azakai@google.com> | 2022-08-26 09:44:48 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-26 09:44:48 -0700 |
commit | 47b29f9ab6caff2070860a60e656ec99239f7521 (patch) | |
tree | e0dfafa03ed95089c73787209abbd45e9f44ecc3 | |
parent | 8b81405778eb85fccbfdbe789beeba5108cf1021 (diff) | |
download | binaryen-47b29f9ab6caff2070860a60e656ec99239f7521.tar.gz binaryen-47b29f9ab6caff2070860a60e656ec99239f7521.tar.bz2 binaryen-47b29f9ab6caff2070860a60e656ec99239f7521.zip |
[NFC] Simplify binary reading logic for functions (#4969)
We do a call to updateMaps() at the end of processNames anyhow, and so we
may as well call addFunction immediately (and the names will get fixed up in that
updateMaps later). The old code for some reason did that for function imports, but
not normal functions. It also stored them separately in temporary storage for some
unclear reason...
-rw-r--r-- | src/wasm-binary.h | 5 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 27 |
2 files changed, 9 insertions, 23 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index e787baa9d..f6924aa14 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -1511,11 +1511,6 @@ public: // We read functions and globals before we know their names, so we need to // backpatch the names later - // we store functions here before wasm.addFunction after we know their names - std::vector<Function*> functions; - // we store function imports here before wasm.addFunctionImport after we know - // their names - std::vector<Function*> functionImports; // at index i we have all refs to the function i std::map<Index, std::vector<Name*>> functionRefs; Function* currFunction = nullptr; diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index f8f3d9fbe..685da0bf1 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -2288,7 +2288,6 @@ void WasmBinaryBuilder::readImports() { auto curr = builder.makeFunction(name, type, {}); curr->module = module; curr->base = base; - functionImports.push_back(curr.get()); wasm.addFunction(std::move(curr)); break; } @@ -2420,8 +2419,9 @@ Signature WasmBinaryBuilder::getSignatureByFunctionIndex(Index index) { void WasmBinaryBuilder::readFunctions() { BYN_TRACE("== readFunctions\n"); + auto numImports = wasm.functions.size(); size_t total = getU32LEB(); - if (total != functionTypes.size() - functionImports.size()) { + if (total != functionTypes.size() - numImports) { throwError("invalid function section size, must equal types"); } for (size_t i = 0; i < total; i++) { @@ -2435,7 +2435,7 @@ void WasmBinaryBuilder::readFunctions() { auto* func = new Function; func->name = Name::fromInt(i); - func->type = getTypeByFunctionIndex(functionImports.size() + i); + func->type = getTypeByFunctionIndex(numImports + i); currFunction = func; if (DWARF) { @@ -2470,7 +2470,7 @@ void WasmBinaryBuilder::readFunctions() { // the form of pthread-related segment initializations. As this is just // one function, it doesn't add significant time, so the optimization of // skipping bodies is still very useful. - auto currFunctionIndex = functionImports.size() + functions.size(); + auto currFunctionIndex = wasm.functions.size(); bool isStart = startIndex == currFunctionIndex; if (!skipFunctionBodies || isStart) { func->body = getBlockOrSingleton(func->getResults()); @@ -2503,7 +2503,7 @@ void WasmBinaryBuilder::readFunctions() { std::swap(func->epilogLocation, debugLocation); currFunction = nullptr; debugLocation.clear(); - functions.push_back(func); + wasm.addFunction(func); } BYN_TRACE(" end function bodies\n"); } @@ -2961,9 +2961,6 @@ void WasmBinaryBuilder::validateBinary() { } void WasmBinaryBuilder::processNames() { - for (auto* func : functions) { - wasm.addFunction(func); - } for (auto& global : globals) { wasm.addGlobal(std::move(global)); } @@ -3277,11 +3274,8 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) { auto index = getU32LEB(); auto rawName = getInlineString(); auto name = processor.process(rawName); - auto numFunctionImports = functionImports.size(); - if (index < numFunctionImports) { - functionImports[index]->setExplicitName(name); - } else if (index - numFunctionImports < functions.size()) { - functions[index - numFunctionImports]->setExplicitName(name); + if (index < wasm.functions.size()) { + wasm.functions[index]->setExplicitName(name); } else { std::cerr << "warning: function index out of bounds in name section, " "function subsection: " @@ -3291,14 +3285,11 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) { } } else if (nameType == BinaryConsts::UserSections::Subsection::NameLocal) { auto numFuncs = getU32LEB(); - auto numFunctionImports = functionImports.size(); for (size_t i = 0; i < numFuncs; i++) { auto funcIndex = getU32LEB(); Function* func = nullptr; - if (funcIndex < numFunctionImports) { - func = functionImports[funcIndex]; - } else if (funcIndex - numFunctionImports < functions.size()) { - func = functions[funcIndex - numFunctionImports]; + if (funcIndex < wasm.functions.size()) { + func = wasm.functions[funcIndex].get(); } else { std::cerr << "warning: function index out of bounds in name section, local " |