diff options
Diffstat (limited to 'src/ast/bits.h')
-rw-r--r-- | src/ast/bits.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/ast/bits.h b/src/ast/bits.h index 0aee50ffe..7a86e70f4 100644 --- a/src/ast/bits.h +++ b/src/ast/bits.h @@ -18,6 +18,8 @@ #define wasm_ast_bits_h #include "support/bits.h" +#include "wasm-builder.h" +#include "ast/literal-utils.h" namespace wasm { @@ -60,6 +62,43 @@ struct Bits { } WASM_UNREACHABLE(); } + + static Expression* makeSignExt(Expression* value, Index bytes, Module& wasm) { + if (value->type == i32) { + if (bytes == 1 || bytes == 2) { + auto shifts = bytes == 1 ? 24 : 16; + Builder builder(wasm); + return builder.makeBinary( + ShrSInt32, + builder.makeBinary( + ShlInt32, + value, + LiteralUtils::makeFromInt32(shifts, i32, wasm) + ), + LiteralUtils::makeFromInt32(shifts, i32, wasm) + ); + } + assert(bytes == 4); + return value; // nothing to do + } else { + assert(value->type == i64); + if (bytes == 1 || bytes == 2 || bytes == 4) { + auto shifts = bytes == 1 ? 56 : (bytes == 2 ? 48 : 32); + Builder builder(wasm); + return builder.makeBinary( + ShrSInt64, + builder.makeBinary( + ShlInt64, + value, + LiteralUtils::makeFromInt32(shifts, i64, wasm) + ), + LiteralUtils::makeFromInt32(shifts, i64, wasm) + ); + } + assert(bytes == 8); + return value; // nothing to do + } + } }; } // namespace wasm |