authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-03-06 19:02:02+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-03-06 19:02:02+01:00
loge4039cecc76d043ddeecee8865b34603e85cab1a
treecdcefacaae4a6cca9d29a0c38b3f622be8a2e373
parent23e2368ac339842b907ccc36580c3a471bb26d43

x64: fix (un)wrapping error unions + refactor


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

src/arch/x86_64/CodeGen.zig+14-9
......@@ -1032,7 +1032,8 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void {
10321032 const dst_bit_size = dst_ty.bitSize(self.target.*);
10331033 const is_power_of_two = (dst_bit_size & (dst_bit_size - 1)) == 0;
10341034 if (!is_power_of_two or dst_bit_size < 8) {
1035 const shift = @intCast(u6, 64 - dst_ty.bitSize(self.target.*));
1035 const max_reg_bit_width = Register.rax.size();
1036 const shift = @intCast(u6, max_reg_bit_width - dst_ty.bitSize(self.target.*));
10361037 const mask = (~@as(u64, 0)) >> shift;
10371038 try self.genBinMathOpMir(.@"and", Type.usize, .{ .register = reg }, .{ .immediate = mask });
10381039
......@@ -1739,7 +1740,7 @@ fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) !void {
17391740 .register => {
17401741 // TODO reuse the operand
17411742 const result = try self.copyToRegisterWithInstTracking(inst, optional_ty, operand);
1742 const shift = @intCast(u8, offset * 8);
1743 const shift = @intCast(u8, offset * @sizeOf(usize));
17431744 try self.shiftRegister(result.register, @intCast(u8, shift));
17441745 break :result result;
17451746 },
......@@ -1809,16 +1810,17 @@ fn airUnwrapErrPayload(self: *Self, inst: Air.Inst.Index) !void {
18091810 operand.freezeIfRegister(&self.register_manager);
18101811 defer operand.unfreezeIfRegister(&self.register_manager);
18111812
1813 const abi_align = err_union_ty.abiAlignment(self.target.*);
18121814 const err_ty = err_union_ty.errorUnionSet();
1815 const err_abi_size = mem.alignForwardGeneric(u32, @intCast(u32, err_ty.abiSize(self.target.*)), abi_align);
18131816 switch (operand) {
18141817 .stack_offset => |off| {
1815 const err_abi_size = @intCast(u32, err_ty.abiSize(self.target.*));
18161818 const offset = off - @intCast(i32, err_abi_size);
18171819 break :result MCValue{ .stack_offset = offset };
18181820 },
18191821 .register => {
18201822 // TODO reuse operand
1821 const shift = @intCast(u6, err_ty.bitSize(self.target.*));
1823 const shift = @intCast(u6, err_abi_size * @sizeOf(usize));
18221824 const result = try self.copyToRegisterWithInstTracking(inst, err_union_ty, operand);
18231825 try self.shiftRegister(result.register.to64(), shift);
18241826 break :result MCValue{
......@@ -1914,8 +1916,9 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void {
19141916 const abi_align = error_union_ty.abiAlignment(self.target.*);
19151917 const err_abi_size = @intCast(u32, error_ty.abiSize(self.target.*));
19161918 const stack_offset = @intCast(i32, try self.allocMem(inst, abi_size, abi_align));
1919 const offset = mem.alignForwardGeneric(u32, err_abi_size, abi_align);
19171920 try self.genSetStack(error_ty, stack_offset, .{ .immediate = 0 }, .{});
1918 try self.genSetStack(payload_ty, stack_offset - @intCast(i32, err_abi_size), operand, .{});
1921 try self.genSetStack(payload_ty, stack_offset - @intCast(i32, offset), operand, .{});
19191922
19201923 return self.finishAir(inst, .{ .stack_offset = stack_offset }, .{ ty_op.operand, .none, .none });
19211924}
......@@ -1937,8 +1940,9 @@ fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void {
19371940 const abi_align = error_union_ty.abiAlignment(self.target.*);
19381941 const err_abi_size = @intCast(u32, error_ty.abiSize(self.target.*));
19391942 const stack_offset = @intCast(i32, try self.allocMem(inst, abi_size, abi_align));
1943 const offset = mem.alignForwardGeneric(u32, err_abi_size, abi_align);
19401944 try self.genSetStack(error_ty, stack_offset, err, .{});
1941 try self.genSetStack(payload_ty, stack_offset - @intCast(i32, err_abi_size), .undef, .{});
1945 try self.genSetStack(payload_ty, stack_offset - @intCast(i32, offset), .undef, .{});
19421946 break :result MCValue{ .stack_offset = stack_offset };
19431947 };
19441948
......@@ -2243,7 +2247,7 @@ fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) !void {
22432247 },
22442248 .register => {
22452249 const shift: u6 = if (layout.tag_align < layout.payload_align)
2246 @intCast(u6, layout.payload_size * 8)
2250 @intCast(u6, layout.payload_size * @sizeOf(usize))
22472251 else
22482252 0;
22492253 const result = try self.copyToRegisterWithInstTracking(inst, union_ty, operand);
......@@ -2819,11 +2823,12 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
28192823 };
28202824
28212825 // Shift by struct_field_offset.
2822 const shift = @intCast(u8, struct_field_offset * 8);
2826 const shift = @intCast(u8, struct_field_offset * @sizeOf(usize));
28232827 try self.shiftRegister(dst_mcv.register, shift);
28242828
28252829 // Mask with reg.size() - struct_field_size
2826 const mask_shift = @intCast(u6, (64 - struct_field_ty.bitSize(self.target.*)));
2830 const max_reg_bit_width = Register.rax.size();
2831 const mask_shift = @intCast(u6, (max_reg_bit_width - struct_field_ty.bitSize(self.target.*)));
28272832 const mask = (~@as(u64, 0)) >> mask_shift;
28282833 try self.genBinMathOpMir(.@"and", Type.usize, dst_mcv, .{ .immediate = mask });
28292834