diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index 49754f03f6..f382a7770c 100644 --- 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 index 62238bdaec..400fbd0462 100644 --- 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;