summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHeejin Ahn <aheejin@gmail.com>2019-06-04 13:31:55 -0700
committerGitHub <noreply@github.com>2019-06-04 13:31:55 -0700
commit14e09685f1994327dec5aec3a7fd5349696600af (patch)
tree5548738e595032d5a1452883aa3fc2ec306848e2 /src
parent9d4f053070fa4a0d61663901d5206c025d694bb2 (diff)
downloadbinaryen-14e09685f1994327dec5aec3a7fd5349696600af.tar.gz
binaryen-14e09685f1994327dec5aec3a7fd5349696600af.tar.bz2
binaryen-14e09685f1994327dec5aec3a7fd5349696600af.zip
Use BinaryIndexes instead of copies in BinaryWriter (NFC) (#2161)
Diffstat (limited to 'src')
-rw-r--r--src/ir/module-utils.h1
-rw-r--r--src/wasm-binary.h11
-rw-r--r--src/wasm/wasm-binary.cpp30
3 files changed, 13 insertions, 29 deletions
diff --git a/src/ir/module-utils.h b/src/ir/module-utils.h
index 067f59416..c13e9f1ca 100644
--- a/src/ir/module-utils.h
+++ b/src/ir/module-utils.h
@@ -19,7 +19,6 @@
#include "ir/find_all.h"
#include "ir/manipulation.h"
-#include "wasm-binary.h"
#include "wasm.h"
namespace wasm {
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index e2323967e..ea1a8ad82 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -28,6 +28,7 @@
#include "asm_v_wasm.h"
#include "asmjs/shared-constants.h"
#include "ir/import-utils.h"
+#include "ir/module-utils.h"
#include "parsing.h"
#include "wasm-builder.h"
#include "wasm-traversal.h"
@@ -902,7 +903,7 @@ inline S32LEB binaryType(Type type) {
class WasmBinaryWriter {
public:
WasmBinaryWriter(Module* input, BufferWithRandomAccess& o, bool debug = false)
- : wasm(input), o(o), debug(debug) {
+ : wasm(input), o(o), debug(debug), indexes(*input) {
prepare();
}
@@ -951,13 +952,6 @@ public:
void writeDataSegments();
void writeEvents();
- // name of the Function => index. first imports, then internals
- std::unordered_map<Name, Index> mappedFunctions;
- // name of the Global => index. first imported globals, then internal globals
- std::unordered_map<Name, uint32_t> mappedGlobals;
- // name of the Event => index. first imported events, then internal events
- std::unordered_map<Name, uint32_t> mappedEvents;
-
uint32_t getFunctionIndex(Name name);
uint32_t getGlobalIndex(Name name);
uint32_t getEventIndex(Name name);
@@ -1003,6 +997,7 @@ private:
Module* wasm;
BufferWithRandomAccess& o;
bool debug;
+ ModuleUtils::BinaryIndexes indexes;
bool debugInfo = true;
std::ostream* sourceMap = nullptr;
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index 91a2184e9..63321cffe 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -17,7 +17,6 @@
#include <algorithm>
#include <fstream>
-#include "ir/module-utils.h"
#include "support/bits.h"
#include "wasm-binary.h"
#include "wasm-stack.h"
@@ -34,11 +33,6 @@ void WasmBinaryWriter::prepare() {
// https://github.com/WebAssembly/spec/pull/301 might want this:
// assert(!func->type.isNull());
}
- ModuleUtils::BinaryIndexes indexes(*wasm);
- mappedFunctions = std::move(indexes.functionIndexes);
- mappedGlobals = std::move(indexes.globalIndexes);
- mappedEvents = std::move(indexes.eventIndexes);
-
importInfo = wasm::make_unique<ImportInfo>(*wasm);
}
@@ -458,18 +452,18 @@ void WasmBinaryWriter::writeDataSegments() {
}
uint32_t WasmBinaryWriter::getFunctionIndex(Name name) {
- assert(mappedFunctions.count(name));
- return mappedFunctions[name];
+ assert(indexes.functionIndexes.count(name));
+ return indexes.functionIndexes[name];
}
uint32_t WasmBinaryWriter::getGlobalIndex(Name name) {
- assert(mappedGlobals.count(name));
- return mappedGlobals[name];
+ assert(indexes.globalIndexes.count(name));
+ return indexes.globalIndexes[name];
}
uint32_t WasmBinaryWriter::getEventIndex(Name name) {
- assert(mappedEvents.count(name));
- return mappedEvents[name];
+ assert(indexes.eventIndexes.count(name));
+ return indexes.eventIndexes[name];
}
void WasmBinaryWriter::writeFunctionTableDeclaration() {
@@ -534,14 +528,10 @@ void WasmBinaryWriter::writeEvents() {
}
void WasmBinaryWriter::writeNames() {
- bool hasContents = false;
- if (wasm->functions.size() > 0) {
- hasContents = true;
- getFunctionIndex(wasm->functions[0]->name); // generate mappedFunctions
- }
- if (!hasContents) {
+ if (wasm->functions.empty()) {
return;
}
+
if (debug) {
std::cerr << "== writeNames" << std::endl;
}
@@ -549,7 +539,7 @@ void WasmBinaryWriter::writeNames() {
writeInlineString(BinaryConsts::UserSections::Name);
auto substart =
startSubsection(BinaryConsts::UserSections::Subsection::NameFunction);
- o << U32LEB(mappedFunctions.size());
+ o << U32LEB(indexes.functionIndexes.size());
Index emitted = 0;
auto add = [&](Function* curr) {
o << U32LEB(emitted);
@@ -558,7 +548,7 @@ void WasmBinaryWriter::writeNames() {
};
ModuleUtils::iterImportedFunctions(*wasm, add);
ModuleUtils::iterDefinedFunctions(*wasm, add);
- assert(emitted == mappedFunctions.size());
+ assert(emitted == indexes.functionIndexes.size());
finishSubsection(substart);
/* TODO: locals */
finishSection(start);