diff --git a/auto_update_tests.py b/auto_update_tests.py index 08c10a9eeba..47060edfeb4 100755 --- a/auto_update_tests.py +++ b/auto_update_tests.py @@ -19,10 +19,10 @@ cmd = ASM2WASM + [os.path.join('test', asm)] wasm = asm.replace('.asm.js', '.fromasm') if not precise: - cmd += ['--emit-potential-traps', '--ignore-implicit-traps'] + cmd += ['--trap-mode=allow', '--ignore-implicit-traps'] wasm += '.imprecise' elif precise == 2: - cmd += ['--emit-clamped-potential-traps'] + cmd += ['--trap-mode=clamp'] wasm += '.clamp' if not opts: wasm += '.no-opts' diff --git a/scripts/test/asm2wasm.py b/scripts/test/asm2wasm.py index db37a237aff..7e0e3278c09 100644 --- a/scripts/test/asm2wasm.py +++ b/scripts/test/asm2wasm.py @@ -35,10 +35,10 @@ def test_asm2wasm(): cmd = ASM2WASM + [os.path.join(options.binaryen_test, asm)] wasm = asm.replace('.asm.js', '.fromasm') if not precise: - cmd += ['--emit-potential-traps', '--ignore-implicit-traps'] + cmd += ['--trap-mode=allow', '--ignore-implicit-traps'] wasm += '.imprecise' elif precise == 2: - cmd += ['--emit-clamped-potential-traps'] + cmd += ['--trap-mode=clamp'] wasm += '.clamp' if not opts: wasm += '.no-opts' diff --git a/scripts/test/s2wasm.py b/scripts/test/s2wasm.py index fcba17d1836..b8027f66871 100755 --- a/scripts/test/s2wasm.py +++ b/scripts/test/s2wasm.py @@ -34,8 +34,8 @@ def test_s2wasm(): extension_arg_map = { '.wast': [], - '.clamp.wast': ['--emit-clamped-potential-traps'], - '.js.wast': ['--emit-jsified-potential-traps'], + '.clamp.wast': ['--trap-mode=clamp'], + '.js.wast': ['--trap-mode=js'], } for dot_s_dir in ['dot_s', 'llvm_autogenerated']: dot_s_path = os.path.join(options.binaryen_test, dot_s_dir) diff --git a/src/ast/trapping.h b/src/ast/trapping.h index 80cc14da913..184e6059b0c 100644 --- a/src/ast/trapping.h +++ b/src/ast/trapping.h @@ -17,6 +17,8 @@ #ifndef wasm_ast_trapping_h #define wasm_ast_trapping_h +#include + #include "pass.h" namespace wasm { @@ -95,6 +97,20 @@ class TrappingFunctionContainer { Expression* makeTrappingBinary(Binary* curr, TrappingFunctionContainer &trappingFunctions); Expression* makeTrappingUnary(Unary* curr, TrappingFunctionContainer &trappingFunctions); +inline TrapMode trapModeFromString(std::string const& str) { + if (str == "allow") { + return TrapMode::Allow; + } else if (str == "clamp") { + return TrapMode::Clamp; + } else if (str == "js") { + return TrapMode::JS; + } else { + throw std::invalid_argument( + "Unsupported trap mode \"" + str + "\". " + "Valid modes are \"allow\", \"js\", and \"clamp\""); + } +} + } // wasm #endif // wasm_ast_trapping_h diff --git a/src/tools/asm2wasm.cpp b/src/tools/asm2wasm.cpp index cbc3c486b77..3ce140b0409 100644 --- a/src/tools/asm2wasm.cpp +++ b/src/tools/asm2wasm.cpp @@ -18,6 +18,8 @@ // asm2wasm console tool // +#include + #include "ast/trapping.h" #include "support/colors.h" #include "support/command-line.h" @@ -78,21 +80,17 @@ int main(int argc, const char *argv[]) { [](Options *o, const std::string &) { std::cerr << "--no-opts is deprecated (use -O0, etc.)\n"; }) - .add("--emit-potential-traps", "-i", "Emit instructions that might trap, like div/rem of 0", Options::Arguments::Zero, - [&trapMode](Options *o, const std::string &) { - trapMode = TrapMode::Allow; - }) - .add("--emit-clamped-potential-traps", "-i", "Clamp instructions that might trap, like float => int", Options::Arguments::Zero, - [&trapMode](Options *o, const std::string &) { - trapMode = TrapMode::Clamp; - }) - .add("--emit-jsified-potential-traps", "-i", "Avoid instructions that might trap, handling them exactly like JS would", Options::Arguments::Zero, - [&trapMode](Options *o, const std::string &) { - trapMode = TrapMode::JS; - }) - .add("--imprecise", "-i", "Imprecise optimizations (old name for --emit-potential-traps)", Options::Arguments::Zero, - [&trapMode](Options *o, const std::string &) { - trapMode = TrapMode::Allow; + .add("--trap-mode", "", + "Strategy for handling potentially trapping instructions. Valid " + "values are \"allow\", \"js\", and \"clamp\"", + Options::Arguments::One, + [&trapMode](Options *o, const std::string &argument) { + try { + trapMode = trapModeFromString(argument); + } catch (std::invalid_argument e) { + std::cerr << "Error: " << e.what() << "\n"; + exit(EXIT_FAILURE); + } }) .add("--wasm-only", "-w", "Input is in WebAssembly-only format, and not actually valid asm.js", Options::Arguments::Zero, [&wasmOnly](Options *o, const std::string &) { diff --git a/src/tools/s2wasm.cpp b/src/tools/s2wasm.cpp index 559188d1f00..8411d40467d 100644 --- a/src/tools/s2wasm.cpp +++ b/src/tools/s2wasm.cpp @@ -15,9 +15,11 @@ */ // -// wasm2asm console tool +// s2wasm console tool // +#include + #include "ast/trapping.h" #include "support/colors.h" #include "support/command-line.h" @@ -83,23 +85,17 @@ int main(int argc, const char *argv[]) { [&allowMemoryGrowth](Options *, const std::string &) { allowMemoryGrowth = true; }) - .add("--emit-potential-traps", "", - "Emit instructions that might trap, like div/rem of 0", - Options::Arguments::Zero, - [&trapMode](Options *o, const std::string &) { - trapMode = TrapMode::Allow; - }) - .add("--emit-clamped-potential-traps", "", - "Clamp instructions that might trap, like float => int", - Options::Arguments::Zero, - [&trapMode](Options *o, const std::string &) { - trapMode = TrapMode::Clamp; - }) - .add("--emit-jsified-potential-traps", "", - "Avoid instructions that might trap, handling them exactly like JS would", - Options::Arguments::Zero, - [&trapMode](Options *o, const std::string &) { - trapMode = TrapMode::JS; + .add("--trap-mode", "", + "Strategy for handling potentially trapping instructions. Valid " + "values are \"allow\", \"js\", and \"clamp\"", + Options::Arguments::One, + [&trapMode](Options *o, const std::string &argument) { + try { + trapMode = trapModeFromString(argument); + } catch (std::invalid_argument e) { + std::cerr << "Error: " << e.what() << "\n"; + exit(EXIT_FAILURE); + } }) .add("--emscripten-glue", "-e", "Generate emscripten glue", Options::Arguments::Zero,