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

The Problem

Generating target machine code directly from the annotated syntax tree is complex because different target machines have different architectures, register sets, and instruction sets. A compiler would need a separate code generator for each target, duplicating most of the logic.

Core Idea

Intermediate code generation is the fourth phase of a compiler. It transforms the annotated syntax tree into a machine-independent intermediate representation (IR). This IR is easier to optimize than source code and easier to translate to multiple target architectures than direct code generation.

How It Works

The intermediate code generator walks the annotated syntax tree and emits IR instructions. Common IR forms include three-address code (TAC) — each instruction has at most three operands — and static single assignment (SSA) form. The IR is designed to be high-level enough for optimization but low-level enough for code generation.

Visual Explanation

icg AnnotatedTree Annotated Syntax Tree IRGen Intermediate Code Generator AnnotatedTree->IRGen IR Three-Address Code  t1 = id + 42  t2 = t1 * 2 IRGen->IR Optimizer Code Optimizer IR->Optimizer for optimization

Key Properties

  • Machine-independent: Same IR can target different architectures
  • Common forms: Three-address code, SSA form, bytecode
  • Simplifies retargeting: New target only needs a new code generator from IR
  • Enables optimization: IR is easier to analyze and transform than source or machine code
  • Decouples front-end from back-end: Front-end produces IR; back-end consumes IR

Connections

Edge Cases & Gotchas

  • IR forms vary: Some compilers use multiple IR forms at different levels of abstraction
  • Addressing modes: Machine-independent IR may not capture all target-specific addressing modes — the code generator handles this mapping
  • SSA vs TAC: SSA form simplifies optimization but requires phi-nodes; TAC is simpler but requires extra data-flow analysis for optimizations