diff options
author | Alon Zakai <azakai@google.com> | 2019-04-26 16:59:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-26 16:59:41 -0700 |
commit | db9124f1de0478dcac525009b6f1589b44a7edd8 (patch) | |
tree | fa26395a0f6cca53cf5cb6e10189f989c5bfa847 /src/passes/TrapMode.cpp | |
parent | 87636dccd404a340d75acb1d96301581343f29ca (diff) | |
download | binaryen-db9124f1de0478dcac525009b6f1589b44a7edd8.tar.gz binaryen-db9124f1de0478dcac525009b6f1589b44a7edd8.tar.bz2 binaryen-db9124f1de0478dcac525009b6f1589b44a7edd8.zip |
Apply format changes from #2048 (#2059)
Mass change to apply clang-format to everything. We are applying this in a PR by me so the (git) blame is all mine ;) but @aheejin did all the work to get clang-format set up and all the manual work to tidy up some things to make the output nicer in #2048
Diffstat (limited to 'src/passes/TrapMode.cpp')
-rw-r--r-- | src/passes/TrapMode.cpp | 237 |
1 files changed, 125 insertions, 112 deletions
diff --git a/src/passes/TrapMode.cpp b/src/passes/TrapMode.cpp index e6327479c..b36427138 100644 --- a/src/passes/TrapMode.cpp +++ b/src/passes/TrapMode.cpp @@ -26,66 +26,84 @@ #include "ir/trapping.h" #include "mixed_arena.h" #include "pass.h" -#include "wasm.h" +#include "support/name.h" #include "wasm-builder.h" #include "wasm-printing.h" #include "wasm-type.h" -#include "support/name.h" +#include "wasm.h" namespace wasm { -Name I64S_REM("i64s-rem"), - I64U_REM("i64u-rem"), - I64S_DIV("i64s-div"), - I64U_DIV("i64u-div"); +Name I64S_REM("i64s-rem"); +Name I64U_REM("i64u-rem"); +Name I64S_DIV("i64s-div"); +Name I64U_DIV("i64u-div"); Name getBinaryFuncName(Binary* curr) { switch (curr->op) { - case RemSInt32: return I32S_REM; - case RemUInt32: return I32U_REM; - case DivSInt32: return I32S_DIV; - case DivUInt32: return I32U_DIV; - case RemSInt64: return I64S_REM; - case RemUInt64: return I64U_REM; - case DivSInt64: return I64S_DIV; - case DivUInt64: return I64U_DIV; - default: return Name(); + case RemSInt32: + return I32S_REM; + case RemUInt32: + return I32U_REM; + case DivSInt32: + return I32S_DIV; + case DivUInt32: + return I32U_DIV; + case RemSInt64: + return I64S_REM; + case RemUInt64: + return I64U_REM; + case DivSInt64: + return I64S_DIV; + case DivUInt64: + return I64U_DIV; + default: + return Name(); } } Name getUnaryFuncName(Unary* curr) { switch (curr->op) { - case TruncSFloat32ToInt32: return F32_TO_INT; - case TruncUFloat32ToInt32: return F32_TO_UINT; - case TruncSFloat32ToInt64: return F32_TO_INT64; - case TruncUFloat32ToInt64: return F32_TO_UINT64; - case TruncSFloat64ToInt32: return F64_TO_INT; - case TruncUFloat64ToInt32: return F64_TO_UINT; - case TruncSFloat64ToInt64: return F64_TO_INT64; - case TruncUFloat64ToInt64: return F64_TO_UINT64; - default: return Name(); + case TruncSFloat32ToInt32: + return F32_TO_INT; + case TruncUFloat32ToInt32: + return F32_TO_UINT; + case TruncSFloat32ToInt64: + return F32_TO_INT64; + case TruncUFloat32ToInt64: + return F32_TO_UINT64; + case TruncSFloat64ToInt32: + return F64_TO_INT; + case TruncUFloat64ToInt32: + return F64_TO_UINT; + case TruncSFloat64ToInt64: + return F64_TO_INT64; + case TruncUFloat64ToInt64: + return F64_TO_UINT64; + default: + return Name(); } } bool isTruncOpSigned(UnaryOp op) { switch (op) { - case TruncUFloat32ToInt32: - case TruncUFloat32ToInt64: - case TruncUFloat64ToInt32: - case TruncUFloat64ToInt64: return false; - default: return true; + case TruncUFloat32ToInt32: + case TruncUFloat32ToInt64: + case TruncUFloat64ToInt32: + case TruncUFloat64ToInt64: + return false; + default: + return true; } } -Function* generateBinaryFunc(Module& wasm, Binary *curr) { +Function* generateBinaryFunc(Module& wasm, Binary* curr) { BinaryOp op = curr->op; Type type = curr->type; bool isI64 = type == i64; Builder builder(wasm); - Expression* result = builder.makeBinary(op, - builder.makeGetLocal(0, type), - builder.makeGetLocal(1, type) - ); + Expression* result = builder.makeBinary( + op, builder.makeGetLocal(0, type), builder.makeGetLocal(1, type)); BinaryOp divSIntOp = isI64 ? DivSInt64 : DivSInt32; UnaryOp eqZOp = isI64 ? EqZInt64 : EqZInt32; Literal minLit = isI64 ? Literal(std::numeric_limits<int64_t>::min()) @@ -96,32 +114,24 @@ Function* generateBinaryFunc(Module& wasm, Binary *curr) { BinaryOp eqOp = isI64 ? EqInt64 : EqInt32; Literal negLit = isI64 ? Literal(int64_t(-1)) : Literal(int32_t(-1)); result = builder.makeIf( - builder.makeBinary(AndInt32, - builder.makeBinary(eqOp, - builder.makeGetLocal(0, type), - builder.makeConst(minLit) - ), - builder.makeBinary(eqOp, - builder.makeGetLocal(1, type), - builder.makeConst(negLit) - ) - ), + builder.makeBinary( + AndInt32, + builder.makeBinary( + eqOp, builder.makeGetLocal(0, type), builder.makeConst(minLit)), + builder.makeBinary( + eqOp, builder.makeGetLocal(1, type), builder.makeConst(negLit))), builder.makeConst(zeroLit), - result - ); + result); } auto func = new Function; func->name = getBinaryFuncName(curr); func->params.push_back(type); func->params.push_back(type); func->result = type; - func->body = builder.makeIf( - builder.makeUnary(eqZOp, - builder.makeGetLocal(1, type) - ), - builder.makeConst(zeroLit), - result - ); + func->body = + builder.makeIf(builder.makeUnary(eqZOp, builder.makeGetLocal(1, type)), + builder.makeConst(zeroLit), + result); return func; } @@ -134,7 +144,7 @@ void makeClampLimitLiterals(Literal& iMin, Literal& fMin, Literal& fMax) { fMax = Literal(FloatType(maxVal) + 1); } -Function* generateUnaryFunc(Module& wasm, Unary *curr) { +Function* generateUnaryFunc(Module& wasm, Unary* curr) { Type type = curr->value->type; Type retType = curr->type; UnaryOp truncOp = curr->op; @@ -148,59 +158,66 @@ Function* generateUnaryFunc(Module& wasm, Unary *curr) { Literal iMin, fMin, fMax; switch (truncOp) { - case TruncSFloat32ToInt32: makeClampLimitLiterals< int32_t, float>(iMin, fMin, fMax); break; - case TruncUFloat32ToInt32: makeClampLimitLiterals<uint32_t, float>(iMin, fMin, fMax); break; - case TruncSFloat32ToInt64: makeClampLimitLiterals< int64_t, float>(iMin, fMin, fMax); break; - case TruncUFloat32ToInt64: makeClampLimitLiterals<uint64_t, float>(iMin, fMin, fMax); break; - case TruncSFloat64ToInt32: makeClampLimitLiterals< int32_t, double>(iMin, fMin, fMax); break; - case TruncUFloat64ToInt32: makeClampLimitLiterals<uint32_t, double>(iMin, fMin, fMax); break; - case TruncSFloat64ToInt64: makeClampLimitLiterals< int64_t, double>(iMin, fMin, fMax); break; - case TruncUFloat64ToInt64: makeClampLimitLiterals<uint64_t, double>(iMin, fMin, fMax); break; - default: WASM_UNREACHABLE(); + case TruncSFloat32ToInt32: + makeClampLimitLiterals<int32_t, float>(iMin, fMin, fMax); + break; + case TruncUFloat32ToInt32: + makeClampLimitLiterals<uint32_t, float>(iMin, fMin, fMax); + break; + case TruncSFloat32ToInt64: + makeClampLimitLiterals<int64_t, float>(iMin, fMin, fMax); + break; + case TruncUFloat32ToInt64: + makeClampLimitLiterals<uint64_t, float>(iMin, fMin, fMax); + break; + case TruncSFloat64ToInt32: + makeClampLimitLiterals<int32_t, double>(iMin, fMin, fMax); + break; + case TruncUFloat64ToInt32: + makeClampLimitLiterals<uint32_t, double>(iMin, fMin, fMax); + break; + case TruncSFloat64ToInt64: + makeClampLimitLiterals<int64_t, double>(iMin, fMin, fMax); + break; + case TruncUFloat64ToInt64: + makeClampLimitLiterals<uint64_t, double>(iMin, fMin, fMax); + break; + default: + WASM_UNREACHABLE(); } auto func = new Function; func->name = getUnaryFuncName(curr); func->params.push_back(type); func->result = retType; - func->body = builder.makeUnary(truncOp, - builder.makeGetLocal(0, type) - ); + func->body = builder.makeUnary(truncOp, builder.makeGetLocal(0, type)); // too small XXX this is different than asm.js, which does frem. here we // clamp, which is much simpler/faster, and similar to native builds - func->body = builder.makeIf( - builder.makeBinary(leOp, - builder.makeGetLocal(0, type), - builder.makeConst(fMin) - ), - builder.makeConst(iMin), - func->body - ); + func->body = builder.makeIf(builder.makeBinary(leOp, + builder.makeGetLocal(0, type), + builder.makeConst(fMin)), + builder.makeConst(iMin), + func->body); // too big XXX see above func->body = builder.makeIf( - builder.makeBinary(geOp, - builder.makeGetLocal(0, type), - builder.makeConst(fMax) - ), + builder.makeBinary( + geOp, builder.makeGetLocal(0, type), builder.makeConst(fMax)), // NB: min here as well. anything out of range => to the min builder.makeConst(iMin), - func->body - ); + func->body); // nan func->body = builder.makeIf( - builder.makeBinary(neOp, - builder.makeGetLocal(0, type), - builder.makeGetLocal(0, type) - ), + builder.makeBinary( + neOp, builder.makeGetLocal(0, type), builder.makeGetLocal(0, type)), // NB: min here as well. anything invalid => to the min builder.makeConst(iMin), - func->body - ); + func->body); return func; } -void ensureBinaryFunc(Binary* curr, Module& wasm, - TrappingFunctionContainer &trappingFunctions) { +void ensureBinaryFunc(Binary* curr, + Module& wasm, + TrappingFunctionContainer& trappingFunctions) { Name name = getBinaryFuncName(curr); if (trappingFunctions.hasFunction(name)) { return; @@ -208,8 +225,9 @@ void ensureBinaryFunc(Binary* curr, Module& wasm, trappingFunctions.addFunction(generateBinaryFunc(wasm, curr)); } -void ensureUnaryFunc(Unary *curr, Module& wasm, - TrappingFunctionContainer &trappingFunctions) { +void ensureUnaryFunc(Unary* curr, + Module& wasm, + TrappingFunctionContainer& trappingFunctions) { Name name = getUnaryFuncName(curr); if (trappingFunctions.hasFunction(name)) { return; @@ -217,7 +235,7 @@ void ensureUnaryFunc(Unary *curr, Module& wasm, trappingFunctions.addFunction(generateUnaryFunc(wasm, curr)); } -void ensureF64ToI64JSImport(TrappingFunctionContainer &trappingFunctions) { +void ensureF64ToI64JSImport(TrappingFunctionContainer& trappingFunctions) { if (trappingFunctions.hasImport(F64_TO_INT)) { return; } @@ -233,7 +251,8 @@ void ensureF64ToI64JSImport(TrappingFunctionContainer &trappingFunctions) { trappingFunctions.addImport(import); } -Expression* makeTrappingBinary(Binary* curr, TrappingFunctionContainer &trappingFunctions) { +Expression* makeTrappingBinary(Binary* curr, + TrappingFunctionContainer& trappingFunctions) { Name name = getBinaryFuncName(curr); if (!name.is() || trappingFunctions.getMode() == TrapMode::Allow) { return curr; @@ -247,7 +266,8 @@ Expression* makeTrappingBinary(Binary* curr, TrappingFunctionContainer &trapping return builder.makeCall(name, {curr->left, curr->right}, type); } -Expression* makeTrappingUnary(Unary* curr, TrappingFunctionContainer &trappingFunctions) { +Expression* makeTrappingUnary(Unary* curr, + TrappingFunctionContainer& trappingFunctions) { Name name = getUnaryFuncName(curr); TrapMode mode = trappingFunctions.getMode(); if (!name.is() || mode == TrapMode::Allow) { @@ -256,13 +276,15 @@ Expression* makeTrappingUnary(Unary* curr, TrappingFunctionContainer &trappingFu Module& wasm = trappingFunctions.getModule(); Builder builder(wasm); - // WebAssembly traps on float-to-int overflows, but asm.js wouldn't, so we must do something - // We can handle this in one of two ways: clamping, which is fast, or JS, which - // is precisely like JS but in order to do that we do a slow ffi - // If i64, there is no "JS" way to handle this, as no i64s in JS, so always clamp if we don't allow traps - // asm.js doesn't have unsigned f64-to-int, so just use the signed one. + // WebAssembly traps on float-to-int overflows, but asm.js wouldn't, so we + // must do something We can handle this in one of two ways: clamping, which is + // fast, or JS, which is precisely like JS but in order to do that we do a + // slow ffi If i64, there is no "JS" way to handle this, as no i64s in JS, so + // always clamp if we don't allow traps asm.js doesn't have unsigned + // f64-to-int, so just use the signed one. if (curr->type != i64 && mode == TrapMode::JS) { - // WebAssembly traps on float-to-int overflows, but asm.js wouldn't, so we must emulate that + // WebAssembly traps on float-to-int overflows, but asm.js wouldn't, so we + // must emulate that ensureF64ToI64JSImport(trappingFunctions); Expression* f64Value = ensureDouble(curr->value, wasm.allocator); return builder.makeCall(F64_TO_INT, {f64Value}, i32); @@ -274,14 +296,11 @@ Expression* makeTrappingUnary(Unary* curr, TrappingFunctionContainer &trappingFu struct TrapModePass : public WalkerPass<PostWalker<TrapModePass>> { public: - // Needs to be non-parallel so that visitModule gets called after visiting // each node in the module, so we can add the functions that we created. bool isFunctionParallel() override { return false; } - TrapModePass(TrapMode mode) : mode(mode) { - assert(mode != TrapMode::Allow); - } + TrapModePass(TrapMode mode) : mode(mode) { assert(mode != TrapMode::Allow); } Pass* create() override { return new TrapModePass(mode); } @@ -293,9 +312,7 @@ public: replaceCurrent(makeTrappingBinary(curr, *trappingFunctions)); } - void visitModule(Module* curr) { - trappingFunctions->addToModule(); - } + void visitModule(Module* curr) { trappingFunctions->addToModule(); } void doWalkModule(Module* module) { trappingFunctions = make_unique<TrappingFunctionContainer>(mode, *module); @@ -309,12 +326,8 @@ private: std::unique_ptr<TrappingFunctionContainer> trappingFunctions; }; -Pass *createTrapModeClamp() { - return new TrapModePass(TrapMode::Clamp); -} +Pass* createTrapModeClamp() { return new TrapModePass(TrapMode::Clamp); } -Pass *createTrapModeJS() { - return new TrapModePass(TrapMode::JS); -} +Pass* createTrapModeJS() { return new TrapModePass(TrapMode::JS); } } // namespace wasm |