summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-08-29 08:17:34 -0700
committerGitHub <noreply@github.com>2022-08-29 08:17:34 -0700
commitfea999ea89f89947cb72a1846ed1743dbe935d09 (patch)
treebe7c5ea933cda5b09878ee3a63ba5b75f3ba623c
parentd5b6972c9963be063d9f8db4ed8c99166c1c7ac5 (diff)
downloadbinaryen-fea999ea89f89947cb72a1846ed1743dbe935d09.tar.gz
binaryen-fea999ea89f89947cb72a1846ed1743dbe935d09.tar.bz2
binaryen-fea999ea89f89947cb72a1846ed1743dbe935d09.zip
[NFC] Simplify binary reading logic for globals (#4980)
Similar to #4969 but for globals.
-rw-r--r--src/wasm-binary.h5
-rw-r--r--src/wasm/wasm-binary.cpp42
2 files changed, 11 insertions, 36 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index 07ae01160..4a4cab13f 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -1540,11 +1540,6 @@ public:
// we store data here after being read from binary, before we know their names
std::vector<std::unique_ptr<DataSegment>> dataSegments;
- // we store globals here before wasm.addGlobal after we know their names
- std::vector<std::unique_ptr<Global>> globals;
- // we store global imports here before wasm.addGlobalImport after we know
- // their names
- std::vector<Global*> globalImports;
// at index i we have all refs to the global i
std::map<Index, std::vector<Name*>> globalRefs;
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index e067ae104..1ea931612 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -2362,7 +2362,6 @@ void WasmBinaryBuilder::readImports() {
mutable_ ? Builder::Mutable : Builder::Immutable);
curr->module = module;
curr->base = base;
- globalImports.push_back(curr.get());
wasm.addGlobal(std::move(curr));
break;
}
@@ -2771,7 +2770,7 @@ void WasmBinaryBuilder::readGlobals() {
throwError("Global mutability must be 0 or 1");
}
auto* init = readExpression();
- globals.push_back(
+ wasm.addGlobal(
Builder::makeGlobal("global$" + std::to_string(i),
type,
init,
@@ -2982,9 +2981,6 @@ void WasmBinaryBuilder::validateBinary() {
}
void WasmBinaryBuilder::processNames() {
- for (auto& global : globals) {
- wasm.addGlobal(std::move(global));
- }
for (auto& segment : elementSegments) {
wasm.addElementSegment(std::move(segment));
}
@@ -3431,11 +3427,8 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
auto index = getU32LEB();
auto rawName = getInlineString();
auto name = processor.process(rawName);
- auto numGlobalImports = globalImports.size();
- if (index < numGlobalImports) {
- globalImports[index]->setExplicitName(name);
- } else if (index - numGlobalImports < globals.size()) {
- globals[index - numGlobalImports]->setExplicitName(name);
+ if (index < wasm.globals.size()) {
+ wasm.globals[index]->setExplicitName(name);
} else {
std::cerr << "warning: global index out of bounds in name section, "
"global subsection: "
@@ -4345,35 +4338,22 @@ void WasmBinaryBuilder::visitLocalSet(LocalSet* curr, uint8_t code) {
void WasmBinaryBuilder::visitGlobalGet(GlobalGet* curr) {
BYN_TRACE("zz node: GlobalGet " << pos << std::endl);
auto index = getU32LEB();
- if (index < globalImports.size()) {
- auto* import = globalImports[index];
- curr->name = import->name;
- curr->type = import->type;
- } else {
- Index adjustedIndex = index - globalImports.size();
- if (adjustedIndex >= globals.size()) {
- throwError("invalid global index");
- }
- auto& glob = globals[adjustedIndex];
- curr->name = glob->name;
- curr->type = glob->type;
+ if (index >= wasm.globals.size()) {
+ throwError("invalid global index");
}
+ auto* global = wasm.globals[index].get();
+ curr->name = global->name;
+ curr->type = global->type;
globalRefs[index].push_back(&curr->name); // we don't know the final name yet
}
void WasmBinaryBuilder::visitGlobalSet(GlobalSet* curr) {
BYN_TRACE("zz node: GlobalSet\n");
auto index = getU32LEB();
- if (index < globalImports.size()) {
- auto* import = globalImports[index];
- curr->name = import->name;
- } else {
- Index adjustedIndex = index - globalImports.size();
- if (adjustedIndex >= globals.size()) {
- throwError("invalid global index");
- }
- curr->name = globals[adjustedIndex]->name;
+ if (index >= wasm.globals.size()) {
+ throwError("invalid global index");
}
+ curr->name = wasm.globals[index]->name;
curr->value = popNonVoidExpression();
globalRefs[index].push_back(&curr->name); // we don't know the final name yet
curr->finalize();