feat(compiler): implement typed registry and production PBX conformance (AGD76)

This commit is contained in:
2026-10-14 21:04:04 +01:00
parent a9ede541af
commit bd5694dbdf
54 changed files with 2298 additions and 357 deletions
@@ -0,0 +1,182 @@
# Typed common Registry and PBX v0
Status: normative. Owner: `compiler/general`. Runtime contract reference:
`runtime` revision `f7d7c167531bb662a1af9d73fc326bb476bf8aa7`,
`docs/specs/runtime/02c-program-registry-and-pbx-v0.md` and
`docs/vm-arch/ISA_CORE.md` in that repository.
## Ownership and handoff
Every executable frontend MUST provide its semantic types through the common
`IRRegistry` model and typed `FunctionMetadata`. The backend MUST NOT inspect
language ASTs, PBS names, Optional/Result syntax, or frontend-specific classes.
Language carriers and their lowering remain owned by the frontend. A carrier
can use ordinary Struct, Array, Trait, Fn and Builtin declarations without
introducing a language category in the backend.
The public model consists of immutable values and ordered `ReadOnlyList`
collections. References use explicit declaration keys and callable IDs, not
object identity or recursive object graphs. It is serializable by design;
no concrete IR codec, wire format or persistence protocol is selected.
A function declares ordered named parameters, ordered logical results, ordered
captures, a lifecycle role and an optional name. Empty names differ from absent
names. Captures are an environment, never hidden parameters. Slot counts are
derived from types; existing count fields are checked caches, not semantic
input. Missing nonempty type metadata is an invalid handoff, not a request to
infer types from counts. Optimizers MUST preserve metadata and typed operands.
## Identity and linking
TypeId, FunctionId and ImplementationId are separate dense image-local index
domains. Index zero is valid. Trait references use TypeIds; field and method
indices are local to their owner. Source declaration keys MUST identify the
same declaration across repeated module imports and distinguish unrelated
nominal declarations, even when names and shapes coincide.
Linking is deterministic. The selected physical entry is FunctionId zero;
remaining functions follow canonical module/callable/source ordering. Struct
and Trait retain nominal identity. Builtin imports deduplicate by BuiltinId,
Array by canonical element identity, and Fn by ordered parameter/result type
identities, excluding names. Forward references and recursion through nominal
anchors are valid. Pure Array/Fn cycles are invalid. Every reference must
resolve before emission. Frontends should supply only used imports.
| BuiltinId | Type | Slots |
| --- | --- | --- |
| `0x0001` | color | 1 |
| `0x0002` | vec2 | 2 |
| `0x0003` | pixel | 3 |
| `0x0010` | Int32 | 1 |
| `0x0011` | Int64 | 1 |
| `0x0012` | Float | 1 |
| `0x0013` | Bool | 1 |
| `0x0014` | String | 1 |
| `0x0015` | Dynamic | 2 |
| `0x0016` | Random | 1 |
The retired input IDs `0x0100`–`0x0102` have no aliases. Struct, Array and Fn
values occupy one reference slot; Trait values occupy two slots. Width does
not establish type identity or authorize conversion.
## Binary contract
All fields are little-endian. The header is 32 bytes: `PBX\0`, u16 version zero,
zero endian/reserved bytes, u32 section count and twenty zero reserved bytes.
Directory entries are u32 kind, file-relative offset and length. Ranges must
not overlap or overflow. The version remains zero; no legacy fallback exists.
Mandatory sections are constants (0), functions (1), Code (2), exports (4),
syscalls (5), types (6), implementations (7) and captures (8). Debug (3) is
optional. Empty tables contain a u32 zero count. Code is raw bytes, not a
counted table. An empty table set is serializable but is not an executable
program: production loading requires a valid ordinary entry.
Lists have u32 counts. Optional names use u8 presence 0/1, then u32 UTF-8 byte
length and bytes when present. UTF-8 is strict. Named types contain TypeId then
optional name; results and captures contain TypeIds without names.
| Record | Ordered payload |
| --- | --- |
| Type | u8 tag, optional name, category payload |
| Builtin (tag 0) | u32 BuiltinId |
| Struct (tag 1) | named fields |
| Array (tag 2) | u32 element TypeId |
| Trait (tag 3) | methods: optional name, parameters, results |
| Fn (tag 4) | parameters, results |
| Function | u8 role, optional name, parameters, results, u32 Code offset, length, extra locals, max temporaries |
| Implementation | u32 owner TypeId, trait TypeId, method count, ordered FunctionIds |
| Captures | function count and one ordered TypeId list per function, including empty lists |
Function Code offsets are Code-relative. Parameter/result slot counts MUST NOT
be serialized. Max temporaries zero means zero. Signature names are independent
of debug names. Constants retain tags Null 0, Int64 1, Float64 2, Boolean 3,
String 4 and Int32 5. Float payloads must be finite. Constant deduplication
requires relocating every PUSH_CONST index; changing only the table is invalid.
Roles are None 0, BeforePause 1, BeforeResume 2 and MemcardBoot 3. Nonzero roles
are unique, cannot target entry zero and cannot capture. Pause/resume have empty
signatures; MemcardBoot has `(Int64) -> Int32`. This metadata does not implement
frontend hook selection, mem.bootstrap gating or persistence.
Implementations are unique per Struct/Trait pair, complete and ordered. Each
method target has an exact signature with a concrete Struct receiver first,
followed by the trait method's explicit parameters. Targets cannot capture.
## Instructions and verification
Instructions use u16 opcodes. Jump operands are absolute u32 byte offsets from
the current function start, not displacements from the next instruction.
A taken jump to the current function end is a return boundary; ordinary
fallthrough past the end is invalid.
| Opcode | Typed operands |
| --- | --- |
| MAKE_CLOSURE `0x52` | FunctionId, Fn TypeId |
| CALL_CLOSURE `0x53` | expected Fn TypeId |
| GET_CAPTURE `0x57` | capture group index |
| CALL_TRAIT `0x58` | Trait TypeId, method index |
| DYN_PACK/UNPACK/CAN_UNPACK `0x90`–`0x92` | TypeId |
| Reference operations `0x93`–`0x96` | source TypeId, target TypeId |
| STRUCT_NEW `0xa0` | Struct TypeId |
| STRUCT_GET/SET `0xa1`–`0xa2` | Struct TypeId, field index |
| Array operations `0xa3`–`0xa6` | Array TypeId |
Direct CALL cannot target a capturing body. Closure signatures match exactly.
Retired SPAWN/YIELD/SLEEP (`0x54`–`0x56`) and raw producer SYSCALL are invalid,
even when unreachable. All function ranges must decode completely; references,
categories, operand boundaries, finite immediates and branch destinations are
checked even in unreachable instructions.
Reachable analysis uses a bounded worklist. Parameters initialize local facts;
extra locals start Null and may change type. Joins require equal temporary
heights. Equal facts survive; conflicting facts widen to Unknown. Type errors
are diagnosed after convergence. Unknown alone neither proves compatibility
nor justifies rejection. Runtime group checks remain authoritative. RET and
jump-to-end require exact result width and compatible known facts. GET/SET_LOCAL
address only parameters plus extra locals. No implicit numeric, trait or Dynamic
conversion is introduced by linking, writing or verification.
HOSTCALL references a declared canonical `(module, name, version)` identity
with exact argument/result slot ABI. Duplicate identities with conflicting ABI,
unused declarations and invalid indices are rejected. Only the runtime loader
binds capabilities and rewrites HOSTCALL to SYSCALL. Intrinsic signatures are
explicit compiler metadata; their IDs and definitive ABI remain runtime-owned.
## Bounds and diagnostics
Product ceilings: PBX 64 MiB, Code 32 MiB, 65,536 types and functions each,
262,144 implementations, 1,048,576 nested records, names 4,096 bytes individually
and 4 MiB aggregate, frame 16,384 slots and results 1,024 slots. Runtime admission
also owns retained Registry 64 MiB, scratch 128 MiB and work 268,435,456. A guest
cannot raise these limits. Java validation is not a substitute for runtime
allocation accounting or final `ValidatedProgramImage::load` admission.
Compilation failures use common LOWER_IRVM/MARSHAL stage diagnostics. Load,
binding and resource failures remain distinct from executed traps. Studio
preserves the full runtime error envelope in `AutomationFailure.runtimeError`,
including null fields and native binding details. The outer initialization
fault PC remains null. A verifier PC stays in `initError.load.pc`; a file offset
MUST NOT become an executed PC.
## Reproducible conformance
Run the focused common tests, then the production harness:
```sh
./gradlew :prometeu-compiler:prometeu-build-pipeline:test --tests '*LinkedRegistryTest' --tests '*PbxRegistryConformanceTest' --tests '*TypedCommonPipelineTest'
python3 tools/pbx-conformance/run.py --runtime ../runtime
```
The Java tests write `build/registry-conformance` inside the build-pipeline
module. The harness builds outside the runtime checkout using its Cargo.lock,
loads Java-produced bytes, executes admitted zero-argument fixtures, validates
structured failures against the published schema and records the runtime hash,
platform, capabilities and limits. Parsing, loading and execution are separate
claims. The vec2 signature fixture is load-only; it is not a zero-argument boot.
The fixed 194-byte minimal image comes from the independent runtime test oracle.
Synthetic common IR additionally exercises linking, lowering, optimization and
emission without PBS. Frontend migration and integrated release readiness are
tracked separately in AGD75; passing common conformance does not claim complete
PBS lowering or complete Studio/runtime integration.