Architecture
source.jh ─► lex ─► parse ─► load ─► sema ─► MIR ─► elab ─► borrow checks ─► effect attributes ─► llvm ─► LLVM C API ─► .o ─► linker ─► program
The compiler writes LLVM IR as text in memory. backend loads LLVM 23’s
shared library, verifies and optimizes the module with the new pass manager,
then emits an object through the target machine C API. A large executable’s
module, once optimized whole, is emitted in up to eight units on separate
threads, each reading the optimized bitcode into its own LLVM context and
keeping its run of functions (Command line). Native linking uses
a C driver for libc startup files; freestanding linking calls ld.lld
directly, and UEFI linking its COFF linker (-flavor link). The compiler needs no LLVM headers or C++ shim to build.
JIHOO_BACKEND=clang retains the previous pipeline for comparisons.
The compiler is written in jihoo, in compiler/, and a released build of
it bootstraps the next (Self-hosting).
| file | what |
|---|---|
lex.jh, parse.jh, ast.jh |
tokens and the syntax tree; newlines end statements |
load.jh |
finds and parses every imported module, picking per-target and per-profile files |
types.jh |
interned types and their memory layout |
sema.jh, body.jh, pat.jh, builtins.jh |
names, types, generic instances, and lowering to MIR |
mir.jh |
the intermediate representation |
elab.jh |
initialization: use after move, drop elaboration and drop flags |
check.jh |
the borrow kernel: exclusivity and the lifetimes of views |
effects.jh |
structural checks for #[no_alloc] and #[no_panic] |
extent.jh |
exact shape and coordinate checks for certified slice access |
llvm.jh |
MIR to LLVM IR text |
backend.jh |
LLVM 23 C API: verification, optimization and object emission |
format.jh |
jihoo fmt: layout from tokens and parser list boundaries |
lsp.jh, json.jh |
jihoo lsp: the language server and the JSON it reads and writes |
main.jh |
the jihoo command |
Sema
Sema checks and lowers in one pass per function instance. A generic function is checked per instance, with its compile-time arguments known, as in Zig: an instance is a declaration and its arguments, and calls make instances, which a queue lowers one by one. Every non-generic function of the program is lowered too, so it is checked even if nothing calls it.
Names are looked up in local variables, then compile-time parameters, then the module, then the prelude’s modules, then builtins. Methods are looked up in the type’s impls, then in impls of traits; impl heads are matched against types to bind their parameters.
Sema decides how each value is used: a place in a value position moves (or
copies, if plain); a place passed to a borrowed parameter is borrowed; an
inout argument is borrowed mutably. It inserts a Drop for every owned
local at the end of its scope, and at return and break.
The jihoo lowerer also tracks statically known closure functions by MIR place, including immutable aliases and captures inside lifted functions. Specialization selects a function while passing its ordinary environment value; borrow checking still sees the environment and its captures. A callback with known inputs and an unknown generic result is first checked with those input types, then its full signature participates in generic argument inference. The same inference applies to comptime callbacks.
MIR
A function is a control-flow graph over places: a local followed by fields, variant payloads, array indices and dereferences. Statements assign rvalues to places, mark locals live and dead, drop places, and forget locals whose parts were taken. Terminators jump, switch, call, assert and return. Every move, copy, borrow and drop is explicit, which is what the two checking passes read.
jihoo emit-mir file.jh prints it.
Compile-time interpreter
compiler/interp.jh executes MIR for const initializers, comptime
expressions, array lengths and compile-time arguments. Simple arithmetic
still uses sema’s constant folder. Other expressions use the normal lowerer
with a compile-time environment and no runtime locals. Reachable function
instances are lowered on demand, without draining the enclosing function’s
pending queue.
Before execution, snapshots of the expression and reachable functions pass
elaboration and the borrow kernel. The runtime compilation pipeline retains
its original bodies. The interpreter follows basic blocks and call frames,
with bounded execution, recursion and allocation. Its memory is separate
from the compiler’s address space: pointers encode allocation identities
and offsets, with checks for lifetime, bounds, initialization and pointer
bytes. Layout and enum payload offsets come from the target’s Types.
Scalar results become MIR constants, strings become static strings, and
aggregates without addresses become Const.Bytes.
Elaboration
elab.jh follows which locals hold a value at every point. Using a local
that may have been moved is an error; a drop of a moved local is removed;
a local moved on some paths only gets a flag, and its drop checks it.
The borrow kernel
check.jh does not trust sema. It finds every borrow in the MIR (a loan of
a place, held by a local), follows loans through views (copies, aggregates,
calls that return views, inout arguments that are views), and computes
which locals are used later. A loan lasts while some local holding it is
used later. Then it checks every access against the loans that last there:
- a place borrowed mutably may not be used through anything else;
- a place borrowed may not be changed, moved or dropped;
- a returned view may only borrow what the parameters point to.
Before checking calls, the kernel derives a relation between argument
origins, returned views and stores into mutable/interior view arguments.
It propagates origins through MIR with a fixed point over function bodies,
including recursive call cycles: when a function’s relation grows, only its
direct callers are inferred again, and inside a body only blocks whose
incoming loans changed are revisited. The loans of all locals at a point are
one flat bit vector, so joining and copying block states does not allocate
per local. Call sites use the resulting relations
instead of assuming every call stores every input borrow. These relations
are computed by the borrow kernel, not supplied by sema. Calls without a
known body keep the conservative rule. Raw reads that can manufacture views
retain all result origins; raw writes and calls exposing raw pointers retain
all possible store origins, except that a closure argument is not stored
where no closure type can be (a comparison given to sort_by on a
Vec(str)): its loans reach a place only as the closure itself. Each
argument has a second origin, what the place it borrows holds: a raw read
through a borrow of a #[view] type gives that origin, not the argument’s
own, and so does a result read through such a call (an iterator adapter’s
item). A call site turns it into the argument’s loans without the borrows
of the places it points into, so an item collected from a vector’s
iterator borrows the vector, not the iterator. A pointer local that only
holds a view’s data pointer, an offset of it or a borrow cast to a
pointer carries those loans, so the view made from it (s[a..b], an
element of a slice, an array made a slice) borrows what they borrowed. An
element read out of a view carries what the elements hold: inferring, the
inner origins of the arguments the view came from; checking, the view’s
loans without the borrows of the locals it is made from (backing); a view made from
another view’s pointer that the function was given, or read through a
borrowed argument, borrows that view’s own or inner origin, and a raw read
of an integer, float or bool is no way to a borrow. Compile-time snapshots compute their own effects
from the reachable, prepared bodies before execution.
The idea comes from proof checkers such as Horn: a large untrusted part (sema) produces something a small part checks in full, so a bug in the large part becomes a compile error and not a dangling pointer.
Effect attributes
After elaboration and borrow checking succeed, check.program runs
effects.check once. For each effect with a marked function instance, the
checker finds direct causes such as allocations, assertions and unknown
calls. It propagates them through direct calls and drops until no more
functions gain the effect. This covers check, code generation and LSP.
JIHOO_NO_ELAB bypasses these checks only for raw emit-mir dumps.
Certified slice accesses are checked separately during lowering. The
extent checker validates the bounds of these
accesses before MIR is emitted, so they introduce no bounds assertion for
#[no_panic] to reject.
Code generation
llvm.jh is simple on purpose:
- every local is an
alloca;mem2regand SROA make registers of them; - fields are byte offsets, computed once in
types.jh, which also lays out enums (a tag, then each variant’s fields); - aggregates are copied with
memcpy, passed by pointer and returned throughsret; - borrowed parameters are pointers with
noaliasanddereferenceable(readonlyfor shared borrows), which the borrow rules make true; - overflow checks use LLVM’s
*.with.overflowintrinsics, panics are cold andnoreturn; - destructors are glue functions per type: the
Dropimpl, then the parts.
The C main stores argc and argv and calls the program’s main; a
freestanding program gets a _start in module-level assembly, and
definitions of memcpy, memmove, memset and memcmp. A UEFI program
(triple x86_64-unknown-windows or aarch64-unknown-windows, so a COFF
object with the Windows calling convention) gets efi_main, the same
memory functions, __chkstk in module-level assembly, and a jihoo.panic
and traps that write to the firmware’s console and end the image through
the boot services’ Exit. cabi.jh classifies C arguments for System V,
AAPCS64 and Win64.
Written to be ported
The compiler was first written in Rust and ported to jihoo line by line,
and it keeps the rules that made that possible: data refers to other data
by index (TyId, Local, BlockId, FnId), references are only
parameters (except the syntax tree, which sema reads through raw pointers),
and the code uses enums, match, Vec, Map and String.