zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 85315bb535a98cb08b4eb8bad6254ef3f5d9714f (tree)
parent c86ba0f9d0d6d1421c93fa09a5172c796110258b
Author: Xavier Bouchoux <xavierb@gmail.com>
Date:   Sun,  8 Oct 2023 11:10:27 +0200

codegen/wasm: fix intcast accross 32-bits boundary

Diffstat:
Msrc/arch/wasm/CodeGen.zig | 16+++++++++++++---
Mtest/behavior/cast_int.zig | 1-
2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig @@ -4353,9 +4353,21 @@ fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro if (op_bits > 32 and op_bits <= 64 and wanted_bits == 32) { try func.emitWValue(operand); try func.addTag(.i32_wrap_i64); + if (given.isSignedInt(mod) and wanted_bitsize < 32) + return func.wrapOperand(.{ .stack = {} }, wanted) + else + return WValue{ .stack = {} }; } else if (op_bits == 32 and wanted_bits > 32 and wanted_bits <= 64) { - try func.emitWValue(operand); + const operand32 = if (given_bitsize < 32 and wanted.isSignedInt(mod)) + try func.signExtendInt(operand, given) + else + operand; + try func.emitWValue(operand32); try func.addTag(if (wanted.isSignedInt(mod)) .i64_extend_i32_s else .i64_extend_i32_u); + if (given.isSignedInt(mod) and wanted_bitsize < 64) + return func.wrapOperand(.{ .stack = {} }, wanted) + else + return WValue{ .stack = {} }; } else if (wanted_bits == 128) { // for 128bit integers we store the integer in the virtual stack, rather than a local const stack_ptr = try func.allocStack(wanted); @@ -4381,8 +4393,6 @@ fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro } return stack_ptr; } else return func.load(operand, wanted, 0); - - return WValue{ .stack = {} }; } fn airIsNull(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void { diff --git a/test/behavior/cast_int.zig b/test/behavior/cast_int.zig @@ -31,7 +31,6 @@ test "coerce i8 to i32 and @intCast back" { } test "coerce non byte-sized integers accross 32bits boundary" { - if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO { var v: u21 = 6417; const a: u32 = v;