The compiler pipeline
Compilation is AOT, in-process, and deterministic. There is no JIT and no deoptimization: whatever the checker proves is final, and the emitted bytecode is the whole story. The model around this stage — the deterministic reference, the archive, instantiate + link — is The compilation model.
source ─► lex/parse (rut-lexer, rut-parser) [The frontend](frontend.md)
─► resolve + typecheck (rut-lir/check)
─► monomorphize + compile bodies (rut-lir/lir)
─► optimize (fold, inline, CSE/LICM, peephole, SROA)
─► FuncCode — typed register bytecode [Typed bytecode](typed-bytecode.md)
─► link + flatten (rut-core/link) [Loading](loading.md)
─► encode (rut-core/binary) [Module binary](module-binary.md)
The unit of compilation is the module. A .d.rut declaration surface runs
through resolve/typecheck and stops there — it has no bodies, so it
publishes a signature surface, never code.
Stages
| stage | crate | in → out | notes |
|---|---|---|---|
| lex + parse | rut-lexer, rut-parser | source → AST + diags | flat arena, no recursion |
| collect | rut-lir/check/collect | AST → types, interfaces | per-module symbol tables |
| resolve | rut-lir/check/resolve | paths → symbols | imports, Self, visibility, use-path routing |
| typecheck | rut-lir/check (Ctx) | expressions → TyIds | bidirectional inference, fused with body compilation |
| monomorphize | rut-lir/check/inst | generic calls → instantiations | a work queue; HIR contains no generic code |
| compile bodies | rut-lir/lir (FnCompiler) | instantiations → FuncCode | one function at a time |
| optimize | rut-lir/lir (peephole, sroa, …) | FuncCode → FuncCode | fixed pipeline, no flags |
| link + flatten | rut-core/link | modules → one Program | type-id rebase, interface-table merge |
| encode | rut-core/binary | Program → bytes | versioned, little-endian, hash-stable |
The middle stages are deliberately one crate: the checker’s
monomorphization queue drives the body compiler and the body compiler
reports new instantiations back through the checking context. The fusion
keeps instantiation admission exact — every substitution-completing call
site is checked against its inline requires bounds as it is compiled.
Resolve
Name resolution walks the AST and binds every path to a symbol:
useimports resolve against the run’s offered pkgs — exact, single-step: a use path resolves only if a pkg with that name was offered. Missing pkgs diagnose against the consumer manifest (see Loading and Dependency kinds).Selfbinds inside impls;pubvisibility is checked per Modules and visibility.- Struct-vs-class is decided here: literals are legal only for structs; classes construct through their class methods.
isexpressions resolve their right-hand side to a concrete type or an interface instantiation id;ison an erasure-typed receiver answers by the box (see opaque — erasure and downcast).host/externsurface references resolve against the declaration surfaces of the packages the module imports, with slot ids attached to every member reference.
Typecheck
Bidirectional inference: expected types flow down, literal types flow up.
Every expression node is decorated with a type id (an index into the
module’s type table). Generic calls get their instantiation inferred or
take it explicitly (downcast<Point>(o)).
Laws enforced here:
- The
==law — primitives andstrcompare by value; every other cell type compares by identity; nullable?Toperands and tuple operands are a compile error (pattern-match instead — destructure the pair). A lint flags==between two obviously fresh composites. isfolding — when the receiver’s static type already answers the question (a concrete receiver,d: I is I), the expression folds to a constant and an always-true/false lint fires.- Union bounds — a generic parameter’s
requires T1 | T2bound is checked at every site that completes the substitution (Type aliases and union bounds). - Interface bounds — a
T requires Ibound checks the interface’s member set once, whereTis chosen: at a generic call’s instantiation and at a satisfying value’s crossing. A type without the members is refused there, naming the first missing member (Interfaces and dispatch). - The crossing rule — an
entry fn’s published signature may only use types that cross the host boundary: primitives,str,bytes,opaque,?Tover a crossing type (nil-flattened), and tuples of crossing types. A violation is a compile error, so a bad surface never reaches the embedder at load time (The host boundary).
There is no orphan rule to enforce — nothing registers. Satisfaction is checked at boundaries only, so the checker’s interface work is a per-call-site member-set match, never a program-wide search.
Monomorphization
Generic functions never reach a binary. The checker’s instantiation queue
compiles one concrete copy per substitution; the queue closes because
instantiating a body can enqueue more. Instantiation names ([i32],
Vec<f32>) are synthesized into the shared interner, and type_id<T>()
folds to a constant from the type table at this point — it never executes
at runtime.
The same law fixes WHERE an instantiation compiles: where the body
lives. Vec<i64> is one type program-wide — the declaring package owns
every instantiation of its generics, and consumers request them: a
consumer’s spelling lays out the concrete shape and routes the bodies to
the declaring package’s compile, so a linked library’s
make() -> Vec<i64> and the consumer’s own Vec<i64> are one row, and
the binary ships one copy of each instantiation’s code.
There is no splice lane and no inline flag left: every module
links. A class’s methods cross on the surface’s inherent rows (the
ordinary call boundary — the ink-pattern packages over host rows
compile like any other), and a generic export links owner-anchored,
the way the section above describes. The swappable std packages
(ink, pouch, flow, nmapset, json, strbuild, futures,
http) are ordinary linked packages. See
Project structure and rut.jsonc.
The type lattice
In a no-JIT VM, compile-time type knowledge is the only knowledge. The IR tracks a per-value lattice:
exact concrete > I (interface-typed, satisfies I)
- Exact types compile to direct calls and known layouts.
- Interface-typed values (
s: Drawable) are unsized: the payload lives in a heap cell and the slot stores the cell handle. One indirect itable call per multi-origin use; fields are inaccessible; no inlining without evidence. The cost is per-call, never per-field — and when a call’s receiver is statically concrete (a single tracked origin, a monomorphic body), the call devirtualizes at body-compile time and the dispatch overhead is zero. - Erasure sits off the lattice. The
opaqueprimitive is reached only through the type-callopaque(v), andopaque::downcast<T>(o)is the refinement: the successful branch re-enters the exact lattice position, so downstream code optimizes as if nothing was erased. Downcast checks are pure dataflow — repeated checks CSE, invariant ones hoist, and a chain over one cell folds to a type-id switch. What the compiler must not assume is the type inside a box at a given program point; speculative devirtualization throughopaqueis JIT behavior and does not exist. - The slice tier parallels interface widening:
Array<T, N> | Vec<T> (concrete) > Slice<T> view (unsized).Nis part of the type’s identity; slices are never boxed or erased.
The intended program shape: exact types on the hot path, interface-typed
values where polymorphism is real, opaque only inside heterogeneous
storage. Lints flag downcast in loop bodies and erasure crossing
non-storage function boundaries.
Optimization
A fixed pipeline with no flags:
- Folding — constant arithmetic,
type_id<T>(),Array<T, N>.len()→ the constantN(with const-index bounds checks folded against it), statically-decidedisprobes, dead branches. - Inlining — single-callee calls and small bodies (a callee op-count budget). Cross-module inlining after linking is future work; binaries carry no cross-function inlined code.
- CSE / LICM over pure operations — type-id loads, downcast checks, field loads on immutable records.
- Peephole + SROA — the rewriters re-intern operand pools on register
remap, so pool sharing stays consistent (see
Typed bytecode). SROA declines a record whose
class carries a
[disposal]member: the cell’s death is observable (it runs runs at refcount zero), so its mint is never deleted. - Pattern lowering — downcast chains become one type-id load plus a
jump table;
whenon enums lowers tobrtableover the member slot.
The pipeline is honest by construction: there is no tier-2 to fall back on, so the emitted code must be right the first time. Every gate measures against the example programs and the benchmark corpus.
Async lowering
async fn compiles to a state machine: each await is a checkpoint
state in the hidden frame, resume dispatch is the existing jump-table op,
locals become frame fields, and suspension is a plain return. The op set
grows zero rows for this — the driven half is an ordinary itable call
through the future’s designated yield slot (the closed Future class’s
engine ABI). The full protocol lives in
Async and await and launched futures.
Determinism
Same source + same dependency versions + same compiler version ⇒ byte-identical output. Serialization is ordered and little-endian everywhere, and the graph compiles dependencies in a fixed post-order before flattening. This is what makes compile caches content-addressable, bundles diffable, and trace restoration by recompilation possible (Diagnostics, traces, and symbolication).