diff options
Diffstat (limited to 'src/ast')
-rw-r--r-- | src/ast/bits.h | 20 | ||||
-rw-r--r-- | src/ast/literal-utils.h | 26 |
2 files changed, 38 insertions, 8 deletions
diff --git a/src/ast/bits.h b/src/ast/bits.h index d88cb5edb..11cf7b06d 100644 --- a/src/ast/bits.h +++ b/src/ast/bits.h @@ -39,6 +39,26 @@ struct Bits { // this is indeed a mask return 32 - CountLeadingZeroes(mask); } + + // gets the number of effective shifts a shift operation does. In + // wasm, only 5 bits matter for 32-bit shifts, and 6 for 64. + static Index getEffectiveShifts(Index amount, WasmType type) { + if (type == i32) { + return amount & 31; + } else if (type == i64) { + return amount & 63; + } + WASM_UNREACHABLE(); + } + + static Index getEffectiveShifts(Const* amount) { + if (amount->type == i32) { + return getEffectiveShifts(amount->value.geti32(), i32); + } else if (amount->type == i64) { + return getEffectiveShifts(amount->value.geti64(), i64); + } + WASM_UNREACHABLE(); + } }; } // namespace wasm diff --git a/src/ast/literal-utils.h b/src/ast/literal-utils.h index 7e75e8bc8..afa8146b9 100644 --- a/src/ast/literal-utils.h +++ b/src/ast/literal-utils.h @@ -23,21 +23,31 @@ namespace wasm { namespace LiteralUtils { -inline Expression* makeZero(WasmType type, Module& wasm) { - Literal value; +inline Literal makeLiteralFromInt32(int32_t x, WasmType type) { switch (type) { - case i32: value = Literal(int32_t(0)); break; - case i64: value = Literal(int64_t(0)); break; - case f32: value = Literal(float(0)); break; - case f64: value = Literal(double(0)); break; + case i32: return Literal(int32_t(x)); break; + case i64: return Literal(int64_t(x)); break; + case f32: return Literal(float(x)); break; + case f64: return Literal(double(x)); break; default: WASM_UNREACHABLE(); } +} + +inline Literal makeLiteralZero(WasmType type) { + return makeLiteralFromInt32(0, type); +} + +inline Expression* makeFromInt32(int32_t x, WasmType type, Module& wasm) { auto* ret = wasm.allocator.alloc<Const>(); - ret->value = value; - ret->type = value.type; + ret->value = makeLiteralFromInt32(x, type); + ret->type = type; return ret; } +inline Expression* makeZero(WasmType type, Module& wasm) { + return makeFromInt32(0, type, wasm); +} + } // namespace LiteralUtils } // namespace wasm |