COMPILER DESIGN/Student Engineering Notebook
Page 09·notes
SYNTAX-DIRECTED TRANSLATION & INTERMEDIATE CODE GENERATION—~30 MIN READ

Intermediate Representations: Three-Address Code, SSA & Forms

“Three-Address Code (TAC), Quadruples vs Triples vs Indirect Triples, and Static Single Assignment (SSA) form.”

1.The Intermediate Representation Spectrum

An Intermediate Representation (IR) serves as the universal pivot of a modern retargetable compiler: an M-language front-end targeting N architectures requires M + N components rather than M × N monolithic compilers.

Three-Address Code (TAC): Linear low-level representation where each instruction has at most one operator on the right-hand side. Complex nested expressions like x = a + b c are linearized into: t1 = b c; t2 = a + t1; x = t2.

Representation Formats:

1. Quadruples: Stores op, arg1, arg2, result. Example: (*, b, c, t1). Facilitates easy optimization because instructions can be moved freely without breaking operand linkages.

2. Triples: Stores op, arg1, arg2. Example: (0) (*, b, c); (1) (+, a, (0)). Saves memory but reordering instructions requires rewriting all subsequent reference indices.

3. Indirect Triples: Maintains an array of pointers pointing to triple entries. Reordering optimizations simply permute the pointer array.

Static Single Assignment (SSA):

In SSA form, each variable is assigned exactly once, and each use refers to a unique definition. When control flow paths merge (e.g., after an if-else construct), a special \phi-function (phi-node) is placed at the merge point to select the appropriate definition based on the predecessor block.

mental model— Why SSA Dominates Modern Compilers

SSA converts data-flow analysis into a trivial lookup: because every variable name corresponds to exactly one static assignment, reaching definitions, constant propagation, and dead code elimination run in linear time.

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION2 Marks (GATE CS)

2. Worked Analysis: Array Address Translation to TAC

⚠️ Pitfall Warning:

Remember row-major index multiplier: i * NumberOfColumns + j.

Problem Statement (Modeled on GATE Pattern):

Translate the assignment statement X = A[i][j] + B[k] into minimal Three-Address Code instructions.

Given:

- Array A has dimensions 10 x 20 (10 rows, 20 columns).

- Each element of A and B occupies 4 bytes.

- Arrays are stored in row-major order starting at index 0.

Step 1: Compute offset for A[i][j]:

- Formula: Offset = (i cols + j) size = (i 20 + j) 4.

- TAC:

(1) t1 = i * 20

(2) t2 = t1 + j

(3) t3 = t2 * 4

(4) t4 = A[t3]

Step 2: Compute offset for B[k]:

- Formula: Offset = k size = k 4.

- TAC:

(5) t5 = k * 4

(6) t6 = B[t5]

Step 3: Perform addition and assignment:

- TAC:

(7) t7 = t4 + t6

(8) X = t7

Result: Exactly 8 Three-Address Code instructions.

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
Compilers: Principles, Techniques, and Tools ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Gate Smashers · Compiler Design↗Instructor: Varun Singla · Covers: Lexical analysis, parsing (LL/LR), SDT, code generation

GATE-focused with worked FIRST/FOLLOW and LR parsing table numericals.

Open YouTube↗
Knowledge Gate · Compiler Design for GATE↗Instructor: Sanchit Jain · Covers: Parsing conflicts, operator precedence, syntax-directed translation

Exam-oriented explanations of ambiguous grammars and parser conflicts.

Open YouTube↗
Stanford · Compilers (Coursera)↗Instructor: Prof. Alex Aiken · Covers: Lexing, parsing, semantic analysis, optimization, code generation

University-depth treatment of the full compiler pipeline — build a real compiler.

Open YouTube↗