Files
ziran/docs/ARCHITECTURE.md
T
waoziandClaude Opus 5.5 9ad5f481ef Name the tools by what they turn into what, and run verified ZX natively
The tools are renamed after their input and output: zi2zx lowers source
or ZIR to executable ZX, zx2native turns ZX into a native ELF, zx2wasm
into a wasm32 module, and zi2c, zi2go and zi2zir keep their roles under
cmd/ folders of the same names. zi2asm and the cmd/zir-* folders are
gone, and tests/tool_names.sh keeps the old names out. zx2native keeps
the direct encoder as its default; the machine pipeline replaces it once
it reaches parity.

The embedding API can run a verified bundle as x86-64 machine code in
memory. Code is written to RW pages, relocated, then switched to RX,
never RWX, and each instance gets its own data
(cmd/zir/zir_native_memory.c, zir_host_native.c). Backend selection is
BundleBackendPolicy: Engine and Native are strict, and Auto chooses by
platform only, never by whether one program fits. A native gap is a
named error, never a switch to the engine. Desktop x86-64 Auto stays on
the engine until the native backend is complete.

cmd/compiler_machine_image.zi adds the target-neutral machine image
contract. The machine verifiers reject unknown relocation kinds,
out-of-range symbols and addends, non-host external calls, native-width
mismatches and missing stack or fuel facts. The machine pipeline lowers
and encodes more of LIR. Docs, site pages, Kryon's build references and
the benchmarks use the new names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-14 22:07:42 -03:00

9.0 KiB

Ziran architecture

This is the agreed target architecture for the Ziran and Kryon repositories. It is not a claim that every part is implemented. Current gaps are tracked in Implementation status.

Repository ownership

Repository Owns Must not own
Ziran Language semantics, parser, checker, laws, standard library, .zir, native backends, .zx linker, verifier, loader, and generic execution runtime Widgets, UI trees, styling, DOM policy, renderers, Kryon-specific names or implicit UI startup
Kryon Importable .zi modules for widgets, layout, themes, interaction, accessibility, rendering, and platform adapters Ziran syntax, compiler branches for widgets, or an application's terminal or product UI

The dependency direction is Kryon → Ziran. A CLI, service, or library can use Ziran without Kryon. Importing Kryon is an explicit source dependency. Kryon's maintained implementation, including its widget and runtime logic, is 100% Ziran source. A platform adapter may call operating-system or third-party APIs through declared FFI or host capabilities; those external libraries are dependencies, not a second copy of Kryon's implementation. Generated C or Go is compiler output, not Kryon source.

Language boundary

Modules, records, functions, imports, and procedure values are general language features. Calls resolve against visible function signatures and record types. Their semantics do not depend on the callee's spelling. Button, Text, and Image mean nothing until imported or declared. Kryon's intended Image(ImageProps) surface is an ordinary library API, including when used for semantic images; its full rendering implementation is still being migrated. A record argument uses Function(Props.{value = 42}); result: Result = Function(Props.{value = 42}) binds a non-void return value.

Ziran has no #ui function modifier, app/route syntax, widget fallback, #style directive, DOM fields in its IR, or renderer-specific type checks. Kryon defines its own composition, styling, navigation, and lifecycle APIs in .zi. If a general language feature is needed for those APIs, it must work for unrelated libraries too and pass the same checker and backend tests.

Language laws constrain semantics, types, evaluation order, IR validity, and backend equivalence. Kryon owns its UI behavior and rendering laws. Language direction sets the additional requirements for law-driven development, LLM tooling, parallel execution, and evolution.

Compilation and execution

.zi modules ──parse/check──> .zir modules
.zir modules ──link/lower/verify/optimize──> portable .zx instructions
.zx ──verify/read──> portable interpreter ──> process
.zx ──verify/read/direct target lowering──> platform binary
.zir modules ──C/Go generation──> C or Go ──downstream toolchain──> platform binary

The direct-target branch is the native pipeline: Ziran lowers the verified ZX instructions directly to machine code, applies the target ABI and platform veneers, and writes the platform binary. It is an experimental, incomplete path, not a description of support on every target. Generated C and generated Go can also become binaries, but those are generated-code routes through their own compilers and linkers; they are not native-backend fallback paths.

This is implemented by the format-specific zi2zx and zx2native route: source-native output serializes and reads ZX first, the same .zx that ziran bundle writes and ziran run executes. zx2wasm lowers the same verified revision-28 payload to a wasm32 module (cmd/zx2wasm/zx_wasm_lower.c), so one .zx gives one .wasm; ziran build --target=wasm and the bundle Wasm tier (cmd/zir/zir_zx_wasm.c) use that lowering too. Native source maps currently lose source origins at the stripped ZX boundary, while source LIR inspection retains them.

The first implemented direct console veneer is the opt-in Linux x86-64 static ELF path. It owns _start, calls Linux write syscall 1 through a retrying write_all, and terminates with exit_group syscall 231. It uses no libc, CRT, or dynamic linker. Other OS/CPU service rows and floating-point print formatting remain named gaps rather than alternate-engine requests.

Inside the compiler, names in that model are interned: an expression's or statement's name and type, and a function's parameter list, are shared immutable strings (KeepName, KeepParameters), so equal spellings within a compilation context are one pointer. Parameter lists are split once into names and types (ParametersOf). Type and procedure resolution keep entry-scope answers, including misses and ambiguity, until declarations or imports change (SymbolLookupsChanged). Lookup file spellings remain part of each key; recursive import walks retain separate cycle state. make sanitize checks each kept answer against a fresh lookup.

Record bodies share immutable parsed field snapshots. Field lookup uses an index over that snapshot, and ownership checking can reuse the fact that every field is a built-in value or pointer. This shortcut never assumes facts about named types, arrays, or generic applications: those still resolve in their owning scope. Rewriting a body selects a new snapshot, so copied or relocated record declarations cannot retain stale field names or ownership facts.

Every backend consumes the same .zir model. The ZX linker produces one verified portable instruction program for the interpreter and direct target lowering. .zx cannot depend on arbitrary target-specific C or Go source fragments. C and Go exporters remain separate consumers of ZIR. Every supported route for the same program must agree on observable language behavior.

Host capabilities are portable typed requirements in the linked program. Platform bindings satisfy those requirements for one host. Direct target lowering separately owns CPU instructions, calling conventions, object and executable formats, and small ABI veneers for services such as process entry, standard-stream writes, and process exit. A platform binding never makes a missing instruction lowering or ABI veneer implemented, and a CPU backend does not silently turn an OS service into a portable capability.

Semantic checking sets ZirFunction.checked only from language validity. Native symbol planning runs afterward, and body eligibility is a separate emitter query. An emitter's inability to represent a valid function does not turn that function into unchecked source. The portable verifier remains the authority for executable .zx support.

Parameter declarations have one immutable parsed view, including binding names, types, namespace modifiers, and defaults. Record fields likewise use the shared field snapshots. Backends consume these views; source spellings remain available for diagnostics, template rewriting, and serialization.

Compilation contexts own interned text, source paths, symbol answers, declaration snapshots, expression caches, and reusable scratch buffers. Source and artifact readers create a context when the caller has not selected one. Programs and linked copies retain the contexts whose bytes they borrow; closing the original program therefore cannot invalidate a linked copy. Source spans keep their original context independently of the module's lookup context. Context pointers and cache identities are never serialized.

Public source parsing is a recoverable operation. Syntax and source-read errors return NULL after reporting the ordinary structured diagnostic. An operation-local allocation transaction releases partial trees, temporary buffers, and open streams after an error, and restores the enclosing parser state. Transactions nest across import callbacks and are local to each thread. Published immutable caches remain owned by their context. See Compiler embedding for lifetime and concurrency rules.

The linker resolves explicit imports, includes the reachable library code, and reports unresolved symbols or unavailable capabilities. A program with no Kryon import has no Kryon code or graphics requirement. A program importing Kryon links the Kryon modules it uses; graphics, input, audio, filesystem, and other host access must be declared as capabilities when needed. The host supplies those capabilities. A missing required capability is a reported error, never silently ignored.

The portable runtime launches a non-graphical .zx without initializing a window or display. Kryon decides how to use the host's graphical capabilities through its imported .zi implementation. The compiler and loader do not inspect widget names to make that decision.

Product boundary

Kryon is a reusable library. Applications keep their own product logic and UI in their repositories and import Kryon. Kapsule's terminal emulator, for example, belongs to Kapsule; Kryon only gains small reusable primitives when multiple applications need them. See Migration for the single breaking cutover and verification gates.