9.7 KiB
IRBackend to IRVM Lowering Specification
Status: Draft v1 (Backend Baseline)
Applies to: executable-backend lowering from the common IRBackend handoff into IRVM
1. Purpose
This document defines the normative lowering contract from executable IRBackend to IRVM.
Its purpose is to make backend lowering deterministic, reviewable, and compatible with runtime ISA/verification authority.
IRBackend is the common frontend-to-backend executable handoff. It is emitted by frontends and consumed by common backend stages. Backend lowering MUST NOT require PBS AST nodes, PBS tokens, PBS parser structures, PBS semantic objects, PBS editorial objects, or equivalent language-owned objects from future frontends.
2. Scope
This document defines:
- required backend preconditions before
IRBackend -> IRVMstarts, - allowed language-neutral
IRBackendcontract concepts at the backend boundary, IRVMstructural obligations needed before optimization/emission,- deterministic function-id assignment and control-flow lowering obligations,
- callsite lowering obligations across function, host-backed, and VM-owned intrinsic paths,
- mandatory compiler-side structural pre-verification before bytecode emission,
- and deterministic rejection obligations for backend-originated failures.
This document does not define:
- binary PBX layout details,
- loader patching internals,
- runtime execution internals,
- one mandatory optimizer implementation,
- or frontend-specific lowering rules before
IRBackendemission.
3. Authority and Precedence
Normative precedence:
- Runtime authority (
docs/specs/hardware/topics/chapter-2.md,chapter-3.md,chapter-9.md,chapter-12.md,chapter-16.md) - Bytecode authority (
docs/specs/bytecode/ISA_CORE.md) - This document
- Frontend-specific lowering specifications, including
docs/specs/compiler-languages/pbs/13. Lowering IRBackend Specification.md
If a rule here conflicts with higher-precedence authorities, it is invalid.
4. Normative Inputs
This document depends on:
docs/specs/compiler-languages/pbs/6.1. Intrinsics and Builtin Types Specification.mddocs/specs/compiler-languages/pbs/6.2. Host ABI Binding and Loader Resolution Specification.md- frontend-specific lowering specifications that emit the common
IRBackendhandoff 15. Bytecode and PBX Mapping Specification.md21. IRVM Optimization Pipeline Specification.md
4.1 Common IRBackend Contract
At this boundary, IRBackend may contain only language-neutral compiler concepts required by executable backend lowering:
- source identity and spans,
- modules and module identifiers,
- callable identities, signatures, names, arity, and type-shape surfaces,
- executable functions,
- instruction kinds and instruction metadata,
- globals and global origins,
- synthetic executable functions and synthetic origins,
- host-call metadata,
- intrinsic metadata,
- reserved metadata required for backend lowering,
- and required runtime or platform capabilities.
The common backend contract MUST express these concepts as backend obligations, not as PBS syntax or PBS semantic rules. When a concept currently originates from PBS, the backend contract still owns only the language-neutral obligation carried by IRBackend.
The common backend contract MUST NOT expose or require:
PbsExpression,PbsStatement,PbsToken, or equivalent PBS-owned syntax objects,- PBS parser cursors, parse contexts, parse nodes, or token kinds,
- PBS semantic validator internals,
- PBS editorial or language-service objects,
- or any type under a language frontend package as part of the public backend handoff.
5. Backend Entry Preconditions
Lowering from IRBackend to IRVM may start only when:
- frontend admission to
IRBackendis complete for the active slice, - required callsite categories (
CALL_FUNC/CALL_HOST/CALL_INTRINSIC) are available, - required canonical host/intrinsic metadata is available for admitted callsites,
- dependency-scoped fail-fast exclusions have already been applied at the
IRBackendboundary, - compiler-selected published-wrapper entrypoint identity is present and unambiguous,
- and target
vm_profileis selected deterministically.
6. IRVM Model Obligations
IRVM v1 is a quasi-ISA backend form:
- it may contain Core ISA ops and internal extension ops (
IRVM_EXT), - each
IRVM_EXTmust declare structural metadata equivalent to opcode-spec needs:pops,pushes,is_branch,is_terminator,
- and
IRVM_EXTmust be eliminable before bytecode emission.
IRVM must preserve, per function:
param_slots,local_slots,return_slots,max_stack_slots,- function entry mapping and deterministic identity.
7. Control-Flow Lowering Obligations
Control-flow lowering must satisfy:
- symbolic labels are allowed internally in lowering,
- final jump immediates must be resolved to
u32offsets relative to function start before emission, - jump targets must be instruction-boundary-valid within function ranges,
- and reachable fallthrough beyond function end without terminator is rejected.
8. Deterministic Function-ID Assignment
Function indexing must be deterministic:
- the compiler-selected published wrapper is the physical entrypoint,
- the published wrapper function id is
0, - remaining functions are ordered by
(moduleId -> modulePool canonical key, callable_name, source_start), - manifest-owned or name-only entrypoint fallback is forbidden,
- and identical admitted input graphs must produce identical function-id assignments.
For PBS executable lowering:
- the userland callable marked with
[Frame]remains the logical frame root, - the wrapper path must contain final
FRAME_RET, - and lowering must preserve that distinction through
IRVM.
These PBS-facing names describe the current frontend source of the lifecycle structure. The backend-owned obligation is the neutral requirement that the compiler-selected published wrapper be the physical entrypoint and that lifecycle state remain explicit in IRBackend.
9. Callsite Lowering Obligations
9.1 Function calls
CALL_FUNC lowers to CALL <func_id> with valid index and arity-consistent signature contracts.
9.2 Host-backed calls
CALL_HOST lowers to pre-load HOSTCALL <sysc_index>, not raw SYSCALL.
When a host-backed callsite carries backend-owned symbolic asset lowering metadata:
- the backend must validate the symbolic asset operand against the backend-owned admitted surface,
- the backend must resolve the symbolic identity to canonical runtime-facing
asset_id, - and the final host-backed lowering path must consume the resolved
asset_id, not the original symbolic address.
9.3 VM-owned intrinsic calls
CALL_INTRINSIC lowers to VM-owned intrinsic path and must remain distinct from host-binding paths.
Final intrinsic id resolution rules:
- compiler must resolve final intrinsic ids from the canonical ISA-scoped intrinsic registry artifact,
- compiler-local hardcoded maps or runtime-source parsing must not be authoritative identity sources,
- runtime and compiler consumers must validate parity against the same canonical artifact in strict CI mode.
9.4 Closure and coroutine constraints (v1)
- closure calls with unknown static callee are rejected in v1,
SPAWNandYIELDare outside the executablecore-v1claim scope and MUST be rejected as unsupported forms in v1 backend admission,SPAWNargument-count andYIELDstack-shape invariants remain reserved for a future profile that explicitly admits these opcodes,- and any profile that admits
SPAWN/YIELDMUST publish dedicated conformance evidence before claiming support.
10. Mandatory Compiler-Side Pre-Verification
Before bytecode emission, backend must run structural pre-verification on lowered IRVM (after resolving to Core-ISA-compatible form), covering at minimum:
- jump target validity,
- stack-depth join consistency,
- underflow/overflow against declared limits,
- return-shape consistency,
- function-id validity,
- callsite arity consistency,
- and structural validity of host/intrinsic call forms.
- and structural validity of backend-owned symbolic-to-operational rewrites such as
Addressable -> asset_id.
For host-backed asset calls in v1, this validation includes backend-owned symbolic-to-operational rewrites selected by preserved host metadata. PBS currently emits this designation from metadata such as [AssetLowering(param = N)], but final operational resolution is a common backend obligation.
This pre-verification does not replace runtime verifier authority.
11. Deterministic Rejection Policy
When backend lowering fails at this boundary:
- rejection must be deterministic,
- diagnostics identity and phase must remain stable,
- and source attribution must be preserved whenever the failure remains source-attributable and user-actionable.
12. Explicit Deferrals
The following are intentionally deferred:
- optimizer pass catalog and tuning policy,
- richer internal IR analysis models beyond v1 obligations,
- advanced closure/lifetime optimizations beyond v1 constraints,
- and executable support for
SPAWN/YIELDincore-v1.
13. Non-Goals
- Defining one in-memory IR implementation graph.
- Redefining runtime verifier/loader internals.
- Defining PBX binary chunk policy in this document.
14. Exit Criteria
This document is healthy when:
IRBackend -> IRVMobligations are explicit and testable,- deterministic function/call/control-flow lowering rules are explicit,
- mandatory pre-verification boundary is explicit,
- common backend obligations are not described as owned by PBS,
- frontend-specific lowering specs only own emission into
IRBackend, - and scope boundaries with optimization/emission specs are explicit.