Skip to content

5 2 core 7 ir and reference interpreter

TransCompilator edited this page Nov 26, 2026 · 1 revision

5.2 Core-7 IR and Reference Interpreter

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/5-2-core-7-ir-and-reference-interpreter

Core-7 IR and Reference Interpreter

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

This section details the Core-7 Intermediate Representation (tc-ir), its type system and layout rules adhering to the System V AMD64 ABI, the exhaustive static verifier, the deterministic textual IR formatter, and the byte-accurate reference interpreter utilized for differential parity checks against native x86-64 code generation.

Sources: crates/tc-ir/src/ir.rs:1-161, crates/tc-ir/src/types.rs:1-114, crates/tc-ir/src/verify.rs:1-74, crates/tc-ir/src/interp.rs:1-58, crates/tc-codegen-x86/tests/phase0_core7_parity.rs:1-110


1. Core-7 IR Data Model and Nodes

The Core-7 intermediate representation is structured around a central Module container holding typed definitions for structs, unions, read-only data blobs, global variables, and functions. Identifiers are strongly typed using index wrappers (FnId, LocalId, GlobalId, RodataId, StructId, UnionId).

graph TD
    Module[Module] --> Structs["Vec<StructDef>"]
    Module --> Unions["Vec<UnionDef>"]
    Module --> Rodata["Vec<RodataDef>"]
    Module --> Globals["Vec<GlobalDef>"]
    Module --> Functions["Vec<Function>"]
    Module --> Entry["FnId (entry)"]
    
    Functions --> Func["Function"]
    Func --> Params["Vec<LocalDef> (params)"]
    Func --> Locals["Vec<LocalDef> (locals)"]
    Func --> Body["Vec<Stmt>"]

    Body --> StmtAssign["Stmt::Assign"]
    Body --> StmtStore["Stmt::Store"]
    Body --> StmtIf["Stmt::If"]
    Body --> StmtLoop["Stmt::Loop"]
    Body --> StmtReturn["Stmt::Return"]

    StmtAssign --> Expr["Expr"]
    Expr --> ExprLocal["Expr::Local"]
    Expr --> ExprBinary["Expr::Binary"]
    Expr --> ExprCall["Expr::Call"]

    subcode ["Code Entity Space"]
    classDef default fill:#fff,stroke:#333,stroke-width:1px;
    end
Loading

Figure 1: Core-7 IR Module and AST Hierarchy.

Sources: crates/tc-ir/src/ir.rs:1-242, crates/tc-ir/src/types.rs:1-47


2. System V AMD64 Type System and Memory Layouts

Primitive types include booleans, signed/unsigned integers (i8-i64, u8-u64), IEEE-754 floating-point numbers (f32, f64), pointers (Type::Ptr), fixed-size arrays (Type::Array), structs, unions, and concrete function pointers (Type::FnPtr).

Memory layout calculation is implemented directly on Module to match System V AMD64 alignment and sizing constraints.

Category Types Supported Size (Bytes) Alignment (Bytes)
Boolean / Byte Type::Bool, Type::I8, Type::U8 1 1
Word Type::I16, Type::U16 2 2
Double Word Type::I32, Type::U32, Type::F32 4 4
Quad Word Type::I64, Type::U64, Type::F64, Pointers, FnPtrs 8 8
Aggregates Type::Array, Type::Struct, Type::Union Sum / Max padded to alignment Max member alignment

Sources: crates/tc-ir/src/types.rs:1-220


3. Exhaustive Static Verifier

The verifier (verify_module) checks type consistency, bounds validation for all table indices (FnId, LocalId, GlobalId, StructId, UnionId), loop nesting sanity for Break and Continue, and ABI compliance.

graph TD
    VerifyModule["verify_module"] --> CheckEntry["Verify entry bounds & signature"]
    VerifyModule --> CheckTables["Verify structs, unions, rodata, globals"]
    VerifyModule --> CheckFuncs["Verify function bodies & locals"]
    
    CheckFuncs --> InferExpr["infer_expr_type"]
    InferExpr --> CheckType["Type match & scalar checks"]
    
    CheckFuncs --> CheckStmt["Verify statement control flow"]
    CheckStmt --> CheckLoop["Ensure Break/Continue inside Loop"]

    SourcesBlock["Sources: crates/tc-ir/src/verify.rs:1-105"]
Loading

Figure 2: Verification Flow in the Exhaustive Verifier.

Sources: crates/tc-ir/src/verify.rs:1-105, crates/tc-ir/src/verify.rs:106-209


4. Deterministic Textual IR Formatter

The formatting engine translates a Module into a deterministic, human-readable textual representation. This representation is used for debugging, logging, and snapshot testing across compilation pipeline passes.

graph LR
    ModuleStruct["Module"] --> FormatModule["format_module"]
    FormatModule --> OutputText["Textual IR String"]
    OutputText --> FormatStructs["struct #id name { fields }"]
    OutputText --> FormatGlobals["global #id name ty storage"]
    OutputText --> FormatFuncs["fn #id name(params) -> ret"]

    SourcesBlock["Sources: crates/tc-ir/src/fmt.rs:1-62"]
Loading

Figure 3: Textual IR Generation Pipeline.

Sources: crates/tc-ir/src/fmt.rs:1-62, crates/tc-ir/src/fmt.rs:165-201


5. Byte-Accurate Reference Interpreter and Parity Validation

The reference interpreter (interpret) executes verified Core-7 IR modules in a simulated 64-bit address space with isolated memory regions (RODATA_BASE, DATA_BASE, STACK_BASE, HEAP_BASE, FN_BASE). It serves as the gold standard for differential parity checks against native x86-64 code generation.

graph TD
    IRModule["Module"] --> Verify["verify_module"]
    Verify --> Interp["interpret (Reference Interpreter)"]
    Verify --> Codegen["compile_to_elf"]
    
    Interp --> InterpOutput["InterpOutput { exit_code, stdout, stderr }"]
    Codegen --> NativeELF["Native ELF64 Binary"]
    NativeELF --> RunELF["run_elf"]
    
    InterpOutput --> AssertParity["assert_parity (Parity Check)"]
    RunELF --> AssertParity

    SourcesBlock["Sources: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:59-110"]
Loading

Figure 4: Differential Parity Testing Pipeline (Natural Language & IR Space to Execution Parity).

Sources: crates/tc-ir/src/interp.rs:1-58, crates/tc-ir/src/interp.rs:141-218, crates/tc-codegen-x86/tests/phase0_core7_parity.rs:59-110


Clone this wiki locally