Skip to content
GATEiit

GATE CSE · Compiler Design

Intermediate code generation

How a compiler turns a parsed program into a machine-independent intermediate form: syntax trees, DAGs, postfix, three-address code (quadruples, triples, indirect triples), SSA, array addressing and control-flow code.

Before you start

This topic comes before: Data flow analyses: constant propagation, liveness analysis and common subexpression elimination, Local optimisation

Concept notes

1. Why an intermediate representation

The front end (lexer, parser, semantic checks) produces an intermediate representation (IR); the back end turns the IR into target code. With an IR, m source languages and n machines need m+n translators instead of m×n, and machine-independent optimisation can be done once, on the IR.

Common IRs in GATE questions:

  • Syntax tree: interior nodes are operators, leaves are operands. One node per occurrence.
  • DAG (directed acyclic graph): a syntax tree in which identical subexpressions share one node. It exposes common subexpressions.
  • Postfix (reverse Polish): the post-order listing of the syntax tree, e.g. a + b * c becomes a b c * +. Natural for stack machines.
  • Three-address code (TAC): a linear sequence of simple instructions, each with at most one operator on the right, e.g. t1 = b * c.
  • SSA form: TAC in which every variable is assigned exactly once.

Trees and DAGs are graphical IRs; postfix and TAC are linear IRs. TAC is the one most GATE numericals use.

2. Three-address code and its storage: quadruples, triples, indirect triples

A TAC instruction has at most three addresses (two operands, one result). Typical forms: x = y op z, x = op y, x = y (copy), goto L, if x relop y goto L, param x / call p, n, indexed copies x = y[i], x[i] = y, and pointer forms x = &y, x = *y.

For y = (p + q) * r the TAC is t1 = p + q, t2 = t1 * r, y = t2.

Form Fields Result referred to by Moving an instruction
Quadruple op, arg1, arg2, result an explicit temporary name Easy: names do not change
Triple op, arg1, arg2 its position, e.g. (0) Hard: every reference to the position must change
Indirect triple triples + a list of pointers to them position of the triple Easy: reorder only the pointer list

Triples save the result field but tie each value to an instruction number. Indirect triples keep that saving and still let an optimiser reorder code. Quadruples need more space and more temporary names in the symbol table.

3. DAGs: sharing common subexpressions

To build a DAG, process the expression bottom up. Before creating a node (op,l,r), look for an existing node with the same operator and the same children; if found, reuse it. This is called value numbering.

Counting rules for GATE questions:

  • Each distinct variable or constant is one leaf, however often it occurs.
  • Each distinct (operator, left child, right child) triple is one interior node.
  • Order matters for non-commutative operators: a−b and b−a are different nodes. Unless a question says so, do not merge a+b with b+a.
  • Parse with the usual precedence and left associativity first: a∗b∗c is (a∗b)∗c.

A DAG never has more nodes than the syntax tree, and the difference is exactly what common-subexpression elimination saves. A node with more than one parent is a common subexpression; it is computed once into a temporary.

*−+abcDAG of (a + b) * (a + b − c)6 nodes; the syntax tree has 9the node a + b has two parents
DAG of (a + b) * (a + b − c): the subexpression a + b is built once and shared

4. Static single assignment (SSA)

In SSA form each variable is assigned exactly once in the program text. Every assignment creates a new version: a1,a2,…, and each use is renamed to the version that reaches it. Incoming values (defined before the code) are often written b0,c0.

Straight-line code:

a = b + c        a1 = b0 + c0
b = a - d   →    b1 = a1 - d0
a = b * c        a2 = b1 * c0

Where control paths join, a variable may have different versions on different incoming edges. SSA inserts a φ-function: x3=ϕ(x1,x2) picks x1 if control came from the first predecessor and x2 if from the second.

Why SSA helps: each use has exactly one reaching definition, so constant propagation, dead-code elimination and value numbering become simple.

Counting questions: the number of new versions equals the number of assignments (plus φ-functions at joins). Read whether the question also counts the incoming versions like b0.

5. Translating arrays and control flow

Arrays. For A[n1][n2] with element size w and 0-based indices, row-major storage gives addr(A[i][j])=base+(i⋅n2+j) w. Column-major uses (j⋅n1+i) w. With lower bounds l1,l2, replace i by i−l1 and j by j−l2. The TAC for x = A[i][j] (row-major, n2=20, w=4) is t1 = i * 20, t2 = t1 + j, t3 = t2 * 4, x = A[t3].

Boolean expressions use short-circuit (jumping) code: in a && b, if a is false, b is never evaluated. For if (a < b && c > d) x = 1; else x = 0;:

    if a < b goto L1
    goto Lf
L1: if c > d goto Lt
    goto Lf
Lt: x = 1
    goto Ln
Lf: x = 0
Ln: ...

Backpatching generates such jumps in one pass: jumps whose targets are not yet known are kept on a truelist or falselist and their targets are filled in when the label becomes known.

Formulas to remember

Row-major address (0-based)A[n1][n2], element size w
base+(i⋅n2+j) w
Column-major address (0-based)
base+(j⋅n1+i) w
Row-major with lower boundsn2 = number of columns =u2−l2+1
base+((i−l1) n2+(j−l2)) w
Interior nodes in a DAGPlus one leaf per distinct variable or constant
#{(op,left,right) distinct}

Common mistakes

  • Merging a + b with b + a, or a − b with b − a, in a DAG when the question does not allow commutativity.
  • Parsing a * b * c as a * (b * c); the usual rule is left associativity, which changes which subexpressions can be shared.
  • Confusing triples (results named by position) with quadruples (results named by temporaries).
  • In SSA counting, forgetting to say whether incoming values such as b0 are included.
  • Using the row count instead of the column count in the row-major formula; row-major multiplies i by the number of columns.
  • Evaluating both operands of && or || in jumping code; short-circuit code must skip the second operand when the first decides the result.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Three-address code, quadruples and triples

numerical

Translate y = (p + q) * r - (p + q) / s into three-address code without any optimisation. Write it as quadruples and as triples. How many instructions remain if common subexpressions are shared?

  1. Step 1: Write the TAC, one operator per instruction, left to right.

    Try it on paper first.

Example 2: Counting DAG nodes

numerical

How many nodes are in the syntax tree and in the DAG of (a+b)∗(a+b−c)?

  1. Step 1: Count the syntax tree: one node per occurrence.

    Try it on paper first.

Example 3: Converting straight-line code to SSA

numerical

Convert to SSA and count the distinct variable names used, counting incoming values as b0, c0, d0:

a = b + c
b = a - d
a = b * c
d = a + b
  1. Step 1: Give each assignment a new version and rename each use to the latest version.

    Try it on paper first.

Example 4: Address of an array element

numerical

int A[10][20] (4-byte int, 0-based, row-major) starts at address 1000. Find the address of A[3][5] and the TAC for x = A[i][j].

  1. Step 1: Count the elements before A[3][5] in row-major order.

    Try it on paper first.

Previous year questions

7 official GATE CSE questions on this topic, newest first.

Practise these 7 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.