diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/asm2wasm.h | 23 | ||||
-rw-r--r-- | src/js/post.js | 3 |
2 files changed, 25 insertions, 1 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index e44d2b5b1..a81f4af68 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -32,7 +32,8 @@ IString GLOBAL("global"), NAN_("NaN"), INFINITY_("Infinity"), CLZ32("clz32"), FROUND("fround"), ASM2WASM("asm2wasm"), - F64_REM("f64-rem"); + F64_REM("f64-rem"), + F64_TO_INT("f64-to-int"); static void abort_on(std::string why) { @@ -802,11 +803,31 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { } else if (ast[1] == B_NOT) { // ~, might be ~~ as a coercion or just a not if (ast[2][0] == UNARY_PREFIX && ast[2][1] == B_NOT) { +#if 0 auto ret = allocator.alloc<Convert>(); ret->op = TruncSFloat64; // equivalent to U, except for error handling, which asm.js doesn't have anyhow ret->value = process(ast[2][2]); ret->type = WasmType::i32; return ret; +#endif + // WebAssembly traps on float-to-int overflows, but asm.js wouldn't, so we must emulate that + CallImport *ret = allocator.alloc<CallImport>(); + ret->target = F64_TO_INT; + ret->operands.push_back(process(ast[2][2])); + ret->type = i32; + static bool addedImport = false; + if (!addedImport) { + addedImport = true; + auto import = allocator.alloc<Import>(); // f64-to-int = asm2wasm.f64-to-int; + import->name = F64_TO_INT; + import->module = ASM2WASM; + import->base = F64_TO_INT; + import->type.name = F64_TO_INT; + import->type.result = i32; + import->type.params.push_back(f64); + wasm.addImport(import); + } + return ret; } // no bitwise unary not, so do xor with -1 auto ret = allocator.alloc<Binary>(); diff --git a/src/js/post.js b/src/js/post.js index 6406674ba..5c854b3df 100644 --- a/src/js/post.js +++ b/src/js/post.js @@ -32,6 +32,9 @@ "f64-rem": function(x, y) { return x % y; }, + "f64-to-int": function(x) { + return x | 0; + }, }, parent: Module // Module inside wasm-js.cpp refers to wasm-js.cpp; this allows access to the outside program. }; |