MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
mim::plug::mach::Emitter Class Referenceabstract

Writes the GNU assembler code of one function on the registers that Color assigned. More...

#include <mim/plug/mach/emit.h>

Public Member Functions

 Emitter (const Target &target, std::ostream &os, AsmEmit &emit, Lam *fun)
virtual ~Emitter ()=default
void emit (bool global)

Protected Member Functions

template<class... Args>
void line (std::format_string< Args... > fmt, Args &&... args)
void label_here (std::string_view label)
const Target & target () const
std::string_view backend () const
Lam * fun () const
std::string label (Lam *lam) const
std::string symbol (Lam *lam)
nat_t nat (const Def *def) const
size_t def_reg (const Def *def) const
 The register of the result def.
size_t use_reg (const Def *def) const
 The register of the operand def, a Target::in, possibly under a Target::is_view.
std::optional< size_t > isa_reg (const Def *def) const
Src source (const Def *def) const
std::ostringstream & body ()
size_t frame_size () const
 The bytes of the callee-saved registers, slots, and spill slots, rounded up to keep sp 16-byte aligned.
Hooks
virtual void step (const App *app)=0
virtual void move (Reg dst, Reg src)=0
virtual void mov_imm (Reg dst, const Def *litlit)=0
virtual void swap (Reg, Reg)
 Exchanges two registers of one class; only called if there is no scratch().
virtual std::optional< Reg > scratch (Target::Class) const
 A register of class cls that breaks a copy cycle instead of swap(), if any.
virtual void push_slot (size_t i)=0
 Pushes the spill slot i; the pops come in reverse order.
virtual void pop_slot (size_t i)=0
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 reload (Reg dst, const Def *type, size_t i)=0
 Loads the spill slot i, a value of type, into dst.
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.
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 jmp (std::string_view label)=0
virtual void branch_cc (size_t cc, std::string_view label)=0
 Branches to label if the condition code cc holds.
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.
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.
virtual void prologue (std::ostream &os)=0
 The code between the function's label and its body; slots() and saved() are final by then.
virtual void leave ()=0
 Tears down the frame that prologue() built.
virtual std::string_view function_type () const
 The argument of .type, e.g. @function.
Flags
void set_flags (const Def *fl)
void clobber_flags ()
bool flags_clobbered () const
void use_flags (const Def *fl) const
 Rejects a use of the flags fl if something clobbered them since.
Frame

The frame holds 8-byte slots: spill slots and the areas of slot instructions.

size_t spill_slot (const Def *def)
size_t alloc_slots (nat_t bytes)
 Reserves bytes and yields the index of their first slot.
size_t num_slots () const
const fe::Vector< size_t > & saved (Target::Class cls) const
 The callee-saved registers of class cls that hold values.
size_t num_saved () const

Friends

void mim_mach_emit (Emitter &emitter, bool global)
 Emitter::emit, compiled once into libmim_mach; Emitter reaches it via GET_FUN_PTR.

Detailed Description

Writes the GNU assembler code of one function on the registers that Color assigned.

The control flow is target-independent: jumps, calls, returns, and the entry are parallel copies, the blocks are laid out by layout(), and the callee-saved registers that hold values are saved. A backend derives from it and emits its instructions, moves, branches, prologue, and epilogue.

Definition at line 43 of file emit.h.

Constructor & Destructor Documentation

◆ Emitter()

mim::plug::mach::Emitter::Emitter ( const Target & target,
std::ostream & os,
AsmEmit & emit,
Lam * fun )
inline

Definition at line 45 of file emit.h.

References emit(), fun(), and target().

Referenced by mim_mach_emit.

◆ ~Emitter()

virtual mim::plug::mach::Emitter::~Emitter ( )
virtualdefault

Member Function Documentation

◆ alloc_slots()

size_t mim::plug::mach::Emitter::alloc_slots ( nat_t bytes)
inlineprotected

Reserves bytes and yields the index of their first slot.

Definition at line 152 of file emit.h.

◆ backend()

std::string_view mim::plug::mach::Emitter::backend ( ) const
inlineprotected

Definition at line 101 of file emit.h.

Referenced by def_reg(), nat(), use_flags(), and use_reg().

◆ body()

std::ostringstream & mim::plug::mach::Emitter::body ( )
inlineprotected

Definition at line 165 of file emit.h.

◆ branch_cc()

virtual void mim::plug::mach::Emitter::branch_cc ( size_t cc,
std::string_view label )
protectedpure virtual

Branches to label if the condition code cc holds.

References label().

◆ branch_if()

virtual void mim::plug::mach::Emitter::branch_if ( size_t r,
std::string_view label,
bool unless = false )
protectedpure virtual

Branches to label if the Bool in register r is set, or with unless, if it is clear.

References label().

◆ call_insn()

virtual void mim::plug::mach::Emitter::call_insn ( std::string_view sym,
std::optional< std::array< size_t, Target::Num_Classes > > num_args )
protectedpure virtual

Calls sym with num_args arguments per register class in the ABI's argument registers.

A known() callee has no num_args: it is not variadic and its parameters may be in any register.

◆ clobber_flags()

void mim::plug::mach::Emitter::clobber_flags ( )
inlineprotected

Definition at line 134 of file emit.h.

◆ def_reg()

size_t mim::plug::mach::Emitter::def_reg ( const Def * def) const
inlineprotected

The register of the result def.

Definition at line 111 of file emit.h.

References backend(), and mim::Def::blame().

◆ emit()

void mim::plug::mach::Emitter::emit ( bool global)
inline

Definition at line 53 of file emit.h.

References mim_mach_emit.

Referenced by Emitter().

◆ flags_clobbered()

bool mim::plug::mach::Emitter::flags_clobbered ( ) const
inlineprotected

Definition at line 135 of file emit.h.

◆ frame_size()

size_t mim::plug::mach::Emitter::frame_size ( ) const
inlineprotected

The bytes of the callee-saved registers, slots, and spill slots, rounded up to keep sp 16-byte aligned.

Definition at line 168 of file emit.h.

References num_saved().

◆ fun()

Lam * mim::plug::mach::Emitter::fun ( ) const
inlineprotected

Definition at line 102 of file emit.h.

Referenced by Emitter().

◆ function_type()

virtual std::string_view mim::plug::mach::Emitter::function_type ( ) const
inlineprotectedvirtual

The argument of .type, e.g. @function.

Definition at line 90 of file emit.h.

◆ isa_reg()

std::optional< size_t > mim::plug::mach::Emitter::isa_reg ( const Def * def) const
inlineprotected

Definition at line 121 of file emit.h.

◆ jmp()

virtual void mim::plug::mach::Emitter::jmp ( std::string_view label)
protectedpure virtual

References label().

◆ label()

std::string mim::plug::mach::Emitter::label ( Lam * lam) const
inlineprotected

Definition at line 103 of file emit.h.

References mim::plug::mach::Target::local_prefix(), and mim::Def::unique_name().

Referenced by branch_cc(), branch_if(), jmp(), and label_here().

◆ label_here()

void mim::plug::mach::Emitter::label_here ( std::string_view label)
inlineprotected

Definition at line 98 of file emit.h.

References label().

◆ leave()

virtual void mim::plug::mach::Emitter::leave ( )
protectedpure virtual

Tears down the frame that prologue() built.

◆ line()

template<class... Args>
void mim::plug::mach::Emitter::line ( std::format_string< Args... > fmt,
Args &&... args )
inlineprotected

Definition at line 94 of file emit.h.

◆ load_arg()

virtual void mim::plug::mach::Emitter::load_arg ( Reg dst,
const Def * type,
size_t i )
protectedpure virtual

Loads the i-th argument that the caller passed on the stack, a value of type, into dst.

◆ mov_imm()

virtual void mim::plug::mach::Emitter::mov_imm ( Reg dst,
const Def * lit )
protectedpure virtual

◆ move()

virtual void mim::plug::mach::Emitter::move ( Reg dst,
Reg src )
protectedpure virtual

◆ nat()

nat_t mim::plug::mach::Emitter::nat ( const Def * def) const
inlineprotected

Definition at line 105 of file emit.h.

References backend(), mim::Def::blame(), and mim::Lit::isa().

◆ num_saved()

size_t mim::plug::mach::Emitter::num_saved ( ) const
inlineprotected

Definition at line 160 of file emit.h.

Referenced by frame_size().

◆ num_slots()

size_t mim::plug::mach::Emitter::num_slots ( ) const
inlineprotected

Definition at line 157 of file emit.h.

◆ pop_slot()

virtual void mim::plug::mach::Emitter::pop_slot ( size_t i)
protectedpure virtual

◆ prologue()

virtual void mim::plug::mach::Emitter::prologue ( std::ostream & os)
protectedpure virtual

The code between the function's label and its body; slots() and saved() are final by then.

◆ push_slot()

virtual void mim::plug::mach::Emitter::push_slot ( size_t i)
protectedpure virtual

Pushes the spill slot i; the pops come in reverse order.

◆ reload()

virtual void mim::plug::mach::Emitter::reload ( Reg dst,
const Def * type,
size_t i )
protectedpure virtual

Loads the spill slot i, a value of type, into dst.

◆ saved()

const fe::Vector< size_t > & mim::plug::mach::Emitter::saved ( Target::Class cls) const
inlineprotected

The callee-saved registers of class cls that hold values.

Definition at line 159 of file emit.h.

◆ scratch()

virtual std::optional< Reg > mim::plug::mach::Emitter::scratch ( Target::Class ) const
inlineprotectedvirtual

A register of class cls that breaks a copy cycle instead of swap(), if any.

Definition at line 65 of file emit.h.

◆ set_flags()

void mim::plug::mach::Emitter::set_flags ( const Def * fl)
inlineprotected

Records that the flags hold the result fl.

Definition at line 133 of file emit.h.

◆ slot_addr()

virtual void mim::plug::mach::Emitter::slot_addr ( size_t dst,
size_t first,
size_t n )
protectedpure virtual

Puts the address of the n slots from first on into the register dst.

◆ source()

Src mim::plug::mach::Emitter::source ( const Def * def) const
inlineprotected

Definition at line 125 of file emit.h.

References mim::Lit::isa(), mim::plug::mach::Target::strip(), and use_reg().

◆ spill()

virtual void mim::plug::mach::Emitter::spill ( Src src,
const Def * type,
size_t i )
protectedpure virtual

Stores src, a value of type, into the spill slot i.

◆ spill_slot()

size_t mim::plug::mach::Emitter::spill_slot ( const Def * def)
inlineprotected

The slot of the spilled value def.

Definition at line 146 of file emit.h.

◆ step()

virtual void mim::plug::mach::Emitter::step ( const App * app)
protectedpure virtual

Emits the instruction app on the chain.

◆ swap()

virtual void mim::plug::mach::Emitter::swap ( Reg ,
Reg  )
inlineprotectedvirtual

Exchanges two registers of one class; only called if there is no scratch().

Definition at line 63 of file emit.h.

◆ symbol()

std::string mim::plug::mach::Emitter::symbol ( Lam * lam)
inlineprotected

Definition at line 104 of file emit.h.

◆ target()

const Target & mim::plug::mach::Emitter::target ( ) const
inlineprotected

Definition at line 100 of file emit.h.

Referenced by Emitter().

◆ use_flags()

void mim::plug::mach::Emitter::use_flags ( const Def * fl) const
inlineprotected

Rejects a use of the flags fl if something clobbered them since.

Definition at line 137 of file emit.h.

References backend(), and mim::Def::blame().

◆ use_reg()

size_t mim::plug::mach::Emitter::use_reg ( const Def * def) const
inlineprotected

The register of the operand def, a Target::in, possibly under a Target::is_view.

Definition at line 116 of file emit.h.

References backend(), mim::Def::blame(), mim::plug::mach::Target::in_reg(), mim::plug::mach::Target::is_in(), mim::Lit::isa(), and mim::plug::mach::Target::unview().

Referenced by source().

◆ mim_mach_emit

void mim_mach_emit ( Emitter & emitter,
bool global )
friend

Emitter::emit, compiled once into libmim_mach; Emitter reaches it via GET_FUN_PTR.

Definition at line 371 of file emit.cpp.

References Emitter(), and mim_mach_emit.

Referenced by emit(), and mim_mach_emit.


The documentation for this class was generated from the following files: