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

See also
mim::plug::x86

The native x86-64 backend. Loading this plugin (mim foo.mim -p x86) appends instruction selection, register allocation, and emission to the compilation pipeline. Instruction selection rewrites core, math, and mem into the axioms below and mach.slot; register allocation is the phases of mach. A literal operand of an instruction is an immediate and has to fit its encoding; mov puts any other constant into a register. Registers are no concern of these types; register allocation introduces them later. The schedule is the mem.M 0 chain: memory instructions sit on it, and scheduling threads all other instructions onto it via sched.

Dependencies

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

Types

Addr

A memory operand disp(base, index, scale); the Bool states whether it has an index. It is no value and occupies no register of its own; only memory instructions and lea take one.

axm Addr: Bool → *;

Base

An address disp(base) without an index.

anx let Base = Addr ff;

Index

An address disp(base, index, scale).

anx let Index = Addr tt;

addr

addr (disp, base) addresses disp + base, like AT&T's disp(base). A literal 0I64 as base means there is none. A literal base folds into disp if the sum fits, else it goes into a register.

axm addr: [disp: I32, base: I64] → Base, normalize_addr;

index

index a (i, scale) addresses a + (i << scale), like AT&T's disp(base, index, scale). An address has at most one index.

axm index: Base → [index: I64, scale: I2] → Index;

These lets name the scales.

anx let scale_1 = 0I2;
anx let scale_2 = 1I2;
anx let scale_4 = 2I2;
anx let scale_8 = 3I2;

Operand Sizes

Instructions are polymorphic in the operand size s and state which sizes they support via refly.check in their codomain. A literal s folds the check away; an unsupported one is an error.

lam is (s n: Nat): Bool = core.ncmp.e (s, n);

gpr

i8, i16, i32, or i64.

anx lam is_gpr (s: Nat): Bool = core.bit2.or 0 (core.bit2.or 0 (is (s, i8), is (s, i16)), core.bit2.or 0 (is (s, i32), is (s, i64)));
anx lam gpr (s: Nat): Nat = refly.check (is_gpr s, s, "x86: operand size must be i8, i16, i32, or i64");

gpr16

i16, i32, or i64.

anx lam gpr16 (s: Nat): Nat =
let ok = core.bit2.or 0 (is (s, i16), core.bit2.or 0 (is (s, i32), is (s, i64)));
refly.check (ok, s, "x86: operand size must be i16, i32, or i64");

logic

Bool or a gpr size.

anx lam logic (s: Nat): Nat =
let ok = core.bit2.or 0 (is (s, 2), core.bit2.or 0 (core.bit2.or 0 (is (s, i8), is (s, i16)), core.bit2.or 0 (is (s, i32), is (s, i64))));
refly.check (ok, s, "x86: operand size must be Bool, i8, i16, i32, or i64");

Instruction Forms

The plugin pretends that x86 uses three-address code and copes with x86's two-address code later on. However, some artifacts shine through: for two-address instructions the tied value operand comes first; the tie is only a register-assignment constraint (add (a, b) computes a + b), and x86.emit reverses the value operands for its AT&T output.

rm

op r, addr: rm f (m, a, addr) is f (a, b) with b loaded from addr.

axm rm: {T C: *, i: Bool} → («2; T» → C) → [mem.M 0, T, Addr i] → [mem.M 0, C];

rm1

op r, addr of a unary operation: rm1 f (m, addr) is f b with b loaded from addr.

axm rm1: {T C: *, i: Bool} → (T → C) → [mem.M 0, Addr i] → [mem.M 0, C];

mr

op addr, r: mr f (m, addr, b) stores f (a, b) to addr with a loaded from addr.

axm mr: {s: Nat, C: *, i: Bool} → («2; Idx s» → C) → [mem.M 0, Addr i, Idx s] → mem.M 0;

Registers

reg

The general-purpose registers in hardware encoding order, as the register r of mach.in r v; the operand size selects the subregister. A register's subtag is its hardware encoding, so they have to stay in this order.

pub mod reg
anx let rax = 0I4;
anx let rcx = 1I4;
anx let rdx = 2I4;
anx let rbx = 3I4;
anx let rsp = 4I4;
anx let rbp = 5I4;
anx let rsi = 6I4;
anx let rdi = 7I4;
anx let r8 = 8I4;
anx let r9 = 9I4;
anx let r10 = 10I4;
anx let r11 = 11I4;
anx let r12 = 12I4;
anx let r13 = 13I4;
anx let r14 = 14I4;
anx let r15 = 15I4;
end

xmm

The floating-point registers in hardware encoding order, as the register r of mach.in r v for a math.F pe value v. A register's subtag is its hardware encoding, so they have to stay in this order.

pub mod xmm
anx let xmm0 = 0I4;
anx let xmm1 = 1I4;
anx let xmm2 = 2I4;
anx let xmm3 = 3I4;
anx let xmm4 = 4I4;
anx let xmm5 = 5I4;
anx let xmm6 = 6I4;
anx let xmm7 = 7I4;
anx let xmm8 = 8I4;
anx let xmm9 = 9I4;
anx let xmm10 = 10I4;
anx let xmm11 = 11I4;
anx let xmm12 = 12I4;
anx let xmm13 = 13I4;
anx let xmm14 = 14I4;
anx let xmm15 = 15I4;
end

Instructions

lea

lea r, addr materializes an address as a value. The address addr (0, b) is b, and addr (d, 0) the literal d.

axm lea: {i: Bool} → Addr i → I64, normalize_lea;

mov

mov r, imm materializes a constant.

axm mov: {s: Nat} → Idx s → Idx (logic s);

load

mov r, addr.

axm load: [s: Nat] → {i: Bool} → [mem.M 0, Addr i] → [mem.M 0, Idx (gpr s)];

store

mov addr, r.

axm store: {s: Nat, i: Bool} → [mem.M 0, Addr i, Idx s] → mem.M 0;

Arithmetic

Two-address arithmetic op r, r/imm.

axm (add, sub): {s: Nat} → «2; Idx s» → Idx (gpr s);
axm (and, or, xor): {s: Nat} → «2; Idx s» → Idx (logic s);
axm imul: {s: Nat} → «2; Idx s» → Idx (gpr16 s);

add3

Three-address add as lea, so neither operand is overwritten; it leaves the flags alone.

axm add3: {s: Nat} → «2; Idx s» → Idx (gpr s);

rsub

rsub (a, b) is a - b overwriting b instead of a, as neg b; add a, b. Only mach.constrain introduces it, where a stays live and b dies.

axm rsub: {s: Nat} → «2; Idx s» → Idx (gpr s);

imul3

imul3 (a, b) is imul $b, a, r: a product with the literal b that does not overwrite a. Only mach.constrain introduces it, where a stays live.

axm imul3: {s: Nat} → «2; Idx s» → Idx (gpr16 s);

Division

div/idiv yield the quotient (rax) and the remainder (rdx).

axm (div, idiv): {s: Nat} → «2; Idx s» → «2; Idx (gpr16 s)»;

Shifts

Shifts by cl or an immediate.

axm (shl, shr, sar): {s: Nat} → «2; Idx s» → Idx (gpr s);

Conversions

movzx, movsx, and truncation to the low bits of the register; they fold a literal. The extensions take their source size explicitly, since a folded load (x86.rm1) leaves no operand to infer it from.

axm movzx: [s: Nat] → [d: Nat] → Idx s → Idx d, normalize_movzx;
axm movsx: [s: Nat] → [d: Nat] → Idx s → Idx d, normalize_movsx;
axm trunc: {s: Nat} → [d: Nat] → Idx s → Idx d, normalize_trunc;

zero x is x with the bits of its register above s cleared, as mach.zext expects.

axm zero: {s: Nat} → Idx s → Idx s;

Comparisons

cmp r, r/imm and test r, r/imm.

axm (cmp, test): {s: Nat} → «2; Idx s» → mach.Flags;

Condition Codes

The condition codes in hardware encoding order:

Subtag cc
o 0
no 1
b 2
ae 3
e 4
ne 5
be 6
a 7
s 8
ns 9
p 10
np 11
l 12
ge 13
le 14
g 15

cc

Tests a condition code on the flags: jcc as a branch condition, setcc otherwise.

axm cc.(o, no, b, ae, e, ne, be, a, s, ns, p, np, l, ge, le, g): mach.Flags → Bool;

cmovcc

cmovcc.l (f, x, y) yields y if l holds on f, else x, like (x, y)#cond.

cmovcc.ne (test (cc.l f, cc.l f), x, y) normalizes to cmovcc.l (f, x, y).

axm cmovcc.(o, no, b, ae, e, ne, be, a, s, ns, p, np, l, ge, le, g): {s: Nat} → [mach.Flags, Idx s, Idx s] → Idx (gpr16 s), normalize_cmovcc;

Floating-Point Instructions

Floating-point values live in the XMM registers; the type math.F pe is one value, so its format depends on pe. An operand is never an immediate: a literal is materialized by fmov first.

fmov

mov r, imm materializes a floating-point constant from the constant pool.

axm fmov: {pe: «2; Nat»} → math.F pe → math.F (mach.fpe pe);

fload

movss/movsd r, addr.

axm fload: [pe: «2; Nat»] → {i: Bool} → [mem.M 0, Addr i] → [mem.M 0, math.F (mach.fpe pe)];

fstore

movss/movsd addr, r.

axm fstore: {pe: «2; Nat», i: Bool} → [mem.M 0, Addr i, math.F pe] → mem.M (mach.fok pe 0);

Floating-Point Arithmetic

Two-address arithmetic op r, r.

axm (fadd, fsub, fmul, fdiv): {pe: «2; Nat»} → «2; math.F pe» → math.F (mach.fpe pe);

Floating-Point Unary

sqrtss/sqrtsd and andps/andpd with a mask clearing the sign bit.

axm (fsqrt, fabs): {pe: «2; Nat»} → math.F pe → math.F (mach.fpe pe);

fcmp

Floating-point comparison; the predicates are those of math.cmp in its order:

Subtag Meaning
f always false
e ordered and equal
l ordered and less
le ordered and less or equal
g ordered and greater
ge ordered and greater or equal
ne ordered and not equal
o ordered (no NaNs)
u unordered (either is NaN)
ue unordered or equal
ul unordered or less
ule unordered or less or equal
ug unordered or greater
uge unordered or greater or equal
une unordered or not equal
t always true
axm fcmp.(f, e, l, le, g, ge, ne, o, u, ue, ul, ule, ug, uge, une, t): {pe: «2; Nat»} → «2; math.F pe» → Idx (mach.fok pe 2);

fsel

fsel (c, x, y) yields y if c holds, else x, like (x, y)#c.

axm fsel: {pe: «2; Nat»} → [Bool, math.F pe, math.F pe] → math.F (mach.fpe pe);

Floating-Point Conversions

Both formats and sizes are explicit, since a folded load (x86.rm1) leaves no operand to infer them from. The instances of x86_isel name the sizes; only i32 and i64 are supported.

axm i2f: [s: Nat] → [pe: «2; Nat»] → Idx s → math.F (mach.fpe pe);
axm u2f: [s: Nat] → [pe: «2; Nat»] → Idx s → math.F (mach.fpe pe);
axm f2i: [pe: «2; Nat»] → [s: Nat] → math.F pe → Idx (mach.fok pe s);
axm f2u: [pe: «2; Nat»] → [s: Nat] → math.F pe → Idx (mach.fok pe s);
axm f2f: [spe: «2; Nat»] → [dpe: «2; Nat»] → math.F spe → math.F (mach.fpe dpe);

Phases

isel

Instruction selection. -X x86:isel-dot=<file> writes the PBQP graph in Graphviz DOT format, with the chosen options highlighted.

axm isel: compile.Phase;

emit

Emits GNU assembler code (AT&T syntax) to <world>.s.

axm emit: compile.Phase;