kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

WebAssembly (planned work)

kit treats WebAssembly as both an input language and an output target, sharing one binary/module layer between the two directions. The frontend path (Wasm in, native object/JIT out) and the minimal final-module backend (C/toy in, single-TU .wasm out) are working baselines. The remaining work is concentrated in three areas: completing the obj/wasm object backend so it can read and write tool-conventions relocatable objects (today it only mirrors raw section bytes); building the Wasm static linker; and closing the last frontend/backend feature gaps (relocations for cross-TU references, atomics sub-word coverage, the C-facing exported wrapper ABI, and the unsupported-proposal diagnostics). This doc is the forward-looking plan. Design rationale, the shared module model, and the API sketch live in ../WASM.md; related design docs are ../OBJ.md and ../LINK.md, and the sibling plan is LINKER.md.

Baseline already in place (do not re-plan): src/wasm core decode/validate/ encode/wat; lang/wasm frontend with native lowering through KitCg and an explicit KitWasmInstance* ABI; src/arch/wasm with arch_impl_wasm, a wasm32 BasicCABI vtable, a structured-CG + CFG-structurer backend, and a single-TU emit_wasm; host-import binding via kit_wasm_set_host_imports. read_wasm/emit_wasm are real (no longer stubbed) but partial.

Object backend (largest gap)

src/obj/wasm is far smaller than the ELF/Mach-O/COFF backends. read.c mirrors each Wasm section into an ObjBuilder section carrying raw payload bytes and synthesizes one function symbol per defined function (enough for objdump -h/-s/-d/-t). emit.c flushes a WasmModule attached under OBJ_EXT_WASM via wasm_encode, or an empty magic+version header. Neither understands tool-conventions object metadata: there is no symbol-table decode, no relocation decode/encode, no linking/reloc.* custom-section handling, and no WasmObjMeta.

Work items:

Static linker

There is no Wasm linker yet. kit_link_exe always builds a native Linker and emits a native LinkImage; a Wasm final module is not a virtual-addressed native image and must not go through that segment layout.

Work items:

This is the path that unblocks multi-TU Wasm output. The single-TU final module the backend produces today needs no relocations; everything cross-TU depends on the object backend and linker above.

Frontend validator coverage

The shared validator (src/wasm/validate.c) and WAT/binary readers already cover the accepted feature subset with typed operand/control stacks, section ordering and index-space checks, branch arity, br_table, limits, segment rules, and start-function signatures, plus the staged proposal gates (threads, typed function refs, tail calls, multi-memory, memory64, bulk memory, non-trapping float-to-int) behind WasmFeatureSet.

Remaining work item:

Frontend lowering gaps

Native lowering covers the MVP numeric/control/memory subset plus the staged proposals (threads, typed refs, tail calls, multi-memory, memory64, bulk memory), all routed through the explicit KitWasmInstance* ABI with import slots and runtime table storage.

Remaining work items:

Backend feature gaps

The wasm32-none backend emits valid single-TU final modules: scalar + indirect-aggregate BasicCABI, structured control flow with a reducible-CFG structurer and br_table switches, linear memory + __stack_pointer, compact data layout with intra-module R_ABS32 relocations, varargs via a caller-packed linear-memory buffer, the bit/overflow intrinsics, atomics via wasm-threads opcodes, memory.copy/memory.fill lowering, inline asm with WAT templates, conventional "memory" export, and (import "env" ...) declarations with import_module/import_name attribute overrides.

Remaining work items, mostly blocked on the object/linker layer or on wider ABI support:

Wasm-to-Wasm

A Toy/C -> Wasm -> run roundtrip exists via make test-wasm-toy/test-wasm-c, but it routes the backend's output back through the lang/wasm frontend's native JIT (Wasm-to-native), not a direct Wasm-to-Wasm path.

Work items:

wasm64 and WASI

Both stay recognized-but-unsupported until the freestanding wasm32 object/link path is solid:

Cleanup

Test targets

Present and green: test-wasm-front, test-wasm-target, test-wasm-toy, test-wasm-c (aggregated under test-wasm). Still to add as the object and link layers land: make test-wasm-obj and make test-wasm-link. Prefer small named fixtures over broad corpus runs; keep external validators (wasm-tools, WABT) as optional comparison oracles, never a hard dependency — kit's own validator is the semantic gate. See TESTING.md.