diff options
-rwxr-xr-x | auto_update_tests.py | 4 | ||||
-rwxr-xr-x | check.py | 19 | ||||
-rw-r--r-- | src/js/wasm.js-post.js | 11 | ||||
-rw-r--r-- | src/tools/asm2wasm.cpp | 24 | ||||
-rw-r--r-- | test/emcc_O2_hello_world.fromasm | 2 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm | 2 | ||||
-rw-r--r-- | test/hello_world.fromasm | 2 | ||||
-rw-r--r-- | test/memorygrowth.fromasm | 2 | ||||
-rw-r--r-- | test/min.fromasm | 2 | ||||
-rw-r--r-- | test/two_sides.fromasm | 2 | ||||
-rw-r--r-- | test/unit.fromasm | 2 |
11 files changed, 62 insertions, 10 deletions
diff --git a/auto_update_tests.py b/auto_update_tests.py index 56c0d831d..447340886 100755 --- a/auto_update_tests.py +++ b/auto_update_tests.py @@ -18,6 +18,10 @@ for asm in sorted(os.listdir('test')): if not opts: cmd += ['--no-opts'] wasm += '.no-opts' + if precise and opts: + # test mem init importing + open('a.mem', 'wb').write(asm) + cmd += ['--mem-init=a.mem'] print '..', asm, wasm print ' ', ' '.join(cmd) actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() @@ -203,7 +203,7 @@ def binary_format_check(wast, verify_final_result=True): assert os.path.exists('ab.wast') # make sure it is a valid wast - cmd = [os.path.join('bin', 'wasm-shell'), 'ab.wast'] + cmd = [os.path.join('bin', 'wasm-opt'), 'ab.wast'] print ' ', ' '.join(cmd) subprocess.check_call(cmd, stdout=subprocess.PIPE) @@ -291,6 +291,10 @@ for asm in tests: if not opts: cmd += ['--no-opts'] wasm += '.no-opts' + if precise and opts: + # test mem init importing + open('a.mem', 'wb').write(asm) + cmd += ['--mem-init=a.mem'] wasm = os.path.join('test', wasm) print '..', asm, wasm actual = run_command(cmd) @@ -676,8 +680,9 @@ if has_emcc: print '\n[ checking wasm.js methods... ]\n' - for method_init in ['interpret-asm2wasm', 'interpret-s-expr', 'asmjs', 'interpret-binary']: - for success in [1, 0]: + for method_init in ['interpret-asm2wasm', 'interpret-s-expr', 'asmjs', 'interpret-binary', 'asmjs,interpret-binary', 'interpret-binary,asmjs']: + # check success and failure for simple modes, only success for combined/fallback ones + for success in [1, 0] if ',' not in method_init else [1]: method = method_init command = ['emcc', '-o', 'a.wasm.js', '-s', 'BINARYEN=1', os.path.join('test', 'hello_world.c') ] command += ['-s', 'BINARYEN_METHOD="' + method + '"'] @@ -696,20 +701,20 @@ if has_emcc: asm = asm.replace('"almost asm"', '"use asm"; var not_in_asm = [].length + (true || { x: 5 }.x);') asm = asm.replace("'almost asm'", '"use asm"; var not_in_asm = [].length + (true || { x: 5 }.x);') with open('a.wasm.asm.js', 'w') as o: o.write(asm) - if method == 'interpret-asm2wasm': + if method.startswith('interpret-asm2wasm'): os.unlink('a.wasm.wast') # we should not need the .wast if not success: break_cashew() # we need cashew - elif method == 'interpret-s-expr': + elif method.startswith('interpret-s-expr'): os.unlink('a.wasm.asm.js') # we should not need the .asm.js if not success: os.unlink('a.wasm.wast') - elif method == 'asmjs': + elif method.startswith('asmjs'): os.unlink('a.wasm.wast') # we should not need the .wast break_cashew() # we don't use cashew, so ok to break it if not success: os.unlink('a.wasm.js') - elif method == 'interpret-binary': + elif method.startswith('interpret-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: diff --git a/src/js/wasm.js-post.js b/src/js/wasm.js-post.js index 1c207b3f3..5a1de4c9e 100644 --- a/src/js/wasm.js-post.js +++ b/src/js/wasm.js-post.js @@ -29,6 +29,7 @@ function integrateWasmJS(Module) { // inputs var method = Module['wasmJSMethod'] || {{{ wasmJSMethod }}} || 'native-wasm,interpret-s-expr'; // by default, try native and then .wast + Module['wasmJSMethod'] = method; var wasmTextFile = Module['wasmTextFile'] || {{{ wasmTextFile }}}; var wasmBinaryFile = Module['wasmBinaryFile'] || {{{ wasmBinaryFile }}}; @@ -100,10 +101,12 @@ function integrateWasmJS(Module) { } var oldView = new Int8Array(oldBuffer); var newView = new Int8Array(newBuffer); - if ({{{ WASM_BACKEND }}}) { - // memory segments arrived in the wast, do not trample them + + // If we have a mem init file, do not trample it + if (!memoryInitializer) { oldView.set(newView.subarray(STATIC_BASE, STATIC_BASE + STATIC_BUMP), STATIC_BASE); } + newView.set(oldView); updateGlobalBuffer(newBuffer); updateGlobalBufferViews(); @@ -215,6 +218,10 @@ function integrateWasmJS(Module) { info.global = global; info.env = env; + if (!('memInitBase' in env)) { + env['memInitBase'] = STATIC_BASE; // tell the memory segments where to place themselves + } + wasmJS['providedTotalMemory'] = Module['buffer'].byteLength; // Prepare to generate wasm, using either asm2wasm or s-exprs diff --git a/src/tools/asm2wasm.cpp b/src/tools/asm2wasm.cpp index beacc884c..af2cf6f5a 100644 --- a/src/tools/asm2wasm.cpp +++ b/src/tools/asm2wasm.cpp @@ -21,6 +21,7 @@ #include "support/colors.h" #include "support/command-line.h" #include "support/file.h" +#include "wasm-builder.h" #include "wasm-printing.h" #include "asm2wasm.h" @@ -40,10 +41,14 @@ int main(int argc, const char *argv[]) { o->extra["output"] = argument; Colors::disable(); }) - .add("--mapped-globals", "-m", "Mapped globals", Options::Arguments::One, + .add("--mapped-globals", "-n", "Mapped globals", Options::Arguments::One, [](Options *o, const std::string &argument) { std::cerr << "warning: the --mapped-globals/-m option is deprecated (a mapped globals file is no longer needed as we use wasm globals)" << std::endl; }) + .add("--mem-init", "-t", "Import a memory initialization file into the output module", Options::Arguments::One, + [](Options *o, const std::string &argument) { + o->extra["mem init"] = argument; + }) .add("--total-memory", "-m", "Total memory size", Options::Arguments::One, [](Options *o, const std::string &argument) { o->extra["total memory"] = argument; @@ -91,6 +96,23 @@ int main(int argc, const char *argv[]) { Asm2WasmBuilder asm2wasm(wasm, pre.memoryGrowth, options.debug, imprecise, opts); asm2wasm.processAsm(asmjs); + // import mem init file, if provided + const auto &memInit = options.extra.find("mem init"); + if (memInit != options.extra.end()) { + auto filename = memInit->second.c_str(); + auto data(read_file<std::vector<char>>(filename, Flags::Binary, options.debug ? Flags::Debug : Flags::Release)); + // the mem init's base is imported + auto* import = new Import; + import->name = Name("memInitBase"); + import->module = Name("env"); + import->base = Name("memInitBase"); + import->kind = Import::Global; + import->globalType = i32; + wasm.addImport(import); + // create the memory segment + wasm.memory.segments.emplace_back(Builder(wasm).makeGetGlobal(import->name, import->globalType), data); + } + if (options.debug) std::cerr << "printing..." << std::endl; Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release); WasmPrinter::printModule(&wasm, output.getStream()); diff --git a/test/emcc_O2_hello_world.fromasm b/test/emcc_O2_hello_world.fromasm index efbb9d052..5c6c31dfa 100644 --- a/test/emcc_O2_hello_world.fromasm +++ b/test/emcc_O2_hello_world.fromasm @@ -1,5 +1,6 @@ (module (memory 256 256) + (data (get_global $memInitBase) "emcc_O2_hello_world.asm.js") (type $FUNCSIG$iiii (func (param i32 i32 i32) (result i32))) (type $FUNCSIG$iii (func (param i32 i32) (result i32))) (type $FUNCSIG$ii (func (param i32) (result i32))) @@ -29,6 +30,7 @@ (import $___syscall140 "env" "___syscall140" (param i32 i32) (result i32)) (import $___syscall146 "env" "___syscall146" (param i32 i32) (result i32)) (import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32)) + (import $memInitBase global "env" "memInitBase" i32) (export "_free" $_free) (export "_main" $_main) (export "_memset" $_memset) diff --git a/test/emcc_hello_world.fromasm b/test/emcc_hello_world.fromasm index 9c533ee9d..14d0b8824 100644 --- a/test/emcc_hello_world.fromasm +++ b/test/emcc_hello_world.fromasm @@ -1,5 +1,6 @@ (module (memory 256 256) + (data (get_global $memInitBase) "emcc_hello_world.asm.js") (type $FUNCSIG$iiii (func (param i32 i32 i32) (result i32))) (type $FUNCSIG$id (func (param f64) (result i32))) (type $FUNCSIG$iii (func (param i32 i32) (result i32))) @@ -38,6 +39,7 @@ (import $i32s-rem "asm2wasm" "i32s-rem" (param i32 i32) (result i32)) (import $i32u-rem "asm2wasm" "i32u-rem" (param i32 i32) (result i32)) (import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32)) + (import $memInitBase global "env" "memInitBase" i32) (export "_i64Subtract" $_i64Subtract) (export "_free" $_free) (export "_main" $_main) diff --git a/test/hello_world.fromasm b/test/hello_world.fromasm index 675ef17ca..0aae371f0 100644 --- a/test/hello_world.fromasm +++ b/test/hello_world.fromasm @@ -1,5 +1,7 @@ (module (memory 256 256) + (data (get_global $memInitBase) "hello_world.asm.js") + (import $memInitBase global "env" "memInitBase" i32) (export "add" $add) (export "memory" memory) (func $add (param $0 i32) (param $1 i32) (result i32) diff --git a/test/memorygrowth.fromasm b/test/memorygrowth.fromasm index 3cec8a5a3..36e9590ed 100644 --- a/test/memorygrowth.fromasm +++ b/test/memorygrowth.fromasm @@ -1,5 +1,6 @@ (module (memory 256 256) + (data (get_global $memInitBase) "memorygrowth.asm.js") (type $FUNCSIG$iiii (func (param i32 i32 i32) (result i32))) (type $FUNCSIG$iii (func (param i32 i32) (result i32))) (type $FUNCSIG$ii (func (param i32) (result i32))) @@ -25,6 +26,7 @@ (import $xa "env" "___unlock" (param i32)) (import $ya "env" "___syscall146" (param i32 i32) (result i32)) (import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32)) + (import $memInitBase global "env" "memInitBase" i32) (export "_free" $fb) (export "_main" $Na) (export "_pthread_self" $ib) diff --git a/test/min.fromasm b/test/min.fromasm index 9c0d90097..9135999dd 100644 --- a/test/min.fromasm +++ b/test/min.fromasm @@ -1,6 +1,8 @@ (module (memory 256 256) + (data (get_global $memInitBase) "min.asm.js") (import $tDP global "env" "tempDoublePtr" i32) + (import $memInitBase global "env" "memInitBase" i32) (export "floats" $floats) (export "memory" memory) (func $floats (param $0 f32) (result f32) diff --git a/test/two_sides.fromasm b/test/two_sides.fromasm index 9d114dc8c..0eed39325 100644 --- a/test/two_sides.fromasm +++ b/test/two_sides.fromasm @@ -1,7 +1,9 @@ (module (memory 256 256) + (data (get_global $memInitBase) "two_sides.asm.js") (type $FUNCSIG$id (func (param f64) (result i32))) (import $f64-to-int "asm2wasm" "f64-to-int" (param f64) (result i32)) + (import $memInitBase global "env" "memInitBase" i32) (export "_test" $_test) (export "memory" memory) (func $_test (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32) diff --git a/test/unit.fromasm b/test/unit.fromasm index 049b4dc8a..530761b62 100644 --- a/test/unit.fromasm +++ b/test/unit.fromasm @@ -1,5 +1,6 @@ (module (memory 256 256) + (data (get_global $memInitBase) "unit.asm.js") (type $FUNCSIG$id (func (param f64) (result i32))) (type $FUNCSIG$ddd (func (param f64 f64) (result f64))) (type $FUNCSIG$iii (func (param i32 i32) (result i32))) @@ -18,6 +19,7 @@ (import $f64-to-int "asm2wasm" "f64-to-int" (param f64) (result i32)) (import $f64-rem "asm2wasm" "f64-rem" (param f64 f64) (result f64)) (import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32)) + (import $memInitBase global "env" "memInitBase" i32) (export "big_negative" $big_negative) (export "pick" $big_negative) (export "memory" memory) |