RFC-039: Compiler Architecture
Abstract
The YaoXiang compiler is organized into four layers: orchestration (L1), frontend (L2), semantics and intermediate representation (L3), and execution (L4). Dependencies flow in one direction; the stages run during a compilation are determined solely by the Driver, and the stage set is exhaustively enumerable; cross-stage obligation fields must be consumed downstream, otherwise the compilation is rejected with W1090 (error level); equivalence evidence for changes is graded C1–C6, while semantic fixes are routed according to the criteria in 07. This RFC defines these contracts; the expansion of each layer is described in 01–08 under docs/src/dev/architecture/.
Four-Layer Model
| Layer | Directory | Responsibilities |
|---|---|---|
| L1 Orchestration | src/driver/ | Determines which stages to run, how many times to run them, and how failures propagate; aggregates cross-stage diagnostics and determines the exit code |
| L2 Frontend | src/frontend/core/lexer/, src/frontend/core/parser/, src/frontend/module/ | Lexical analysis, syntax analysis, module discovery and registration; produces the AST |
| L3 Semantics and IR | src/frontend/core/sema/, src/middle/ir/, src/middle/lower/, src/middle/passes/ | Type checking, static analysis, monomorphization, AST → ModuleIR, IR-level validation |
| L4 Execution | src/middle/bytecode/, src/middle/passes/codegen/, src/backends/, src/std/ | Bytecode format and generation, interpreter, runtime, standard library |
| Shared Data Domain | src/ast/ | The single source of truth for AST and type representations; constructed by L2 and consumed by L3 |
| Peripheral Tools | src/formatter/, src/lsp/, src/repl/, src/package/, src/util/ | Consume only the pub APIs of L1–L4; do not participate in compilation stages |
Layer rules:
- Dependencies only point downward. L2 does not
usesema,middle, orbackends; L3 does notuselexerorparser; L4 does not refer back to L1 or L2. - The number of
include!occurrences is 0. - Opcode constants are defined only in
src/middle/bytecode/opcode.rs;backends/consumes the vocabulary and does not define it. - Peripheral tools do not touch
pub(crate); the count of cross-layerpub(crate)leaks may only decrease, never increase. - These rules are checked by
scripts/ci/check-boundary.py,check-concepts.py, andcheck-fanout.py.
Stage Contracts
Compilation stages are an exhaustively enumerable set, not a list registered at runtime. Adding a stage requires adding a Stage variant; the exhaustive match in Driver::dispatch enforces handling at compile time.
src/driver/stage.rs defines 12 stages, arranged in Stage::ALL in topological order of data dependencies:
VendorConsistency, Discovery, Parsing, Registry, RoleClassification, Typecheck, DeadCodeAnalysis, ProofExecution, GlobalSlotAlloc, IrGeneration, Linking, Monomorphization.
Each stage's scope is declared by StageScope: PerModule (executed once per compilation unit) or Project (executed once per project). Project-level stages are structurally guaranteed by dispatch to run only once.
src/driver/program.rs describes a compilation as a Program:
ProgramKindhas six variants:SingleFile,MultiFile,Check,Lsp,Embedded, andWasmPlayground.- The stage table may use only the predefined combinations from
ProgramKind::stages(); callers cannot pass arbitrary arrays. Aggregationdetermines diagnostic aggregation:FailFastis used for compilation paths, whileCollectAllis used forcheckand LSP.
Driver is the only place where Program is interpreted; the aggregation of cross-stage diagnostics and the determination of exit codes are in src/driver/diagnostics.rs.
Obligation Ledger
The fields of TypeCheckResult fall into two categories: cross-stage obligation fields, and non-obligation fields that do not constitute cross-stage contracts. The union of the two categories must equal all fields of the struct; scripts/ci/check-obligations.py performs a bidirectional reconciliation, and every added or removed field must be registered accordingly.
Obligations are delivered by the producer (deliver) and read by the consumer; Driver::run calls assert_drained once at the end of the stage table to settle them. The following cases pass:
- The obligation is empty.
- The consumer stage is not in the stage table for this
ProgramKind. - The consumer stage was skipped (
UpstreamFailed/ConfigDisabled). - The obligation has been delivered, and the consumption ledger has no unconsumed keys.
All other cases produce W1090, with emission severity Severity::Error. The code remains in the W family: the same fact is not assigned two codes, and the severity is explicitly specified at the settlement point.
ReleasePlan and overload_resolutions also carry a consumption ledger (ConsumptionLedger): entries are keyed by PlanId, and retrieving an entry records it as consumed; if unconsumed keys remain after a compilation run, the result is W1090. The audit surface DriverOutcome.obligations provides settlement details on every run.
Equivalence Criteria
Changes are graded by equivalence strength; not every change is required to use the same criterion:
| Category | Change | Criterion |
|---|---|---|
| C1 | Pure relocation (splitting files, changing directories, extracting submodules) | Zero diff in the normalized IR snapshot |
| C2 | Orchestration changes (staged orchestration, unified Driver) | Same diagnostic set for every entry point + same corpus behavior |
| C3 | Type representation convergence | Same diagnostic codes and messages (order may be normalized) |
| C4 | IR shape changes (SSA conversion) | Behavioral equivalence + IR structural invariants |
| C5 | Frontend paradigm changes | AST snapshot + diagnostic code/span + behavior, all three used |
| C6 | Pure deletion | Confirm that there are no references; no equivalence criterion |
The criteria are divided into three layers:
- IR static verifier (
verify,verify_loose, andverify_ssainsrc/middle/ir/verify.rs): dominance, a single-definition-point property,Phipredecessor consistency, existing jump targets, out-of-bounds slots, type consistency, and inner isolation. - Normalized IR snapshots (
src/middle/ir/tests/snapshots/): stripSpan, rename temporaries in order of appearance, relativize global slots, and sort predecessors; snapshots are checked in, and diffs are manually reviewed when updating them. - End-to-end corpus differencing: compare diagnostic lists, exit codes, standard output, and standard error item by item.
Semantic fixes do not follow the equivalence criteria; they follow the routing in 03 §Semantic Fix Triage: A/B/C-class fixes that can identify an RFC/SPEC section and provide a minimal counterexample may be corrected alongside the change; D/E/F-class fixes must first undergo user adjudication.
Glossary
| Term | Definition |
|---|---|
| Stage | An exhaustively enumerable step in a compilation; the set is an enum and cannot be registered at runtime |
| Orchestration Layer (L1) | The layer that determines which stages to run, how many times to run them, and how failures propagate |
| Obligations | Contract items produced by a stage that must be consumed downstream or compilation fails |
| Obligations | The container for all obligations, settled collectively at the end of the stage table |
| Equivalence Oracle | Executable checks that prove behavior is unchanged, graded C1–C6 |
| Separation of Concerns | Each module handles only one category of responsibility; no line-count or size threshold is imposed |
Related Documents
| Document | Layer | Topic |
|---|---|---|
| 01-architecture.md | Cross-layer/L1 | Layering and dependencies, directory responsibilities, feature routing, stages and the obligation ledger |
| 02-frontend.md | L2 | Lexing, parsing, type representations, type identity, and type-table gating |
| 03-ir-and-criteria.md | L3/Cross-layer | IR/SSA, lowering, bytecode, and equivalence criteria C1–C6 |
