HIR linearises evaluation order, records link facts and becomes the first backend-facing semantic IR.

Lowering meaning into explicit control flow

Nested expressions are friendly to humans and slightly rude to machines. Evaluation order hides in tree shape. Early exits hide in nesting. A borrow checker that must rediscover all of that from sugar will rediscover it wrong.

HIR (a high-level intermediate representation in Moth's pipeline) lowers fully typed AST into the first backend-facing semantic IR. Meaning stays. Shape changes. Nested source sugar becomes an explicit branching structure analyses can walk.

Lowering without changing meaning

An intermediate representation is a program form meant for the compiler's internal work rather than the author's eyes.

Lowering transforms one representation into another while preserving the semantic contract the language already fixed.

Compare a nested call in AST spirit:

return greet(visitor)

with ordered runtime work in HIR spirit:

tmp0 = load visitor
tmp1 = call greet(tmp0)
return tmp1

The second form makes evaluation order and temporary storage obvious. Backends and analyses stop arguing about tree walk conventions.

HIR building blocks

Teaching vocabulary:

Expression side effects linearise into statement preludes. The final value then feeds the next operation or terminator. Plain binary operations may remain for booleans and comparisons. Runtime template string construction lowers through explicit string append operations. Runtime scalar arithmetic lowers through explicit checked numeric statements.

Numeric statements carry canonical exact operand/result domains, the operator and the actual failure mode. They consume completed AST facts, never pending numeric origins, receiving constraints or inference recipes. Equal fixed types preserve their eligible arithmetic width or precision. I8 + I8 checks at I8 and each F16 operation rounds and checks finiteness before the next operation consumes its result, regardless of scratch carrier width.

Builtin runtime casts carry explicit source and target types. Source-authored cast evidence has already become an ordinary direct call. A separate NumericProofs table can justify predicate elision, but changes neither canonical types nor failure delivery and cannot grant target capabilities.

Runtime template work

Neutral template handoff from AST becomes ordinary string and control-flow operations in HIR:

Compile-time entry fragments and TIR itself do not enter HIR. Folded fragment metadata lives in module metadata lanes. HIR validation checks executable fragment operations only.

The greeting's runtime pieces, if any remain dynamic, look like ordinary operations over strings and calls. Static compile-time fragment text stays metadata until the HTML builder inserts it.

Call targets and link facts

Source calls use explicit target classes:

Generated concrete functions add another stable target class once materialised.

HIR stores no dependency aliases as semantic authority and no backend runtime names as identity. Beside HIR, the compiler records per-function link facts: call edges, helper and capability needs, numeric and cast operation presence, resource uses, project-context provenance and generated-function requests. Statement-level numeric safety belongs to the separate NumericProofs side table paired with executable HIR.

Those facts are the linking authority later stages consume.

Later stages read the facts on the right without touching the control flow on the left. Analysis writes side tables; it never rewrites the IR to taste.

A backend learns its dependencies by reading those facts, not by guessing callees from names in source.

Validation boundary

HIR validation completes before borrow validation or target validation. It checks definition identities, type links, CFG shape, terminators, local and place references, start-function metadata and related invariants.

NaN and infinity in HIR are internal invariant failures, not casual user values.

A backend-neutral structured HIR view may be derived when a structured lowerer needs it. It is not a second semantic authority. Cache it only as derived data.

Borrow validation will read validated HIR and write side tables. It will not rewrite the IR to taste. Numeric proof analysis likewise reads validated HIR and writes statement-keyed facts before publication without rewriting the IR.

What can fail here

Keep internal failures separate from source diagnostics. Authors did not "misuse HIR." The compiler misfired.

Why Moth does it this way

Alternative: lower AST directly into JavaScript or Wasm per backend.

Choice: backend-neutral HIR with explicit control flow and side facts.

Benefit: analyses and targets share one validated runtime representation. A borrow bug fixed once stays fixed for every backend.

Cost: lowering must materialise temporaries and preserve more explicit state.

Roadmap and current status

Exit state

You leave with validated HIR and per-function link facts. Next: what memory guarantees analysis can derive from that control flow, and why GC does not replace borrow checking.

Compiler design / Previous / Next