Skip to content
Maksym Skorina
← All notes
2 min read

S6c: the compiler emits its own closure

Every function of every loaded module is now emitted as LLVM IR. A stage1 link is still ahead, and the compiler fails loudly until then.

self-hosting · compiler · LLVM

Self-hosting is not a switch. It is a sequence of stages that each remove one reason the compiler cannot compile itself. Stage S6c is the first one where the emitter produces the whole closure.

What S6c does

Every function of every loaded module becomes a define i64 @owner$name, private functions included, in the module’s source order and before the entry region. The module literals are numbered first. On the two-file corpus, the emitted module matches kshanex --emit-ir byte for byte.

That sounds narrow. It is the difference between emitting an entry point that calls into a declared runtime and emitting a program that contains every function it calls. The closure is what a stage1 link needs.

The shape

The target is not exotic. It is the same shape the compiler already emits for user code:

; The shape S6c emits, not a dump of the current binary.
define i64 @arith$add(i64 %a, i64 %b) {
entry:
  %sum = add i64 %a, %b
  ret i64 %sum
}

The hard part is everything around it. The merged front end parses four modules, loads the import closure, resolves qualified module calls (mod.func(...) → @mod$func), and emits the ; ModuleID header, the escaped literal globals, the runtime declarations, and the C @main trampoline around the user region. clang -c accepts the self-built .ll. The corpus driver links its object. The merged front end emits its own entry module.

The remaining fixpoint needs the emitter to lower the whole closure: 108 functions, 95 while, 566 if, 17 match, 77 string locals, lists, structs/enums, 9 library methods, and 5 builtins. Those are not decoration. They are the language features the compiler uses to describe itself.

Why it matters

A compiler that can describe its own front end is a compiler whose type system, string representation, module system, and ABI have been forced through a real program. A self-hosting milestone is a stress test, not a stunt.

The compiler is written in Rust from scratch: 70,316 lines, 3,265 tests, its own VS Code language server, and no external crates. The language it compiles has effect rows, a direct LLVM IR backend, and a 10,372-line standard library written in .knx. The self-hosting path is how those pieces meet.

A stage1 link is the next milestone. Until then, the correct failure mode is the current one: stop at the first shape that is not lowered, with a loud error, instead of producing a partial binary that looks like progress.

The code and the tests are the source of truth. The version number is frozen on purpose.

—views2 min read

Comments

Loading…
0/1200

No comments yet. Add the first note.