authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-10 00:57:54+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-10 00:57:54+02:00
log485d8819b33e3fca1eb63b0d109019d0c3fc15fc
treeccd53cb90b53be7bdc1b5e24e543b3907178d47f
parentfc5a6e0e327c1b671aa782d24059e27e5aa55c84

aarch64: update codegen to using a global index rather than local index


2 files changed, 37 insertions(+), 54 deletions(-)

src/arch/aarch64/CodeGen.zig+35-50
......@@ -139,21 +139,10 @@ const MCValue = union(enum) {
139139 /// If the type is a pointer, it means the pointer address is at
140140 /// this memory location.
141141 memory: u64,
142 /// The value is in memory referenced indirectly via a GOT entry
143 /// index.
144 ///
145 /// If the type is a pointer, it means the pointer is referenced
146 /// indirectly via GOT. When lowered, linker will emit
147 /// relocations of type ARM64_RELOC_GOT_LOAD_PAGE21 and
148 /// ARM64_RELOC_GOT_LOAD_PAGEOFF12.
149 got_load: u32,
150 /// The value is in memory referenced directly via symbol index.
151 ///
152 /// If the type is a pointer, it means the pointer is referenced
153 /// directly via symbol index. When lowered, linker will emit a
154 /// relocation of type ARM64_RELOC_PAGE21 and
155 /// ARM64_RELOC_PAGEOFF12.
156 direct_load: u32,
142 /// The value is in memory but requires a linker relocation fixup:
143 /// * got - the value is referenced indirectly via GOT entry index (the linker emits a got-type reloc)
144 /// * direct - the value is referenced directly via symbol index index (the linker emits a displacement reloc)
145 linker_load: struct { @"type": enum { got, direct }, sym_index: u32 },
157146 /// The value is one of the stack variables.
158147 ///
159148 /// If the type is a pointer, it means the pointer address is in
......@@ -2959,8 +2948,7 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo
29592948 .memory,
29602949 .stack_offset,
29612950 .stack_argument_offset,
2962 .got_load,
2963 .direct_load,
2951 .linker_load,
29642952 => {
29652953 const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr);
29662954 try self.load(dst_mcv, .{ .register = addr_reg }, ptr_ty);
......@@ -3197,8 +3185,7 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
31973185 .memory,
31983186 .stack_offset,
31993187 .stack_argument_offset,
3200 .got_load,
3201 .direct_load,
3188 .linker_load,
32023189 => {
32033190 const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr);
32043191 try self.store(.{ .register = addr_reg }, value, ptr_ty, value_ty);
......@@ -3493,7 +3480,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
34933480 const func = func_payload.data;
34943481 const fn_owner_decl = mod.declPtr(func.owner_decl);
34953482 try self.genSetReg(Type.initTag(.u64), .x30, .{
3496 .got_load = fn_owner_decl.link.macho.sym_index,
3483 .linker_load = .{
3484 .@"type" = .got,
3485 .sym_index = fn_owner_decl.link.macho.sym_index,
3486 },
34973487 });
34983488 // blr x30
34993489 _ = try self.addInst(.{
......@@ -4427,8 +4417,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
44274417 .register = cond_reg,
44284418 });
44294419 },
4430 .got_load,
4431 .direct_load,
4420 .linker_load,
44324421 .memory,
44334422 .stack_argument_offset,
44344423 .stack_offset,
......@@ -4479,13 +4468,10 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
44794468 });
44804469 },
44814470 .memory => |addr| try self.genSetReg(Type.usize, src_reg, .{ .immediate = addr }),
4482 .got_load,
4483 .direct_load,
4484 => |sym_index| {
4485 const tag: Mir.Inst.Tag = switch (mcv) {
4486 .got_load => .load_memory_ptr_got,
4487 .direct_load => .load_memory_ptr_direct,
4488 else => unreachable,
4471 .linker_load => |load_struct| {
4472 const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
4473 .got => .load_memory_ptr_got,
4474 .direct => .load_memory_ptr_direct,
44894475 };
44904476 const mod = self.bin_file.options.module.?;
44914477 _ = try self.addInst(.{
......@@ -4494,7 +4480,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
44944480 .payload = try self.addExtra(Mir.LoadMemoryPie{
44954481 .register = @enumToInt(src_reg),
44964482 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
4497 .sym_index = sym_index,
4483 .sym_index = load_struct.sym_index,
44984484 }),
44994485 },
45004486 });
......@@ -4594,13 +4580,10 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
45944580 });
45954581 },
45964582 .register_with_overflow => unreachable, // doesn't fit into a register
4597 .got_load,
4598 .direct_load,
4599 => |sym_index| {
4600 const tag: Mir.Inst.Tag = switch (mcv) {
4601 .got_load => .load_memory_got,
4602 .direct_load => .load_memory_direct,
4603 else => unreachable,
4583 .linker_load => |load_struct| {
4584 const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
4585 .got => .load_memory_got,
4586 .direct => .load_memory_direct,
46044587 };
46054588 const mod = self.bin_file.options.module.?;
46064589 _ = try self.addInst(.{
......@@ -4609,7 +4592,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
46094592 .payload = try self.addExtra(Mir.LoadMemoryPie{
46104593 .register = @enumToInt(reg),
46114594 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
4612 .sym_index = sym_index,
4595 .sym_index = load_struct.sym_index,
46134596 }),
46144597 },
46154598 });
......@@ -4741,8 +4724,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
47414724 .register_with_overflow => {
47424725 return self.fail("TODO implement genSetStackArgument {}", .{mcv});
47434726 },
4744 .got_load,
4745 .direct_load,
4727 .linker_load,
47464728 .memory,
47474729 .stack_argument_offset,
47484730 .stack_offset,
......@@ -4785,13 +4767,10 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
47854767 });
47864768 },
47874769 .memory => |addr| try self.genSetReg(ptr_ty, src_reg, .{ .immediate = @intCast(u32, addr) }),
4788 .got_load,
4789 .direct_load,
4790 => |sym_index| {
4791 const tag: Mir.Inst.Tag = switch (mcv) {
4792 .got_load => .load_memory_ptr_got,
4793 .direct_load => .load_memory_ptr_direct,
4794 else => unreachable,
4770 .linker_load => |load_struct| {
4771 const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
4772 .got => .load_memory_ptr_got,
4773 .direct => .load_memory_ptr_direct,
47954774 };
47964775 const mod = self.bin_file.options.module.?;
47974776 _ = try self.addInst(.{
......@@ -4800,7 +4779,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
48004779 .payload = try self.addExtra(Mir.LoadMemoryPie{
48014780 .register = @enumToInt(src_reg),
48024781 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
4803 .sym_index = sym_index,
4782 .sym_index = load_struct.sym_index,
48044783 }),
48054784 },
48064785 });
......@@ -5107,7 +5086,10 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
51075086 // Because MachO is PIE-always-on, we defer memory address resolution until
51085087 // the linker has enough info to perform relocations.
51095088 assert(decl.link.macho.sym_index != 0);
5110 return MCValue{ .got_load = decl.link.macho.sym_index };
5089 return MCValue{ .linker_load = .{
5090 .@"type" = .got,
5091 .sym_index = decl.link.macho.sym_index,
5092 } };
51115093 } else if (self.bin_file.cast(link.File.Coff)) |_| {
51125094 return self.fail("TODO codegen COFF const Decl pointer", .{});
51135095 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {
......@@ -5129,7 +5111,10 @@ fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue {
51295111 const vaddr = elf_file.local_symbols.items[local_sym_index].st_value;
51305112 return MCValue{ .memory = vaddr };
51315113 } else if (self.bin_file.cast(link.File.MachO)) |_| {
5132 return MCValue{ .direct_load = local_sym_index };
5114 return MCValue{ .linker_load = .{
5115 .@"type" = .direct,
5116 .sym_index = local_sym_index,
5117 } };
51335118 } else if (self.bin_file.cast(link.File.Coff)) |_| {
51345119 return self.fail("TODO lower unnamed const in COFF", .{});
51355120 } else if (self.bin_file.cast(link.File.Plan9)) |_| {
src/arch/aarch64/Emit.zig+2-4
......@@ -681,12 +681,10 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
681681 };
682682 // Add relocation to the decl.
683683 const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?;
684 const target = macho_file.getGlobalByIndex(relocation.sym_index);
684685 try atom.relocs.append(emit.bin_file.allocator, .{
685686 .offset = offset,
686 .target = .{
687 .sym_index = relocation.sym_index,
688 .file = null,
689 },
687 .target = target,
690688 .addend = 0,
691689 .subtractor = null,
692690 .pcrel = true,