authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-07-25 16:27:26+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-08-11 11:08:00+02:00
log699bc6171dcc18d681de754ca2eeb63645aea889
tree0b98b208a1375aad3d44ebed9b377e969369fab4
parent305b113a53cd5905d837ea620e2bcae8912c5938
signaturelock-open Commit is signed but in an unrecognized format.

wasm: Keep `intcast` values on stack


1 files changed, 9 insertions(+), 10 deletions(-)

src/arch/wasm/CodeGen.zig+9-10
......@@ -2153,8 +2153,8 @@ fn binOpBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerErr
21532153 const rhs_high_bit = try self.load(rhs, Type.u64, 0);
21542154 const rhs_low_bit = try self.load(rhs, Type.u64, 8);
21552155
2156 const low_op_res = try (try self.binOp(lhs_low_bit, rhs_low_bit, Type.u64, op)).toLocal(self, Type.u64);
21572156 const high_op_res = try (try self.binOp(lhs_high_bit, rhs_high_bit, Type.u64, op)).toLocal(self, Type.u64);
2157 const low_op_res = try self.binOp(lhs_low_bit, rhs_low_bit, Type.u64, op);
21582158
21592159 const lt = if (op == .add) blk: {
21602160 break :blk try self.cmp(high_op_res, rhs_high_bit, Type.u64, .lt);
......@@ -3190,12 +3190,13 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
31903190 return self.fail("todo Wasm intcast for bitsize > 128", .{});
31913191 }
31923192
3193 return self.intcast(operand, operand_ty, ty);
3193 return (try self.intcast(operand, operand_ty, ty)).toLocal(self, ty);
31943194}
31953195
31963196/// Upcasts or downcasts an integer based on the given and wanted types,
31973197/// and stores the result in a new operand.
31983198/// Asserts type's bitsize <= 128
3199/// NOTE: May leave the result on the top of the stack.
31993200fn intcast(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!WValue {
32003201 const given_info = given.intInfo(self.target);
32013202 const wanted_info = wanted.intInfo(self.target);
......@@ -3227,7 +3228,7 @@ fn intcast(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!W
32273228 given,
32283229 if (wanted.isSignedInt()) Type.i64 else Type.u64,
32293230 );
3230 break :blk tmp;
3231 break :blk try tmp.toLocal(self, Type.u64);
32313232 } else operand;
32323233
32333234 // store msb first
......@@ -3244,9 +3245,7 @@ fn intcast(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!W
32443245 return stack_ptr;
32453246 } else return self.load(operand, wanted, 0);
32463247
3247 const result = try self.allocLocal(wanted);
3248 try self.addLabel(.local_set, result.local);
3249 return result;
3248 return WValue{ .stack = {} };
32503249}
32513250
32523251fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!WValue {
......@@ -3478,7 +3477,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
34783477 if (wasm_bits != wanted_bits) {
34793478 return self.wrapOperand(result, wanted_ty);
34803479 }
3481 return result;
3480 return result.toLocal(self, wanted_ty);
34823481}
34833482
34843483fn airBoolToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
......@@ -4387,7 +4386,7 @@ fn airAddSubWithOverflowBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type,
43874386 } else if (op == .sub) blk: {
43884387 break :blk try (try self.cmp(lhs_high_bit, rhs_high_bit, Type.u64, .lt)).toLocal(self, Type.u32);
43894388 } else unreachable;
4390 const tmp = try self.intcast(lt, Type.u32, Type.u64);
4389 const tmp = try (try self.intcast(lt, Type.u32, Type.u64)).toLocal(self, Type.u64);
43914390 const tmp_op = try (try self.binOp(low_op_res, tmp, Type.u64, op)).toLocal(self, Type.u64);
43924391
43934392 const overflow_bit = if (is_signed) blk: {
......@@ -4500,9 +4499,9 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
45004499 const wrap = try self.intcast(shr, new_ty, lhs_ty);
45014500 _ = try self.cmp(wrap, zero, lhs_ty, .neq);
45024501 try self.addLabel(.local_set, overflow_bit.local);
4503 break :blk try self.intcast(bin_op, new_ty, lhs_ty);
4502 break :blk try (try self.intcast(bin_op, new_ty, lhs_ty)).toLocal(self, lhs_ty);
45044503 } else {
4505 const down_cast = try self.intcast(bin_op, new_ty, lhs_ty);
4504 const down_cast = try (try self.intcast(bin_op, new_ty, lhs_ty)).toLocal(self, lhs_ty);
45064505 const shr = try (try self.binOp(down_cast, .{ .imm32 = int_info.bits - 1 }, lhs_ty, .shr)).toLocal(self, lhs_ty);
45074506
45084507 const shr_res = try self.binOp(bin_op, .{ .imm64 = int_info.bits }, new_ty, .shr);