diff options
-rw-r--r-- | src/passes/LegalizeJSInterface.cpp | 43 | ||||
-rw-r--r-- | test/passes/legalize-js-interface.txt | 37 | ||||
-rw-r--r-- | test/passes/legalize-js-interface.wast | 8 | ||||
-rw-r--r-- | test/threads.wasm-only.fromasm | 10 | ||||
-rw-r--r-- | test/threads.wasm-only.fromasm.clamp | 10 | ||||
-rw-r--r-- | test/threads.wasm-only.fromasm.clamp.no-opts | 10 | ||||
-rw-r--r-- | test/threads.wasm-only.fromasm.imprecise | 10 | ||||
-rw-r--r-- | test/threads.wasm-only.fromasm.imprecise.no-opts | 10 | ||||
-rw-r--r-- | test/threads.wasm-only.fromasm.no-opts | 10 | ||||
-rw-r--r-- | test/wasm-only.fromasm | 10 | ||||
-rw-r--r-- | test/wasm-only.fromasm.clamp | 10 | ||||
-rw-r--r-- | test/wasm-only.fromasm.clamp.no-opts | 10 | ||||
-rw-r--r-- | test/wasm-only.fromasm.imprecise | 10 | ||||
-rw-r--r-- | test/wasm-only.fromasm.imprecise.no-opts | 10 | ||||
-rw-r--r-- | test/wasm-only.fromasm.no-opts | 10 |
15 files changed, 208 insertions, 0 deletions
diff --git a/src/passes/LegalizeJSInterface.cpp b/src/passes/LegalizeJSInterface.cpp index 7f6ee6b48..23a4868ac 100644 --- a/src/passes/LegalizeJSInterface.cpp +++ b/src/passes/LegalizeJSInterface.cpp @@ -35,6 +35,8 @@ namespace wasm { Name TEMP_RET_0("tempRet0"); +Name GET_TEMP_RET_0("getTempRet0"); +Name SET_TEMP_RET_0("setTempRet0"); struct LegalizeJSInterface : public Pass { void run(PassRunner* runner, Module* module) override { @@ -99,12 +101,18 @@ struct LegalizeJSInterface : public Pass { passRunner.add<FixImports>(&illegalToLegal); passRunner.run(); } + + if (needTempRet0Helpers) { + addTempRet0Helpers(module); + } } private: // map of illegal to legal names for imports std::map<Name, Name> illegalToLegal; + bool needTempRet0Helpers = false; + template<typename T> bool isIllegal(T* t) { for (auto param : t->params) { @@ -233,6 +241,41 @@ private: LiteralUtils::makeZero(i32, *module), Builder::Mutable )); + needTempRet0Helpers = true; + } + } + + void addTempRet0Helpers(Module* module) { + // We should also let JS access the tempRet0 global, which + // is necessary to send/receive 64-bit return values. + auto exportIt = [&](Function* func) { + auto* export_ = new Export; + export_->name = func->name; + export_->value = func->name; + export_->kind = ExternalKind::Function; + module->addExport(export_); + }; + if (!module->getExportOrNull(GET_TEMP_RET_0)) { + Builder builder(*module); + auto* func = new Function(); + func->name = GET_TEMP_RET_0; + func->result = i32; + func->body = builder.makeGetGlobal(TEMP_RET_0, i32); + module->addFunction(func); + exportIt(func); + } + if (!module->getExportOrNull(SET_TEMP_RET_0)) { + Builder builder(*module); + auto* func = new Function(); + func->name = SET_TEMP_RET_0; + func->result = none; + func->params.push_back(i32); + func->body = builder.makeSetGlobal( + TEMP_RET_0, + builder.makeGetLocal(0, i32) + ); + module->addFunction(func); + exportIt(func); } } }; diff --git a/test/passes/legalize-js-interface.txt b/test/passes/legalize-js-interface.txt new file mode 100644 index 000000000..8f833de87 --- /dev/null +++ b/test/passes/legalize-js-interface.txt @@ -0,0 +1,37 @@ +(module + (type $0 (func (result i64))) + (global $tempRet0 (mut i32) (i32.const 0)) + (export "func" (func $legalstub$func)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) + (func $func (; 0 ;) (type $0) (result i64) + (unreachable) + ) + (func $legalstub$func (; 1 ;) (result i32) + (local $0 i64) + (set_local $0 + (call $func) + ) + (set_global $tempRet0 + (i32.wrap/i64 + (i64.shr_u + (get_local $0) + (i64.const 32) + ) + ) + ) + (i32.wrap/i64 + (get_local $0) + ) + ) + (func $getTempRet0 (; 2 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 3 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) +) +(module +) diff --git a/test/passes/legalize-js-interface.wast b/test/passes/legalize-js-interface.wast new file mode 100644 index 000000000..bae2a8c75 --- /dev/null +++ b/test/passes/legalize-js-interface.wast @@ -0,0 +1,8 @@ +(module + (export "func" (func $func)) + (func $func (result i64) + (unreachable) + ) +) +(module) + diff --git a/test/threads.wasm-only.fromasm b/test/threads.wasm-only.fromasm index 79ef9eb00..f633af016 100644 --- a/test/threads.wasm-only.fromasm +++ b/test/threads.wasm-only.fromasm @@ -4,6 +4,8 @@ (global $tempRet0 (mut i32) (i32.const 0)) (data (get_global $memoryBase) "threads.wasm-only.asm.js") (export "test64" (func $legalstub$test64)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $test64 (; 0 ;) (result i64) (local $0 i64) (local $1 i64) @@ -65,4 +67,12 @@ (get_local $0) ) ) + (func $getTempRet0 (; 2 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 3 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/threads.wasm-only.fromasm.clamp b/test/threads.wasm-only.fromasm.clamp index 79ef9eb00..f633af016 100644 --- a/test/threads.wasm-only.fromasm.clamp +++ b/test/threads.wasm-only.fromasm.clamp @@ -4,6 +4,8 @@ (global $tempRet0 (mut i32) (i32.const 0)) (data (get_global $memoryBase) "threads.wasm-only.asm.js") (export "test64" (func $legalstub$test64)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $test64 (; 0 ;) (result i64) (local $0 i64) (local $1 i64) @@ -65,4 +67,12 @@ (get_local $0) ) ) + (func $getTempRet0 (; 2 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 3 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/threads.wasm-only.fromasm.clamp.no-opts b/test/threads.wasm-only.fromasm.clamp.no-opts index 35b506e2b..971d690f4 100644 --- a/test/threads.wasm-only.fromasm.clamp.no-opts +++ b/test/threads.wasm-only.fromasm.clamp.no-opts @@ -7,6 +7,8 @@ (global $STACKTOP (mut i32) (get_global $STACKTOP$asm2wasm$import)) (global $tempRet0 (mut i32) (i32.const 0)) (export "test64" (func $legalstub$test64)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $test64 (; 0 ;) (result i64) (local $x i64) (local $y i64) @@ -87,4 +89,12 @@ (get_local $0) ) ) + (func $getTempRet0 (; 2 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 3 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/threads.wasm-only.fromasm.imprecise b/test/threads.wasm-only.fromasm.imprecise index ccbb5870f..2c6fa3f18 100644 --- a/test/threads.wasm-only.fromasm.imprecise +++ b/test/threads.wasm-only.fromasm.imprecise @@ -2,6 +2,8 @@ (import "env" "memory" (memory $0 (shared 256 256))) (global $tempRet0 (mut i32) (i32.const 0)) (export "test64" (func $legalstub$test64)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $test64 (; 0 ;) (result i64) (local $0 i64) (local $1 i64) @@ -63,4 +65,12 @@ (get_local $0) ) ) + (func $getTempRet0 (; 2 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 3 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/threads.wasm-only.fromasm.imprecise.no-opts b/test/threads.wasm-only.fromasm.imprecise.no-opts index 35b506e2b..971d690f4 100644 --- a/test/threads.wasm-only.fromasm.imprecise.no-opts +++ b/test/threads.wasm-only.fromasm.imprecise.no-opts @@ -7,6 +7,8 @@ (global $STACKTOP (mut i32) (get_global $STACKTOP$asm2wasm$import)) (global $tempRet0 (mut i32) (i32.const 0)) (export "test64" (func $legalstub$test64)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $test64 (; 0 ;) (result i64) (local $x i64) (local $y i64) @@ -87,4 +89,12 @@ (get_local $0) ) ) + (func $getTempRet0 (; 2 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 3 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/threads.wasm-only.fromasm.no-opts b/test/threads.wasm-only.fromasm.no-opts index 35b506e2b..971d690f4 100644 --- a/test/threads.wasm-only.fromasm.no-opts +++ b/test/threads.wasm-only.fromasm.no-opts @@ -7,6 +7,8 @@ (global $STACKTOP (mut i32) (get_global $STACKTOP$asm2wasm$import)) (global $tempRet0 (mut i32) (i32.const 0)) (export "test64" (func $legalstub$test64)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $test64 (; 0 ;) (result i64) (local $x i64) (local $y i64) @@ -87,4 +89,12 @@ (get_local $0) ) ) + (func $getTempRet0 (; 2 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 3 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/wasm-only.fromasm b/test/wasm-only.fromasm index 46a388b48..596ab5eb6 100644 --- a/test/wasm-only.fromasm +++ b/test/wasm-only.fromasm @@ -17,6 +17,8 @@ (export "illegalParam" (func $legalstub$illegalParam)) (export "illegalResult" (func $legalstub$illegalResult)) (export "keepAlive" (func $keepAlive)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $loads (; 4 ;) (drop (i32.load8_s @@ -1057,4 +1059,12 @@ ) ) ) + (func $getTempRet0 (; 29 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 30 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/wasm-only.fromasm.clamp b/test/wasm-only.fromasm.clamp index 46a388b48..596ab5eb6 100644 --- a/test/wasm-only.fromasm.clamp +++ b/test/wasm-only.fromasm.clamp @@ -17,6 +17,8 @@ (export "illegalParam" (func $legalstub$illegalParam)) (export "illegalResult" (func $legalstub$illegalResult)) (export "keepAlive" (func $keepAlive)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $loads (; 4 ;) (drop (i32.load8_s @@ -1057,4 +1059,12 @@ ) ) ) + (func $getTempRet0 (; 29 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 30 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/wasm-only.fromasm.clamp.no-opts b/test/wasm-only.fromasm.clamp.no-opts index 6e4a75bf2..680fe87ea 100644 --- a/test/wasm-only.fromasm.clamp.no-opts +++ b/test/wasm-only.fromasm.clamp.no-opts @@ -28,6 +28,8 @@ (export "illegalParam" (func $legalstub$illegalParam)) (export "illegalResult" (func $legalstub$illegalResult)) (export "keepAlive" (func $keepAlive)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $loads (; 9 ;) (local $i i32) (local $f f32) @@ -1919,4 +1921,12 @@ ) ) ) + (func $getTempRet0 (; 45 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 46 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/wasm-only.fromasm.imprecise b/test/wasm-only.fromasm.imprecise index 72e62809e..982600bef 100644 --- a/test/wasm-only.fromasm.imprecise +++ b/test/wasm-only.fromasm.imprecise @@ -15,6 +15,8 @@ (export "illegalParam" (func $legalstub$illegalParam)) (export "illegalResult" (func $legalstub$illegalResult)) (export "keepAlive" (func $keepAlive)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $stores (; 4 ;) (local $0 i32) (local $1 f64) @@ -736,4 +738,12 @@ ) ) ) + (func $getTempRet0 (; 23 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 24 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/wasm-only.fromasm.imprecise.no-opts b/test/wasm-only.fromasm.imprecise.no-opts index a414e6822..eaa5d7973 100644 --- a/test/wasm-only.fromasm.imprecise.no-opts +++ b/test/wasm-only.fromasm.imprecise.no-opts @@ -28,6 +28,8 @@ (export "illegalParam" (func $legalstub$illegalParam)) (export "illegalResult" (func $legalstub$illegalResult)) (export "keepAlive" (func $keepAlive)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $loads (; 9 ;) (local $i i32) (local $f f32) @@ -1754,4 +1756,12 @@ ) ) ) + (func $getTempRet0 (; 37 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 38 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) diff --git a/test/wasm-only.fromasm.no-opts b/test/wasm-only.fromasm.no-opts index 6e4a75bf2..680fe87ea 100644 --- a/test/wasm-only.fromasm.no-opts +++ b/test/wasm-only.fromasm.no-opts @@ -28,6 +28,8 @@ (export "illegalParam" (func $legalstub$illegalParam)) (export "illegalResult" (func $legalstub$illegalResult)) (export "keepAlive" (func $keepAlive)) + (export "getTempRet0" (func $getTempRet0)) + (export "setTempRet0" (func $setTempRet0)) (func $loads (; 9 ;) (local $i i32) (local $f f32) @@ -1919,4 +1921,12 @@ ) ) ) + (func $getTempRet0 (; 45 ;) (result i32) + (get_global $tempRet0) + ) + (func $setTempRet0 (; 46 ;) (param $0 i32) + (set_global $tempRet0 + (get_local $0) + ) + ) ) |