Repository navigation
5 3 x86 64 code generation and zero libc elf linking
Relevant source files
The following files were used as context for generating this wiki page:
- crates/tc-codegen-x86/src/bin/battletest_fuzz_ir.rs
- crates/tc-codegen-x86/src/emit.rs
- crates/tc-codegen-x86/src/link.rs
- crates/tc-codegen-x86/tests/battletest_core_regressions.rs
- crates/tc-codegen-x86/tests/phase0_core7_parity.rs
- crates/tc-ir/src/fmt.rs
- crates/tc-ir/src/interp.rs
- crates/tc-ir/src/ir.rs
- crates/tc-semantics/src/bin/lexicon_sweep.rs
- crates/tc-syntax/src/token.rs
This page covers the architecture, implementation, and verification of the x86-64 backend (tc-codegen-x86). The backend translates verified Core-7 intermediate representation (tc_ir::Module) into Intel-syntax x86_64 assembly, compiles it via system assemblers and linkers (as and ld), and emits standalone, statically linked ELF64 binaries that require zero C library dependencies (libc). It also documents the differential parity and fuzzing infrastructure used to guarantee semantic equivalence between native execution and the reference interpreter.
The code generation pipeline begins with emit_module, which constructs an Emitter instance over a verified Module crates/tc-codegen-x86/src/emit.rs:18-25. The output assembly defaults to Intel syntax using .intel_syntax noprefix crates/tc-codegen-x86/src/emit.rs:35 and includes sections for read-only data (.rodata), initialized globals (.data), zero-initialized uninitialized storage (.bss), and executable instructions (.text) crates/tc-codegen-x86/src/emit.rs:36-116.
The emitter adheres to the System V AMD64 ABI for integer and floating-point registers:
-
Integer Argument Registers:
rdi,rsi,rdx,rcx,r8,r9(INT_ARG_REGS) crates/tc-codegen-x86/src/emit.rs:12 -
Floating-Point SSE Registers:
xmm0throughxmm7(SSE_ARG_REGS) crates/tc-codegen-x86/src/emit.rs:13-15
The entry point of the generated binary is explicitly defined as _start crates/tc-codegen-x86/src/emit.rs:122-123. If the entry function accepts parameters, _start unpacks argc, argv, and environment pointers from the stack (rsp), aligns the stack pointer to a 16-byte boundary via and rsp, -16, and invokes the lower-level function wrapper tc_fn_{id} crates/tc-codegen-x86/src/emit.rs:124-135. Upon return, it moves the exit code into edi, loads the exit system call number (60), and issues a raw syscall instruction without depending on libc initialization hooks crates/tc-codegen-x86v/src/emit.rs:136-149 (cite: crates/tc-codegen-x86/src/emit.rs:136-149).
graph TD
subcode["Code Entity Space"]
Module[tc_ir::Module] --> Emitter[tc_codegen_x86::emit_module]
Emitter --> FunctionEmitter[FunctionEmitter::emit]
FunctionEmitter --> Assembly["\".intel_syntax noprefix\""]
Assembly --> Entry["_start Bootstrap"]
Entry --> Syscall["Raw Syscall 60 (exit)"]
Sources["Sources: crates/tc-codegen-x86/src/emit.rs"]
Sources: crates/tc-codegen-x86/src/emit.rs:18-150
Once the assembly text is generated, assemble_and_link handles translation to machine code without linking against standard dynamic or static C libraries crates/tc-codegen-x86/src/link.rs:13-14.
To support concurrent compilation tasks and test runners, assemble_and_link generates a unique temporary directory using process identifiers and an atomic counter (TEMP_COUNTER) crates/tc-codegen-x86/src/link.rs:10-17. It writes the assembly to module.s and invokes external GNU toolchain utilities crates/tc-codegen-x86/src/link.rs:20-22:
-
Assembler (
as): Invoked with--64to produce an ELF object file (module.o) crates/tc-codegen-x86/src/link.rs:30-40. Errors are captured and mapped toCodegenError::Assemblercrates/tc-codegen-x86/src/link.rs:37-39. -
Linker (
ld): Invoked with-staticto produce a fully self-contained static ELF64 binary (out_path) crates/tc-codegen-x86/src/link.rs:42-47. Errors are mapped toCodegenError::Linkercrates/tc-codegen-x86/src/link.rs:48-52.
graph TD
subcode["Code Entity Space"]
AssemblyText[AssemblyString] --> WriteAsm["fs::write(module.s)"]
WriteAsm --> AsCommand["Command::new(\"as\") --64"]
AsCommand --> ObjFile[module.o]
ObjFile --> LdCommand["Command::new(\"ld\") -static"]
LdCommand --> StaticELF[StaticELF64Binary]
Sources["Sources: crates/tc-codegen-x86/src/link.rs"]
Sources: crates/tc-codegen-x86/src/link.rs:13-58
To verify code generation correctness, TransCompilator employs strict differential testing between the static ELF64 binary execution and the byte-accurate reference interpreter (tc_ir::interpret) crates/tc-ir/src/interp.rs:1, crates/tc-codegen-x86/tests/phase0_core7_parity.rs:1-13 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:1-13).
The test harness function assert_parity verifies modules through a rigorous sequence crates/tc-codegen-x86/tests/phase0_core7_parity.rs:59-109:
- Verifies module structural and typing correctness using
verify_modulecrates/tc-codegen-x86/tests/phase0_core7_parity.rs:66 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:66). - Executes the module in the reference interpreter (
interpret) to capture expected exit codes,stdout, andstderrcrates/tc-codegen-x86/tests/phase0_core7_parity.rs:68-82 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:68-82). - Compiles the module to a temporary ELF64 binary using
compile_to_elfcrates/tc-codegen-x86/tests/phase0_core7_parity.rs:85 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:85). - Spawns the compiled binary (
run_elf) with controlledargvandstdinstreams crates/tc-codegen-x86/tests/phase0_core7_parity.rs:34-57, 86 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:34-57, 86). - Asserts exact byte-for-byte equality of exit codes, standard output, and standard error crates/tc-codegen-x86/tests/phase0_core7_parity.rs:89-108 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:89-108).
graph TD
subcode["Code Entity Space"]
ModuleInput[Module] --> Verify["tc_ir::verify_module"]
Verify --> Interpret["tc_ir::interpret"]
Verify --> Compile["tc_codegen_x86::compile_to_elf"]
Interpret --> InterpResult[InterpOutput]
Compile --> ELFBinary[BinaryPath]
ELFBinary --> RunELF["run_elf()"]
RunELF --> ELFResult[RunResult]
InterpResult --> AssertEqual["assert_eq!(ELFResult, InterpResult)"]
ELFResult --> AssertEqual
Sources["Sources: crates/tc-codegen-x86/tests/phase0_core7_parity.rs"]
Sources: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:59-109
The fuzzer binary (battletest_fuzz_ir) generates deterministic, verified IR modules across multiple categories (scalars, floating-point operations, control flow loops, indirect calls, structs, and raw syscalls) based on pseudo-random seeds crates/tc-codegen-x86/src/bin/battletest_fuzz_ir.rs:58-68 (cite: crates/tc-codegen-x86/src/bin/battletest_fuzz_ir.rs:58-68). Each generated module is compiled to an ELF64 binary and run against the reference interpreter to catch edge cases in register allocation, sign/zero extensions, float-to-int saturations, and stack layout offsets.
Sources: [crates/tc-codegen-x86/src/bin/battletest_fuzz_ir.rs:1-520], [crates/tc-codegen-x86/tests/battletest_core_regressions.rs:1-259]*
The TransCompilator Project