summaryrefslogtreecommitdiff
path: root/src/ast/trapping.h
diff options
context:
space:
mode:
authorjgravelle-google <jgravelle@google.com>2017-10-02 13:51:55 -0700
committerGitHub <noreply@github.com>2017-10-02 13:51:55 -0700
commita9f91b9774d117a13c231ef0f40861372456878f (patch)
tree1bda2f8fed8a3affe5538732f4ac11c6b8b2599a /src/ast/trapping.h
parent28d670ade33ab7a1d091bafee243a2c5ffc93bc9 (diff)
downloadbinaryen-a9f91b9774d117a13c231ef0f40861372456878f.tar.gz
binaryen-a9f91b9774d117a13c231ef0f40861372456878f.tar.bz2
binaryen-a9f91b9774d117a13c231ef0f40861372456878f.zip
Share trap mode between asm2wasm and s2wasm (#1168)
* Extract Asm2WasmBuilder::TrapMode to shared FloatTrapMode * Extract makeTrappingI32Binary * Extract makeTrappingI64Binary * Extract asm2wasm test script into scripts/test/asm2wasm.py This matches s2wasm.py, and makes iterating on asm2wasm slightly faster. * Simplify callsites with an arg struct * Combine func adding across i32 and i64 * Support f32-to-int in asm2wasm * Add BinaryenTrapMode pass, run pass from s2wasm * BinaryenTrapMode pass takes trap context as a parameter * Pass fully supports non-trapping binary ops * Defer adding functions until after iteration (hackily) * Update asm2wasm to work with deferred function adding, rebuild tests * Extract makeTrappingFloatToInt32 * Extract makeTrappingFloatToInt64 * Add unary conversions to trap pass * Add functions in the pass itself * Set s2wasm trap mode with command-line arguments * Print BINARYEN_PASS_DEBUG state when testing * Get asm2wasm using the BinaryenTrapMode pass instead of handling it inline * Also handle f32 to int in asm2wasm * Make BinaryenTrapMode only need a FloatTrapMode from the caller * Just pass the current binary Expression directly * Combine makeTrappingI32Binary with makeTrappingI64Binary * Pass Unary expr to makeTrappingFloatToInt32 * Unify makeTrappingFloatToInt32 & 64 * Move makeTrapping* functions inside BinaryenTrapMode, make addedFunctions non-static * Remove FloatTrapContext * Minor cleanups * Extract some smaller subfunctions * Emit name switch/casing, rename is32Bit to isI64 for consistency * Rename BinaryenTrapMode to FloatTrap, make trap mode a nested enum * Add some comments explaining why FloatTrap is non-parallel * Rename addedFunctions to generatedFunctions for precision * Rename move and split float-clamp.h to passes/FloatTrap.(h|cpp) * Use builder instead of allocator * Instantiate trap handling passes via the pass manager * Move passes/FloatTrap.h to ast/trapping.h * Add helper function to add trap-handling passes * Add trap mode pass tests * Rename FloatTrap.cpp to TrapMode.cpp * Add s2wasm trap mode tests. Force float->int conversion to be signed * Add trapping_sint_div_s test to unit.asm.js * Fix flake8 issues with test scripts * Update pass description comment * Extract building functions methods * Make generate functions into top-level functions * Add GeneratedTrappingFunctions class to manage function/import additions * Move ensure/makeTrapping functions outside class scope * Use GeneratedTrappingFunctions to add immediately in asm2wasm mode * Remove trapping_sint_div_s test We only added it to test that trapping divisions would get constant-folded at the correct time. Now that we're not changing the timing of trapping modes, the test is unneeded (and problematic). * Review feedback, add validator/*.wasm to .gitignore * Add support for unsigned float-to-int conversion * Use opcode directly instead of bools * Update s2wasm clamp test for unsigned ftoi
Diffstat (limited to 'src/ast/trapping.h')
-rw-r--r--src/ast/trapping.h100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/ast/trapping.h b/src/ast/trapping.h
new file mode 100644
index 000000000..80cc14da9
--- /dev/null
+++ b/src/ast/trapping.h
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2017 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef wasm_ast_trapping_h
+#define wasm_ast_trapping_h
+
+#include "pass.h"
+
+namespace wasm {
+
+enum class TrapMode {
+ Allow,
+ Clamp,
+ JS
+};
+
+inline void addTrapModePass(PassRunner& runner, TrapMode trapMode) {
+ if (trapMode == TrapMode::Clamp) {
+ runner.add("trap-mode-clamp");
+ } else if (trapMode == TrapMode::JS) {
+ runner.add("trap-mode-js");
+ }
+}
+
+class TrappingFunctionContainer {
+public:
+ TrappingFunctionContainer(TrapMode mode, Module &wasm, bool immediate = false)
+ : mode(mode),
+ wasm(wasm),
+ immediate(immediate) { }
+
+ bool hasFunction(Name name) {
+ return functions.find(name) != functions.end();
+ }
+ bool hasImport(Name name) {
+ return imports.find(name) != imports.end();
+ }
+
+ void addFunction(Function* function) {
+ functions[function->name] = function;
+ if (immediate) {
+ wasm.addFunction(function);
+ }
+ }
+ void addImport(Import* import) {
+ imports[import->name] = import;
+ if (immediate) {
+ wasm.addImport(import);
+ }
+ }
+
+ void addToModule() {
+ if (!immediate) {
+ for (auto &pair : functions) {
+ wasm.addFunction(pair.second);
+ }
+ for (auto &pair : imports) {
+ wasm.addImport(pair.second);
+ }
+ }
+ functions.clear();
+ imports.clear();
+ }
+
+ TrapMode getMode() {
+ return mode;
+ }
+
+ Module& getModule() {
+ return wasm;
+ }
+
+private:
+ std::map<Name, Function*> functions;
+ std::map<Name, Import*> imports;
+
+ TrapMode mode;
+ Module& wasm;
+ bool immediate;
+};
+
+Expression* makeTrappingBinary(Binary* curr, TrappingFunctionContainer &trappingFunctions);
+Expression* makeTrappingUnary(Unary* curr, TrappingFunctionContainer &trappingFunctions);
+
+} // wasm
+
+#endif // wasm_ast_trapping_h