Overview
Codegen converts abstract TAC instructions into concrete x86-64 assembly instructions, allocating stack space for variables and temporaries. Input:tacky::Program<'db> (TAC IR)Output:
asm::Program<'db> (assembly IR)Modules:
crates/mcc/src/codegen/mod.rs, crates/mcc/src/codegen/asm.rs
Entry Point
Function Lowering Pipeline
Assembly IR
Assembly IR is defined incodegen/asm.rs:
Program Structure
Instructions
Operands
Operators
Stack Allocation
Variables and temporaries are allocated on the stack:Instruction Selection
Return
%eax (x86-64 convention).
Unary Operations
Binary Operations
Most binary operations:%r10d as scratch register to avoid memory-to-memory operations.
Division and Modulo
Division uses special hardware requirements:idiv behavior:
- Divides
EDX:EAX(64-bit) by operand - Quotient →
EAX - Remainder →
EDX
Comparisons
cmpl + setcc sequences (handled during rendering).
Control Flow
Stack Frame Setup
After lowering all instructions, allocate stack space:subq $N, %rsp during rendering.
Instruction Fix-Up
Some instruction forms are invalid x86-64 and need rewriting:Memory-to-Memory Moves
Division with Immediate
Comparison Edge Cases
Example
TAC Input:Related Stages
- Previous: Lowering – Produces TAC input
- Next: Assembly Rendering – Converts IR to text
- Assembly IR Definition:
crates/mcc/src/codegen/asm.rs