37using Moves = fe::Vector<std::pair<Reg, Src>>;
50 , ret_(
fun->ret_var()) {}
53 void emit(
bool global) { fun_->world().driver().GET_FUN_PTR(
"mach",
mim_mach_emit)(*
this, global); }
76 virtual void slot_addr(
size_t dst,
size_t first,
size_t n) = 0;
81 virtual void branch_if(
size_t r, std::string_view
label,
bool unless =
false) = 0;
84 virtual void call_insn(std::string_view sym, std::optional<std::array<size_t, Target::Num_Classes>> num_args) = 0;
93 template<
class... Args>
94 void line(std::format_string<Args...> fmt, Args&&... args) {
95 std::print(body_,
"\t");
96 std::println(body_, fmt, std::forward<Args>(args)...);
101 std::string_view
backend()
const {
return target_.name(); }
104 std::string
symbol(
Lam* lam) {
return emit_.symbol(lam); }
106 if (
auto l =
Lit::isa(def))
return *l;
107 def->
blame(
"{} backend: expected a literal, got `{}`",
backend(), def).bail();
112 if (
auto r = fe::lookup(regs_, def))
return *r;
113 def->
blame(
"{} backend: `{}` has no register",
backend(), def).bail();
118 if (
Lit::isa(def)) def->
blame(
"{} backend: literal `{}` cannot be an operand here",
backend(), def).bail();
119 def->
blame(
"{} backend: `{}` is not in a register",
backend(), def).bail();
122 if (
auto r = fe::lookup(regs_, def))
return *r;
138 if (fl != flags_) fl->
blame(
"{} backend: flags `{}` are clobbered before their use",
backend(), fl).bail();
147 auto [i, ins] = slots_.emplace(def, num_slots_);
148 if (ins) ++num_slots_;
153 auto res = num_slots_;
154 num_slots_ += (bytes + 7) / 8;
161 return std::ranges::fold_left(saved_,
size_t(0), [](
size_t n,
const auto& rs) {
return n + rs.size(); });
165 std::ostringstream&
body() {
return body_; }
173 void run(
bool global);
174 void insn(
const App*);
175 void block(
Lam* lam,
const fe::Vector<const App*>& insns);
176 using SlotMoves = fe::Vector<std::pair<size_t, size_t>>;
178 void parallel(
Moves moves, SlotMoves slot_moves);
180 std::pair<Moves, SlotMoves> moves(
Lam*
target,
const Def* arg);
186 std::array<size_t, Target::Num_Classes> pass(
const Def* arg,
size_t num_args,
const ParamRegs* conv);
189 void call(
Lam* callee,
const Def* arg);
200 const Liveness* live_ =
nullptr;
201 std::ostringstream body_;
204 size_t num_slots_ = 0;
207 bool inlining_ =
false;
209 Lam* next_ =
nullptr;
210 const Def* flags_ =
nullptr;
fe::Error & blame(fe::cite_string< Args... > s, Args &&... args) const
Reports an error that blames this; chain Error::n for Notes and Error::bail to throw.
std::string unique_name() const
name + "_" + Def::gid
static std::optional< T > isa(const Def *def)
The machine a native backend compiles for, as far as libmim needs to know it.
The emit Phase of a native backend: writes GNU assembler code for each external function and each fun...
Writes the GNU assembler code of one function on the registers that Color assigned.
virtual void spill(Src src, const Def *type, size_t i)=0
Stores src, a value of type, into the spill slot i.
virtual void mov_imm(Reg dst, const Def *lit)=0
virtual void prologue(std::ostream &os)=0
The code between the function's label and its body; slots() and saved() are final by then.
size_t spill_slot(const Def *def)
std::string label(Lam *lam) const
Src source(const Def *def) const
virtual void pop_slot(size_t i)=0
virtual void branch_if(size_t r, std::string_view label, bool unless=false)=0
Branches to label if the Bool in register r is set, or with unless, if it is clear.
const Target & target() const
std::ostringstream & body()
virtual void reload(Reg dst, const Def *type, size_t i)=0
Loads the spill slot i, a value of type, into dst.
void set_flags(const Def *fl)
void use_flags(const Def *fl) const
Rejects a use of the flags fl if something clobbered them since.
virtual std::string_view function_type() const
The argument of .type, e.g. @function.
virtual void jmp(std::string_view label)=0
friend void mim_mach_emit(Emitter &, bool)
Emitter::emit, compiled once into libmim_mach; Emitter reaches it via GET_FUN_PTR.
virtual void push_slot(size_t i)=0
Pushes the spill slot i; the pops come in reverse order.
virtual void move(Reg dst, Reg src)=0
void line(std::format_string< Args... > fmt, Args &&... args)
virtual void swap(Reg, Reg)
Exchanges two registers of one class; only called if there is no scratch().
size_t use_reg(const Def *def) const
The register of the operand def, a Target::in, possibly under a Target::is_view.
virtual void slot_addr(size_t dst, size_t first, size_t n)=0
Puts the address of the n slots from first on into the register dst.
virtual void leave()=0
Tears down the frame that prologue() built.
virtual void step(const App *app)=0
const fe::Vector< size_t > & saved(Target::Class cls) const
The callee-saved registers of class cls that hold values.
virtual void branch_cc(size_t cc, std::string_view label)=0
Branches to label if the condition code cc holds.
bool flags_clobbered() const
nat_t nat(const Def *def) const
virtual std::optional< Reg > scratch(Target::Class) const
A register of class cls that breaks a copy cycle instead of swap(), if any.
std::string symbol(Lam *lam)
size_t frame_size() const
The bytes of the callee-saved registers, slots, and spill slots, rounded up to keep sp 16-byte aligne...
size_t def_reg(const Def *def) const
The register of the result def.
virtual void call_insn(std::string_view sym, std::optional< std::array< size_t, Target::Num_Classes > > num_args)=0
Calls sym with num_args arguments per register class in the ABI's argument registers.
void label_here(std::string_view label)
std::string_view backend() const
size_t alloc_slots(nat_t bytes)
Reserves bytes and yields the index of their first slot.
std::optional< size_t > isa_reg(const Def *def) const
virtual ~Emitter()=default
virtual void load_arg(Reg dst, const Def *type, size_t i)=0
Loads the i-th argument that the caller passed on the stack, a value of type, into dst.
Emitter(const Target &target, std::ostream &os, AsmEmit &emit, Lam *fun)
Liveness of the values of one scheduled function, as a side table.
The machine facts of a native backend that the phases of mach need.
static const Def * strip(const Def *def)
The v of in r v, zext d v, or zext d (in r v), otherwise def.
static size_t in_reg(const Def *in)
The register r of in r v.
Class
The register classes; Stack values live in a spill slot and are unbounded.
static bool is_in(const Def *def)
static const Def * unview(const Def *def)
The v of zext d v, otherwise def.
static constexpr std::string_view local_prefix()
The prefix of an assembler-local label.
fe::Vector< std::optional< size_t > > ParamRegs
The register per parameter of a function with a convention of its own, or none if it is unused.
void mim_mach_emit(Emitter &, bool global)
Emitter::emit, compiled once into libmim_mach; Emitter reaches it via GET_FUN_PTR.
Lam * forwarded(const LamMap< Lam * > &forward, Lam *lam)
Where a jump to lam goes once the blocks that forward skips are skipped.
fe::Vector< std::pair< Reg, Src > > Moves
The source of a copy: a register or a literal; reg.cls is the class of either.
GIDMap< const Def *, To > DefMap
GIDMap< Lam *, To > LamMap
A physical register of a register class.
bool operator==(const Reg &) const =default