Intermediate Code Generation
05 May, 2026
Three-address codes
# It is a sequence of statements of the general form
𝑥 = 𝑦 op 𝑧
where 𝑥, 𝑦 and 𝑧 are names, constants, or compiler generated temporaries and op stands for any
operator.
# Example: A source language expression like
𝑥+𝑦∗𝑧
might be translated into a sequence
𝑡1 = 𝑦 ∗ 𝑧
𝑡2 = 𝑥 + 𝑡1
where 𝑡1 and 𝑡2 are temporaries.
# Consider the assignment statement.
𝑎 = 𝑏 ∗ −𝑐 + 𝑏 ∗ −𝑐
Figure 1: Syntax tree
Three address code:
𝑡1 = minus 𝑐 𝑡2 = 𝑏 ∗ 𝑡1
𝑡3 = minus 𝑐 𝑡4 = 𝑏 ∗ 𝑡3
𝑡5 = 𝑡2 + 𝑡4 𝑎 = 𝑡5
Some common three-address codes
1
1. Assignment statements of the form
𝑥 = 𝑦 op 𝑧
where op is a binary operator.
2. Assignment instruction of the form
𝑥 = op 𝑦
where op is a unary operator.
3. Copy statements of the form
𝑥=𝑦
where the value of 𝑦 is copied to 𝑥.
4. The unconditional jump
goto 𝐿
The three-address statement with label 𝐿 is the next to be executed.
5. Conditional jumps such as
if 𝑥 relop 𝑦 goto 𝐿
6. Indexed assignments of the form
𝑥 = 𝑦[𝑖]
This statement sets 𝑥 to the value in the location 𝑖 units beyond location 𝑦.
𝑥[𝑖] = 𝑦
This statement sets the location 𝑖 units beyond 𝑥 to 𝑦.
7. Address and pointer assignements of the form
𝑥 = &𝑦
This statement stores the location of 𝑦 in 𝑥. (There is a distinction between the meaning of
identifiers on the left and right sides of an assignment. In each of the assignments, 𝑖 = 5 and 𝑖 =
𝑖 + 1, the right side specifies an integer value while the left side specifies where the value is to be
stored. The term r-value are what we usually think as values while l-values are locations.)
𝑥=∗𝑦
In this statement, presumably 𝑦 is a pointer or a temporary whose r-value is a location. The r-
value of 𝑥 is made equal to the contents of the location.
∗𝑥=𝑦
This statement sets the r-value of the object pointed to by 𝑥 to the r-value of 𝑦.
Implementation of three-address codes
Quadruples
# A quadruple is a record structure with four fields which are called op, arg1 , arg2 and result.
# The three address statement,
𝑥 = 𝑦(𝑧)
is represented by replacing 𝑦 in arg1 , 𝑧 in arg2 and 𝑥 in result.
2
# Statements with unary expressions like 𝑥 = −𝑦 or 𝑥 = 𝑦 do not use arg2 .
# The quadruples for the assignment
𝑎 = 𝑏 ∗ −𝑐 + 𝑏 ∗ −𝑐
are:
Figure 2: Quadruple structure
Triples
# Here three-address statements can be represented by records with only three fields op, arg1 and
arg2 .
# The fields arg1 and arg2 for the argument of op are either pointers to the symbol table (for
program defined names or constants) or pointers into the triple structure (for temporary values).
# Parenthesized numbers represent pointers into the triple structures while symbol table pointers
are represented by the names itself.
#
Figure 3: Triple structure
Indirect triples
# Another implementation of three-address code that has been considered is that of listing pointers
to triples rather than listing the triples themselves.
# Example: Let us use an array statements to list pointers to triples in the desired order.
Translate the following expression:
𝑎 ∗ −(𝑏 + 𝑐)
3
into a syntax table, three-address code and quadruple.