6#include "tpde/AssemblerElf.hpp"
7#include "tpde/AssignmentPartRef.hpp"
8#include "tpde/CompilerBase.hpp"
9#include "tpde/DWARF.hpp"
10#include "tpde/ELF.hpp"
11#include "tpde/arm64/FunctionWriterA64.hpp"
12#include "tpde/base.hpp"
13#include "tpde/util/SmallVector.hpp"
14#include "tpde/util/misc.hpp"
20#if defined(ASM) || defined(ASMNC) || defined(ASMC)
21 #error Got definition for ASM macros from somewhere else. Maybe you included compilers for multiple architectures?
25#define ASMC(compiler, op, ...) \
26 ((compiler)->text_writer.write_inst(de64_##op(__VA_ARGS__)))
28#define ASM(...) ASMC(this, __VA_ARGS__)
30#define ASMNC(op, ...) \
31 (this->text_writer.write_inst_unchecked(de64_##op(__VA_ARGS__)))
33#define ASMIFC(compiler, op, ...) \
34 ((compiler)->text_writer.try_write_inst(de64_##op(__VA_ARGS__)))
36#define ASMIF(...) ASMIFC(this, __VA_ARGS__)
111 constexpr explicit AsmReg() noexcept : Reg((u8)0xFF) {}
113 constexpr AsmReg(
const REG
id) noexcept : Reg((u8)
id) {}
115 constexpr AsmReg(
const Reg base) noexcept : Reg(base) {}
117 constexpr explicit AsmReg(
const u8
id) noexcept : Reg(
id) {
118 assert(
id <= SP || (
id >= V0 &&
id <= V31));
121 constexpr explicit AsmReg(
const u64
id) noexcept : Reg(
id) {
122 assert(
id <= SP || (
id >= V0 &&
id <= V31));
125 operator DA_GReg() const noexcept {
127 return DA_GReg{reg_id};
130 operator DA_GRegZR() const noexcept {
132 assert(reg_id != SP);
133 return DA_GRegZR{reg_id};
136 operator DA_GRegSP() const noexcept {
137 assert(reg_id <= SP);
138 return DA_GRegSP{reg_id};
141 operator DA_VReg() const noexcept {
142 assert(reg_id >= V0 && reg_id <= V31);
143 return DA_VReg{
static_cast<u8
>(reg_id - V0)};
148 create_bitmask(
const std::initializer_list<AsmReg::REG> regs) {
150 for (
const auto reg : regs) {
157constexpr static u64 create_bitmask(
const std::array<AsmReg, N> regs) {
159 for (
const auto reg : regs) {
160 set |= 1ull << reg.id();
166class CCAssignerAAPCS :
public CCAssigner {
167 static constexpr CCInfo Info{
170 0xFFFF'FFFF'FFFF'FFFF &
171 ~create_bitmask({AsmReg::SP, AsmReg::FP, AsmReg::R16, AsmReg::R17}),
173 .callee_saved_regs = create_bitmask({
193 .arg_regs = create_bitmask({
217 u32 ngrn = 0, nsrn = 0, nsaa = 0;
218 u32 ret_ngrn = 0, ret_nsrn = 0;
221 CCAssignerAAPCS() noexcept : CCAssigner(Info) {}
223 void reset()
noexcept override {
224 ngrn = nsrn = nsaa = ret_ngrn = ret_nsrn = 0;
227 void assign_arg(CCAssignment &arg)
noexcept override {
228 if (arg.byval) [[unlikely]] {
229 nsaa = util::align_up(nsaa, arg.align < 8 ? 8 : arg.align);
230 arg.stack_off = nsaa;
235 if (arg.sret) [[unlikely]] {
236 arg.reg = AsmReg{AsmReg::R8};
240 if (arg.bank == RegBank{0}) {
242 ngrn = util::align_up(ngrn, 2);
244 if (ngrn + arg.consecutive < 8) {
245 arg.reg = Reg{AsmReg::R0 + ngrn};
249 nsaa = util::align_up(nsaa, arg.align < 8 ? 8 : arg.align);
250 arg.stack_off = nsaa;
254 if (nsrn + arg.consecutive < 8) {
255 arg.reg = Reg{AsmReg::V0 + nsrn};
259 u32 size = util::align_up(arg.size, 8);
260 nsaa = util::align_up(nsaa, size);
261 arg.stack_off = nsaa;
267 u32 get_stack_size()
noexcept override {
return nsaa; }
269 void assign_ret(CCAssignment &arg)
noexcept override {
270 assert(!arg.byval && !arg.sret);
271 if (arg.bank == RegBank{0}) {
273 ret_ngrn = util::align_up(ret_ngrn, 2);
275 if (ret_ngrn + arg.consecutive < 8) {
276 arg.reg = Reg{AsmReg::R0 + ret_ngrn};
282 if (ret_nsrn + arg.consecutive < 8) {
283 arg.reg = Reg{AsmReg::V0 + ret_nsrn};
292struct PlatformConfig : CompilerConfigDefault {
293 using Assembler = tpde::elf::AssemblerElfA64;
294 using AsmReg = tpde::a64::AsmReg;
298 static constexpr RegBank GP_BANK{0};
299 static constexpr RegBank FP_BANK{1};
300 static constexpr bool FRAME_INDEXING_NEGATIVE =
false;
301 static constexpr u32 PLATFORM_POINTER_SIZE = 8;
302 static constexpr u32 NUM_BANKS = 2;
306template <IRAdaptor Adaptor,
308 template <
typename,
typename,
typename>
typename BaseTy =
310 typename Config = PlatformConfig>
311struct CompilerA64 : BaseTy<Adaptor, Derived, Config> {
312 using Base = BaseTy<Adaptor, Derived, Config>;
314 using IRValueRef =
typename Base::IRValueRef;
315 using IRBlockRef =
typename Base::IRBlockRef;
316 using IRFuncRef =
typename Base::IRFuncRef;
318 using ScratchReg =
typename Base::ScratchReg;
319 using ValuePartRef =
typename Base::ValuePartRef;
320 using ValuePart =
typename Base::ValuePart;
321 using GenericValuePart =
typename Base::GenericValuePart;
323 using RegisterFile =
typename Base::RegisterFile;
325 using CallArg =
typename Base::CallArg;
332 static constexpr u32 NUM_FIXED_ASSIGNMENTS[PlatformConfig::NUM_BANKS] = {5,
335 enum CPU_FEATURES : u32 {
339 CPU_FEATURES cpu_feats = CPU_BASELINE;
346 u64 fixed_assignment_nonallocatable_mask =
347 create_bitmask({AsmReg::R0, AsmReg::R1});
348 u32 func_start_off = 0u, func_prologue_alloc = 0u;
352 AsmReg func_arg_stack_add_reg = AsmReg::make_invalid();
358 u32 scalar_arg_count = 0xFFFF'FFFF, vec_arg_count = 0xFFFF'FFFF;
359 u32 reg_save_frame_off = 0;
360 util::SmallVector<u32, 8> func_ret_offs = {};
363 class CallBuilder :
public Base::template CallBuilderBase<CallBuilder> {
364 u32 stack_adjust_off = 0;
368 void set_stack_used()
noexcept;
373 : Base::template CallBuilderBase<CallBuilder>(compiler, assigner) {}
375 void add_arg_byval(ValuePart &vp, CCAssignment &cca)
noexcept;
376 void add_arg_stack(ValuePart &vp, CCAssignment &cca)
noexcept;
377 void call_impl(std::variant<SymRef, ValuePart> &&) noexcept;
378 void reset_stack() noexcept;
382 explicit CompilerA64(Adaptor *adaptor,
383 const CPU_FEATURES cpu_features = CPU_BASELINE)
384 : Base{adaptor}, cpu_feats(cpu_features) {
385 static_assert(std::is_base_of_v<CompilerA64, Derived>);
388 void start_func(u32)
noexcept {}
395 CCAssignment cca)
noexcept;
400 void finish_func(u32 func_idx)
noexcept;
402 void reset() noexcept;
406 void gen_func_epilog() noexcept;
409 spill_reg(const AsmReg reg, const u32 frame_off, const u32 size) noexcept;
411 void load_from_stack(AsmReg dst,
414 bool sign_extend = false) noexcept;
416 void load_address_of_stack_var(AsmReg dst, AssignmentPartRef ap) noexcept;
418 void mov(AsmReg dst, AsmReg src, u32 size) noexcept;
420 GenericValuePart val_spill_slot(AssignmentPartRef ap) noexcept {
421 assert(ap.stack_valid() && !ap.variable_ref());
422 return typename GenericValuePart::Expr(AsmReg::R29, ap.frame_off());
425 AsmReg gval_expr_as_reg(GenericValuePart &gv)
noexcept;
428 void alloca_fixed(u64 size, u32 align, ValuePart &res)
noexcept;
435 ValuePart &res)
noexcept;
441 AsmReg dst)
noexcept;
446 AsmReg dst)
noexcept {
447 assert(size <=
sizeof(const_u64));
451 AsmReg select_fixed_assignment_reg(AssignmentPartRef, IRValueRef)
noexcept;
489 constexpr Jump(
Kind kind) : kind(kind), cmp_is_32(false), test_bit(0) {
490 assert(kind !=
Cbz && kind !=
Cbnz && kind !=
Tbz && kind !=
Tbnz);
494 constexpr Jump(
Kind kind, AsmReg cmp_reg,
bool cmp_is_32)
495 : kind(kind), cmp_reg(cmp_reg), cmp_is_32(cmp_is_32), test_bit(0) {
496 assert(kind ==
Cbz || kind ==
Cbnz);
500 constexpr Jump(
Kind kind, AsmReg cmp_reg, u8 test_bit)
501 : kind(kind), cmp_reg(cmp_reg), cmp_is_32(false), test_bit(test_bit) {
502 assert(kind ==
Tbz || kind ==
Tbnz);
505 constexpr Jump change_kind(Kind new_kind)
const {
512 Jump invert_jump(Jump jmp)
noexcept;
513 Jump swap_jump(Jump jmp)
noexcept;
532 bool is_64)
noexcept;
536 AsmReg dst, AsmReg src,
bool sign, u32 from, u32 to)
noexcept;
540 ASM(BFIx, dst, src, lsb, width);
544 ASM(UBFIZx, dst, src, lsb, width);
558 std::span<CallArg> arguments,
559 typename Base::ValueRef *result,
560 bool variable_args =
false);
564 void switch_emit_cmp(AsmReg cmp_reg,
567 bool width_is_32)
noexcept;
571 void switch_emit_cmpeq(Label case_label,
575 bool width_is_32)
noexcept;
577 bool switch_emit_jump_table(Label default_label,
578 std::span<const Label> labels,
583 bool width_is_32)
noexcept;
585 void switch_emit_binary_step(Label case_label,
590 bool width_is_32)
noexcept;
594 ScratchReg
tls_get_addr(SymRef sym, TLSModel model)
noexcept;
596 bool has_cpu_feats(CPU_FEATURES feats)
const noexcept {
597 return ((cpu_feats & feats) == feats);
601template <IRAdaptor Adaptor,
603 template <
typename,
typename,
typename>
class BaseTy,
605void CompilerA64<Adaptor, Derived, BaseTy, Config>::CallBuilder::
606 set_stack_used() noexcept {
607 if (stack_adjust_off == 0) {
608 this->compiler.text_writer.ensure_space(16);
609 stack_adjust_off = this->compiler.text_writer.offset();
610 this->compiler.text_writer.cur_ptr() += 4;
614template <IRAdaptor Adaptor,
616 template <
typename,
typename,
typename>
class BaseTy,
618void CompilerA64<Adaptor, Derived, BaseTy, Config>::CallBuilder::add_arg_byval(
619 ValuePart &vp, CCAssignment &cca)
noexcept {
620 AsmReg ptr_reg = vp.load_to_reg(&this->compiler);
621 AsmReg tmp_reg = AsmReg::R16;
623 auto size = cca.size;
625 for (u32 off = 0; off < size;) {
626 if (size - off >= 8) {
627 ASMC(&this->compiler, LDRxu, tmp_reg, ptr_reg, off);
628 ASMC(&this->compiler, STRxu, tmp_reg, DA_SP, cca.stack_off + off);
630 }
else if (size - off >= 4) {
631 ASMC(&this->compiler, LDRwu, tmp_reg, ptr_reg, off);
632 ASMC(&this->compiler, STRwu, tmp_reg, DA_SP, cca.stack_off + off);
634 }
else if (size - off >= 2) {
635 ASMC(&this->compiler, LDRHu, tmp_reg, ptr_reg, off);
636 ASMC(&this->compiler, STRHu, tmp_reg, DA_SP, cca.stack_off + off);
639 ASMC(&this->compiler, LDRBu, tmp_reg, ptr_reg, off);
640 ASMC(&this->compiler, STRBu, tmp_reg, DA_SP, cca.stack_off + off);
646template <IRAdaptor Adaptor,
648 template <
typename,
typename,
typename>
class BaseTy,
650void CompilerA64<Adaptor, Derived, BaseTy, Config>::CallBuilder::add_arg_stack(
651 ValuePart &vp, CCAssignment &cca)
noexcept {
654 auto reg = vp.has_reg() ? vp.cur_reg() : vp.load_to_reg(&this->compiler);
655 if (this->compiler.register_file.reg_bank(reg) == Config::GP_BANK) {
657 case 1: ASMC(&this->compiler, STRBu, reg, DA_SP, cca.stack_off);
break;
658 case 2: ASMC(&this->compiler, STRHu, reg, DA_SP, cca.stack_off);
break;
659 case 4: ASMC(&this->compiler, STRwu, reg, DA_SP, cca.stack_off);
break;
660 case 8: ASMC(&this->compiler, STRxu, reg, DA_SP, cca.stack_off);
break;
661 default: TPDE_UNREACHABLE(
"invalid GP reg size");
664 assert(this->compiler.register_file.reg_bank(reg) == Config::FP_BANK);
666 case 1: ASMC(&this->compiler, STRbu, reg, DA_SP, cca.stack_off);
break;
667 case 2: ASMC(&this->compiler, STRhu, reg, DA_SP, cca.stack_off);
break;
668 case 4: ASMC(&this->compiler, STRsu, reg, DA_SP, cca.stack_off);
break;
669 case 8: ASMC(&this->compiler, STRdu, reg, DA_SP, cca.stack_off);
break;
670 case 16: ASMC(&this->compiler, STRqu, reg, DA_SP, cca.stack_off);
break;
671 default: TPDE_UNREACHABLE(
"invalid FP reg size");
676template <IRAdaptor Adaptor,
678 template <
typename,
typename,
typename>
class BaseTy,
680void CompilerA64<Adaptor, Derived, BaseTy, Config>::CallBuilder::call_impl(
681 std::variant<SymRef, ValuePart> &&target)
noexcept {
683 if (stack_adjust_off != 0) {
684 auto *text_data = this->compiler.text_writer.begin_ptr();
685 u32 *write_ptr =
reinterpret_cast<u32 *
>(text_data + stack_adjust_off);
686 u32 stack_size = this->assigner.get_stack_size();
687 sub = util::align_up(stack_size, stack_size < 0x1000 ? 0x10 : 0x1000);
688 *write_ptr = de64_SUBxi(DA_SP, DA_SP, sub);
690 assert(this->assigner.get_stack_size() == 0);
695 auto fp_regs = RegisterFile::bank_regs(Config::FP_BANK);
696 auto fp_csrs = fp_regs & this->assigner.get_ccinfo().callee_saved_regs;
697 auto used_fp_csrs = fp_csrs & this->compiler.register_file.used;
698 for (
auto reg_id : util::BitSetIterator<>{used_fp_csrs}) {
700 ValLocalIdx local_idx = this->compiler.register_file.reg_local_idx(reg);
701 auto part = this->compiler.register_file.reg_part(reg);
702 AssignmentPartRef ap{this->compiler.val_assignment(local_idx), part};
703 if (ap.part_size() > 8) {
704 this->compiler.evict(ap);
708 if (
auto *sym = std::get_if<SymRef>(&target)) {
709 ASMC(&this->compiler, BL, 0);
710 this->compiler.reloc_text(
711 *sym, elf::R_AARCH64_CALL26, this->compiler.text_writer.offset() - 4);
713 ValuePart &tvp = std::get<ValuePart>(target);
714 if (tvp.can_salvage()) {
715 ASMC(&this->compiler, BLR, tvp.salvage(&this->compiler));
717 AsmReg reg = this->compiler.permanent_scratch_reg;
718 tvp.reload_into_specific_fixed(&this->compiler, reg);
719 ASMC(&this->compiler, BLR, reg);
721 tvp.reset(&this->compiler);
724 if (stack_adjust_off != 0) {
725 ASMC(&this->compiler, ADDxi, DA_SP, DA_SP, sub);
729template <IRAdaptor Adaptor,
731 template <
typename,
typename,
typename>
typename BaseTy,
734 CCAssigner *cc_assigner)
noexcept {
735 func_ret_offs.clear();
736 func_start_off = this->text_writer.offset();
738 const CCInfo &cc_info = cc_assigner->get_ccinfo();
745 this->stack.frame_size = 16;
747 auto csr = cc_info.callee_saved_regs;
748 auto csr_gp = csr & this->register_file.bank_regs(Config::GP_BANK);
749 auto csr_fp = csr & this->register_file.bank_regs(Config::FP_BANK);
750 u32 gp_saves = std::popcount(csr_gp);
751 u32 fp_saves = std::popcount(csr_fp);
753 u32 reg_save_size = 4 * ((gp_saves + 1) / 2 + (fp_saves + 1) / 2);
755 this->stack.frame_size += util::align_up(gp_saves * 8 + fp_saves * 8, 16);
758 func_prologue_alloc = reg_save_size + 12;
759 this->text_writer.ensure_space(func_prologue_alloc);
760 this->text_writer.cur_ptr() += func_prologue_alloc;
765 if (this->adaptor->cur_is_vararg()) [[unlikely]] {
766 this->stack.frame_used =
true;
767 reg_save_frame_off = this->stack.frame_size;
771 this->stack.frame_size += 8 * 8 + 8 * 16 + 16;
772 this->text_writer.ensure_space(4 * 8);
773 ASMNC(STPx, DA_GP(0), DA_GP(1), DA_SP, reg_save_frame_off);
774 ASMNC(STPx, DA_GP(2), DA_GP(3), DA_SP, reg_save_frame_off + 16);
775 ASMNC(STPx, DA_GP(4), DA_GP(5), DA_SP, reg_save_frame_off + 32);
776 ASMNC(STPx, DA_GP(6), DA_GP(7), DA_SP, reg_save_frame_off + 48);
777 ASMNC(STPq, DA_V(0), DA_V(1), DA_SP, reg_save_frame_off + 64);
778 ASMNC(STPq, DA_V(2), DA_V(3), DA_SP, reg_save_frame_off + 96);
779 ASMNC(STPq, DA_V(4), DA_V(5), DA_SP, reg_save_frame_off + 128);
780 ASMNC(STPq, DA_V(6), DA_V(7), DA_SP, reg_save_frame_off + 160);
783 this->func_arg_stack_add_off = ~0u;
788 template <
typename,
typename,
typename>
typename BaseTy,
792 ValuePart &&vp, CCAssignment cca)
noexcept {
793 if (cca.reg.valid()) [[likely]] {
794 vp.set_value_reg(
this, cca.reg);
798 this->register_file.allocatable |= u64{1} << cca.reg.id();
802 AsmReg dst = vp.alloc_reg(
this);
804 this->text_writer.ensure_space(8);
805 AsmReg stack_reg = AsmReg::R17;
807 assert(!(this->register_file.allocatable & (u64{1} << stack_reg.id())) &&
808 "x17 must not be allocatable");
809 if (this->func_arg_stack_add_off == ~0u) {
810 this->func_arg_stack_add_off = this->text_writer.offset();
811 this->func_arg_stack_add_reg = stack_reg;
813 ASMNC(ADDxi, stack_reg, DA_SP, 0);
817 ASMNC(ADDxi, dst, stack_reg, cca.stack_off);
818 }
else if (cca.bank == Config::GP_BANK) {
820 case 1: ASMNC(LDRBu, dst, stack_reg, cca.stack_off);
break;
821 case 2: ASMNC(LDRHu, dst, stack_reg, cca.stack_off);
break;
822 case 4: ASMNC(LDRwu, dst, stack_reg, cca.stack_off);
break;
823 case 8: ASMNC(LDRxu, dst, stack_reg, cca.stack_off);
break;
824 default: TPDE_UNREACHABLE(
"invalid GP reg size");
827 assert(cca.bank == Config::FP_BANK);
829 case 1: ASMNC(LDRbu, dst, stack_reg, cca.stack_off);
break;
830 case 2: ASMNC(LDRhu, dst, stack_reg, cca.stack_off);
break;
831 case 4: ASMNC(LDRsu, dst, stack_reg, cca.stack_off);
break;
832 case 8: ASMNC(LDRdu, dst, stack_reg, cca.stack_off);
break;
833 case 16: ASMNC(LDRqu, dst, stack_reg, cca.stack_off);
break;
834 default: TPDE_UNREACHABLE(
"invalid FP reg size");
842 template <
typename,
typename,
typename>
typename BaseTy,
845 CCAssigner *cc_assigner)
noexcept {
849 if (this->adaptor->cur_is_vararg()) [[unlikely]] {
850 this->stack.frame_used =
true;
851 AsmReg stack_reg = AsmReg::R17;
853 assert(!(this->register_file.allocatable & (u64{1} << stack_reg.id())) &&
854 "x17 must not be allocatable");
855 if (this->func_arg_stack_add_off == ~0u) {
856 this->func_arg_stack_add_off = this->text_writer.offset();
857 this->func_arg_stack_add_reg = stack_reg;
859 ASMC(
this, ADDxi, stack_reg, DA_SP, 0);
861 ASM(ADDxi, stack_reg, stack_reg, cc_assigner->get_stack_size());
862 ASM(STRxu, stack_reg, DA_GP(29), this->reg_save_frame_off + 192);
867 const CCInfo &cc_info = cc_assigner->get_ccinfo();
868 auto arg_regs = this->register_file.allocatable & cc_info.arg_regs;
869 u32 ngrn = 8 - util::cnt_lz<u16>((arg_regs & 0xff) << 8 | 0x80);
870 u32 nsrn = 8 - util::cnt_lz<u16>(((arg_regs >> 32) & 0xff) << 8 | 0x80);
871 this->scalar_arg_count = ngrn;
872 this->vec_arg_count = nsrn;
878 template <
typename,
typename,
typename>
typename BaseTy,
880void CompilerA64<Adaptor, Derived, BaseTy, Config>::finish_func(
881 u32 func_idx)
noexcept {
882 auto csr =
derived()->cur_cc_assigner()->get_ccinfo().callee_saved_regs;
883 u64 saved_regs = this->register_file.clobbered & csr;
885 auto stack_reg = DA_SP;
886 if (this->stack.has_dynamic_alloca) {
887 stack_reg = DA_GP(29);
890 auto final_frame_size = util::align_up(this->stack.frame_size, 16);
891 if (final_frame_size > 4095) {
893 final_frame_size = util::align_up(final_frame_size, 4096);
894 assert(final_frame_size < 16 * 1024 * 1024);
897 bool needs_stack_frame =
898 this->stack.frame_used || this->stack.generated_call ||
899 this->stack.has_dynamic_alloca || saved_regs != 0 ||
900 (this->register_file.clobbered & (u64{1} << AsmReg::LR));
902 this->text_writer.eh_begin_fde(this->get_personality_sym());
904 u32 prologue_size = 0;
905 if (needs_stack_frame) [[likely]] {
907 util::SmallVector<u32, 16> prologue;
913 if (!func_ret_offs.empty() && final_frame_size <= 0x1f8) {
914 this->text_writer.eh_write_inst(dwarf::DW_CFA_remember_state);
916 this->text_writer.eh_write_inst(dwarf::DW_CFA_advance_loc, 1);
917 this->text_writer.eh_write_inst(dwarf::DW_CFA_def_cfa_offset,
919 if (final_frame_size <= 0x1f8) {
921 de64_STPx_pre(DA_GP(29), DA_GP(30), DA_SP, -
int(final_frame_size)));
922 prologue.push_back(de64_MOV_SPx(DA_GP(29), DA_SP));
924 if (!func_ret_offs.empty()) {
925 this->text_writer.eh_write_inst(dwarf::DW_CFA_remember_state);
927 prologue.push_back(de64_SUBxi(DA_SP, DA_SP, final_frame_size));
928 prologue.push_back(de64_STPx(DA_GP(29), DA_GP(30), DA_SP, 0));
929 prologue.push_back(de64_MOV_SPx(DA_GP(29), DA_SP));
933 auto fde_prologue_adv_off = this->text_writer.eh_writer.size();
934 this->text_writer.eh_write_inst(dwarf::DW_CFA_advance_loc, 0);
935 this->text_writer.eh_write_inst(dwarf::DW_CFA_def_cfa_register,
936 dwarf::a64::DW_reg_fp);
937 this->text_writer.eh_write_inst(
938 dwarf::DW_CFA_offset, dwarf::a64::DW_reg_fp, final_frame_size / 8);
939 this->text_writer.eh_write_inst(
940 dwarf::DW_CFA_offset, dwarf::a64::DW_reg_lr, final_frame_size / 8 - 1);
942 AsmReg last_reg = AsmReg::make_invalid();
944 for (
auto reg : util::BitSetIterator{saved_regs}) {
945 u8 dwarf_base = reg < 32 ? dwarf::a64::DW_reg_x0 : dwarf::a64::DW_reg_v0;
946 u8 dwarf_reg = dwarf_base + reg % 32;
947 u32 cfa_off = (final_frame_size - frame_off) / 8 - last_reg.valid();
948 if ((dwarf_reg & dwarf::DWARF_CFI_PRIMARY_OPCODE_MASK) == 0) {
949 this->text_writer.eh_write_inst(
950 dwarf::DW_CFA_offset, dwarf_reg, cfa_off);
952 this->text_writer.eh_write_inst(
953 dwarf::DW_CFA_offset_extended, dwarf_reg, cfa_off);
956 if (last_reg.valid()) {
957 const auto reg_bank = this->register_file.reg_bank(AsmReg{reg});
958 const auto last_bank = this->register_file.reg_bank(last_reg);
959 if (reg_bank == last_bank) {
960 if (reg_bank == Config::GP_BANK) {
962 de64_STPx(last_reg, AsmReg{reg}, stack_reg, frame_off));
965 de64_STPd(last_reg, AsmReg{reg}, stack_reg, frame_off));
968 last_reg = AsmReg::make_invalid();
970 assert(last_bank == Config::GP_BANK && reg_bank == Config::FP_BANK);
971 prologue.push_back(de64_STRxu(last_reg, stack_reg, frame_off));
973 last_reg = AsmReg{reg};
976 last_reg = AsmReg{reg};
980 if (last_reg.valid()) {
981 if (this->register_file.reg_bank(last_reg) == Config::GP_BANK) {
982 prologue.push_back(de64_STRxu(last_reg, stack_reg, frame_off));
984 assert(this->register_file.reg_bank(last_reg) == Config::FP_BANK);
985 prologue.push_back(de64_STRdu(last_reg, stack_reg, frame_off));
989 assert(prologue.size() *
sizeof(u32) <= func_prologue_alloc);
991 assert(prologue.size() < 0x4c);
992 this->text_writer.eh_writer.data()[fde_prologue_adv_off] =
993 dwarf::DW_CFA_advance_loc | (prologue.size() - 1);
995 std::memcpy(this->text_writer.begin_ptr() + func_start_off,
997 prologue.size() *
sizeof(u32));
999 prologue_size = prologue.size() *
sizeof(u32);
1004 u32 *inst_ptr =
reinterpret_cast<u32 *
>(raw_inst_ptr);
1005 if (needs_stack_frame) {
1006 *inst_ptr = de64_ADDxi(func_arg_stack_add_reg, DA_SP, final_frame_size);
1008 *inst_ptr = de64_MOV_SPx(func_arg_stack_add_reg, DA_SP);
1012 if (!func_ret_offs.empty()) {
1013 u8 *text_data = this->text_writer.begin_ptr();
1014 if (func_ret_offs.back() == this->text_writer.offset() - 4) {
1015 this->text_writer.cur_ptr() -= 4;
1016 func_ret_offs.pop_back();
1018 for (
auto ret_off : func_ret_offs) {
1019 u32 *write_ptr =
reinterpret_cast<u32 *
>(text_data + ret_off);
1020 *write_ptr = de64_B((this->text_writer.offset() - ret_off) / 4);
1024 this->text_writer.ensure_space(prologue_size + 4);
1026 if (this->stack.has_dynamic_alloca) {
1027 ASMNC(MOV_SPx, DA_SP, DA_GP(29));
1030 AsmReg last_reg = AsmReg::make_invalid();
1032 for (
auto reg : util::BitSetIterator{saved_regs}) {
1033 if (last_reg.valid()) {
1034 const auto reg_bank = this->register_file.reg_bank(AsmReg{reg});
1035 const auto last_bank = this->register_file.reg_bank(last_reg);
1036 if (reg_bank == last_bank) {
1037 if (reg_bank == Config::GP_BANK) {
1038 ASMNC(LDPx, last_reg, AsmReg{reg}, stack_reg, frame_off);
1040 ASMNC(LDPd, last_reg, AsmReg{reg}, stack_reg, frame_off);
1043 last_reg = AsmReg::make_invalid();
1045 assert(last_bank == Config::GP_BANK && reg_bank == Config::FP_BANK);
1046 ASMNC(LDRxu, last_reg, stack_reg, frame_off);
1048 last_reg = AsmReg{reg};
1053 last_reg = AsmReg{reg};
1056 if (last_reg.valid()) {
1057 if (this->register_file.reg_bank(last_reg) == Config::GP_BANK) {
1058 ASMNC(LDRxu, last_reg, stack_reg, frame_off);
1060 ASMNC(LDRdu, last_reg, stack_reg, frame_off);
1063 if (needs_stack_frame) {
1064 u32 body_start = func_start_off + func_prologue_alloc;
1065 this->text_writer.eh_advance(this->text_writer.offset() - body_start + 4);
1066 this->text_writer.eh_write_inst(dwarf::DW_CFA_restore_state);
1067 if (final_frame_size <= 0x1f8) {
1068 ASMNC(LDPx_post, DA_GP(29), DA_GP(30), DA_SP, final_frame_size);
1071 ASMNC(LDPx, DA_GP(29), DA_GP(30), DA_SP, 0);
1073 ASMNC(ADDxi, DA_SP, DA_SP, final_frame_size);
1074 this->text_writer.eh_write_inst(dwarf::DW_CFA_advance_loc, 1);
1075 this->text_writer.eh_write_inst(dwarf::DW_CFA_def_cfa_offset, 0);
1079 ASMNC(RET, DA_GP(30));
1083 this->text_writer.remove_prologue_bytes(func_start_off + prologue_size,
1084 func_prologue_alloc - prologue_size);
1085 auto func_size = this->text_writer.offset() - func_start_off;
1086 auto func_sym = this->func_syms[func_idx];
1087 auto func_sec = this->text_writer.get_sec_ref();
1088 this->assembler.sym_def(func_sym, func_sec, func_start_off, func_size);
1089 this->text_writer.eh_end_fde();
1090 this->text_writer.except_encode_func();
1093template <IRAdaptor Adaptor,
1095 template <
typename,
typename,
typename>
typename BaseTy,
1097void CompilerA64<Adaptor, Derived, BaseTy, Config>::reset() noexcept {
1098 func_ret_offs.clear();
1102template <IRAdaptor Adaptor,
1104 template <
typename,
typename,
typename>
typename BaseTy,
1106void CompilerA64<Adaptor, Derived, BaseTy, Config>::gen_func_epilog() noexcept {
1108 func_ret_offs.push_back(this->text_writer.offset());
1109 this->text_writer.ensure_space(4);
1110 this->text_writer.cur_ptr() += 4;
1113template <IRAdaptor Adaptor,
1115 template <
typename,
typename,
typename>
typename BaseTy,
1117void CompilerA64<Adaptor, Derived, BaseTy, Config>::spill_reg(
1118 const AsmReg reg,
const u32 frame_off,
const u32 size)
noexcept {
1119 assert(this->stack.frame_used);
1120 assert((size & (size - 1)) == 0);
1121 assert(util::align_up(frame_off, size) == frame_off);
1123 assert(frame_off < 0x1'000'000);
1124 this->text_writer.ensure_space(8);
1126 u32 off = frame_off;
1127 auto addr_base = AsmReg{AsmReg::FP};
1128 if (off >= 0x1000 * size) [[unlikely]] {
1135 assert(-
static_cast<i32
>(frame_off) < 0);
1136 if (reg.id() <= AsmReg::R30) {
1138 case 1: ASMNC(STRBu, reg, addr_base, off);
break;
1139 case 2: ASMNC(STRHu, reg, addr_base, off);
break;
1140 case 4: ASMNC(STRwu, reg, addr_base, off);
break;
1141 case 8: ASMNC(STRxu, reg, addr_base, off);
break;
1142 default: TPDE_UNREACHABLE(
"invalid register spill size");
1146 case 1: ASMNC(STRbu, reg, addr_base, off);
break;
1147 case 2: ASMNC(STRhu, reg, addr_base, off);
break;
1148 case 4: ASMNC(STRsu, reg, addr_base, off);
break;
1149 case 8: ASMNC(STRdu, reg, addr_base, off);
break;
1150 case 16: ASMNC(STRqu, reg, addr_base, off);
break;
1151 default: TPDE_UNREACHABLE(
"invalid register spill size");
1156template <IRAdaptor Adaptor,
1158 template <
typename,
typename,
typename>
typename BaseTy,
1160void CompilerA64<Adaptor, Derived, BaseTy, Config>::load_from_stack(
1162 const i32 frame_off,
1164 const bool sign_extend)
noexcept {
1165 assert(this->stack.frame_used);
1166 assert((size & (size - 1)) == 0);
1167 assert(util::align_up(frame_off, size) == frame_off);
1169 assert(frame_off >= 0 && frame_off < 0x1'000'000);
1170 this->text_writer.ensure_space(8);
1172 u32 off = frame_off;
1173 auto addr_base = AsmReg{AsmReg::FP};
1174 if (off >= 0x1000 * size) [[unlikely]] {
1177 ASMNC(ADDxi, addr_base, DA_GP(29), off & ~0xfff);
1181 if (dst.id() <= AsmReg::R30) {
1184 case 1: ASMNC(LDRBu, dst, addr_base, off);
break;
1185 case 2: ASMNC(LDRHu, dst, addr_base, off);
break;
1186 case 4: ASMNC(LDRwu, dst, addr_base, off);
break;
1187 case 8: ASMNC(LDRxu, dst, addr_base, off);
break;
1188 default: TPDE_UNREACHABLE(
"invalid register spill size");
1192 case 1: ASMNC(LDRSBwu, dst, addr_base, off);
break;
1193 case 2: ASMNC(LDRSHwu, dst, addr_base, off);
break;
1194 case 4: ASMNC(LDRSWxu, dst, addr_base, off);
break;
1195 case 8: ASMNC(LDRxu, dst, addr_base, off);
break;
1196 default: TPDE_UNREACHABLE(
"invalid register spill size");
1202 assert(!sign_extend);
1205 case 1: ASMNC(LDRbu, dst, addr_base, off);
break;
1206 case 2: ASMNC(LDRhu, dst, addr_base, off);
break;
1207 case 4: ASMNC(LDRsu, dst, addr_base, off);
break;
1208 case 8: ASMNC(LDRdu, dst, addr_base, off);
break;
1209 case 16: ASMNC(LDRqu, dst, addr_base, off);
break;
1210 default: TPDE_UNREACHABLE(
"invalid register spill size");
1214template <IRAdaptor Adaptor,
1216 template <
typename,
typename,
typename>
typename BaseTy,
1218void CompilerA64<Adaptor, Derived, BaseTy, Config>::load_address_of_stack_var(
1219 const AsmReg dst,
const AssignmentPartRef ap)
noexcept {
1220 assert(this->stack.frame_used);
1221 auto frame_off = ap.variable_stack_off();
1222 assert(frame_off >= 0);
1223 if (!ASMIF(ADDxi, dst, DA_GP(29), frame_off)) {
1225 ASM(ADDx_uxtw, dst, DA_GP(29), dst, 0);
1229template <IRAdaptor Adaptor,
1231 template <
typename,
typename,
typename>
typename BaseTy,
1233void CompilerA64<Adaptor, Derived, BaseTy, Config>::mov(
1234 const AsmReg dst,
const AsmReg src,
const u32 size)
noexcept {
1235 this->text_writer.ensure_space(4);
1236 assert(dst.valid());
1237 assert(src.valid());
1238 if (dst.id() <= AsmReg::SP && src.id() <= AsmReg::SP) {
1239 assert(dst.id() != AsmReg::SP && src.id() != AsmReg::SP);
1241 ASMNC(MOVx, dst, src);
1243 ASMNC(MOVw, dst, src);
1245 }
else if (dst.id() >= AsmReg::V0 && src.id() >= AsmReg::V0) {
1246 ASMNC(ORR16b, dst, src, src);
1247 }
else if (dst.id() <= AsmReg::SP) {
1248 assert(dst.id() != AsmReg::SP);
1250 assert(src.id() >= AsmReg::V0);
1253 ASMNC(FMOVws, dst, src);
1255 ASMNC(FMOVxd, dst, src);
1259 assert(src.id() <= AsmReg::R30);
1260 assert(dst.id() >= AsmReg::V0);
1263 ASMNC(FMOVsw, dst, src);
1265 ASMNC(FMOVdx, dst, src);
1270template <IRAdaptor Adaptor,
1272 template <
typename,
typename,
typename>
typename BaseTy,
1274AsmReg CompilerA64<Adaptor, Derived, BaseTy, Config>::gval_expr_as_reg(
1275 GenericValuePart &gv)
noexcept {
1276 auto &expr = std::get<typename GenericValuePart::Expr>(gv.state);
1278 ScratchReg scratch{
derived()};
1279 if (!expr.has_base() && !expr.has_index()) {
1280 AsmReg dst = scratch.alloc_gp();
1281 derived()->materialize_constant(expr.disp, Config::GP_BANK, 8, dst);
1283 }
else if (!expr.has_base() && expr.has_index()) {
1284 AsmReg index_reg = expr.index_reg();
1285 if (std::holds_alternative<ScratchReg>(expr.index)) {
1286 scratch = std::move(std::get<ScratchReg>(expr.index));
1288 (void)scratch.alloc_gp();
1290 AsmReg dst = scratch.cur_reg();
1291 if ((expr.scale & (expr.scale - 1)) == 0) {
1292 const auto shift = util::cnt_tz<u64>(expr.scale);
1293 ASM(LSLxi, dst, index_reg, shift);
1296 derived()->materialize_constant(expr.scale, Config::GP_BANK, 8, tmp2);
1297 ASM(MULx, dst, index_reg, tmp2);
1299 }
else if (expr.has_base() && expr.has_index()) {
1300 AsmReg base_reg = expr.base_reg();
1301 AsmReg index_reg = expr.index_reg();
1302 if (std::holds_alternative<ScratchReg>(expr.base)) {
1303 scratch = std::move(std::get<ScratchReg>(expr.base));
1304 }
else if (std::holds_alternative<ScratchReg>(expr.index)) {
1305 scratch = std::move(std::get<ScratchReg>(expr.index));
1307 (void)scratch.alloc_gp();
1309 AsmReg dst = scratch.cur_reg();
1310 if ((expr.scale & (expr.scale - 1)) == 0) {
1311 const auto shift = util::cnt_tz<u64>(expr.scale);
1312 ASM(ADDx_lsl, dst, base_reg, index_reg, shift);
1315 derived()->materialize_constant(expr.scale, Config::GP_BANK, 8, tmp2);
1316 ASM(MADDx, dst, index_reg, tmp2, base_reg);
1318 }
else if (expr.has_base() && !expr.has_index()) {
1319 AsmReg base_reg = expr.base_reg();
1320 if (std::holds_alternative<ScratchReg>(expr.base)) {
1321 scratch = std::move(std::get<ScratchReg>(expr.base));
1323 (void)scratch.alloc_gp();
1325 AsmReg dst = scratch.cur_reg();
1326 if (expr.disp != 0 && ASMIF(ADDxi, dst, base_reg, expr.disp)) {
1328 }
else if (dst != base_reg) {
1329 ASM(MOVx, dst, base_reg);
1332 TPDE_UNREACHABLE(
"inconsistent GenericValuePart::Expr");
1335 AsmReg dst = scratch.cur_reg();
1336 if (expr.disp != 0) {
1337 if (!ASMIF(ADDxi, dst, dst, expr.disp)) {
1339 derived()->materialize_constant(expr.disp, Config::GP_BANK, 8, tmp2);
1340 ASM(ADDx, dst, dst, tmp2);
1344 gv.state = std::move(scratch);
1348template <IRAdaptor Adaptor,
1350 template <
typename,
typename,
typename>
typename BaseTy,
1353 u64 size, u32 align, ValuePart &res)
noexcept {
1354 assert(this->stack.has_dynamic_alloca &&
1355 "function marked as not having dynamic allocas can't have alloca");
1356 assert(align != 0 && (align & (align - 1)) == 0 &&
"invalid alignment");
1357 size = tpde::util::align_up(size, 16);
1358 AsmReg res_reg = res.alloc_reg(
this);
1359 if (size >= 0x10'0000) {
1362 ASM(SUBx_uxtx, res_reg, DA_SP, tmp, 0);
1363 }
else if (size >= 0x1000) {
1364 ASM(SUBxi, res_reg, DA_SP, size & 0xff'f000);
1365 ASM(SUBxi, res_reg, res_reg, size & 0xfff);
1367 ASM(SUBxi, res_reg, DA_SP, size & 0xfff);
1372 ASM(ANDxi, res_reg, res_reg, ~(u64{align} - 1));
1376 ASM(MOV_SPx, DA_SP, res_reg);
1382 template <
typename,
typename,
typename>
typename BaseTy,
1385 u64 elem_size, ValuePart &&count, u32 align, ValuePart &res)
noexcept {
1386 assert(this->stack.has_dynamic_alloca &&
1387 "function marked as not having dynamic allocas can't have alloca");
1388 assert(align != 0 && (align & (align - 1)) == 0 &&
"invalid alignment");
1389 AsmReg size_reg = count.has_reg() ? count.cur_reg() : count.load_to_reg(
this);
1390 AsmReg res_reg = res.alloc_try_reuse(
this, count);
1392 if (elem_size == 0) {
1393 ASM(MOVZw, res_reg, 0);
1394 }
else if ((elem_size & (elem_size - 1)) == 0) {
1395 const auto shift = util::cnt_tz(elem_size);
1397 ASM(SUBx_uxtx, res_reg, DA_SP, size_reg, shift);
1399 ASM(LSLxi, res_reg, size_reg, shift);
1400 ASM(SUBx_uxtx, res_reg, DA_SP, res_reg, 0);
1405 ASM(MULx, res_reg, size_reg, tmp);
1406 ASM(SUBx_uxtx, res_reg, DA_SP, res_reg, 0);
1409 align = align > 16 ? align : 16;
1410 if (elem_size & (align - 1)) {
1411 ASM(ANDxi, res_reg, res_reg, ~(u64{align} - 1));
1414 ASM(MOV_SPx, DA_SP, res_reg);
1419 template <
typename,
typename,
typename>
typename BaseTy,
1422 const u64 *data,
const RegBank bank,
const u32 size, AsmReg dst)
noexcept {
1423 this->text_writer.ensure_space(5 * 4);
1425 const auto const_u64 = data[0];
1426 if (bank == Config::GP_BANK) {
1428 if (const_u64 == 0) {
1429 ASMNC(MOVZw, dst, 0);
1433 this->text_writer.cur_ptr() +=
1435 de64_MOVconst(
reinterpret_cast<u32 *
>(this->text_writer.cur_ptr()),
1441 assert(bank == Config::FP_BANK);
1444 if (ASMIF(FMOVsi, dst, std::bit_cast<float>((u32)const_u64))) {
1446 }
else if (ASMIF(MOVId, dst,
static_cast<u32
>(const_u64))) {
1449 }
else if (size == 8) {
1450 if (ASMIF(FMOVdi, dst, std::bit_cast<double>(const_u64))) {
1452 }
else if (ASMIF(MOVId, dst, const_u64)) {
1455 }
else if (size == 16) {
1456 const auto high_u64 = data[1];
1457 if (const_u64 == high_u64 && ASMIF(MOVI2d, dst, const_u64)) {
1459 }
else if (high_u64 == 0 && ASMIF(MOVId, dst, const_u64)) {
1480 auto rodata = this->assembler.get_data_section(
true,
false);
1481 std::span<const u8> raw_data{
reinterpret_cast<const u8 *
>(data), size};
1482 auto sym = this->assembler.sym_def_data(
1484 this->text_writer.ensure_space(8);
1486 sym, elf::R_AARCH64_ADR_PREL_PG_HI21, this->text_writer.offset(), 0);
1489 sym, elf::R_AARCH64_LDST128_ABS_LO12_NC, this->text_writer.offset(), 0);
1494 TPDE_FATAL(
"unable to materialize constant");
1499 template <
typename,
typename,
typename>
typename BaseTy,
1502 CompilerA64<Adaptor, Derived, BaseTy, Config>::select_fixed_assignment_reg(
1503 AssignmentPartRef ap, IRValueRef)
noexcept {
1504 RegBank bank = ap.bank();
1505 if (bank == Config::FP_BANK && ap.part_size() > 8) {
1508 return AsmReg::make_invalid();
1512 assert(bank.id() <= Config::NUM_BANKS);
1513 auto reg_mask = this->register_file.bank_regs(bank);
1514 reg_mask &= ~fixed_assignment_nonallocatable_mask;
1516 const auto find_possible_regs = [
this,
1517 reg_mask](
const u64 preferred_regs) -> u64 {
1519 u64 free_regs = this->register_file.allocatable & ~this->register_file.used;
1520 return free_regs & preferred_regs & reg_mask;
1524 auto csr =
derived()->cur_cc_assigner()->get_ccinfo().callee_saved_regs;
1525 if (!this->stack.is_leaf_function) {
1527 possible_regs = find_possible_regs(csr);
1531 possible_regs = find_possible_regs(~csr);
1532 if (possible_regs == 0) {
1534 possible_regs = find_possible_regs(csr);
1538 if (possible_regs == 0) {
1539 return AsmReg::make_invalid();
1543 if ((possible_regs & ~this->register_file.used) != 0) {
1544 return AsmReg{util::cnt_tz(possible_regs & ~this->register_file.used)};
1547 for (
const auto reg_id : util::BitSetIterator<>{possible_regs}) {
1548 const auto reg = AsmReg{reg_id};
1550 assert(!this->register_file.is_fixed(reg));
1552 const auto local_idx = this->register_file.reg_local_idx(reg);
1553 const auto part = this->register_file.reg_part(reg);
1554 assert(local_idx != Base::INVALID_VAL_LOCAL_IDX);
1556 auto *assignment = this->val_assignment(local_idx);
1557 auto ap = AssignmentPartRef{assignment, part};
1558 if (ap.modified()) {
1565 return AsmReg::make_invalid();
1568template <IRAdaptor Adaptor,
1570 template <
typename,
typename,
typename>
class BaseTy,
1572typename CompilerA64<Adaptor, Derived, BaseTy, Config>::Jump
1573 CompilerA64<Adaptor, Derived, BaseTy, Config>::invert_jump(
1574 Jump jmp)
noexcept {
1595 default: TPDE_UNREACHABLE(
"invalid jump kind");
1599template <IRAdaptor Adaptor,
1601 template <
typename,
typename,
typename>
typename BaseTy,
1603typename CompilerA64<Adaptor, Derived, BaseTy, Config>::Jump
1604 CompilerA64<Adaptor, Derived, BaseTy, Config>::swap_jump(
1605 Jump jmp)
noexcept {
1626 default: TPDE_UNREACHABLE(
"invalid jump kind for swap_jump");
1630template <IRAdaptor Adaptor,
1632 template <
typename,
typename,
typename>
typename BaseTy,
1635 Jump jmp, Label target_label)
noexcept {
1636 const auto is_pending = this->text_writer.label_is_pending(target_label);
1637 this->text_writer.ensure_space(4);
1641 this->text_writer.label_ref(target_label,
1642 this->text_writer.offset() - 4,
1643 LabelFixupKind::AARCH64_BR);
1645 const auto label_off = this->text_writer.label_offset(target_label);
1646 const auto cur_off = this->text_writer.offset();
1647 assert(cur_off >= label_off);
1648 const auto diff = cur_off - label_off;
1649 assert((diff & 0b11) == 0);
1650 assert(diff < 128 * 1024 * 1024);
1652 ASMNC(B, -
static_cast<ptrdiff_t
>(diff) / 4);
1660 const auto label_off = this->text_writer.label_offset(target_label);
1661 const auto cur_off = this->text_writer.offset();
1662 assert(cur_off >= label_off);
1663 off = cur_off - label_off;
1664 assert((off & 0b11) == 0);
1665 assert(off < 128 * 1024 * 1024);
1668 if (off <= 1024 * 1024) {
1669 auto imm19 = -
static_cast<ptrdiff_t
>(off) / 4;
1671 if (jmp.cmp_is_32) {
1672 ASMNC(CBZw, jmp.cmp_reg, imm19);
1674 ASMNC(CBZx, jmp.cmp_reg, imm19);
1677 if (jmp.cmp_is_32) {
1678 ASMNC(CBNZw, jmp.cmp_reg, imm19);
1680 ASMNC(CBNZx, jmp.cmp_reg, imm19);
1685 this->text_writer.label_ref(target_label,
1686 this->text_writer.offset() - 4,
1687 LabelFixupKind::AARCH64_COND_BR);
1690 assert(!is_pending);
1691 this->text_writer.ensure_space(2 * 4);
1694 if (jmp.cmp_is_32) {
1695 ASMNC(CBNZw, jmp.cmp_reg, 2);
1697 ASMNC(CBNZx, jmp.cmp_reg, 2);
1700 if (jmp.cmp_is_32) {
1701 ASMNC(CBZw, jmp.cmp_reg, 2);
1703 ASMNC(CBZx, jmp.cmp_reg, 2);
1707 ASMNC(B, -
static_cast<ptrdiff_t
>(off + 4) / 4);
1715 const auto label_off = this->text_writer.label_offset(target_label);
1716 const auto cur_off = this->text_writer.offset();
1717 assert(cur_off >= label_off);
1718 off = cur_off - label_off;
1719 assert((off & 0b11) == 0);
1720 assert(off < 128 * 1024 * 1024);
1723 if (off <= 32 * 1024) {
1724 auto imm14 = -
static_cast<ptrdiff_t
>(off) / 4;
1726 ASMNC(TBZ, jmp.cmp_reg, jmp.test_bit, imm14);
1728 ASMNC(TBNZ, jmp.cmp_reg, jmp.test_bit, imm14);
1732 this->text_writer.label_ref(target_label,
1733 this->text_writer.offset() - 4,
1734 LabelFixupKind::AARCH64_TEST_BR);
1737 assert(!is_pending);
1738 this->text_writer.ensure_space(2 * 4);
1742 ASMNC(TBNZ, jmp.cmp_reg, jmp.test_bit, 2);
1744 ASMNC(TBZ, jmp.cmp_reg, jmp.test_bit, 2);
1747 ASMNC(B, -
static_cast<ptrdiff_t
>(off + 4) / 4);
1752 Da64Cond cond, cond_compl;
1810 default: TPDE_UNREACHABLE(
"invalid jump kind");
1816 const auto label_off = this->text_writer.label_offset(target_label);
1817 const auto cur_off = this->text_writer.offset();
1818 assert(cur_off >= label_off);
1819 off = cur_off - label_off;
1820 assert((off & 0b11) == 0);
1821 assert(off < 128 * 1024 * 1024);
1824 if (off <= 1024 * 1024) {
1825 ASMNC(BCOND, cond, -
static_cast<ptrdiff_t
>(off) / 4);
1828 this->text_writer.label_ref(target_label,
1829 this->text_writer.offset() - 4,
1830 LabelFixupKind::AARCH64_COND_BR);
1833 assert(!is_pending);
1834 this->text_writer.ensure_space(2 * 4);
1837 ASMNC(BCOND, cond_compl, 2);
1839 ASMNC(B, -
static_cast<ptrdiff_t
>(off + 4) / 4);
1844 template <
typename,
typename,
typename>
class BaseTy,
1847 Jump jmp)
noexcept {
1864 default: TPDE_UNREACHABLE(
"invalid jump kind for conversion to Da64Cond");
1870 template <
typename,
typename,
typename>
class BaseTy,
1873 Jump cc, AsmReg dst)
noexcept {
1879 template <
typename,
typename,
typename>
class BaseTy,
1882 Jump cc, AsmReg dst)
noexcept {
1887 template <
typename,
typename,
typename>
class BaseTy,
1893 AsmReg false_select,
1894 bool is_64)
noexcept {
1895 this->text_writer.ensure_space(4);
1898 ASMNC(CSELx, dst, true_select, false_select, cond);
1900 ASMNC(CSELw, dst, true_select, false_select, cond);
1906 template <
typename,
typename,
typename>
class BaseTy,
1909 AsmReg dst, AsmReg src,
bool sign, u32 from, u32 to)
noexcept {
1910 assert(from < to && to <= 64);
1914 ASM(SBFXw, dst, src, 0, from);
1916 ASM(SBFXx, dst, src, 0, from);
1920 ASM(UBFXw, dst, src, 0, from);
1922 ASM(UBFXx, dst, src, 0, from);
1929 template <
typename,
typename,
typename>
typename BaseTy,
1932 std::variant<SymRef, ValuePart> &&target,
1933 std::span<CallArg> arguments,
1934 typename Base::ValueRef *result,
1938 for (
auto &arg : arguments) {
1939 cb.add_arg(std::move(arg));
1941 cb.call(std::move(target));
1943 cb.add_ret(*result);
1949 template <
typename,
typename,
typename>
typename BaseTy,
1951void CompilerA64<Adaptor, Derived, BaseTy, Config>::switch_emit_cmp(
1952 AsmReg cmp_reg, AsmReg tmp_reg, u64 case_value,
bool width_is_32)
noexcept {
1954 if (!ASMIF(CMPwi, cmp_reg, case_value)) {
1956 ASM(CMPw, cmp_reg, tmp_reg);
1959 if (!ASMIF(CMPxi, cmp_reg, case_value)) {
1961 ASM(CMPx, cmp_reg, tmp_reg);
1966template <IRAdaptor Adaptor,
1968 template <
typename,
typename,
typename>
typename BaseTy,
1970void CompilerA64<Adaptor, Derived, BaseTy, Config>::switch_emit_cmpeq(
1975 bool width_is_32)
noexcept {
1976 switch_emit_cmp(cmp_reg, tmp_reg, case_value, width_is_32);
1980template <IRAdaptor Adaptor,
1982 template <
typename,
typename,
typename>
typename BaseTy,
1984bool CompilerA64<Adaptor, Derived, BaseTy, Config>::switch_emit_jump_table(
1985 Label default_label,
1986 std::span<const Label> labels,
1991 bool width_is_32)
noexcept {
1992 if (low_bound != 0) {
1993 switch_emit_cmp(cmp_reg, tmp_reg, low_bound, width_is_32);
1996 switch_emit_cmp(cmp_reg, tmp_reg, high_bound, width_is_32);
1999 if (low_bound != 0) {
2000 if (!ASMIF(SUBxi, cmp_reg, cmp_reg, low_bound)) {
2002 ASM(SUBx, cmp_reg, cmp_reg, tmp_reg);
2007 this->text_writer.ensure_space(4 * 4 + 4 * labels.size());
2009 Label jump_table = this->text_writer.label_create();
2010 u32 adr_off = this->text_writer.offset();
2011 this->text_writer.write_unchecked(u32(0));
2014 ASMNC(LDRSWxr_uxtw, cmp_reg, tmp_reg, cmp_reg,
true);
2016 ASMNC(LDRSWxr_lsl, cmp_reg, tmp_reg, cmp_reg,
true);
2018 ASMNC(ADDx, tmp_reg, tmp_reg, cmp_reg);
2021 u32 table_off = this->text_writer.offset();
2022 this->text_writer.label_place(jump_table, table_off);
2023 for (Label label : labels) {
2024 this->text_writer.label_ref(
2025 label, this->text_writer.offset(), LabelFixupKind::AARCH64_JUMP_TABLE);
2026 this->text_writer.write_unchecked(table_off);
2029 assert(table_off - adr_off <= 1 * 1024 * 1024);
2030 u32 *adr =
reinterpret_cast<u32 *
>(this->text_writer.begin_ptr() + adr_off);
2031 *adr = de64_ADR(tmp_reg, adr_off, table_off);
2035template <IRAdaptor Adaptor,
2037 template <
typename,
typename,
typename>
typename BaseTy,
2039void CompilerA64<Adaptor, Derived, BaseTy, Config>::switch_emit_binary_step(
2045 bool width_is_32)
noexcept {
2046 switch_emit_cmpeq(case_label, cmp_reg, tmp_reg, case_value, width_is_32);
2050template <IRAdaptor Adaptor,
2052 template <
typename,
typename,
typename>
typename BaseTy,
2054CompilerA64<Adaptor, Derived, BaseTy, Config>::ScratchReg
2056 SymRef sym, TLSModel model)
noexcept {
2059 case TLSModel::GlobalDynamic: {
2060 assert(!this->stack.is_leaf_function);
2061 this->stack.generated_call =
true;
2062 ScratchReg r0_scratch{
this};
2063 AsmReg r0 = r0_scratch.alloc_specific(AsmReg::R0);
2064 ScratchReg r1_scratch{
this};
2065 AsmReg r1 = r1_scratch.alloc_specific(AsmReg::R1);
2068 if (this->register_file.is_used(Reg{AsmReg::LR})) {
2072 this->text_writer.ensure_space(0x18);
2074 sym, elf::R_AARCH64_TLSDESC_ADR_PAGE21, this->text_writer.offset(), 0);
2075 ASMNC(ADRP, r0, 0, 0);
2077 sym, elf::R_AARCH64_TLSDESC_LD64_LO12, this->text_writer.offset(), 0);
2078 ASMNC(LDRxu, r1, r0, 0);
2080 sym, elf::R_AARCH64_TLSDESC_ADD_LO12, this->text_writer.offset(), 0);
2081 ASMNC(ADDxi, r0, r0, 0);
2083 sym, elf::R_AARCH64_TLSDESC_CALL, this->text_writer.offset(), 0);
2085 ASMNC(MRS, r1, 0xde82);
2087 ASMNC(ADDx, r0, r1, r0);
@ LOCAL
Symbol with local linkage, must be defined.
Helper class to write function text.
AArch64 AAPCS calling convention.
Helper class for building call sequences.
CallBuilder(Derived &compiler, CCAssigner &assigner) noexcept
Constructor.
Helper class to write function text for AArch64.
The IRAdaptor specifies the interface with which the IR-independent parts of the compiler interact wi...
void evict_reg(Reg reg) noexcept
constexpr Jump(Kind kind, AsmReg cmp_reg, bool cmp_is_32)
Cbz/Cbnz branch.
@ Tbnz
Test single bit and branch if not zero (Xn register)
@ Jge
Signed greater than or equal (N == V)
@ Jls
Unsigned lower or same (!(C == 1 && Z == 0))
@ Jhi
Unsigned higher (C == 1 && Z == 0)
@ Tbz
Test single bit and branch if zero (Xn register)
@ Jmi
Minus, negative (N == 1)
@ Jlo
Unsigned lower (C == 0)
@ Jlt
Signed less than (N != V)
@ Jgt
Signed greater than (Z == 0 && N == V)
@ Jhs
Unsigned higher or same (C == 1)
@ Jvc
No Overflow (V == 0)
@ Cbnz
Compare and branch if not zero (Wn or Xn register)
@ Jle
Signed lessthan or equal (!(Z == 0 && N == V))
@ Jcc
Carry clear (C == 0)
@ Jpl
Plus, positive or zero (N == 0)
@ Cbz
Compare and branch if zero (Wn or Xn register)
constexpr Jump(Kind kind, AsmReg cmp_reg, u8 test_bit)
Tbz/Tbnz branch.
constexpr Jump(Kind kind)
Unconditional or conditional branch based on flags.
constexpr Jump()
Unconditional branch.
void generate_raw_bfiz(AsmReg dst, AsmReg src, u32 lsb, u32 width) noexcept
Bitfield insert in zero. src is not modified.
u32 func_arg_stack_add_off
Offset to the add sp, sp, XXX instruction that the argument handling uses to access stack arguments i...
AsmReg permanent_scratch_reg
Permanent scratch register, e.g.
void generate_raw_set(Jump cc, AsmReg dst) noexcept
Set dst to 1 if cc is true, otherwise set it to zero.
std::optional< i32 > prologue_assign_arg_part(ValuePart &&vp, CCAssignment cca) noexcept
Assign argument part.
void alloca_dynamic(u64 elem_size, ValuePart &&count, u32 align, ValuePart &res) noexcept
Dynamic alloca of a dynamically-sized region (elem_size * count bytes).
void prologue_begin(CCAssigner *cc_assigner) noexcept
Begin prologue, prepare for assigning arguments.
void materialize_constant(u64 const_u64, RegBank bank, u32 size, AsmReg dst) noexcept
Materialize constant into a register.
void generate_raw_jump(Jump jmp, Label target) noexcept
Generate jump instruction to target label.
void generate_raw_bfi(AsmReg dst, AsmReg src, u32 lsb, u32 width) noexcept
Bitfield insert. src is not modified.
void generate_call(std::variant< SymRef, ValuePart > &&target, std::span< CallArg > arguments, typename Base::ValueRef *result, bool variable_args=false)
Generate a function call.
void prologue_end(CCAssigner *cc_assigner) noexcept
Finish prologue.
void alloca_fixed(u64 size, u32 align, ValuePart &res) noexcept
Dynamic alloca of a fixed-size region.
void generate_raw_mask(Jump cc, AsmReg dst) noexcept
Set all bits of dst to 1 if cc is true, otherwise set dst to zero.
Da64Cond jump_to_cond(Jump jmp) noexcept
Convert jump condition to disarms Da64Cond.
void generate_raw_select(Jump cc, AsmReg dst, AsmReg true_select, AsmReg false_select, bool is_64) noexcept
Moves true_select into dst if cc is true, otherwise move false_select into dst.
ScratchReg tls_get_addr(SymRef sym, TLSModel model) noexcept
Generate code sequence to load address of sym into a register.
void materialize_constant(const u64 *data, RegBank bank, u32 size, AsmReg dst) noexcept
Materialize constant into a register.
void generate_raw_intext(AsmReg dst, AsmReg src, bool sign, u32 from, u32 to) noexcept
Integer extension. src is not modified.