authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-02-23 12:39:58+01:00
committergravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-02-23 21:57:59+01:00
log4683f94463cf7165bcaf88c8de73dd7ed5279c60
tree00b04a1395b44f898f5360ea149da286f294cc4f
parentacec06cfaf9a82ec8037a23993ff36fa72eb6e82
signaturelock-open Commit is signed but in an unrecognized format.

stage2 AArch64: remove MIR load_memory instruction

This instruction now just represents loading from a hard-coded adrress after extracting the other use cases for load_memory into load_got and load_direct.

4 files changed, 20 insertions(+), 78 deletions(-)

src/arch/aarch64/CodeGen.zig+4-9
......@@ -3318,15 +3318,10 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
33183318 });
33193319 },
33203320 .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 });
3321 // The value is in memory at a hard-coded address.
3322 // If the type is a pointer, it means the pointer address is at this memory location.
3323 try self.genSetReg(ty, reg, .{ .immediate = addr });
3324 try self.genLdrRegister(reg, reg, ty.abiSize(self.target.*));
33303325 },
33313326 .stack_offset => |unadjusted_off| {
33323327 const abi_size = ty.abiSize(self.target.*);
src/arch/aarch64/Emit.zig+12-61
......@@ -108,7 +108,6 @@ 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),
114113
......@@ -210,16 +209,6 @@ fn instructionSize(emit: *Emit, inst: Mir.Inst.Index) usize {
210209 .load_memory_got,
211210 .load_memory_direct,
212211 => 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 },
223212 .pop_regs, .push_regs => {
224213 const reg_list = emit.mir.instructions.items(.data)[inst].reg_list;
225214 const number_of_regs = @popCount(u32, reg_list);
......@@ -655,21 +644,6 @@ fn mirLogicalShiftedRegister(emit: *Emit, inst: Mir.Inst.Index) !void {
655644 }
656645}
657646
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
673647fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
674648 const tag = emit.mir.instructions.items(.tag)[inst];
675649 const payload = emit.mir.instructions.items(.data)[inst].payload;
......@@ -741,6 +715,7 @@ fn mirLoadStoreRegisterPair(emit: *Emit, inst: Mir.Inst.Index) !void {
741715fn mirLoadStoreStack(emit: *Emit, inst: Mir.Inst.Index) !void {
742716 const tag = emit.mir.instructions.items(.tag)[inst];
743717 const load_store_stack = emit.mir.instructions.items(.data)[inst].load_store_stack;
718 const rt = load_store_stack.rt;
744719
745720 const raw_offset = emit.stack_size - load_store_stack.offset;
746721 const offset = switch (tag) {
......@@ -760,7 +735,7 @@ fn mirLoadStoreStack(emit: *Emit, inst: Mir.Inst.Index) !void {
760735 }
761736 },
762737 .ldr_stack, .str_stack => blk: {
763 const alignment: u32 = switch (load_store_stack.rt.size()) {
738 const alignment: u32 = switch (rt.size()) {
764739 32 => 4,
765740 64 => 8,
766741 else => unreachable,
......@@ -777,36 +752,12 @@ fn mirLoadStoreStack(emit: *Emit, inst: Mir.Inst.Index) !void {
777752 };
778753
779754 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 )),
755 .ldr_stack => try emit.writeInstruction(Instruction.ldr(rt, .sp, offset)),
756 .ldrb_stack => try emit.writeInstruction(Instruction.ldrb(rt, .sp, offset)),
757 .ldrh_stack => try emit.writeInstruction(Instruction.ldrh(rt, .sp, offset)),
758 .str_stack => try emit.writeInstruction(Instruction.str(rt, .sp, offset)),
759 .strb_stack => try emit.writeInstruction(Instruction.strb(rt, .sp, offset)),
760 .strh_stack => try emit.writeInstruction(Instruction.strh(rt, .sp, offset)),
810761 else => unreachable,
811762 }
812763}
......@@ -914,7 +865,7 @@ fn mirPushPopRegs(emit: *Emit, inst: Mir.Inst.Index) !void {
914865 if (count == number_of_regs - 1) {
915866 try emit.writeInstruction(Instruction.ldr(
916867 reg,
917 Register.sp,
868 .sp,
918869 Instruction.LoadStoreOffset.imm_post_index(16),
919870 ));
920871 } else {
......@@ -924,7 +875,7 @@ fn mirPushPopRegs(emit: *Emit, inst: Mir.Inst.Index) !void {
924875 try emit.writeInstruction(Instruction.ldp(
925876 reg,
926877 other_reg,
927 Register.sp,
878 .sp,
928879 Instruction.LoadStorePairOffset.post_index(16),
929880 ));
930881 }
......@@ -944,7 +895,7 @@ fn mirPushPopRegs(emit: *Emit, inst: Mir.Inst.Index) !void {
944895 if (count == number_of_regs - 1) {
945896 try emit.writeInstruction(Instruction.str(
946897 reg,
947 Register.sp,
898 .sp,
948899 Instruction.LoadStoreOffset.imm_pre_index(-16),
949900 ));
950901 } else {
......@@ -954,7 +905,7 @@ fn mirPushPopRegs(emit: *Emit, inst: Mir.Inst.Index) !void {
954905 try emit.writeInstruction(Instruction.stp(
955906 other_reg,
956907 reg,
957 Register.sp,
908 .sp,
958909 Instruction.LoadStorePairOffset.pre_index(-16),
959910 ));
960911 }
src/arch/aarch64/Mir.zig-4
......@@ -56,10 +56,6 @@ pub const Inst = struct {
5656 dbg_line,
5757 /// Bitwise Exclusive OR (shifted register)
5858 eor_shifted_register,
59 /// Pseudo-instruction: Load memory
60 ///
61 /// Payload is `load_memory`
62 load_memory,
6359 /// Payload is `LoadMemoryPie`
6460 load_memory_got,
6561 /// Payload is `LoadMemoryPie`
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