| ... | @@ -641,20 +641,33 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { | ... | @@ -641,20 +641,33 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { |
| 641 | const cc = self.fn_type.fnCallingConvention(); | 641 | const cc = self.fn_type.fnCallingConvention(); |
| 642 | if (cc != .Naked) { | 642 | if (cc != .Naked) { |
| 643 | // TODO Finish function prologue and epilogue for aarch64. | 643 | // TODO Finish function prologue and epilogue for aarch64. |
| 644 | // Reserve the stack for local variables, etc. | | |
| 645 | | 644 | |
| 646 | // stp fp, lr, [sp, #-16]! | 645 | // stp fp, lr, [sp, #-16]! |
| | 646 | // mov fp, sp |
| | 647 | // sub sp, sp, #reloc |
| 647 | writeInt(u32, try self.code.addManyAsArray(4), Instruction.stp( | 648 | writeInt(u32, try self.code.addManyAsArray(4), Instruction.stp( |
| 648 | .x29, | 649 | .x29, |
| 649 | .x30, | 650 | .x30, |
| 650 | Register.sp, | 651 | Register.sp, |
| 651 | Instruction.LoadStorePairOffset.pre_index(-16), | 652 | Instruction.LoadStorePairOffset.pre_index(-16), |
| 652 | ).toU32()); | 653 | ).toU32()); |
| | 654 | writeInt(u32, try self.code.addManyAsArray(4), Instruction.add(.x29, .xzr, 0, false).toU32()); |
| | 655 | const backpatch_reloc = self.code.items.len; |
| | 656 | try self.code.resize(backpatch_reloc + 4); |
| 653 | | 657 | |
| 654 | try self.dbgSetPrologueEnd(); | 658 | try self.dbgSetPrologueEnd(); |
| 655 | | 659 | |
| 656 | try self.genBody(self.mod_fn.body); | 660 | try self.genBody(self.mod_fn.body); |
| 657 | | 661 | |
| | 662 | // Backpatch stack offset |
| | 663 | const stack_end = self.max_end_stack; |
| | 664 | const aligned_stack_end = mem.alignForward(stack_end, self.stack_align); |
| | 665 | if (math.cast(u12, aligned_stack_end)) |size| { |
| | 666 | writeInt(u32, self.code.items[backpatch_reloc..][0..4], Instruction.sub(.xzr, .xzr, size, false).toU32()); |
| | 667 | } else |_| { |
| | 668 | return self.failSymbol("TODO AArch64: allow larger stacks", .{}); |
| | 669 | } |
| | 670 | |
| 658 | try self.dbgSetEpilogueBegin(); | 671 | try self.dbgSetEpilogueBegin(); |
| 659 | | 672 | |
| 660 | // exitlude jumps | 673 | // exitlude jumps |
| ... | @@ -690,6 +703,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { | ... | @@ -690,6 +703,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { |
| 690 | Register.sp, | 703 | Register.sp, |
| 691 | Instruction.LoadStorePairOffset.post_index(16), | 704 | Instruction.LoadStorePairOffset.post_index(16), |
| 692 | ).toU32()); | 705 | ).toU32()); |
| | 706 | // add sp, sp, #stack_size |
| | 707 | writeInt(u32, try self.code.addManyAsArray(4), Instruction.add(.xzr, .xzr, @intCast(u12, aligned_stack_end), false).toU32()); |
| 693 | // ret lr | 708 | // ret lr |
| 694 | writeInt(u32, try self.code.addManyAsArray(4), Instruction.ret(null).toU32()); | 709 | writeInt(u32, try self.code.addManyAsArray(4), Instruction.ret(null).toU32()); |
| 695 | } else { | 710 | } else { |