Kern
On this page

M0 S0-S2 hosted integer model

Branch: m0-s0-types. Baseline: 42427ba. Local commit only; no push or release.

Delivered

Both compilers accept nominal usize, isize, i32, and u32 in declarations, literals, conversions, arithmetic, comparisons, matches, generic instantiations, API output, and canonical formatting. Type spellings remain ordinary lexer identifiers, so no new keyword tokens are needed. usize has the hosted u64 representation but is a distinct type.

len, arg_count, indexes, subslice bounds, allocation counts, arena capacities, and truncate lengths use usize. The compiler's table indexes, offsets and hosted word accumulators and the affected examples/tests were migrated. Fixed OS builtin signatures remain intact, with explicit conversions at compiler boundaries.

Hosted profiles carry word_bytes=8, stack_align=16, and arg_words=6. They feed address shifting, allocation, element stride, scalar/composite words and frame rounding. Struct offsets remain word indexes. The self-hosted driver selects the profile before parsing/checking; backend frame/offset arithmetic uses that profile.

Arithmetic on the new fixed-width types uses canonical hosted 64-bit intermediates and checked 32-bit result bounds. This keeps one 8-byte slot per scalar and avoids truncation: i32 is sign-extended, u32 zero-extended. Casts reject values outside the destination range. Arithmetic overflow (including i32::MIN / -1, remainder and negation), division by zero, and width-specific shift counts have runtime fixtures. Bit operations retain matching unsigned operand types and mask discarded bits to that width. New casts are admitted in read-only contract predicates.

Count ranges default to usize. Explicit integer bounds retain their nominal type, including signed ranges; comparison signedness follows the operand type. This preserves existing negative ranges in core.kn and enums.kn and their frozen assembly. Requiring every range to be unsigned would contradict the brief's unchanged-semantics and byte-identical-output requirements.

Hosted layout table

Each target column is words / storage bytes. All stored types align to 8 bytes. void has no storage. u8 and bool slice elements have stride 1; their locals and fields still occupy a whole slot. Other slice element strides equal storage bytes.

Typemacos-arm64linux-arm64linux-x64
void0 / 00 / 00 / 0
i641 / 81 / 81 / 8
u641 / 81 / 81 / 8
i321 / 81 / 81 / 8
u321 / 81 / 81 / 8
usize1 / 81 / 81 / 8
isize1 / 81 / 81 / 8
u81 / 81 / 81 / 8
bool1 / 81 / 81 / 8
internal pointer1 / 81 / 81 / 8
any slice, including nested slices2 / 162 / 162 / 16
Arena3 / 243 / 243 / 24
Handle[T]2 / 162 / 162 / 16
structsum of field words / 8 × wordssamesame
enum(1 + largest payload words) / 8 × wordssamesame

The 60-row executable fixture table is tests/layout/hosted.tsv. It additionally pins Pair {u32,u32} to 2 words/16 bytes, Mixed {u8,bool,u32,[]u64} to 5/40, eight scalar fields to 8/64, and an enum with a slice payload to 3/24. The Rust unit test parses tests/layout/hosted.kn and checks the actual generation helpers. Self-hosted frontend/profile tests check its independent records and prevent fake reserved-kind entries from being interned as one-slot Arena/Handle types.

Golden proof

Before implementation, both compilers were built from 42427ba. Six original sources were frozen on all three hosted targets: core, enums, sieve, slices, wc, and the calc project. The two sources without main use asm-test; the other four use asm. tests/golden/fixtures.tsv records that choice.

All 36/36 raw assembly hashes match, without normalization or regenerated expected values. Each compiler has 18 independently frozen hashes:

ManifestSHA-256 of the manifest
tests/golden/hosted.sha256bd02efe8e95ece2e5c043065d95c60fc43c1746034b626ec9bff04f8325d7359
tests/golden/stage0.sha25652559ad1af896e99fe0cda96954a1c0f8a61ca875c3932c1f4a76a6619756a06

Run sh tests/golden.sh build/kern hosted and sh tests/golden.sh build/kern-stage0 stage0. The existing CIP stage0 and feature jobs now enforce these checks. SPEC.md and self-hosted kern spec are identical; .cip/gen-spec.py regenerates the printer reproducibly.

Validation

CheckResult
stage0 buildPASS
Rust unit tests, including layout table60 passed, 1 pre-existing ignored
stage0 builds self-hosted compilerPASS
self-hosted check compiler and fmt compiler --checkPASS
host language harness331/331
feature suitesall 10 passed: runner 29, audit 25, structure 13, ownership 19, generics 19, contracts 17, writers 18, runtime 13, system 18, safe OS 6
full self-host proof on final sourcePASS: 85 source tests, byte-identical stage2/3/4, blocked external tools, harness 331/331, all feature suites, policy 144/144
differential optimizer corpus20/20
frozen hosted assembly36/36
extra Linux arm64/x64 width and trap executions96/96, both compilers; four runtime tests and 23 exact trap fixtures per compiler/architecture

The verified fixed-point executable SHA-256 is d9f6350797ba50582d27e637b9fa5eb7a394be2574dcbbef7c828c79a787a177. The preserved build/kern-selfhost (stage3) and compiler/build/compiler (stage4) are byte-identical; the proof script also compared stage2. Full local logs are in build/m0-s0-evidence/ (generated, excluded from git).

The full bootstrap proof verifies stage2 = stage3 = stage4 byte-for-byte, external compiler/assembler/linker tools blocked, all 85 compiler source tests, the language harness, all feature suites, and the 144-case self-host policy suite. Host-only acceptance does not claim a full Linux regression run; the extra Linux run is limited to the new fixed-width runtime and trap fixtures.

Files changed

M0-S0-CHANGES.txt is the complete file inventory. The substantive changes are:

  • Rust: ast.rs, parse.rs, check.rs, contracts.rs, gen.rs, gen/expr.rs, gen/stmt.rs, main.rs, profile.rs, test_hash.rs; embedded source-test migrations in generic.rs, ownership.rs, and contracts.rs.
  • Kern: model kind/type records, parser, typing, builtin IDs, integer helpers, profile module, backend arithmetic/casts/layout, contract effect admission, API output and spec printer; mechanical count/index annotations throughout the hosted compiler.
  • Fixtures: nine nominal/literal error cases, 23 traps, four runtime tests, an API project, the hosted layout table, and the frozen golden table/manifests/checker.
  • Existing examples, optimizer/ownership/index fixtures and hosted feature-script source snippets needed by the nominal usize migration.
  • SPEC.md, the spec-printer generator, and golden checks in existing CI jobs; job selection includes the new fixture dependencies.

The pre-existing untracked M0-S0-BRIEF.md is excluded from the commit. CONCURRENCY.md and all MCU runtime scripts are untouched. No new target, 4-byte storage, capabilities, kern.target, or 32-bit ABI was introduced.

Exact work remaining for S3 and the 32-bit ABI

The design calls S3 the layout/profile step; the machine ABI implementation is S5/S6. This lane leaves those boundaries explicit:

  1. Add non-host profile selection, thread it through all frontends (including test/API/check paths), and make integer literal widths/default integer type profile-dependent. Current usize/isize width and default i64 remain hosted 64-bit; the profile constructors expose only the existing three targets.
  2. Implement 4-byte slots and words(i64/u64)=2; recompute struct/enum offsets, slice (ptr32,len32), Arena/Handle storage, element strides and alignments. Current scalar_words and primitive records deliberately remain one hosted word. Audit address/load/store scaling and Arena alignment masks for 4-byte words; hosted machine prologues and runtime assembly still encode the 64-bit ABI.
  3. Split both drivers so check and a new kern layout command do not call code generation. Add the per-type slots/bytes/alignment CLI and non-host layout/literal fixtures before depending on a 32-bit emitter. Move the six-word argument check out of codegen into the checker using profile data.
  4. Implement AAPCS argument placement in r0-r3 plus stack, still bounded to six source argument words. Reserve r0 for aggregate sret; use r0:r1 for results, r2 for the fallible flag, preserve r4-r11, and align frames to 8 bytes. Define stack argument/result storage and the caller/callee paths in both backends.
  5. Implement canonical 32-bit loads/stores and arithmetic, checked word conversions and 32-bit shift limits. Implement i64/u64 as low-word-first pairs in locals, fields, slices, arguments, returns and errors; provide two-word arithmetic, comparisons and division/multiplication helpers. No such two-word path exists here.
  6. Add Thumb/RV32 emission and target dispatch, freestanding reset/vector/data/BSS startup, linker scripts, the board trap hook, and either the planned clang link path or later ELF32/native encoders. Validate call-convention fixtures, ELF class and machine, and arithmetic/traps under QEMU; assembly hashes on hosts cannot establish those properties.
  7. Keep capability/profile-file work separate (S4), including wide policy and E170-E175. Add target identity to test-cache versioning before admitting any non-host test run. Nothing in this commit grants or diagnoses MCU capabilities.

Source: M0-S0-RESULTS.md in the repository.