diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-28 12:17:52 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-28 12:59:22 -0800 |
commit | 6d36e9640efa7bbefc05e4db0fc6c97d79053c9b (patch) | |
tree | 7bab27202fda1d6b67888d607ad43d697a0b6d21 | |
parent | 86491ff1fb2a7bebc357b28db74385785c0644ef (diff) | |
download | binaryen-6d36e9640efa7bbefc05e4db0fc6c97d79053c9b.tar.gz binaryen-6d36e9640efa7bbefc05e4db0fc6c97d79053c9b.tar.bz2 binaryen-6d36e9640efa7bbefc05e4db0fc6c97d79053c9b.zip |
refactor wasm.js so that it will be able to support multiple wasm loading methods
-rw-r--r-- | src/asm2wasm-main.cpp | 4 | ||||
-rw-r--r-- | src/asm2wasm.h | 30 | ||||
-rw-r--r-- | src/binaryen-shell.cpp | 4 | ||||
-rw-r--r-- | src/js/post.js | 5 | ||||
-rw-r--r-- | src/shared-constants.h | 60 | ||||
-rw-r--r-- | src/wasm-js.cpp | 31 | ||||
-rw-r--r-- | src/wasm-s-parser.h | 26 |
7 files changed, 99 insertions, 61 deletions
diff --git a/src/asm2wasm-main.cpp b/src/asm2wasm-main.cpp index d31413695..c8a5a4c30 100644 --- a/src/asm2wasm-main.cpp +++ b/src/asm2wasm-main.cpp @@ -7,6 +7,10 @@ using namespace cashew; using namespace wasm; +namespace wasm { +int debug = 0; +} + int main(int argc, char **argv) { debug = getenv("ASM2WASM_DEBUG") ? getenv("ASM2WASM_DEBUG")[0] - '0' : 0; diff --git a/src/asm2wasm.h b/src/asm2wasm.h index 7f7be358b..ed376f576 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -7,42 +7,16 @@ #include "wasm.h" #include "emscripten-optimizer/optimizer.h" #include "mixed_arena.h" +#include "shared-constants.h" namespace wasm { using namespace cashew; -int debug = 0; // wasm::debug is set in main(), typically from an env var +extern int debug; // wasm::debug is set in main(), typically from an env var // Utilities -IString GLOBAL("global"), NAN_("NaN"), INFINITY_("Infinity"), - TOPMOST("topmost"), - INT8ARRAY("Int8Array"), - INT16ARRAY("Int16Array"), - INT32ARRAY("Int32Array"), - UINT8ARRAY("Uint8Array"), - UINT16ARRAY("Uint16Array"), - UINT32ARRAY("Uint32Array"), - FLOAT32ARRAY("Float32Array"), - FLOAT64ARRAY("Float64Array"), - IMPOSSIBLE_CONTINUE("impossible-continue"), - MATH("Math"), - IMUL("imul"), - CLZ32("clz32"), - FROUND("fround"), - ASM2WASM("asm2wasm"), - F64_REM("f64-rem"), - F64_TO_INT("f64-to-int"), - GLOBAL_MATH("global.Math"), - ABS("abs"), - FLOOR("floor"), - SQRT("sqrt"), - I32_TEMP("asm2wasm_i32_temp"), - DEBUGGER("debugger"), - GROW_WASM_MEMORY("__growWasmMemory"), - NEW_SIZE("newSize"); - static void abort_on(std::string why) { std::cerr << why << '\n'; abort(); diff --git a/src/binaryen-shell.cpp b/src/binaryen-shell.cpp index 197de545b..5f6b3c6db 100644 --- a/src/binaryen-shell.cpp +++ b/src/binaryen-shell.cpp @@ -16,6 +16,10 @@ using namespace cashew; using namespace wasm; +namespace wasm { +int debug = 0; +} + // Globals MixedArena globalAllocator; diff --git a/src/js/post.js b/src/js/post.js index 596831595..72cd836ba 100644 --- a/src/js/post.js +++ b/src/js/post.js @@ -84,6 +84,7 @@ function integrateWasmJS(Module) { // Generate a module instance of the asm.js converted into wasm. var code = Module['read'](Module['asmjsCodeFile']); + // TODO: support wasm s-expression loading here // wasm code would create its own buffer, at this time. But static init code might have // written to the buffer already, and we must copy it over. We could just avoid @@ -106,9 +107,11 @@ function integrateWasmJS(Module) { var temp = wasmJS['_malloc'](code.length + 1); wasmJS['writeAsciiToMemory'](code, temp); - wasmJS['_load_asm'](temp); + wasmJS['_load_asm2wasm'](temp); wasmJS['_free'](temp); + wasmJS['_instantiate'](temp); + // write the provided data to a location the wasm instance can get at it. info.global = global; info.env = env; diff --git a/src/shared-constants.h b/src/shared-constants.h new file mode 100644 index 000000000..8d7cbd888 --- /dev/null +++ b/src/shared-constants.h @@ -0,0 +1,60 @@ +#ifndef _shared_constants_h_ +#define _shared_constants_h_ + +#include "emscripten-optimizer/optimizer.h" + +namespace wasm { + +cashew::IString GLOBAL("global"), + NAN_("NaN"), + INFINITY_("Infinity"), + TOPMOST("topmost"), + INT8ARRAY("Int8Array"), + INT16ARRAY("Int16Array"), + INT32ARRAY("Int32Array"), + UINT8ARRAY("Uint8Array"), + UINT16ARRAY("Uint16Array"), + UINT32ARRAY("Uint32Array"), + FLOAT32ARRAY("Float32Array"), + FLOAT64ARRAY("Float64Array"), + IMPOSSIBLE_CONTINUE("impossible-continue"), + MATH("Math"), + IMUL("imul"), + CLZ32("clz32"), + FROUND("fround"), + ASM2WASM("asm2wasm"), + F64_REM("f64-rem"), + F64_TO_INT("f64-to-int"), + GLOBAL_MATH("global.Math"), + ABS("abs"), + FLOOR("floor"), + SQRT("sqrt"), + I32_TEMP("asm2wasm_i32_temp"), + DEBUGGER("debugger"), + GROW_WASM_MEMORY("__growWasmMemory"), + NEW_SIZE("newSize"), + MODULE("module"), + FUNC("func"), + PARAM("param"), + RESULT("result"), + MEMORY("memory"), + SEGMENT("segment"), + EXPORT("export"), + IMPORT("import"), + TABLE("table"), + LOCAL("local"), + TYPE("type"), + CALL("call"), + CALL_IMPORT("call_import"), + CALL_INDIRECT("call_indirect"), + BR_IF("br_if"), + NEG_INFINITY("-infinity"), + NEG_NAN("-nan"), + CASE("case"), + BR("br"), + FAKE_RETURN("fake_return_waka123"); + +} + +#endif // _shared_constants_h_ + diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp index 561dc83ae..93e5b9205 100644 --- a/src/wasm-js.cpp +++ b/src/wasm-js.cpp @@ -10,28 +10,34 @@ #include "asm2wasm.h" #include "wasm-interpreter.h" +#include "wasm-s-parser.h" using namespace cashew; using namespace wasm; +namespace wasm { +int debug = 0; +} + // global singletons Asm2WasmBuilder* asm2wasm = nullptr; ModuleInstance* instance = nullptr; AllocatingModule* module = nullptr; bool wasmJSDebug = false; -// receives asm.js code, parses into wasm and returns an instance handle. -// this creates a module, an external interface, a builder, and a module instance, -// all of which are then the responsibility of the caller to free. -// note: this modifies the input. -extern "C" void EMSCRIPTEN_KEEPALIVE load_asm(char *input) { +static void prepare2wasm() { assert(instance == nullptr); // singleton - #if WASM_JS_DEBUG wasmJSDebug = 1; #else wasmJSDebug = EM_ASM_INT_V({ return !!Module['outside']['WASM_JS_DEBUG'] }); // Set WASM_JS_DEBUG on the outside Module to get debugging #endif +} + +// receives asm.js code, parses into wasm. +// note: this modifies the input. +extern "C" void EMSCRIPTEN_KEEPALIVE load_asm2wasm(char *input) { + prepare2wasm(); Asm2WasmPreProcessor pre; input = pre.process(input); @@ -54,10 +60,21 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_asm(char *input) { if (wasmJSDebug) std::cerr << "optimizing...\n"; asm2wasm->optimize(); +} - if (wasmJSDebug) std::cerr << *module << '\n'; +// loads wasm code in s-expression format +extern "C" void EMSCRIPTEN_KEEPALIVE load_s_expr2wasm(char *input) { + prepare2wasm(); + abort(); +} + +// instantiates the loaded wasm (which might be from asm2wasm, or +// s-expressions, or something else) with a JS external interface. +extern "C" void EMSCRIPTEN_KEEPALIVE instantiate() { + if (wasmJSDebug) std::cerr << "instantiating module: \n" << *module << '\n'; if (wasmJSDebug) std::cerr << "generating exports...\n"; + EM_ASM({ Module['asmExports'] = {}; }); diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 531341b01..792867981 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -9,38 +9,14 @@ #include "wasm.h" #include "mixed_arena.h" +#include "shared-constants.h" namespace wasm { -int debug = 0; // wasm::debug is set in main(), typically from an env var - using namespace cashew; // Globals -IString MODULE("module"), - FUNC("func"), - PARAM("param"), - RESULT("result"), - MEMORY("memory"), - SEGMENT("segment"), - EXPORT("export"), - IMPORT("import"), - TABLE("table"), - LOCAL("local"), - TYPE("type"), - CALL("call"), - CALL_IMPORT("call_import"), - CALL_INDIRECT("call_indirect"), - BR_IF("br_if"), - INFINITY_("infinity"), - NEG_INFINITY("-infinity"), - NAN_("nan"), - NEG_NAN("-nan"), - CASE("case"), - BR("br"), - FAKE_RETURN("fake_return_waka123"); - int unhex(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; |