Skip to content

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 ​

LayerDirectoryResponsibilities
L1 Orchestrationsrc/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 Frontendsrc/frontend/core/lexer/, src/frontend/core/parser/, src/frontend/module/Lexical analysis, syntax analysis, module discovery and registration; produces the AST
L3 Semantics and IRsrc/frontend/core/sema/, src/middle/ir/, src/middle/lower/, src/middle/passes/Type checking, static analysis, monomorphization, AST → ModuleIR, IR-level validation
L4 Executionsrc/middle/bytecode/, src/middle/passes/codegen/, src/backends/, src/std/Bytecode format and generation, interpreter, runtime, standard library
Shared Data Domainsrc/ast/The single source of truth for AST and type representations; constructed by L2 and consumed by L3
Peripheral Toolssrc/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 use sema, middle, or backends; L3 does not use lexer or parser; 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-layer pub(crate) leaks may only decrease, never increase.
  • These rules are checked by scripts/ci/check-boundary.py, check-concepts.py, and check-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:

  • ProgramKind has six variants: SingleFile, MultiFile, Check, Lsp, Embedded, and WasmPlayground.
  • The stage table may use only the predefined combinations from ProgramKind::stages(); callers cannot pass arbitrary arrays.
  • Aggregation determines diagnostic aggregation: FailFast is used for compilation paths, while CollectAll is used for check and 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:

CategoryChangeCriterion
C1Pure relocation (splitting files, changing directories, extracting submodules)Zero diff in the normalized IR snapshot
C2Orchestration changes (staged orchestration, unified Driver)Same diagnostic set for every entry point + same corpus behavior
C3Type representation convergenceSame diagnostic codes and messages (order may be normalized)
C4IR shape changes (SSA conversion)Behavioral equivalence + IR structural invariants
C5Frontend paradigm changesAST snapshot + diagnostic code/span + behavior, all three used
C6Pure deletionConfirm that there are no references; no equivalence criterion

The criteria are divided into three layers:

  1. IR static verifier (verify, verify_loose, and verify_ssa in src/middle/ir/verify.rs): dominance, a single-definition-point property, Phi predecessor consistency, existing jump targets, out-of-bounds slots, type consistency, and inner isolation.
  2. Normalized IR snapshots (src/middle/ir/tests/snapshots/): strip Span, rename temporaries in order of appearance, relativize global slots, and sort predecessors; snapshots are checked in, and diffs are manually reviewed when updating them.
  3. 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 ​

TermDefinition
StageAn 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
ObligationsContract items produced by a stage that must be consumed downstream or compilation fails
ObligationsThe container for all obligations, settled collectively at the end of the stage table
Equivalence OracleExecutable checks that prove behavior is unchanged, graded C1–C6
Separation of ConcernsEach module handles only one category of responsibility; no line-count or size threshold is imposed
DocumentLayerTopic
01-architecture.mdCross-layer/L1Layering and dependencies, directory responsibilities, feature routing, stages and the obligation ledger
02-frontend.mdL2Lexing, parsing, type representations, type identity, and type-table gating
03-ir-and-criteria.mdL3/Cross-layerIR/SSA, lowering, bytecode, and equivalence criteria C1–C6

References ​