Sub topic 7
Why compilers use intermediate code
This chapter explains what intermediate code is and why a compiler builds it between parsing and machine code generation. It assumes you know parse trees and basic syntax-directed translation, and that you can read C-style code. Later chapters cover specific forms, such as syntax trees, DAGs and three-address code, in detail.
Where intermediate code sits
A compiler front end reads source text and checks it. It produces a parse tree or syntax tree and does type checking. The back end chooses machine instructions and registers. Intermediate code, also called an intermediate representation (IR), is the program form that passes from the front end to the back end.
The front end depends on the source language. The back end depends on the target machine. The IR depends on neither, or depends on them as little as possible.
Reason 1: Retargeting with fewer compilers
Suppose you need compilers for m source languages and n machines. Without an IR, each pair needs its own compiler, so you write m × n compilers. With a shared IR, you write m front ends and n back ends, which is m + n pieces of work.
| Source languages (m) | Target machines (n) | Without IR (m × n) | With IR (m + n) |
|---|---|---|---|
| 2 | 2 | 4 | 4 |
| 3 | 4 | 12 | 7 |
| 5 | 5 | 25 | 10 |
The saving grows as m and n grow. A new language needs only a new front end that emits the IR. A new machine needs only a new back end that reads the IR.
There is a limit to this idea. A single IR rarely hides every difference between languages. Features such as exceptions, garbage collection or closures need to be encoded in the IR, and that can be awkward. Differences in word size or alignment can also leak into the IR.
Reason 2: A good home for optimization
Machine-independent optimization is much easier on an IR than on source code or machine code.
- Source code has nested syntax, many statement forms and implicit operations. Analyzing it directly is complicated.
- Machine code has fixed registers, addressing modes and instruction quirks. Analyses written for one machine do not carry over to another.
- An IR has a small, regular set of operations. One optimizer can serve every front end and back end.
The IR also makes implicit work explicit. Consider the C statement x = a[i] + b;. The source hides the address calculation, which is the base address of a plus i times the element size. A three-address IR spells it out as separate steps, so an optimizer can see repeated address calculations and remove them.
Reason 3: Easier code generation
Translating directly from a syntax tree to machine code forces the code generator to handle expression structure and machine details together. Splitting the work into two steps makes each simpler:
- The front end flattens the tree into simple steps, each doing one operation and naming its result.
- The back end maps those simple steps to instructions and assigns registers.
Kinds of intermediate representation
IRs differ in how close they stay to the source. The usual classification is by level.
| Level | Example forms | What it keeps | Typical use |
|---|---|---|---|
| High level | Syntax tree, DAG | Source structure, such as loops and array accesses | Type checking, simple analyses, detecting repeated subexpressions |
| Mid level | Three-address code, control flow graph | Explicit temporaries and jumps, but no machine registers | Machine-independent optimization and data flow analysis |
| Low level | Machine-like instruction lists | Registers, addresses and instruction patterns | Register allocation and instruction selection |
Representations can also be graphical (syntax trees, DAGs, control flow graphs) or linear (three-address code, stack code). Postfix notation is a linear form, and a stack machine can run it directly.
Here is the expression a + b * c in two linear forms.
Postfix: a b c * +
Stack machine code: push a
push b
push c
mul
add
Three-address code: t1 = b * c
t2 = a + t1Stack code needs no names for intermediate results, because the stack holds them. Three-address code names every result with a temporary. Those names are what let an optimizer reorder code and track where each value is used, so compilers favor three-address code for optimization.
The cost of an IR
An IR is not free.
- It adds a translation step and extra memory use. A one-pass compiler that generates target code directly compiles faster, and some small compilers do this.
- Each translation can lose information. If the IR drops type or loop structure, later phases must rediscover it, as the compiler does when it finds loops from jumps in a control flow graph.
- Many compilers use several IRs in sequence, such as a tree first and then three-address code. Each added level costs more translation code.
Worked example: flattening one statement
Take a = b * -c + b * -c. A syntax tree for it has two identical subtrees for b * -c. Flattening the tree into three-address code gives:
t1 = minus c
t2 = b * t1
t3 = minus c
t4 = b * t3
t5 = t2 + t4
a = t5Each instruction has at most one operator on its right side. The temporaries t1 to t5 hold intermediate values. The code computes b * -c twice. An optimizer that sees this explicit form can replace the second computation with a reuse of t2, giving the shorter sequence:
t1 = minus c
t2 = b * t1
t5 = t2 + t2
a = t5This is safe only because b and c do not change between the two computations. Later chapters show how to check such conditions with data flow analysis.
Properties of a good IR
A useful IR has these properties:
- Language independence. It does not expose source-specific syntax.
- Machine independence. It does not commit to registers or instruction formats, so one optimized IR can feed many back ends.
- Simplicity. It has few operation types, so analyses are short to write.
- Explicitness. Address arithmetic, control flow and temporaries are visible.
- Ease of rewriting. Instructions can be moved, deleted or replaced without breaking references. This property is why indirect triples exist, as a later chapter shows.
Common mistakes
- Treating the IR as one fixed format. Real compilers use several, and the right choice depends on the phase.
- Saying that an IR removes all machine dependence. It reduces dependence, and some details, such as data sizes, still appear.
- Assuming optimization works on source code. Most machine-independent optimization works on the IR.
- Confusing the IR with the target code. The IR still has an unlimited supply of temporaries, while target code must fit a fixed number of registers.
Exercise
A company has 4 source languages and 6 target machines. How many separate compiler components must it write without a shared IR, and how many with one? Name the two kinds of component in the second case.
Show answer
Without an IR it needs 4 × 6 = 24 compilers. With a shared IR it needs 4 front ends plus 6 back ends, which is 10 components. The front ends translate source into the IR, and the back ends translate the IR into target code.
Exercise
Write three-address code for x = (p + q) * (p + q). How many instructions does it have, and what could an optimizer change?
Show answer
One translation is: t1 = p + q; t2 = p + q; t3 = t1 t2; x = t3. It has 4 instructions. Since p and q do not change between the two additions, the optimizer can drop the second addition and write t3 = t1 t1, which leaves 3 instructions.
Exercise
Give two reasons a compiler might prefer three-address code over stack code as the IR for optimization.
Show answer
Three-address code names every intermediate result with a temporary, so the optimizer can track where a value is defined and used. It also lets instructions be reordered or removed more easily, because they do not depend on an implicit stack order.