• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

After analysis and optimization, the compiler holds an optimized intermediate representation. This IR must be translated into actual machine instructions (or assembly) that the target processor can execute — including instruction selection, register allocation, and addressing mode decisions.

Core Idea

Code generation is the final phase of a compiler. It takes the optimized intermediate representation and produces target machine code (or assembly). The generated code must be correct, efficient, and make effective use of the target architecture’s resources (registers, memory, instruction set).

How It Works

The code generator performs three key tasks: instruction selection (mapping IR operations to target instructions), register allocation (assigning variables to CPU registers), and instruction ordering (scheduling instructions for efficiency). It produces relocatable object code or assembly that is passed to an assembler or linker.

Visual Explanation

code_generation cluster_tasks Code Generator Tasks OptimizedIR Optimized IR Gen Code Generator OptimizedIR->Gen Target Target Machine Code / Assembly Gen->Target ISel Instruction Selection Gen->ISel RegAlloc Register Allocation ISel->RegAlloc ISched Instruction Scheduling RegAlloc->ISched

Key Properties

  • Input: Optimized intermediate representation
  • Output: Target machine code (assembly or binary)
  • Three tasks: Instruction selection, register allocation, instruction scheduling
  • Target-dependent: The code generator is specific to the target architecture
  • Produces: Object code that the linker/loader handles next

Connections

Edge Cases & Gotchas

  • Register spilling: When there are more live variables than registers, some must be spilled to memory — frequent spilling destroys performance
  • Strange instructions: Some architectures have complex instructions (VLIW, SIMD) that require careful pattern matching during instruction selection
  • PIC vs absolute code: Position-independent code requires different addressing strategies than absolute code