0016. Structured typed Core IR
- Status: Accepted; single-file source identity superseded by 0068
- Date: 2026-10-30
- Plan: M3.1–M3.6, M4, M5, M6
- Review: Compiler and backend
Context
The temporary AST-to-C backend re-derives evaluation order and divergence from a span-keyed type table. C and the reference interpreter need one typed, ordered representation. This supersedes 0011 and 0014; M3.3 removes their implementation.
Decision
Use structured statements over atoms, with source-order
function IDs and per-function local and loop IDs. Parameters
occupy locals 0..params. Strings are interned in
source-order first use. The data model is:
Program { file: String, strings: Vec<String>, funcs: Vec<Func>, main: FuncId }
FuncId(u32), LocalId(u32), LoopId(u32), StrId(u32); Pos { line: u32, col: u32 }
Ty = I64 | Bool | Str | Unit
Func { name: String, pos: Pos, params: u32, ret: Ty, locals: Vec<LocalDecl>, body: Block }
LocalDecl { name: Option<String>, ty: Ty } // None is a compiler temporary
Block { stmts: Vec<Stmt> }; Stmt { line: u32, kind: StmtKind }
StmtKind = Decl(LocalId) | Let { dst: LocalId, value: Rvalue }
| Set { dst: LocalId, value: Rvalue } | Eval(Rvalue)
| If { cond: Operand, then: Block, els: Block }
| Loop { id: LoopId, pos: Pos, body: Block } | Break(LoopId) | Continue(LoopId)
| Return(Operand) | Print { value: Option<Operand>, newline: bool }
Rvalue = Use(Operand) | Call { func: FuncId, args: Vec<Operand> }
| Arith { op: ArithOp, lhs: Operand, rhs: Operand, pos: Pos }
| Neg { operand: Operand, pos: Pos } | Cmp { op: CmpOp, lhs: Operand, rhs: Operand }
| Not(Operand)
Operand = Local(LocalId) | Const(Const); Const = I64(i64) | Bool(bool) | Str(StrId) | Unit
ArithOp = Add | Sub | Mul | Div | Rem; CmpOp = Eq | Ne | Lt | Le | Gt | Ge
Five invariants bind lowering and both backends:
- Operands are side-effect free; effects and traps occur only in
Call, checkedArith, checkedNegandPrint, in statement order. This is the entire left-to-right guarantee. Snapshot a direct name before later sibling operands only when a later operand may assign. - Each trapping rvalue carries the operator token's
Pos, computed withSourceFile::line_col(O(1) ASCII lines, Unicode-scalar columns). Both backends printProgram.file:line:col, withfileidentical tosrc.name. &&and||becomeDecl,SetandIf; there is no short-circuit rvalue.- Nothing after a diverging statement is lowered. Non-unit
functions cannot fall through; a falling-through unit function
ends with explicit
Return(Const::Unit)on its closing-brace line. - The IR is C-free. Backends select helpers from operand
Ty; unit remains integer zero in C, but has a real IR constant.
Lower while to
loop L { cond; if !cond { break L }; body }, without
implicit loop exit. Lower its condition before pushing its
source-loop target, so control in a condition targets an enclosing
loop. Print interpolation as one Print per segment,
then a final newline only on fallthrough, preserving output before
traps. Destination passing writes only at expression tails. An
always-on verifier checks declarations/scopes, operand types, call
arity, enclosing loop targets and return types.
The deterministic text starts ir 1 "file.hn".
Functions print
fn @0 fib(n.0: i64) -> i64 at 1:4 {; locals use
name.id or tN. Every statement has a
line: prefix, e.g.
5: let t2: i64 = sub n.0, 1 at 5:18. Calls print
call @0 fib(t2); loops
loop L0 at 3:5 { ... }. Strings are inline and
escaped, empty else is omitted, and positions appear
only where used. This is a versioned inspection format, not a
source language.
Alternatives considered
- SSA control-flow graph — premature complexity
and less readable C; try the
ssaexperiment from CP-3.1 before adopting it. - Expression trees — each backend would independently implement ordering and short-circuiting again.
- Reuse the span table — keeps accidental source identity and duplicated divergence analysis instead of explicit typed IDs.
Consequences
M3.2 adds lowering, text snapshots and verification; M3.3 replaces C emission and deletes the span table. M3.4 consumes the same IR in the interpreter. Goldens remain the lowering oracle because backend agreement alone cannot detect a shared lowering bug.
Extension points, without dead variants now: M4 adds
Ty::Record(LayoutId) and Union(LayoutId)
through Program.layouts, and rvalues
Construct, Field, Tag; M5
adds Alloc; Func gains effects. M6 moves
Print behind a platform call (0005). New
variants deliberately force exhaustive backend matches to
change.