authorgravatar for xavierb@gmail.comXavier Bouchoux <xavierb@gmail.com> 2023-10-08 11:10:27+02:00
committergravatar for xavierb@gmail.comXavier Bouchoux <xavierb@gmail.com> 2023-10-08 11:37:49+02:00
log85315bb535a98cb08b4eb8bad6254ef3f5d9714f
treea67dfef7804df7718368f09288be664fc31adcc6
parentc86ba0f9d0d6d1421c93fa09a5172c796110258b

codegen/wasm: fix intcast accross 32-bits boundary


2 files changed, 13 insertions(+), 4 deletions(-)

src/arch/wasm/CodeGen.zig+13-3
...@@ -4353,9 +4353,21 @@ fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro...@@ -4353,9 +4353,21 @@ fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro
4353 if (op_bits > 32 and op_bits <= 64 and wanted_bits == 32) {4353 if (op_bits > 32 and op_bits <= 64 and wanted_bits == 32) {
4354 try func.emitWValue(operand);4354 try func.emitWValue(operand);
4355 try func.addTag(.i32_wrap_i64);4355 try func.addTag(.i32_wrap_i64);
4356 if (given.isSignedInt(mod) and wanted_bitsize < 32)
4357 return func.wrapOperand(.{ .stack = {} }, wanted)
4358 else
4359 return WValue{ .stack = {} };
4356 } else if (op_bits == 32 and wanted_bits > 32 and wanted_bits <= 64) {4360 } else if (op_bits == 32 and wanted_bits > 32 and wanted_bits <= 64) {
4357 try func.emitWValue(operand);4361 const operand32 = if (given_bitsize < 32 and wanted.isSignedInt(mod))
4362 try func.signExtendInt(operand, given)
4363 else
4364 operand;
4365 try func.emitWValue(operand32);
4358 try func.addTag(if (wanted.isSignedInt(mod)) .i64_extend_i32_s else .i64_extend_i32_u);4366 try func.addTag(if (wanted.isSignedInt(mod)) .i64_extend_i32_s else .i64_extend_i32_u);
4367 if (given.isSignedInt(mod) and wanted_bitsize < 64)
4368 return func.wrapOperand(.{ .stack = {} }, wanted)
4369 else
4370 return WValue{ .stack = {} };
4359 } else if (wanted_bits == 128) {4371 } else if (wanted_bits == 128) {
4360 // for 128bit integers we store the integer in the virtual stack, rather than a local4372 // for 128bit integers we store the integer in the virtual stack, rather than a local
4361 const stack_ptr = try func.allocStack(wanted);4373 const stack_ptr = try func.allocStack(wanted);
...@@ -4381,8 +4393,6 @@ fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro...@@ -4381,8 +4393,6 @@ fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro
4381 }4393 }
4382 return stack_ptr;4394 return stack_ptr;
4383 } else return func.load(operand, wanted, 0);4395 } else return func.load(operand, wanted, 0);
4384
4385 return WValue{ .stack = {} };
4386}4396}
43874397
4388fn airIsNull(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void {4398fn airIsNull(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void {
test/behavior/cast_int.zig-1
...@@ -31,7 +31,6 @@ test "coerce i8 to i32 and @intCast back" {...@@ -31,7 +31,6 @@ test "coerce i8 to i32 and @intCast back" {
31}31}
3232
33test "coerce non byte-sized integers accross 32bits boundary" {33test "coerce non byte-sized integers accross 32bits boundary" {
34 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
35 {34 {
36 var v: u21 = 6417;35 var v: u21 = 6417;
37 const a: u32 = v;36 const a: u32 = v;