summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2015-11-24 15:07:47 -0800
committerAlon Zakai <alonzakai@gmail.com>2015-11-24 15:09:49 -0800
commit83cd8a4e2618e2883c97fc3a2fe58b544f8bf778 (patch)
treee8cadfd3c00f461e66c5eba2c8781eda1c5345bf
parente97c5c94c14e83ff7535bafb80a9ae0bae332030 (diff)
downloadbinaryen-83cd8a4e2618e2883c97fc3a2fe58b544f8bf778.tar.gz
binaryen-83cd8a4e2618e2883c97fc3a2fe58b544f8bf778.tar.bz2
binaryen-83cd8a4e2618e2883c97fc3a2fe58b544f8bf778.zip
create an AllocatingModule which handles allocation for its elements
-rw-r--r--src/asm2wasm-main.cpp2
-rw-r--r--src/asm2wasm.h6
-rw-r--r--src/binaryen-shell.cpp4
-rw-r--r--src/mixed_arena.h11
-rw-r--r--src/wasm-js.cpp4
-rw-r--r--src/wasm-s-parser.h6
-rw-r--r--test/example/find_div0s.cpp2
7 files changed, 23 insertions, 12 deletions
diff --git a/src/asm2wasm-main.cpp b/src/asm2wasm-main.cpp
index aca9434bb..d31413695 100644
--- a/src/asm2wasm-main.cpp
+++ b/src/asm2wasm-main.cpp
@@ -35,7 +35,7 @@ int main(int argc, char **argv) {
Ref asmjs = builder.parseToplevel(input);
if (debug) std::cerr << "wasming...\n";
- Module wasm;
+ AllocatingModule wasm;
wasm.memory.initial = wasm.memory.max = 16*1024*1024; // we would normally receive this from the compiler
Asm2WasmBuilder asm2wasm(wasm, pre.memoryGrowth);
asm2wasm.processAsm(asmjs);
diff --git a/src/asm2wasm.h b/src/asm2wasm.h
index 6cbb4e084..20ea6f056 100644
--- a/src/asm2wasm.h
+++ b/src/asm2wasm.h
@@ -143,9 +143,9 @@ struct Asm2WasmPreProcessor {
//
class Asm2WasmBuilder {
- Module& wasm;
+ AllocatingModule& wasm;
- MixedArena allocator;
+ MixedArena &allocator;
// globals
@@ -272,7 +272,7 @@ private:
}
public:
- Asm2WasmBuilder(Module& wasm, bool memoryGrowth) : wasm(wasm), nextGlobal(8), maxGlobal(1000), memoryGrowth(memoryGrowth) {}
+ Asm2WasmBuilder(AllocatingModule& wasm, bool memoryGrowth) : wasm(wasm), allocator(wasm.allocator), nextGlobal(8), maxGlobal(1000), memoryGrowth(memoryGrowth) {}
void processAsm(Ref ast);
void optimize();
diff --git a/src/binaryen-shell.cpp b/src/binaryen-shell.cpp
index d54552976..9c1a05447 100644
--- a/src/binaryen-shell.cpp
+++ b/src/binaryen-shell.cpp
@@ -234,7 +234,7 @@ int main(int argc, char **argv) {
size_t i = 0;
while (i < root.size()) {
if (debug) std::cerr << "parsing s-expressions to wasm...\n";
- Module wasm;
+ AllocatingModule wasm;
SExpressionWasmBuilder builder(wasm, *root[i], [&]() { abort(); });
i++;
@@ -280,7 +280,7 @@ int main(int argc, char **argv) {
std::cerr << curr << '\n';
if (id == ASSERT_INVALID) {
// a module invalidity test
- Module wasm;
+ AllocatingModule wasm;
bool invalid = false;
jmp_buf trapState;
std::unique_ptr<SExpressionWasmBuilder> builder;
diff --git a/src/mixed_arena.h b/src/mixed_arena.h
index 4d0d85c67..b2fbe06a6 100644
--- a/src/mixed_arena.h
+++ b/src/mixed_arena.h
@@ -41,5 +41,16 @@ struct MixedArena {
extern MixedArena globalAllocator;
+#ifdef __wasm_h__
+namespace wasm {
+
+class AllocatingModule : public Module {
+public:
+ MixedArena allocator;
+};
+
+}
+#endif
+
#endif // mixed_arena_h
diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp
index d0951f671..561dc83ae 100644
--- a/src/wasm-js.cpp
+++ b/src/wasm-js.cpp
@@ -17,7 +17,7 @@ using namespace wasm;
// global singletons
Asm2WasmBuilder* asm2wasm = nullptr;
ModuleInstance* instance = nullptr;
-Module* module = nullptr;
+AllocatingModule* module = nullptr;
bool wasmJSDebug = false;
// receives asm.js code, parses into wasm and returns an instance handle.
@@ -42,7 +42,7 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_asm(char *input) {
cashew::Parser<Ref, DotZeroValueBuilder> builder;
Ref asmjs = builder.parseToplevel(input);
- module = new Module();
+ module = new AllocatingModule();
module->memory.initial = EM_ASM_INT_V({
return Module['providedTotalMemory']; // we receive the size of memory from emscripten
});
diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h
index 8fbedcbf2..718c459ca 100644
--- a/src/wasm-s-parser.h
+++ b/src/wasm-s-parser.h
@@ -233,15 +233,15 @@ private:
//
class SExpressionWasmBuilder {
- Module& wasm;
- MixedArena allocator;
+ AllocatingModule& wasm;
+ MixedArena& allocator;
std::function<void ()> onError;
int functionCounter;
std::vector<Call*> calls; // we only know call types afterwards, so we set their type in a post-pass
public:
// Assumes control of and modifies the input.
- SExpressionWasmBuilder(Module& wasm, Element& module, std::function<void ()> onError) : wasm(wasm), onError(onError), functionCounter(0) {
+ SExpressionWasmBuilder(AllocatingModule& wasm, Element& module, std::function<void ()> onError) : wasm(wasm), allocator(wasm.allocator), onError(onError), functionCounter(0) {
assert(module[0]->str() == MODULE);
for (unsigned i = 1; i < module.size(); i++) {
parseModuleElement(*module[i]);
diff --git a/test/example/find_div0s.cpp b/test/example/find_div0s.cpp
index 60eed5f62..d09f1de55 100644
--- a/test/example/find_div0s.cpp
+++ b/test/example/find_div0s.cpp
@@ -27,7 +27,7 @@ int main() {
// Parse the S-Expression text, and prepare to build a WebAssembly module.
SExpressionParser parser(input);
Element& root = *parser.root;
- Module module;
+ AllocatingModule module;
// The parsed code has just one element, the module. Build the module
// from that (and abort on any errors, but there won't be one here).