Skip to content

5 middle end and backend

TransCompilator edited this page Nov 26, 2026 · 1 revision

5 Middle End and Backend

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/5-middle-end-and-backend

Middle End and Backend

Relevant source files

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

The Middle End and Backend subsystem of TransCompilator bridges structured English semantics into executable native binaries and byte-accurate interpretations. It takes typed abstract syntax from the front end, resolves scopes and types, performs demand-driven monomorphization, lowers constructs into Core-7 intermediate representation (IR), and routes execution either through a reference interpreter or down to standalone x86-64 ELF64 binaries.

For detailed specifications, see the child pages:

Sources: crates/tc-ir/src/interp.rs:1-60, crates/tc-codegen-x86/src/emit.rs:1-25


5.1. Type Checking and Monomorphization

The type checking phase manages lexical scopes, symbol resolution, and strict type checking over semantic expressions. It ensures type safety before any code generation or interpretation occurs. Additionally, it implements demand-driven monomorphization to instantiate specialized versions of polymorphic or generic phrases, lowering them into concrete Core-7 IR types and functions while providing actionable diagnostics and fix suggestions.

For detailed type checking, scope management, and monomorphization rules, see Type Checking and Monomorphization.

graph TD
    TypedAST["Typed AST"] -->|ResolveScopes| TypeEnv["TypeEnv"]
    TypeEnv -->|StrictCheck| Monomorphize["tc_typeck Monomorphizer"]
    Monomorphize -->|Lower| IRModule["Module"]

    classDef default fill:#fff,stroke:#000,stroke-width:1px;
Loading

(Diagram 1: Natural Language Space to Code Entity Space mapping for type checking and IR lowering)

Sources: crates/tc-codegen-x86/tests/battletest_core_regressions.rs:45-68


5.2. Core-7 IR and Reference Interpreter

Core-7 IR defines the low-level intermediate representation for TransCompilator, including Module, Function, Stmt, Expr, and primitive data types (Type). The module verifier (verify_module) checks structural invariants, while the reference interpreter (interpret) provides a byte-accurate execution engine used extensively for differential parity checks against native code generation.

For details on the IR data model, verifier rules, and the reference interpreter implementation, see Core-7 IR and Reference Interpreter.

graph TD
    ModuleDef["Module"] -->|verify_module| Verifier["Static Verifier"]
    ModuleDef -->|interpret| Interpreter["Reference Interpreter"]
    Interpreter -->|Produces| InterpOutput["InterpOutput"]

    classDef default fill:#fff,stroke:#000,stroke-width:1px;
Loading

(Diagram 2: IR verification and interpretation pipeline)

Sources: crates/tc-ir/src/interp.rs:1-58, crates/tc-codegen-x86/tests/battletest_core_regressions.rs:9-11


5.3. x86-64 Code Generation and Zero-libc ELF Linking

The backend translates verified Core-7 IR modules into Intel-syntax x86_64 assembly via emit_module. It invokes system assemblers and linkers to generate standalone, zero-libc static ELF64 binaries. Differential parity tests and automated IR fuzzers compare native execution results against the reference interpreter to guarantee correctness.

For instruction emission strategies, assembler invocation, and zero-libc linking, see x86-64 Code Generation and Zero-libc ELF Linking.

graph TD
    VerifiedIR["Module"] -->|compile_to_elf| Emitter["emit_module"]
    Emitter -->|Assembler/Linker| ELF64["Static ELF64 Binary"]
    ELF64 -->|Command::new| Execution["Native Execution"]

    classDef default fill:#fff,stroke:#000,stroke-width:1px;
Loading

(Diagram 3: Native compilation pipeline bridging IR to static ELF64 binaries)

Sources: crates/tc-codegen-x86/src/emit.rs:17-44, crates/tc-codegen-x86/tests/battletest_core_regressions.rs:45-68


Clone this wiki locally