Kern
On this page

Kern compiler implementation

This is a native compiler written in Kern. It implements the language, all three targets, ownership, generics/interfaces, contracts, all quality rules, formatter, API/spec commands, direct executable writers, incremental isolated tests and mutation auditing. No Rust compiler, evaluator or embedded compiler binary is used at runtime. A pinned self-hosted CIP release binary (.cip/bootstrap.lock) provides the bootstrap compiler; the historical Rust stage0 has been removed.

The normal commands are check, asm, asm-test, build, run, test, fmt, rules, api and spec, with project-default paths, target selection and explicit --cc fallback. macOS tests share one validated in-memory image and fork independent children; Linux and --cc share one temporary executable. Successful tests use semantic local and recursive-call-group hashes. Declared inputs are frozen, undeclared reads trap, and audits execute compiled mutants.

Compiler-local policy

kern check compiler, kern fmt compiler --check and kern test compiler use the explicit compiler/kern.rules profile. Product defaults are unchanged. Unused declarations/imports/public exports, public test reachability, colocated tests, helper scope, duplicate bodies, bool parameters and formatting remain enabled. Internal-only exports and unused code are removed rather than exempted.

Profile settingReason
240 function lines, 100 branches, nesting 6Explicit complete AST/opcode visitors and ownership transfer cases
1000 file lines, 32 module files, 16 importsColocated compiler conformance tests and independently maintained phase files
20 record fields, 128 public module itemsFlat numeric-ID compiler tables and stable AST tag constants
512 characters per lineQuoted language conformance programs and assembly metadata
40 test lines, 100 tests per function, 30000 msShared parser/vector coverage and the full compiler self-check
magic_number = offInstruction masks, register fields, binary formats and ABI offsets

No ownership, module visibility, contract or test-admission safety checks can be disabled by this profile. Integration scripts additionally run the compiler against the language/CLI corpus, independent of its own source-test reachability.

Representation

Source names use byte offsets, not borrowed pointers into reallocatable buffers. store.Vector[Element] is used only with scalar or non-owning record elements. All owned buffers live in model.Program and have one explicit release path. Recursive ASTs use numeric node/list IDs, never recursive heap ownership.

Program
+- Sources: bytes, files, modules
+- Syntax: tokens, nodes, links, decoded literals
`- Symbols: types, definitions, functions, constants, items, imports, references

Node/list ID zero is absent. Type and symbol IDs are ordinary vector indices; model.NONE denotes an unresolved symbol. Names point into source bytes; decoded string/test names point into the literal buffer. Type applications are interned with a base type and linked explicit type arguments.

Parser/backend field contract:

Nodefirstsecondthirdlistextra
Integermagnitude/bitsunusedunusedunusedunused
Binaryleft noderight nodeoperatorunusedunused
Callstatic type parameter or zerotype argumentsmoduleargumentsfunction ID
Nameunusedunusedmoduleunusedresolved local/constant
Fieldbase nodeunusedunusedunusedword offset
Indexbase nodeindex nodeunusedunusedunused
Subslicebase nodelower nodeupper nodeunusedunused
Struct/varianttype IDunusedunusedfields/argumentsvariant index
Unaryoperand nodeunusedunusedunusedunused
Letinitializerunusedunusedunusedlocal ID
Assigndestinationvaluecompound operator or zerounusedunused
Ifconditionyes listno listunusedunused
Whileconditionunusedunusedbodyunused
Foriterablehidden pointer localhidden length localbodyloop local
Rangelowerupperhidden upper localbodyloop local
Return/assert/fail/discardexpression or zerounusedunusedunusedunused
Matchsubjectelse listunusedarmschecked match kind
Armbody listbinding listunusedpatternsunused
Patternliteral node or zerounusedunusednamesvariant index
Field entryinitializer or zerounusedunusedunusedword offset
Type parameterbound interface type or zerounusedunusedunusedunused
Contractpredicateunusedunusedunusedunused

name, token, ty and flags retain source identity, checked type and modes. Function metadata keeps source definitions distinct from concrete instances. The direct bootstrap regenerates this compiler from its own sources. Bootstrap, source-policy, language/CLI and runtime tests are separate verification gates.

Parser

The table-based parser handles declarations, ownership modes, explicit generic applications, static interface calls/mappings, contracts, tests/declared inputs, expressions, loops and enum/error/literal matches. Syntax recursion is bounded. Interface field lists contain function IDs; ordinary definition field lists contain node IDs. Implementations are item nodes whose first/second fields hold interface/concrete type IDs, and whose list contains method-to-name mappings. Integer nodes retain an unsigned magnitude and a negative flag until checking. Match nodes retain an explicit else flag, including an empty else body.

The typing phase resolves local/constant/function names, scopes, field layouts, explicit conversions, reference modes, result/error matches and contract scopes. Generic instances clone table nodes and cache their explicit type arguments. Interface methods are excluded from executable function checking. Instantiated definitions keep source identity separately from concrete layout identity.

Verification

sh tests/test_selfhost.sh build/kern checks front-end conformance and source tests. tests/test_selfhost_rules.sh exercises static quality-rule fixtures. tests/test_selfhost_backend.sh checks assembly fixed points with an external assembler. tests/test_bootstrap.sh checks the direct bootstrap: stage2, stage3 and stage4 must be byte-identical while cc, clang, as, ld and rustc are blocked after stage1. The self-built compiler then compiles its source tests, examples, wide ABI cases and exact runtime traps.

The explicit bootstrap PATH OUTPUT and bootstrap-test PATH OUTPUT inspector commands intentionally skip quality policy. Normal build and test do not. tokens, parse, types, types-test, and assemble-mac/arm/x64 remain diagnostic tools. tests/run.sh SELF_BUILT_COMPILER and all feature scripts verify the official CLI independently of those bootstrap entry points.

sh tests/test_selfhost_cli.sh build/kern-bootstrap performs normal, policy-enforced stage1/2/3 builds, checks stage2/3 byte identity with external tools blocked, runs its own source tests, then runs the full CLI and feature suites through stage3. It keeps build/kern-selfhost. sh tests/test_selfhost_cross.sh build/kern-bootstrap freezes the sources and repeats that proof in isolated Linux arm64 and x64 containers, keeping build/kern-selfhost-linux-arm64 and build/kern-selfhost-linux-x64.

The source backend prioritizes explicit, checked lowering over global register allocation. Semantic test cache hits skip generation and loading, but still hash the compiler executable; that fingerprint can dominate very small cached runs.

The effect analysis propagates I/O, untracked-state and non-read-only effects through recursive call groups. Pure tests, declared-input tests and production callers obey their separate admission rules. Contracts retain their source clauses and lower to precondition/postcondition checks, with one hidden result initializer on successful returns; failure returns bypass postconditions.

Source: compiler/README.md in the repository.