Chapter 6

Intermediate code generation

Intermediate representations make program structure explicit while keeping many choices independent of the target machine. Read each representation's operand conventions before counting operations, variables or addresses.

6.1 Trees, three-address code, quadruples and triples

Past papers · CS 2014–2026 (23 papers): 4 questions, 5 marks

A parse tree preserves grammar details. An AST removes purely grammatical scaffolding. A DAG can share equivalent subexpressions. A control-flow graph represents possible execution between blocks and may contain cycles; a tree cannot. AST node arity depends on its construct, while a switch can give a control-flow node many successors.

Three-address code (TAC) usually has at most one elementary operation and at most three explicit addresses per instruction, such as t = a op b. Common forms also include unary operations, copies, conditional/unconditional jumps, parameter passing, calls, returns, indexed loads/stores and pointer operations.

RepresentationRecord for t = a op bWhere a result is named
Quadruple(op, a, b, t)Explicit result field
Triple(op, a, b)Position of the triple
Indirect tripleTriple plus an execution-order pointer listTriple identity remains stable while list order changes

Moving triples can require updating positional references; quadruples keep explicit temporary names. Unary operations need one operand. An array read produces a value, while an array write identifies a destination and a value. Notations such as =[] and []= are conventions, not universal machine opcodes.

6.2 Static single assignment

Past papers · CS 2014–2026 (23 papers): 3 questions, 4 marks

SSA gives each defined name one static definition. A use refers to the version representing the most recent definition along that path. Rename right-hand-side uses with the current versions before creating the new left-hand-side version. Inputs already have initial versions.

At a control-flow join, x3=ϕ(x1,x2)x_3=\phi(x_1,x_2) selects the argument associated with the incoming predecessor edge; it does not add the values or choose based on their magnitudes. A loop header may need a phi combining its initial value and the value carried by the back edge. SSA names are not physical registers, and assignments inside a loop can execute repeatedly despite having one static definition.

6.3 Array addressing and storage widths

Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks

Assume contiguous elements of width ww bytes and dimensions with explicit lower bounds. A base address is the first stored element, not necessarily the element with all indices zero. Padding and arrays of pointers require another layout description.

6.4 Boolean control flow and backpatching

Past papers · CS 2014–2026 (23 papers): 1 question, 1 mark

A Boolean expression can compute a true/false value or direct control to true/false destinations. Short-circuit A && B evaluates BB only when AA is true; A || B evaluates BB only when AA is false. This distinction matters when operands can have side effects or fail.

Backpatching emits a jump before its target is known, retains its instruction index in a list, and fills the target later. makelist(i) creates a list containing index ii; merge(p,q) joins lists; backpatch(p,L) sets every unresolved target in list pp to label LL. This permits one-pass generation for Boolean expressions and control statements.

For if E then S1 else S2, patch E.TE.T to S1S1 and E.FE.F to S2S2; add a jump from the end of S1S1 past S2S2. For while E do S, patch true exits to the body and the body's normal completion back to the test; false exits leave the loop. A statement's nextlist contains jumps whose normal successor is not yet known. Patch it when that successor's label becomes available.

Last-minute recall

  • An IR expresses program computation; a symbol table describes entities.
  • Triple positions name results; quadruples have explicit result fields.
  • SSA means one static definition per name.
  • A phi chooses according to the incoming edge.
  • Row-major order makes the last index vary fastest.
  • Strides do not determine the outermost array bound.
  • AND patches true exits to its right operand; OR patches false exits.
  • Short-circuit code must preserve which operands execute.

Revise offline: the whole Compiler Design handbook as one PDF

43 pages · 2.0 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections