""" build_kernel.py — Precompile the canonical FORTH kernel to a binary + address map. Builds the full kernel ONCE at base=0 (the device base) and writes: build/kernel.bin — the raw kernel image (offset 0 → NEXT, the bytes that ship in the cave at 0x004690, modulo the base shift; tests load THIS and run the exact device bytes) build/kernel.map.json — the address map (every word's absolute addr, NEXT, QUIT_cfa, INTERPRET_cfa, HALT_spin, sizes, SRAM layout) Tests load the binary instead of rebuilding. clang (the slow ~25ms/subprocess assembler) becomes a BUILD-TIME concern, never a TEST-TIME concern. A staleness guard (test_precompiled_kernel.py) fails loud if the binary doesn't match a fresh build, telling you to `make kernel.bin`. This is the source of truth for the precompiled artifact. The Makefile wraps it; `uv run python -m src.build_kernel` works directly. """ import json import os from src.kernel_builder import KernelBuilder BUILD_DIR = "build" BIN_PATH = os.path.join(BUILD_DIR, "kernel.bin") MAP_PATH = os.path.join(BUILD_DIR, "kernel.map.json") def build(base=0): """Build the canonical kernel at `base` (default 0 = device base). Returns (image_bytes, addr_map_dict, kernel_builder). The builder is returned so callers (firmware_patcher, size_budget) can read kb.words/quit_cfa/etc.""" kb = KernelBuilder(base=base) kb.build_all_primitives() kb.build_layer1_primitives() kb.add_halt() kb.build_interpret() kb.build_quit() kb.build_dot() # Lay an entry for EVERY code word so any primitive can be threaded by tests # (build_interpret/build_quit only lay the ~15 words they reference). This # mirrors what test_kernel_primitive_smoke / test_kernel_io_smoke do at the # top of their _build() — idempotent on already-laid names. Without it, the # precompiled map's `entries` would miss `+`/`DROP`/etc. that the smoke tests # look up, and the loader couldn't substitute for a live build. for name in kb.words: if name in ("NEXT", "HALT"): continue if name not in kb.entries: kb.lay_entry(name) image = bytes(kb.code[:kb.next_addr]) addr_map = { "base": base, "size": kb.next_addr, "cave_budget": 6512, "next_abs": kb.words["NEXT"][0], "interpret_cfa": kb.interpret_cfa, "quit_cfa": kb.quit_cfa, "halt_spin": kb.halt_spin, "halt_cfa": kb.halt_cfa, "docol_code": kb.words["DOCOL"][0] | 1, "exit_code": kb.words["EXIT"][0] | 1, # Every word's absolute code address + body size. "words": {n: {"addr": addr, "size": len(body)} for n, (addr, body) in kb.words.items()}, # Colon-word cfa addresses. "colon_addrs": dict(kb.colon_addrs), "entries": dict(kb.entries), # Canonical ASM source for every code word (the string passed to # add_code_word / add_next, pre-`b NEXT`). Tests import this instead of # re-transcribing bodies — single source of truth. See KernelBuilder.sources. "sources": dict(kb.sources), } return image, addr_map, kb def write(image, addr_map): """Write the binary + map to build/. Creates the dir if needed.""" os.makedirs(BUILD_DIR, exist_ok=True) with open(BIN_PATH, "wb") as f: f.write(image) with open(MAP_PATH, "w") as f: json.dump(addr_map, f, indent=2, sort_keys=True) return BIN_PATH, MAP_PATH if __name__ == "__main__": image, addr_map, kb = build(base=0) bin_path, map_path = write(image, addr_map) print(f"kernel.bin : {len(image)}B / {addr_map['cave_budget']}B " f"({len(image)*100//addr_map['cave_budget']}%)") print(f"NEXT : 0x{addr_map['next_abs']:06x}") print(f"INTERPRET : 0x{addr_map['interpret_cfa']:06x}") print(f"QUIT : 0x{addr_map['quit_cfa']:06x}") print(f"HALT spin : 0x{addr_map['halt_spin']:06x}") print(f"wrote {bin_path} + {map_path}")