Skip to content

Commit 3cd0cd1

Browse files
committed
wasm: leave signedAbsValue values on the stack
1 parent cde16f6 commit 3cd0cd1

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/arch/wasm/CodeGen.zig

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,10 +4309,10 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!W
43094309
// for signed integers, we first apply signed shifts by the difference in bits
43104310
// to get the signed value, as we store it internally as 2's complement.
43114311
const lhs = if (wasm_bits != int_info.bits and is_signed) blk: {
4312-
break :blk try self.signAbsValue(lhs_op, lhs_ty);
4312+
break :blk try (try self.signAbsValue(lhs_op, lhs_ty)).toLocal(self, lhs_ty);
43134313
} else lhs_op;
43144314
const rhs = if (wasm_bits != int_info.bits and is_signed) blk: {
4315-
break :blk try self.signAbsValue(rhs_op, lhs_ty);
4315+
break :blk try (try self.signAbsValue(rhs_op, lhs_ty)).toLocal(self, lhs_ty);
43164316
} else rhs_op;
43174317

43184318
const bin_op = try (try self.binOp(lhs, rhs, lhs_ty, op)).toLocal(self, lhs_ty);
@@ -4420,9 +4420,9 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
44204420
} else shl;
44214421

44224422
const overflow_bit = if (wasm_bits != int_info.bits and is_signed) blk: {
4423-
const abs = try self.signAbsValue(shl, lhs_ty);
44244423
// emit lhs to stack to we can keep 'wrapped' on the stack also
44254424
try self.emitWValue(lhs);
4425+
const abs = try self.signAbsValue(shl, lhs_ty);
44264426
const wrapped = try self.wrapBinOp(abs, rhs, lhs_ty, .shr);
44274427
break :blk try self.cmp(.{ .stack = {} }, wrapped, lhs_ty, .neq);
44284428
} else blk: {
@@ -4909,10 +4909,10 @@ fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
49094909
return self.fail("TODO: `@divFloor` for signed integers larger than '{d}' bits", .{int_bits});
49104910
};
49114911
const lhs_res = if (wasm_bits != int_bits) blk: {
4912-
break :blk try self.signAbsValue(lhs, ty);
4912+
break :blk try (try self.signAbsValue(lhs, ty)).toLocal(self, ty);
49134913
} else lhs;
49144914
const rhs_res = if (wasm_bits != int_bits) blk: {
4915-
break :blk try self.signAbsValue(rhs, ty);
4915+
break :blk try (try self.signAbsValue(rhs, ty)).toLocal(self, ty);
49164916
} else rhs;
49174917

49184918
const zero = switch (wasm_bits) {
@@ -4994,10 +4994,9 @@ fn divSigned(self: *Self, lhs: WValue, rhs: WValue, ty: Type) InnerError!WValue
49944994
}
49954995

49964996
if (wasm_bits != int_bits) {
4997-
const lhs_abs = try self.signAbsValue(lhs, ty);
4998-
const rhs_abs = try self.signAbsValue(rhs, ty);
4999-
try self.emitWValue(lhs_abs);
5000-
try self.emitWValue(rhs_abs);
4997+
// Leave both values on the stack
4998+
_ = try self.signAbsValue(lhs, ty);
4999+
_ = try self.signAbsValue(rhs, ty);
50015000
} else {
50025001
try self.emitWValue(lhs);
50035002
try self.emitWValue(rhs);
@@ -5009,6 +5008,8 @@ fn divSigned(self: *Self, lhs: WValue, rhs: WValue, ty: Type) InnerError!WValue
50095008
return result;
50105009
}
50115010

5011+
/// Retrieves the absolute value of a signed integer
5012+
/// NOTE: Leaves the result value on the stack.
50125013
fn signAbsValue(self: *Self, operand: WValue, ty: Type) InnerError!WValue {
50135014
const int_bits = ty.intInfo(self.target).bits;
50145015
const wasm_bits = toWasmBits(int_bits) orelse {
@@ -5037,9 +5038,8 @@ fn signAbsValue(self: *Self, operand: WValue, ty: Type) InnerError!WValue {
50375038
},
50385039
else => unreachable,
50395040
}
5040-
const result = try self.allocLocal(ty);
5041-
try self.addLabel(.local_set, result.local);
5042-
return result;
5041+
5042+
return WValue{ .stack = {} };
50435043
}
50445044

50455045
fn airCeilFloorTrunc(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
@@ -5130,8 +5130,12 @@ fn signedSat(self: *Self, lhs_operand: WValue, rhs_operand: WValue, ty: Type, op
51305130
const wasm_bits = toWasmBits(int_info.bits).?;
51315131
const is_wasm_bits = wasm_bits == int_info.bits;
51325132

5133-
const lhs = if (!is_wasm_bits) try self.signAbsValue(lhs_operand, ty) else lhs_operand;
5134-
const rhs = if (!is_wasm_bits) try self.signAbsValue(rhs_operand, ty) else rhs_operand;
5133+
const lhs = if (!is_wasm_bits) lhs: {
5134+
break :lhs try (try self.signAbsValue(lhs_operand, ty)).toLocal(self, ty);
5135+
} else lhs_operand;
5136+
const rhs = if (!is_wasm_bits) rhs: {
5137+
break :rhs try (try self.signAbsValue(rhs_operand, ty)).toLocal(self, ty);
5138+
} else rhs_operand;
51355139

51365140
const max_val: u64 = @intCast(u64, (@as(u65, 1) << @intCast(u7, int_info.bits - 1)) - 1);
51375141
const min_val: i64 = (-@intCast(i64, @intCast(u63, max_val))) - 1;

0 commit comments

Comments
 (0)