From c70c33359498bfe33a2f60cfcc5ea401a277d5bf Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Mon, 4 Dec 2023 13:31:34 -0500 Subject: [PATCH] x86_64: fix packed struct field reuse --- src/arch/x86_64/CodeGen.zig | 8 +++++--- test/behavior/packed-struct.zig | 1 - 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index e1cc9470cb..0a4c9844dc 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -7324,8 +7324,8 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { .load_frame => |frame_addr| { const field_abi_size: u32 = @intCast(field_ty.abiSize(mod)); if (field_off % 8 == 0) { - const off_mcv = - src_mcv.address().offset(@intCast(@divExact(field_off, 8))).deref(); + const field_byte_off = @divExact(field_off, 8); + const off_mcv = src_mcv.address().offset(@intCast(field_byte_off)).deref(); const field_bit_size = field_ty.bitSize(mod); if (field_abi_size <= 8) { @@ -7350,7 +7350,9 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { try self.copyToRegisterWithInstTracking(inst, field_ty, dst_mcv); } - const dst_mcv = if (self.reuseOperand(inst, operand, 0, src_mcv)) + const container_abi_size: u32 = @intCast(container_ty.abiSize(mod)); + const dst_mcv = if (field_byte_off + field_abi_size <= container_abi_size and + self.reuseOperand(inst, operand, 0, src_mcv)) off_mcv else dst: { const dst_mcv = try self.allocRegOrMem(inst, true); diff --git a/test/behavior/packed-struct.zig b/test/behavior/packed-struct.zig index adbb0b977a..f4aceaa82d 100644 --- a/test/behavior/packed-struct.zig +++ b/test/behavior/packed-struct.zig @@ -804,7 +804,6 @@ test "nested packed struct at non-zero offset" { } test "nested packed struct at non-zero offset 2" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO