From 0b8acfc0139652d1c0f284c7650812a309574586 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 5 May 2017 09:42:31 -0700 Subject: ctor-eval fixes (#996) * fix wasm-ctor-eval, we need to look for the STACKTOP etc. imports, they may not be named, if this build is not with -g * pack memory after ctor evalling, since we merge it up which is less efficient * do some useful opts after ctor-evalling, to clean things up --- src/tools/wasm-ctor-eval.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/tools/wasm-ctor-eval.cpp') diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index 160e4a4f2..67c9f9707 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -32,6 +32,7 @@ #include "wasm-interpreter.h" #include "wasm-builder.h" #include "ast/memory-utils.h" +#include "ast/global-utils.h" using namespace wasm; @@ -153,10 +154,14 @@ public: // prepare scratch memory stack.resize(STACK_SIZE); // fill usable values for stack imports - auto total = STACK_START + STACK_SIZE; - globals["STACKTOP"] = Literal(int32_t(STACK_START)); - globals["STACK_MAX"] = Literal(int32_t(STACK_START + STACK_SIZE)); + if (auto* stackTop = GlobalUtils::getGlobalInitializedToImport(wasm, "env", "STACKTOP")) { + globals[stackTop->name] = Literal(int32_t(STACK_START)); + } + if (auto* stackMax = GlobalUtils::getGlobalInitializedToImport(wasm, "env", "STACK_MAX")) { + globals[stackMax->name] = Literal(int32_t(STACK_START)); + } // tell the module to accept writes up to the stack end + auto total = STACK_START + STACK_SIZE; memorySize = total / Memory::kPageSize; } @@ -398,6 +403,17 @@ int main(int argc, const char* argv[]) { } evalCtors(wasm, ctors); + // Do some useful optimizations after the evalling + { + PassRunner passRunner(&wasm); + passRunner.add("memory-packing"); // we flattened it, so re-optimize + passRunner.add("remove-unused-names"); + passRunner.add("dce"); + passRunner.add("merge-blocks"); + passRunner.add("vacuum"); + passRunner.run(); + } + if (options.extra.count("output") > 0) { if (options.debug) std::cerr << "writing..." << std::endl; ModuleWriter writer; -- cgit v1.2.3