MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
The mach Plugin

See also
mim::plug::mach

The machine-independent surface of the native backends x86 and arm. A backend plugin loads it and supplies its own instructions, registers, and condition codes; this plugin holds the types, pseudo-instructions, and phases they share. The phases work on the one loaded backend (Driver::target) and read their arguments as that backend's, e.g. -X x86:k=<n>.

Dependencies

plugin core;
plugin mem;
plugin math;
plugin refly;
import compile;

Types

Flags

The condition flags that a comparison sets and a condition code tests.

axm Flags: *;

Spill

A spill slot holding a value of type T. It is no register value; the emitter gives it a place in the stack frame.

axm Spill: * → *;

Registers

in

in r v is v, which lives in register r of the class of v's type; n is the number of registers of the backend. Register allocation wraps every use of a value, so all uses of v share one in r v. It is no instruction: it is neither scheduled nor on the chain.

axm in: {n: Nat} → [r: Idx n] → {T: *} → T → T;

zext

zext d v is v zero-extended to Idx d, where the register of v holds it zero-extended already, as after a load. Like in, it is no instruction: uses of it are uses of v in the register of v. It folds a literal, which a backend keeps as an operand.

axm zext: {s: Nat} → [d: Nat] → Idx s → Idx d, normalize_zext;

Instructions

sched

Places the pure instruction f x on the mem.M 0 chain right after m: let (m, y) = mach.sched x86.add m (a, b). The chain token makes each placement a distinct Def, so copies, spills, and reloads at different points never merge.

axm sched: {D C: *} → (D → C) → mem.M 0 → D → [mem.M 0, C];

slot

Reserves n bytes in the stack frame and yields their address.

axm slot: [n: Nat] → mem.M 0 → [mem.M 0, I64];

load

Loads a T from the address p.

axm load: [T: *] → [mem.M 0, p: I64] → [mem.M 0, T];

store

Stores x to the address p.

axm store: {T: *} → [mem.M 0, p: I64, x: T] → mem.M 0;

spill

spill x stores x into a spill slot; it is placed on the chain via sched like any other pure instruction.

axm spill: {T: *} → T → Spill T;

reload

reload x loads x from its spill slot.

axm reload: {T: *} → Spill T → T;

Floating-Point Formats

A backend supports math.f32 and math.f64 only; its float instructions check the format in their codomain.

fpe

math.f32 or math.f64.

lam is_fpe (pe: «2; Nat»): Bool =
let (p, e) = pe;
let (p32, e32) = math.f32;
let (p64, e64) = math.f64;
let eq32 = core.bit2.and 0 (core.ncmp.e (p, p32), core.ncmp.e (e, e32));
let eq64 = core.bit2.and 0 (core.ncmp.e (p, p64), core.ncmp.e (e, e64));
core.bit2.or 0 (eq32, eq64);
anx lam fpe (pe: «2; Nat»): «2; Nat» = refly.check (is_fpe pe, pe, "native backend: float type must be f32 or f64");

fok

x, if pe is math.f32 or math.f64: the check of fpe for a codomain that does not name the format, e.g. Idx (fok pe 2) for Bool.

anx lam fok {T: *} (pe: «2; Nat») (x: T): T = refly.check (is_fpe pe, x, "native backend: float type must be f32 or f64");

float

The types of math's float operations and of a backend's float instructions, as the parameters of isel rule families.

pub mod float
pub let Op1 = Nat → {pe: «2; Nat»} → math.F pe → math.F pe;
pub let Op2 = Nat → {pe: «2; Nat»} → «2; math.F pe» → math.F pe;
pub let Pred = Nat → {pe: «2; Nat»} → «2; math.F pe» → Bool;
pub let Insn1 = {pe: «2; Nat»} → math.F pe → math.F (fpe pe);
pub let Insn2 = {pe: «2; Nat»} → «2; math.F pe» → math.F (fpe pe);

A math operation op onto a backend's instruction insn.

rule arith (op: Op2, insn: Insn2) {pe: «2; Nat»} (k: Nat, a b: math.F pe): op k (a, b) => insn (a, b);
rule unary (op: Op1, insn: Insn1) {pe: «2; Nat»} (k: Nat, x: math.F pe): op k x => insn x;
end

Instruction Selection

lit

The guard lit x of an isel rule holds if x is a literal.

axm lit: {T: *} → T → Bool, normalize_lit;

Phases

libcall

Turns each operation that a backend has no instruction for into a call of its C library function (see mim::plug::mach::LibCall). A math operation becomes a call of libm, e.g. math.exp on an f32 of expf; it covers exp, tri, pow, rt.cb, er, gamma, round, arith.rem, and extrema.fmin/fmax on f32 and f64. mem.strlen, mem.malloc, and mem.free become calls of strlen, malloc, and free. It runs before mem.add_mem, which threads the memory through the calls of the memory-free ones.

axm libcall: compile.Phase;

legalize

Turns every Nat value and every pointer into an I64, so that instruction selection only sees register types (see mim::plug::mach::Legalize). mem.lea becomes a core.wrap.add of its byte offset by the layout of core.trait, mem.load and mem.store become load and store, mem.mslot of a literal size becomes slot, and the Nat operations become their I64 counterparts. Only address space 0 is supported.

axm legalize: compile.Phase;

schedule

Makes each continuation's mem.M 0 chain total over the instructions of the backend, in a topological order (see mim::plug::mach::Schedule). Memory instructions are rethreaded onto the chain; all others are wrapped in sched.

axm schedule: compile.Phase;

close

Closes every join of a scheduled function over its values (see mim::plug::mach::Close): the values it uses but does not define become parameters, which every jump passes. A branch target or a return continuation has a single predecessor and stays nested in it. The parameters of a join are sorted by register class, the mem.M 0 first.

axm close: compile.Phase;

spilling

Spills and reloads until at most k values of each register class are live at every point (Braun & Hack, CC 2009). It assumes the ABI at every call; regalloc spills with the conventions of known callees instead. k is the number of allocatable registers of the class by default, and -X <backend>:k=<n> overrides it for the general-purpose one.

axm spilling: compile.Phase;

constrain

Moves the operands that an instruction needs in a fixed register, and a two-address operand that stays live, into a fresh continuation right before it. The jump is the copy, so the copies' live ranges end at the instruction and the interference graph stays chordal. The fixed operands are wrapped in in. Likewise, a fixed result that stays live is passed on to a fresh continuation right after its instruction.

axm constrain: compile.Phase;

regalloc

Allocates registers function by function, callees first: it spills like spilling, splits like constrain, and colors. Coloring assigns a register to every value by one PBQP per function and register class (Buchwald, Zwinkau, Bersch, CC 2011) and wraps each use of a value in in. Jumps, calls, and returns are parallel copies: their arguments only prefer the registers of the parameters they bind. It removes chain instructions whose results are all dead. A function that is only called or jumped to gets a convention of its own, so its callers spill and color against its actual clobbers. -X <backend>:color-dot=<file> writes the PBQP graph of each function in Graphviz DOT format, with the chosen options highlighted.

axm regalloc: compile.Phase;

live

Prints the liveness of every scheduled function to stdout, or to -X <backend>:live=<file>: per block its parameters, the live-in and live-out values, and the maximum number of live values per register class. It is not part of the default pipeline.

axm live: compile.Phase;