- docs/roadmap/ -> docs/plan/ (implementation-plan.md becomes plan/README.md) - docs/reference/design-decisions.md -> docs/design/decisions.md (+ DD-008..010) - SPEC.md and docs/spec/INDEX.md merged into docs/spec/README.md - new docs/design/open-questions.md: spec gaps found so far (Q-001..Q-018) - new docs/dev/architecture.md and docs/dev/testing.md - docs/README.md: layout, precedence, where new documents go - update links in README, AGENTS, CONTRIBUTING, sources and test docs
102 lines
5.1 KiB
Markdown
102 lines
5.1 KiB
Markdown
# Reference Implementation Architecture
|
|
|
|
Non-normative. Describes how the Zig reference implementation is organised. The spec defines behavior; this file defines where code goes.
|
|
|
|
## Pipeline
|
|
|
|
```text
|
|
.aji source text AIR (.air)
|
|
│ │
|
|
frontend: lex → parse → lower (P6) │
|
|
│ │
|
|
└──────────────► AIR module ◄────────┘
|
|
│ air: parse / print / (binary, later)
|
|
▼
|
|
verifier passes in spec 08 order
|
|
│
|
|
┌───────────────┼───────────────┐
|
|
▼ ▼ ▼
|
|
interpreter audit reports LLVM backend (P7)
|
|
(+ det. scheduler, (unsafe, effects,
|
|
trace, replay) capabilities)
|
|
```
|
|
|
|
Nothing executes or lowers unverified safe AIR (spec 07 §7). `check.zig` is the single front door used by the CLI, the conformance runner and later `ajid`.
|
|
|
|
## Source layout
|
|
|
|
Current files are marked ✓; the rest is the planned layout. Split a file into a directory when it passes roughly 1 000 lines or gains a second responsibility.
|
|
|
|
```text
|
|
src/
|
|
root.zig ✓ library root, re-exports
|
|
main.zig ✓ `aji` CLI
|
|
check.zig ✓ parse → verify front door
|
|
diag.zig ✓ diagnostic envelope, error-code registry
|
|
air.zig ✓ (stub) → air/
|
|
air/
|
|
types.zig type table, primitive and composite types
|
|
module.zig module, function, block, instruction, value ids
|
|
ops.zig opcode enum + per-opcode metadata (from contract tables)
|
|
parse.zig text AIR lexer and parser
|
|
print.zig canonical text serializer
|
|
binary.zig binary AIR (after opcode freeze, RFC 0004)
|
|
verifier.zig ✓ (stub) → verifier/
|
|
verifier/
|
|
cfg.zig ssa.zig types.zig init.zig
|
|
ownership.zig borrow.zig region.zig island.zig
|
|
resource.zig effects.zig caps.zig
|
|
send_share.zig tasks.zig atomics.zig
|
|
bounds.zig unsafe.zig
|
|
budget.zig deterministic resource budget (E5101)
|
|
interp.zig ✓ (stub) → interp/
|
|
interp/
|
|
machine.zig values, frames, instruction dispatch
|
|
memory.zig heap, regions, islands, provenance, generations
|
|
sched.zig deterministic scheduler, seeds, choice recording
|
|
weak.zig weak-memory model for atomics (P5)
|
|
trace.zig replay.zig
|
|
runtime/ intrinsics: io, fs, net, clock, random (capability-gated)
|
|
frontend/ .aji lexer, parser, lowering, formatter (P6)
|
|
backend/llvm/ AIR → LLVM IR (P7)
|
|
report/ audit artifacts: unsafe, effects, capabilities, contracts
|
|
ajid/ long-running agent protocol server (P8)
|
|
tools/
|
|
conformance.zig ✓ conformance runner
|
|
tests/
|
|
conformance/ ✓ .air accept/reject (and later run) tests
|
|
```
|
|
|
|
## Trusted computing base
|
|
|
|
From `AGENTS.md`: verifier, AIR interpreter semantics, backend, unsafe runtime modules, ABI/platform descriptions. In this tree that is `air/`, `verifier/`, `interp/`, `runtime/`, `backend/`. The frontend, CLI, `ajid` and report generators are outside the TCB: a frontend bug can produce wrong AIR, but the verifier still rejects unsafe AIR.
|
|
|
|
Rules for TCB code:
|
|
|
|
- no heuristics in the verifier: every rejection maps to a spec rule and a registry code;
|
|
- no dependency outside the Zig standard library;
|
|
- every verifier pass is a pure function of the AIR module and the target description;
|
|
- the interpreter never relies on host undefined behavior to implement Aji semantics.
|
|
|
|
## Determinism rules
|
|
|
|
These hold for the whole toolchain, not only deterministic mode:
|
|
|
|
- iteration over hash maps never affects output; sort before emitting;
|
|
- diagnostics are emitted in a defined order (by source position, then code);
|
|
- no wall-clock time, randomness or environment variables influence verification or diagnostics;
|
|
- the interpreter's sources of nondeterminism (scheduling, clock, random, I/O ordering) go through `interp/sched.zig` so they can be seeded and recorded.
|
|
|
|
## Memory management in the implementation
|
|
|
|
- Per-compilation arena for AIR and verifier state; free it as a whole.
|
|
- `std.testing.allocator` in unit tests to catch leaks.
|
|
- No global mutable state; pass allocators and `std.Io` explicitly.
|
|
|
|
## Zig conventions
|
|
|
|
- Zig 0.17.0, formatted with `zig fmt`.
|
|
- Errors that are Aji diagnostics are values (`diag.Diagnostic`), not Zig errors. Zig errors are reserved for I/O failure and out-of-memory.
|
|
- Unit tests live next to the code (`test` blocks); behavior visible to users is tested through `tests/conformance`.
|
|
- Doc comments (`//!`, `///`) cite the spec section they implement, e.g. `// spec 03 §8.2`.
|