authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-24 00:00:15+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-02-24 00:00:15+01:00
log9d098318e2b16a35803d84b70b48c04111f2dddf
treedf9d5d2e3aa86c46bcc1ae4f577e35c92babb9e6
parent136a43934bc08dc3aee85f1182904b97456601d3
parentf91fe9afb92dda2b7b1ae37147ce7af40101d5ea
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10977 from joachimschmidt557/stage2-aarch64

stage2 AArch64: more support for PIE targets (Mach-O)

10 files changed, 92 insertions(+), 118 deletions(-)

src/arch/aarch64/CodeGen.zig+30-32
......@@ -1717,6 +1717,8 @@ fn reuseOperand(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, op_ind
17171717
17181718fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!void {
17191719 const elem_ty = ptr_ty.elemType();
1720 const elem_size = elem_ty.abiSize(self.target.*);
1721
17201722 switch (ptr) {
17211723 .none => unreachable,
17221724 .undef => unreachable,
......@@ -1736,17 +1738,16 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo
17361738 self.register_manager.freezeRegs(&.{addr_reg});
17371739 defer self.register_manager.unfreezeRegs(&.{addr_reg});
17381740
1739 const abi_size = elem_ty.abiSize(self.target.*);
17401741 switch (dst_mcv) {
17411742 .dead => unreachable,
17421743 .undef => unreachable,
17431744 .compare_flags_signed, .compare_flags_unsigned => unreachable,
17441745 .embedded_in_code => unreachable,
17451746 .register => |dst_reg| {
1746 try self.genLdrRegister(dst_reg, addr_reg, abi_size);
1747 try self.genLdrRegister(dst_reg, addr_reg, elem_size);
17471748 },
17481749 .stack_offset => |off| {
1749 if (abi_size <= 8) {
1750 if (elem_size <= 8) {
17501751 const tmp_reg = try self.register_manager.allocReg(null);
17511752 self.register_manager.freezeRegs(&.{tmp_reg});
17521753 defer self.register_manager.unfreezeRegs(&.{tmp_reg});
......@@ -1766,17 +1767,7 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo
17661767 const tmp_reg = regs[3];
17671768
17681769 // sub dst_reg, fp, #off
1769 const elem_size = @intCast(u32, elem_ty.abiSize(self.target.*));
1770 const adj_off = off + elem_size;
1771 const offset = math.cast(u12, adj_off) catch return self.fail("TODO load: larger stack offsets", .{});
1772 _ = try self.addInst(.{
1773 .tag = .sub_immediate,
1774 .data = .{ .rr_imm12_sh = .{
1775 .rd = dst_reg,
1776 .rn = .x29,
1777 .imm12 = offset,
1778 } },
1779 });
1770 try self.genSetReg(ptr_ty, dst_reg, .{ .ptr_stack_offset = off });
17801771
17811772 // mov len, #elem_size
17821773 try self.genSetReg(Type.usize, len_reg, .{ .immediate = elem_size });
......@@ -2046,14 +2037,11 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
20462037 },
20472038 .memory,
20482039 .stack_offset,
2049 => {
2050 const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr);
2051 try self.store(.{ .register = addr_reg }, value, ptr_ty, value_ty);
2052 },
20532040 .got_load,
20542041 .direct_load,
20552042 => {
2056 return self.fail("TODO implement storing to {}", .{ptr});
2043 const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr);
2044 try self.store(.{ .register = addr_reg }, value, ptr_ty, value_ty);
20572045 },
20582046 }
20592047}
......@@ -3142,10 +3130,6 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
31423130 },
31433131 .got_load,
31443132 .direct_load,
3145 => |sym_index| {
3146 _ = sym_index;
3147 return self.fail("TODO implement set stack variable from {}", .{mcv});
3148 },
31493133 .memory,
31503134 .stack_offset,
31513135 => {
......@@ -3187,6 +3171,25 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
31873171 });
31883172 },
31893173 .memory => |addr| try self.genSetReg(Type.usize, src_reg, .{ .immediate = addr }),
3174 .got_load,
3175 .direct_load,
3176 => |sym_index| {
3177 const tag: Mir.Inst.Tag = switch (mcv) {
3178 .got_load => .load_memory_ptr_got,
3179 .direct_load => .load_memory_ptr_direct,
3180 else => unreachable,
3181 };
3182 _ = try self.addInst(.{
3183 .tag = tag,
3184 .data = .{
3185 .payload = try self.addExtra(Mir.LoadMemoryPie{
3186 .register = @enumToInt(src_reg),
3187 .atom_index = self.mod_fn.owner_decl.link.macho.local_sym_index,
3188 .sym_index = sym_index,
3189 }),
3190 },
3191 });
3192 },
31903193 else => unreachable,
31913194 }
31923195
......@@ -3318,15 +3321,10 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
33183321 });
33193322 },
33203323 .memory => |addr| {
3321 _ = try self.addInst(.{
3322 .tag = .load_memory,
3323 .data = .{
3324 .load_memory = .{
3325 .register = @enumToInt(reg),
3326 .addr = @intCast(u32, addr),
3327 },
3328 },
3329 });
3324 // The value is in memory at a hard-coded address.
3325 // If the type is a pointer, it means the pointer address is at this memory location.
3326 try self.genSetReg(ty, reg, .{ .immediate = addr });
3327 try self.genLdrRegister(reg, reg, ty.abiSize(self.target.*));
33303328 },
33313329 .stack_offset => |unadjusted_off| {
33323330 const abi_size = ty.abiSize(self.target.*);
src/arch/aarch64/Emit.zig+47-71
......@@ -108,9 +108,10 @@ pub fn emitMir(
108108
109109 .eor_shifted_register => try emit.mirLogicalShiftedRegister(inst),
110110
111 .load_memory => try emit.mirLoadMemory(inst),
112111 .load_memory_got => try emit.mirLoadMemoryPie(inst),
113112 .load_memory_direct => try emit.mirLoadMemoryPie(inst),
113 .load_memory_ptr_got => try emit.mirLoadMemoryPie(inst),
114 .load_memory_ptr_direct => try emit.mirLoadMemoryPie(inst),
114115
115116 .ldp => try emit.mirLoadStoreRegisterPair(inst),
116117 .stp => try emit.mirLoadStoreRegisterPair(inst),
......@@ -209,17 +210,9 @@ fn instructionSize(emit: *Emit, inst: Mir.Inst.Index) usize {
209210 switch (tag) {
210211 .load_memory_got,
211212 .load_memory_direct,
213 .load_memory_ptr_got,
214 .load_memory_ptr_direct,
212215 => return 2 * 4,
213 .load_memory => {
214 const load_memory = emit.mir.instructions.items(.data)[inst].load_memory;
215 const addr = load_memory.addr;
216
217 // movz, [movk, ...], ldr
218 if (addr <= math.maxInt(u16)) return 2 * 4;
219 if (addr <= math.maxInt(u32)) return 3 * 4;
220 if (addr <= math.maxInt(u48)) return 4 * 4;
221 return 5 * 4;
222 },
223216 .pop_regs, .push_regs => {
224217 const reg_list = emit.mir.instructions.items(.data)[inst].reg_list;
225218 const number_of_regs = @popCount(u32, reg_list);
......@@ -655,21 +648,6 @@ fn mirLogicalShiftedRegister(emit: *Emit, inst: Mir.Inst.Index) !void {
655648 }
656649}
657650
658fn mirLoadMemory(emit: *Emit, inst: Mir.Inst.Index) !void {
659 assert(emit.mir.instructions.items(.tag)[inst] == .load_memory);
660 const load_memory = emit.mir.instructions.items(.data)[inst].load_memory;
661 const reg = @intToEnum(Register, load_memory.register);
662 const addr = load_memory.addr;
663 // The value is in memory at a hard-coded address.
664 // If the type is a pointer, it means the pointer address is at this memory location.
665 try emit.moveImmediate(reg, addr);
666 try emit.writeInstruction(Instruction.ldr(
667 reg,
668 reg,
669 Instruction.LoadStoreOffset.none,
670 ));
671}
672
673651fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
674652 const tag = emit.mir.instructions.items(.tag)[inst];
675653 const payload = emit.mir.instructions.items(.data)[inst].payload;
......@@ -681,12 +659,25 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
681659 const offset = @intCast(u32, emit.code.items.len);
682660 try emit.writeInstruction(Instruction.adrp(reg, 0));
683661
684 // ldr reg, reg, offset
685 try emit.writeInstruction(Instruction.ldr(
686 reg,
687 reg,
688 Instruction.LoadStoreOffset.imm(0),
689 ));
662 switch (tag) {
663 .load_memory_got,
664 .load_memory_direct,
665 => {
666 // ldr reg, reg, offset
667 try emit.writeInstruction(Instruction.ldr(
668 reg,
669 reg,
670 Instruction.LoadStoreOffset.imm(0),
671 ));
672 },
673 .load_memory_ptr_got,
674 .load_memory_ptr_direct,
675 => {
676 // add reg, reg, offset
677 try emit.writeInstruction(Instruction.add(reg, reg, 0, false));
678 },
679 else => unreachable,
680 }
690681
691682 if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
692683 const atom = macho_file.atom_by_index_table.get(data.atom_index).?;
......@@ -699,8 +690,12 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
699690 .pcrel = true,
700691 .length = 2,
701692 .@"type" = switch (tag) {
702 .load_memory_got => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGE21),
703 .load_memory_direct => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_PAGE21),
693 .load_memory_got,
694 .load_memory_ptr_got,
695 => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGE21),
696 .load_memory_direct,
697 .load_memory_ptr_direct,
698 => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_PAGE21),
704699 else => unreachable,
705700 },
706701 });
......@@ -713,8 +708,12 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
713708 .pcrel = false,
714709 .length = 2,
715710 .@"type" = switch (tag) {
716 .load_memory_got => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12),
717 .load_memory_direct => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_PAGEOFF12),
711 .load_memory_got,
712 .load_memory_ptr_got,
713 => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12),
714 .load_memory_direct,
715 .load_memory_ptr_direct,
716 => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_PAGEOFF12),
718717 else => unreachable,
719718 },
720719 });
......@@ -741,6 +740,7 @@ fn mirLoadStoreRegisterPair(emit: *Emit, inst: Mir.Inst.Index) !void {
741740fn mirLoadStoreStack(emit: *Emit, inst: Mir.Inst.Index) !void {
742741 const tag = emit.mir.instructions.items(.tag)[inst];
743742 const load_store_stack = emit.mir.instructions.items(.data)[inst].load_store_stack;
743 const rt = load_store_stack.rt;
744744
745745 const raw_offset = emit.stack_size - load_store_stack.offset;
746746 const offset = switch (tag) {
......@@ -760,7 +760,7 @@ fn mirLoadStoreStack(emit: *Emit, inst: Mir.Inst.Index) !void {
760760 }
761761 },
762762 .ldr_stack, .str_stack => blk: {
763 const alignment: u32 = switch (load_store_stack.rt.size()) {
763 const alignment: u32 = switch (rt.size()) {
764764 32 => 4,
765765 64 => 8,
766766 else => unreachable,
......@@ -777,36 +777,12 @@ fn mirLoadStoreStack(emit: *Emit, inst: Mir.Inst.Index) !void {
777777 };
778778
779779 switch (tag) {
780 .ldr_stack => try emit.writeInstruction(Instruction.ldr(
781 load_store_stack.rt,
782 Register.sp,
783 offset,
784 )),
785 .ldrb_stack => try emit.writeInstruction(Instruction.ldrb(
786 load_store_stack.rt,
787 Register.sp,
788 offset,
789 )),
790 .ldrh_stack => try emit.writeInstruction(Instruction.ldrh(
791 load_store_stack.rt,
792 Register.sp,
793 offset,
794 )),
795 .str_stack => try emit.writeInstruction(Instruction.str(
796 load_store_stack.rt,
797 Register.sp,
798 offset,
799 )),
800 .strb_stack => try emit.writeInstruction(Instruction.strb(
801 load_store_stack.rt,
802 Register.sp,
803 offset,
804 )),
805 .strh_stack => try emit.writeInstruction(Instruction.strh(
806 load_store_stack.rt,
807 Register.sp,
808 offset,
809 )),
780 .ldr_stack => try emit.writeInstruction(Instruction.ldr(rt, .sp, offset)),
781 .ldrb_stack => try emit.writeInstruction(Instruction.ldrb(rt, .sp, offset)),
782 .ldrh_stack => try emit.writeInstruction(Instruction.ldrh(rt, .sp, offset)),
783 .str_stack => try emit.writeInstruction(Instruction.str(rt, .sp, offset)),
784 .strb_stack => try emit.writeInstruction(Instruction.strb(rt, .sp, offset)),
785 .strh_stack => try emit.writeInstruction(Instruction.strh(rt, .sp, offset)),
810786 else => unreachable,
811787 }
812788}
......@@ -914,7 +890,7 @@ fn mirPushPopRegs(emit: *Emit, inst: Mir.Inst.Index) !void {
914890 if (count == number_of_regs - 1) {
915891 try emit.writeInstruction(Instruction.ldr(
916892 reg,
917 Register.sp,
893 .sp,
918894 Instruction.LoadStoreOffset.imm_post_index(16),
919895 ));
920896 } else {
......@@ -924,7 +900,7 @@ fn mirPushPopRegs(emit: *Emit, inst: Mir.Inst.Index) !void {
924900 try emit.writeInstruction(Instruction.ldp(
925901 reg,
926902 other_reg,
927 Register.sp,
903 .sp,
928904 Instruction.LoadStorePairOffset.post_index(16),
929905 ));
930906 }
......@@ -944,7 +920,7 @@ fn mirPushPopRegs(emit: *Emit, inst: Mir.Inst.Index) !void {
944920 if (count == number_of_regs - 1) {
945921 try emit.writeInstruction(Instruction.str(
946922 reg,
947 Register.sp,
923 .sp,
948924 Instruction.LoadStoreOffset.imm_pre_index(-16),
949925 ));
950926 } else {
......@@ -954,7 +930,7 @@ fn mirPushPopRegs(emit: *Emit, inst: Mir.Inst.Index) !void {
954930 try emit.writeInstruction(Instruction.stp(
955931 other_reg,
956932 reg,
957 Register.sp,
933 .sp,
958934 Instruction.LoadStorePairOffset.pre_index(-16),
959935 ));
960936 }
src/arch/aarch64/Mir.zig+11-3
......@@ -56,14 +56,22 @@ pub const Inst = struct {
5656 dbg_line,
5757 /// Bitwise Exclusive OR (shifted register)
5858 eor_shifted_register,
59 /// Pseudo-instruction: Load memory
59 /// Loads the contents into a register
6060 ///
61 /// Payload is `load_memory`
62 load_memory,
6361 /// Payload is `LoadMemoryPie`
6462 load_memory_got,
63 /// Loads the contents into a register
64 ///
6565 /// Payload is `LoadMemoryPie`
6666 load_memory_direct,
67 /// Loads the address into a register
68 ///
69 /// Payload is `LoadMemoryPie`
70 load_memory_ptr_got,
71 /// Loads the address into a register
72 ///
73 /// Payload is `LoadMemoryPie`
74 load_memory_ptr_direct,
6775 /// Load Pair of Registers
6876 ldp,
6977 /// Pseudo-instruction: Load from stack
src/arch/aarch64/bits.zig+4-4
......@@ -1486,19 +1486,19 @@ test "serialize instructions" {
14861486 .expected = 0b1_00_10000_1111111111111111110_00010,
14871487 },
14881488 .{ // stp x1, x2, [sp, #8]
1489 .inst = Instruction.stp(.x1, .x2, Register.sp, Instruction.LoadStorePairOffset.signed(8)),
1489 .inst = Instruction.stp(.x1, .x2, .sp, Instruction.LoadStorePairOffset.signed(8)),
14901490 .expected = 0b10_101_0_010_0_0000001_00010_11111_00001,
14911491 },
14921492 .{ // ldp x1, x2, [sp, #8]
1493 .inst = Instruction.ldp(.x1, .x2, Register.sp, Instruction.LoadStorePairOffset.signed(8)),
1493 .inst = Instruction.ldp(.x1, .x2, .sp, Instruction.LoadStorePairOffset.signed(8)),
14941494 .expected = 0b10_101_0_010_1_0000001_00010_11111_00001,
14951495 },
14961496 .{ // stp x1, x2, [sp, #-16]!
1497 .inst = Instruction.stp(.x1, .x2, Register.sp, Instruction.LoadStorePairOffset.pre_index(-16)),
1497 .inst = Instruction.stp(.x1, .x2, .sp, Instruction.LoadStorePairOffset.pre_index(-16)),
14981498 .expected = 0b10_101_0_011_0_1111110_00010_11111_00001,
14991499 },
15001500 .{ // ldp x1, x2, [sp], #16
1501 .inst = Instruction.ldp(.x1, .x2, Register.sp, Instruction.LoadStorePairOffset.post_index(16)),
1501 .inst = Instruction.ldp(.x1, .x2, .sp, Instruction.LoadStorePairOffset.post_index(16)),
15021502 .expected = 0b10_101_0_001_1_0000010_00010_11111_00001,
15031503 },
15041504 .{ // and x0, x4, x2
test/behavior/align.zig-1
......@@ -305,7 +305,6 @@ fn testIndex2(ptr: [*]align(4) u8, index: usize, comptime T: type) !void {
305305}
306306
307307test "alignment of function with c calling convention" {
308 if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest;
309308 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
310309
311310 var runtime_nothing = &nothing;
test/behavior/basic.zig-3
......@@ -48,7 +48,6 @@ const g1: i32 = 1233 + 1;
4848var g2: i32 = 0;
4949
5050test "global variables" {
51 if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest;
5251 try expect(g2 == 0);
5352 g2 = g1;
5453 try expect(g2 == 1234);
......@@ -604,7 +603,6 @@ test "comptime cast fn to ptr" {
604603}
605604
606605test "equality compare fn ptrs" {
607 if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest;
608606 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
609607
610608 var a = &emptyFn;
......@@ -612,7 +610,6 @@ test "equality compare fn ptrs" {
612610}
613611
614612test "self reference through fn ptr field" {
615 if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest;
616613 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
617614 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
618615
test/behavior/bugs/2006.zig-1
......@@ -6,7 +6,6 @@ const S = struct {
66 p: *S,
77};
88test "bug 2006" {
9 if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest;
109 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
1110 var a: S = undefined;
1211 a = S{ .p = undefined };
test/behavior/cast.zig-1
......@@ -1013,7 +1013,6 @@ test "cast from array reference to fn: comptime fn ptr" {
10131013 try expect(@ptrToInt(f) == @ptrToInt(&global_array));
10141014}
10151015test "cast from array reference to fn: runtime fn ptr" {
1016 if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest;
10171016 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
10181017 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
10191018 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
test/behavior/slice.zig-1
......@@ -81,7 +81,6 @@ fn assertLenIsZero(msg: []const u8) !void {
8181}
8282
8383test "access len index of sentinel-terminated slice" {
84 if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest;
8584 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
8685
8786 const S = struct {
test/behavior/struct.zig-1
......@@ -66,7 +66,6 @@ const SmallStruct = struct {
6666};
6767
6868test "lower unnamed constants" {
69 if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest;
7069 var foo = SmallStruct{ .a = 1, .b = 255 };
7170 try expect(foo.first() == 1);
7271 try expect(foo.second() == 255);