From f02c4903db7368a108067723e2ab66a8bc60b7b7 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Sat, 12 Mar 2016 14:09:58 -0800 Subject: refactor wasm.js and prepare for loading and interpreting binaries --- src/wasm-js.cpp | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'src/wasm-js.cpp') diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp index 000092da8..6a2b4aedd 100644 --- a/src/wasm-js.cpp +++ b/src/wasm-js.cpp @@ -26,6 +26,7 @@ #include "asm2wasm.h" #include "wasm-interpreter.h" #include "wasm-s-parser.h" +#include "wasm-binary.h" #include "wasm-printing.h" using namespace cashew; @@ -100,6 +101,20 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_asm2wasm(char *input) { } } +void finalizeModule() { + uint32_t providedMemory = EM_ASM_INT_V({ + return Module['providedTotalMemory']; // we receive the size of memory from emscripten + }); + if (providedMemory & ~Memory::kPageMask) { + std::cerr << "Error: provided memory is not a multiple of the 64k wasm page size\n"; + exit(EXIT_FAILURE); + } + module->memory.initial = providedMemory / Memory::kPageSize; + module->memory.max = (module->exportsMap.find(GROW_WASM_MEMORY) != module->exportsMap.end()) ? -1 : module->memory.initial; + + // global mapping is done in js in post.js +} + // loads wasm code in s-expression format extern "C" void EMSCRIPTEN_KEEPALIVE load_s_expr2wasm(char *input, char *mappedGlobals) { prepare2wasm(); @@ -119,17 +134,25 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_s_expr2wasm(char *input, char *mappedG abort(); }); - uint32_t providedMemory = EM_ASM_INT_V({ - return Module['providedTotalMemory']; // we receive the size of memory from emscripten - }); - if (providedMemory & ~Memory::kPageMask) { - std::cerr << "Error: provided memory is not a multiple of the 64k wasm page size\n"; - exit(EXIT_FAILURE); + finalizeModule(); +} + +// loads wasm code in binary format +extern "C" void EMSCRIPTEN_KEEPALIVE load_binary2wasm(char *raw, int32_t size, char *mappedGlobals) { + prepare2wasm(); + + if (wasmJSDebug) std::cerr << "wasm-binary parsing...\n"; + + module = new AllocatingModule(); + std::vector input; + input.resize(size); + for (int32_t i = 0; i < size; i++) { + input[i] = raw[i]; } - module->memory.initial = providedMemory / Memory::kPageSize; - module->memory.max = (module->exportsMap.find(GROW_WASM_MEMORY) != module->exportsMap.end()) ? -1 : module->memory.initial; + WasmBinaryBuilder parser(*module, input, debug); + parser.read(); - // global mapping is done in js in post.js + finalizeModule(); } // instantiates the loaded wasm (which might be from asm2wasm, or -- cgit v1.2.3 From 6b215997b21fdf86c430fc184909804af7b44069 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Sat, 12 Mar 2016 16:27:09 -0800 Subject: finish wasm-binary method and update emscripten --- bin/wasm.js | 182 ++++++++++++++++++++++++------------------------- check.py | 9 ++- src/js/wasm.js-post.js | 11 ++- src/wasm-js.cpp | 4 +- test/emscripten | 2 +- 5 files changed, 108 insertions(+), 100 deletions(-) (limited to 'src/wasm-js.cpp') diff --git a/bin/wasm.js b/bin/wasm.js index 382003d2e..22d499f67 100644 --- a/bin/wasm.js +++ b/bin/wasm.js @@ -57210,67 +57210,6 @@ function __ZN4wasm17WasmBinaryBuilder8getInt16Ev(i1) { return i1 & 65535 | 0; } -function _load_binary2wasm(i5, i6, i1) { - i5 = i5 | 0; - i6 = i6 | 0; - i1 = i1 | 0; - var i2 = 0, i3 = 0, i4 = 0; - i4 = STACKTOP; - STACKTOP = STACKTOP + 112 | 0; - i2 = i4 + 100 | 0; - i3 = i4; - __ZL12prepare2wasmv(); - if (HEAP8[46196] | 0) __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc(43392, 14946) | 0; - i1 = __Znwj(164) | 0; - _memset(i1 | 0, 0, 164) | 0; - __ZN4wasm16AllocatingModuleC2Ev(i1); - HEAP32[10548] = i1; - HEAP32[i2 >> 2] = 0; - HEAP32[i2 + 4 >> 2] = 0; - HEAP32[i2 + 8 >> 2] = 0; - __ZNSt3__16vectorIcNS_9allocatorIcEEE6resizeEj(i2, i6); - i1 = 0; - while (1) { - if ((i1 | 0) >= (i6 | 0)) break; - HEAP8[(HEAP32[i2 >> 2] | 0) + i1 >> 0] = HEAP8[i5 + i1 >> 0] | 0; - i1 = i1 + 1 | 0; - } - i6 = HEAP32[10548] | 0; - i5 = (HEAP32[10543] | 0) != 0; - HEAP32[i3 >> 2] = i6; - HEAP32[i3 + 4 >> 2] = i6 + 148; - HEAP32[i3 + 8 >> 2] = i2; - HEAP8[i3 + 12 >> 0] = i5 & 1; - i5 = i3 + 16 | 0; - i6 = i3 + 36 | 0; - HEAP32[i3 + 64 >> 2] = 0; - HEAP32[i3 + 68 >> 2] = 0; - HEAP32[i5 >> 2] = 0; - HEAP32[i5 + 4 >> 2] = 0; - HEAP32[i5 + 8 >> 2] = 0; - HEAP32[i5 + 12 >> 2] = 0; - HEAP32[i6 >> 2] = 0; - HEAP32[i6 + 4 >> 2] = 0; - HEAP32[i6 + 8 >> 2] = 0; - HEAP32[i6 + 12 >> 2] = 0; - HEAP32[i6 + 16 >> 2] = 0; - HEAP32[i6 + 20 >> 2] = 0; - HEAP32[i3 + 60 >> 2] = i3 + 64; - i6 = i3 + 72 | 0; - HEAP32[i6 >> 2] = 0; - HEAP32[i6 + 4 >> 2] = 0; - HEAP32[i6 + 8 >> 2] = 0; - HEAP32[i6 + 12 >> 2] = 0; - HEAP32[i6 + 16 >> 2] = 0; - HEAP32[i6 + 20 >> 2] = 0; - __ZN4wasm17WasmBinaryBuilder4readEv(i3); - __Z14finalizeModulev(); - __ZN4wasm17WasmBinaryBuilderD2Ev(i3); - __ZNSt3__113__vector_baseIcNS_9allocatorIcEEED2Ev(i2); - STACKTOP = i4; - return; -} - function __ZN4wasm17WasmBinaryBuilder8getInt32Ev(i1) { i1 = i1 | 0; var i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0; @@ -57304,6 +57243,66 @@ function __ZN4wasm17WasmBinaryBuilder8getInt32Ev(i1) { return i1 | 0; } +function _load_binary2wasm(i4, i5) { + i4 = i4 | 0; + i5 = i5 | 0; + var i1 = 0, i2 = 0, i3 = 0, i6 = 0; + i6 = STACKTOP; + STACKTOP = STACKTOP + 112 | 0; + i2 = i6 + 100 | 0; + i3 = i6; + __ZL12prepare2wasmv(); + if (HEAP8[46196] | 0) __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc(43392, 14946) | 0; + i1 = __Znwj(164) | 0; + _memset(i1 | 0, 0, 164) | 0; + __ZN4wasm16AllocatingModuleC2Ev(i1); + HEAP32[10548] = i1; + HEAP32[i2 >> 2] = 0; + HEAP32[i2 + 4 >> 2] = 0; + HEAP32[i2 + 8 >> 2] = 0; + __ZNSt3__16vectorIcNS_9allocatorIcEEE6resizeEj(i2, i5); + i1 = 0; + while (1) { + if ((i1 | 0) >= (i5 | 0)) break; + HEAP8[(HEAP32[i2 >> 2] | 0) + i1 >> 0] = HEAP8[i4 + i1 >> 0] | 0; + i1 = i1 + 1 | 0; + } + i5 = HEAP32[10548] | 0; + i4 = (HEAP32[10543] | 0) != 0; + HEAP32[i3 >> 2] = i5; + HEAP32[i3 + 4 >> 2] = i5 + 148; + HEAP32[i3 + 8 >> 2] = i2; + HEAP8[i3 + 12 >> 0] = i4 & 1; + i4 = i3 + 16 | 0; + i5 = i3 + 36 | 0; + HEAP32[i3 + 64 >> 2] = 0; + HEAP32[i3 + 68 >> 2] = 0; + HEAP32[i4 >> 2] = 0; + HEAP32[i4 + 4 >> 2] = 0; + HEAP32[i4 + 8 >> 2] = 0; + HEAP32[i4 + 12 >> 2] = 0; + HEAP32[i5 >> 2] = 0; + HEAP32[i5 + 4 >> 2] = 0; + HEAP32[i5 + 8 >> 2] = 0; + HEAP32[i5 + 12 >> 2] = 0; + HEAP32[i5 + 16 >> 2] = 0; + HEAP32[i5 + 20 >> 2] = 0; + HEAP32[i3 + 60 >> 2] = i3 + 64; + i5 = i3 + 72 | 0; + HEAP32[i5 >> 2] = 0; + HEAP32[i5 + 4 >> 2] = 0; + HEAP32[i5 + 8 >> 2] = 0; + HEAP32[i5 + 12 >> 2] = 0; + HEAP32[i5 + 16 >> 2] = 0; + HEAP32[i5 + 20 >> 2] = 0; + __ZN4wasm17WasmBinaryBuilder4readEv(i3); + __Z14finalizeModulev(); + __ZN4wasm17WasmBinaryBuilderD2Ev(i3); + __ZNSt3__113__vector_baseIcNS_9allocatorIcEEED2Ev(i2); + STACKTOP = i6; + return; +} + function __ZN4wasm22SExpressionWasmBuilder14makeBreakTableERNS_7ElementE(i11, i10) { i11 = i11 | 0; i10 = i10 | 0; @@ -61775,37 +61774,6 @@ function __ZN6cashew6ParserINS_3RefENS_19DotZeroValueBuilderEE9skipSpaceERPc(i4) return; } -function _load_s_expr2wasm(i3, i1) { - i3 = i3 | 0; - i1 = i1 | 0; - var i2 = 0, i4 = 0, i5 = 0; - i4 = STACKTOP; - STACKTOP = STACKTOP + 32 | 0; - i2 = i4; - __ZL12prepare2wasmv(); - if (HEAP8[46196] | 0) __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc(43392, 14916) | 0; - i1 = __Znwj(24) | 0; - __ZN4wasm17SExpressionParserC2EPc(i1, i3); - HEAP32[10545] = i1; - i1 = HEAP32[i1 + 20 >> 2] | 0; - if (HEAP8[46196] | 0 ? (__ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_c(__ZN4wasmlsERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEERNS_7ElementE(43308, i1) | 0, 10) | 0, HEAP8[46196] | 0) : 0) __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc(43392, 14637) | 0; - i5 = __Znwj(164) | 0; - _memset(i5 | 0, 0, 164) | 0; - __ZN4wasm16AllocatingModuleC2Ev(i5); - HEAP32[10548] = i5; - i3 = __Znwj(104) | 0; - i1 = __ZN4wasm7Element4listEv(i1) | 0; - i1 = HEAP32[HEAP32[i1 >> 2] >> 2] | 0; - HEAP32[i2 + 16 >> 2] = i2; - HEAP32[i2 >> 2] = 3620; - __ZN4wasm22SExpressionWasmBuilderC2ERNS_16AllocatingModuleERNS_7ElementENSt3__18functionIFvvEEEb(i3, i5, i1, i2, 0); - HEAP32[10546] = i3; - __ZNSt3__18functionIFvvEED2Ev(i2); - __Z14finalizeModulev(); - STACKTOP = i4; - return; -} - function __ZNSt3__112basic_stringIwNS_11char_traitsIwEENS_9allocatorIwEEE6appendIPwEENS_9enable_ifIXsr21__is_forward_iteratorIT_EE5valueERS5_E4typeES9_S9_(i9, i5, i8) { i9 = i9 | 0; i5 = i5 | 0; @@ -62142,6 +62110,36 @@ function __ZN4wasm17WasmBinaryBuilder11visitSelectEPNS_6SelectE(i3, i2) { return; } +function _load_s_expr2wasm(i1) { + i1 = i1 | 0; + var i2 = 0, i3 = 0, i4 = 0, i5 = 0; + i3 = STACKTOP; + STACKTOP = STACKTOP + 32 | 0; + i2 = i3; + __ZL12prepare2wasmv(); + if (HEAP8[46196] | 0) __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc(43392, 14916) | 0; + i4 = __Znwj(24) | 0; + __ZN4wasm17SExpressionParserC2EPc(i4, i1); + HEAP32[10545] = i4; + i1 = HEAP32[i4 + 20 >> 2] | 0; + if (HEAP8[46196] | 0 ? (__ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_c(__ZN4wasmlsERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEERNS_7ElementE(43308, i1) | 0, 10) | 0, HEAP8[46196] | 0) : 0) __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc(43392, 14637) | 0; + i5 = __Znwj(164) | 0; + _memset(i5 | 0, 0, 164) | 0; + __ZN4wasm16AllocatingModuleC2Ev(i5); + HEAP32[10548] = i5; + i4 = __Znwj(104) | 0; + i1 = __ZN4wasm7Element4listEv(i1) | 0; + i1 = HEAP32[HEAP32[i1 >> 2] >> 2] | 0; + HEAP32[i2 + 16 >> 2] = i2; + HEAP32[i2 >> 2] = 3620; + __ZN4wasm22SExpressionWasmBuilderC2ERNS_16AllocatingModuleERNS_7ElementENSt3__18functionIFvvEEEb(i4, i5, i1, i2, 0); + HEAP32[10546] = i4; + __ZNSt3__18functionIFvvEED2Ev(i2); + __Z14finalizeModulev(); + STACKTOP = i3; + return; +} + function __ZNSt3__16vectorIN4wasm7LiteralENS_9allocatorIS2_EEE21__push_back_slow_pathIRKS2_EEvOT_(i7, i6) { i7 = i7 | 0; i6 = i6 | 0; diff --git a/check.py b/check.py index ff85449c7..e42bae2a4 100755 --- a/check.py +++ b/check.py @@ -586,7 +586,7 @@ if has_emcc: print '\n[ checking wasm.js methods... ]\n' - for method_init in [None, 'asm2wasm', 'wasm-s-parser', 'just-asm']: + for method_init in [None, 'asm2wasm', 'wasm-s-parser', 'just-asm', 'wasm-binary']: for success in [1, 0]: method = method_init command = ['emcc', '-o', 'a.wasm.js', '-s', 'BINARYEN=1', os.path.join('test', 'hello_world.c') ] @@ -598,7 +598,7 @@ if has_emcc: subprocess.check_call(command) see_polyfill = 'var WasmJS = ' in open('a.wasm.js').read() - if method and 'asm2wasm' not in method and 'wasm-s-parser' not in method: + if method and 'asm2wasm' not in method and 'wasm-s-parser' not in method and 'wasm-binary' not in method: assert not see_polyfill, 'verify polyfill was not added - we specified a method, and it does not need it' else: assert see_polyfill, 'we need the polyfill' @@ -621,6 +621,11 @@ if has_emcc: break_cashew() # we don't use cashew, so ok to break it if not success: os.unlink('a.wasm.js') + elif method == 'wasm-binary': + os.unlink('a.wasm.wast') # we should not need the .wast + os.unlink('a.wasm.asm.js') # we should not need the .asm.js + if not success: + os.unlink('a.wasm.wasm') else: 1/0 if has_node: diff --git a/src/js/wasm.js-post.js b/src/js/wasm.js-post.js index 3d476c62d..12e9e5315 100644 --- a/src/js/wasm.js-post.js +++ b/src/js/wasm.js-post.js @@ -236,14 +236,19 @@ function integrateWasmJS(Module) { } else { code = Module['read'](method == 'asm2wasm' ? asmjsCodeFile : wasmTextFile); } - var temp = wasmJS['_malloc'](code.length + 1); - wasmJS['writeAsciiToMemory'](code, temp); + var temp; if (method == 'asm2wasm') { + temp = wasmJS['_malloc'](code.length + 1); + wasmJS['writeAsciiToMemory'](code, temp); wasmJS['_load_asm2wasm'](temp); } else if (method === 'wasm-s-parser') { + temp = wasmJS['_malloc'](code.length + 1); + wasmJS['writeAsciiToMemory'](code, temp); wasmJS['_load_s_expr2wasm'](temp); } else if (method === 'wasm-binary') { - wasmJS['_load_binary2wasm'](temp); + temp = wasmJS['_malloc'](code.length); + wasmJS['HEAPU8'].set(code, temp); + wasmJS['_load_binary2wasm'](temp, code.length); } else { throw 'what? ' + method; } diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp index 6a2b4aedd..3083b3b9e 100644 --- a/src/wasm-js.cpp +++ b/src/wasm-js.cpp @@ -116,7 +116,7 @@ void finalizeModule() { } // loads wasm code in s-expression format -extern "C" void EMSCRIPTEN_KEEPALIVE load_s_expr2wasm(char *input, char *mappedGlobals) { +extern "C" void EMSCRIPTEN_KEEPALIVE load_s_expr2wasm(char *input) { prepare2wasm(); if (wasmJSDebug) std::cerr << "wasm-s-expression parsing...\n"; @@ -138,7 +138,7 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_s_expr2wasm(char *input, char *mappedG } // loads wasm code in binary format -extern "C" void EMSCRIPTEN_KEEPALIVE load_binary2wasm(char *raw, int32_t size, char *mappedGlobals) { +extern "C" void EMSCRIPTEN_KEEPALIVE load_binary2wasm(char *raw, int32_t size) { prepare2wasm(); if (wasmJSDebug) std::cerr << "wasm-binary parsing...\n"; diff --git a/test/emscripten b/test/emscripten index 90a4391ba..b4e694961 160000 --- a/test/emscripten +++ b/test/emscripten @@ -1 +1 @@ -Subproject commit 90a4391bab7211dafdffe4e7c04979edb93660ee +Subproject commit b4e694961408bc530155161998f0165e5d4c8ba9 -- cgit v1.2.3