authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-03-06 19:37:43+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-06 19:37:43+01:00
log12e636c24e92dbe02508b89c1363c357ccef2192
treecdcefacaae4a6cca9d29a0c38b3f622be8a2e373
parentbf972e44d5d0ce704cae99957d565c55ea16335d
parente4039cecc76d043ddeecee8865b34603e85cab1a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11068 from Luukdegram/codegen-fixes

stage2: Fix codegen for unions and error unions

2 files changed, 43 insertions(+), 35 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
src/codegen.zig+29-26
......@@ -573,16 +573,10 @@ pub fn generateSymbol(
573573 const layout = typed_value.ty.unionGetLayout(target);
574574
575575 if (layout.payload_size == 0) {
576 switch (try generateSymbol(bin_file, src_loc, .{
576 return generateSymbol(bin_file, src_loc, .{
577577 .ty = typed_value.ty.unionTagType().?,
578578 .val = union_obj.tag,
579 }, code, debug_output, reloc_info)) {
580 .appended => {},
581 .externally_managed => |external_slice| {
582 code.appendSliceAssumeCapacity(external_slice);
583 },
584 .fail => |em| return Result{ .fail = em },
585 }
579 }, code, debug_output, reloc_info);
586580 }
587581
588582 // Check if we should store the tag first.
......@@ -703,20 +697,30 @@ pub fn generateSymbol(
703697
704698 const abi_align = typed_value.ty.abiAlignment(target);
705699
706 const error_val = if (!is_payload) typed_value.val else Value.initTag(.zero);
707 const begin = code.items.len;
708 switch (try generateSymbol(bin_file, src_loc, .{
709 .ty = error_ty,
710 .val = error_val,
711 }, code, debug_output, reloc_info)) {
712 .appended => {},
713 .externally_managed => |external_slice| {
714 code.appendSliceAssumeCapacity(external_slice);
715 },
716 .fail => |em| return Result{ .fail = em },
700 {
701 const error_val = if (!is_payload) typed_value.val else Value.initTag(.zero);
702 const begin = code.items.len;
703 switch (try generateSymbol(bin_file, src_loc, .{
704 .ty = error_ty,
705 .val = error_val,
706 }, code, debug_output, reloc_info)) {
707 .appended => {},
708 .externally_managed => |external_slice| {
709 code.appendSliceAssumeCapacity(external_slice);
710 },
711 .fail => |em| return Result{ .fail = em },
712 }
713 const unpadded_end = code.items.len - begin;
714 const padded_end = mem.alignForwardGeneric(u64, unpadded_end, abi_align);
715 const padding = try math.cast(usize, padded_end - unpadded_end);
716
717 if (padding > 0) {
718 try code.writer().writeByteNTimes(0, padding);
719 }
717720 }
718721
719722 if (payload_ty.hasRuntimeBits()) {
723 const begin = code.items.len;
720724 const payload_val = if (typed_value.val.castTag(.eu_payload)) |val| val.data else Value.initTag(.undef);
721725 switch (try generateSymbol(bin_file, src_loc, .{
722726 .ty = payload_ty,
......@@ -728,14 +732,13 @@ pub fn generateSymbol(
728732 },
729733 .fail => |em| return Result{ .fail = em },
730734 }
731 }
735 const unpadded_end = code.items.len - begin;
736 const padded_end = mem.alignForwardGeneric(u64, unpadded_end, abi_align);
737 const padding = try math.cast(usize, padded_end - unpadded_end);
732738
733 const unpadded_end = code.items.len - begin;
734 const padded_end = mem.alignForwardGeneric(u64, unpadded_end, abi_align);
735 const padding = try math.cast(usize, padded_end - unpadded_end);
736
737 if (padding > 0) {
738 try code.writer().writeByteNTimes(0, padding);
739 if (padding > 0) {
740 try code.writer().writeByteNTimes(0, padding);
741 }
739742 }
740743
741744 return Result{ .appended = {} };