The Wayback Machine - https://web.archive.org/web/20261125231637/https://www.geeksforgeeks.org/compiler-design/three-address-code-compiler/

Three-Address Code in Compiler

Last Updated : 18 Nov, 2026

Three-Address Code (TAC) is an intermediate representation used in compiler design to simplify complex expressions into a sequence of simple instructions. Each instruction contains at most three address-like operands: two source operands and one destination.

  • Intermediate results are typically stored in temporary variables.
  • TAC makes the order of operations explicit.
  • It simplifies code optimization and target code generation.

Applications of Three-Address Code

  • Code Optimization: Simplifies program operations and enables optimization techniques such as constant folding, common subexpression elimination, and dead code elimination.
  • Code Generation: Serves as an intermediate representation for generating target machine instructions.
  • Program Analysis: Simplifies analysis of expressions, control flow, and data dependencies.
  • Language Translation: Can serve as an intermediate representation between source languages and target languages.

General Representation

Three-address code commonly uses the following instruction forms:

Instruction

Description

a = b

Assignment

a = op b

Unary operation

a = b op c

Binary operation

Here, a, b, and c can represent variables, constants, or compiler-generated temporary variables, while op represents an operator.

Examples of Three-Address Code

Example 1: Conversion of an Arithmetic Expression

Convert the expression a * -(b + c) into three-address code.

The expression is evaluated in the following order:

  • Evaluate b + c.
  • Apply unary minus to the result.
  • Multiply the result by a.

Three-address code:

t1 = b + c
t2 = -t1
t3 = a * t2

Example 2: Conversion of a Loop

Convert the following loop into three-address code:

for (i = 1; i <= 10; i++) {
a[i] = x * 5;
}

Three-address code:

i = 1

L1:
if i > 10 goto L2
t1 = x * 5
a[i] = t1
i = i + 1
goto L1

L2:

The loop condition is represented using a conditional jump, and the loop body is executed until i exceeds 10.

Representations of Three-Address Code

Three-address code can be represented using three common structures:

1. Quadruples

A quadruple consists of four fields:

  • op: Operator
  • arg1: First operand
  • arg2: Second operand
  • result: Destination of the operation

Quadruples explicitly store intermediate results using temporary variables, making instructions easier to rearrange during optimization.

Example: a = b * -c + b * -c

Three-address code:

t1 = -c
t2 = b * t1
t3 = -c
t4 = b * t3
t5 = t2 + t4
a = t5

Quadruple Representation:

op

arg1

arg2

result

uminus

c

—

t1

*

b

t1

t2

uminus

c

—

t3

*

b

t3

t4

+

t2

t4

t5

=

t5

—

a

The same expression is calculated twice in this example. An optimization such as common subexpression elimination could reduce the repeated computation.

2. Triples

Triples consist of three fields:

  • op: Operator
  • arg1: First operand
  • arg2: Second operand

Intermediate results are referred to by the position or index of the instruction that produces them, rather than using explicit temporary variables.

For the same expression:

Index

op

arg1

arg2

0

uminus

c

—

1

*

b

(0)

2

uminus

c

—

3

*

b

(2)

4

+

(1)

(3)

5

=

(4)

a

Here, (0), (1), and so on refer to the results of the corresponding triple instructions.

3. Indirect Triples

Indirect triples use a separate list of pointers or instruction references to specify the execution order of triples.

  • The triple instructions remain unchanged.
  • The pointer list determines the order in which instructions are executed.
  • Rearranging the pointer list makes code movement easier without modifying references within the triples.

This representation simplifies code rearrangement compared with directly rearranging triples.

Example: Quadruples, Triples, and Indirect Triples

Convert the expression:

(x+y)*(y+z)+(x+y+z)

Three-Address Code

t1 = x + y
t2 = y + z
t3 = t1 * t2
t4 = t1 + z
t5 = t3 + t4

This reuses t1 = x + y when computing the final expression.

Quadruple Representation

op

arg1

arg2

result

+

x

y

t1

+

y

z

t2

*

t1

t2

t3

+

t1

z

t4

+

t3

t4

t5

Triples Representation

Index

op

arg1

arg2

0

+

x

y

1

+

y

z

2

*

(0)

(1)

3

+

(0)

z

4

+

(2)

(3)

Indirect Triples

The triples remain the same, while the pointer list specifies their execution order.

Pointer

Triple Index

0

0

1

1

2

2

3

3

4

4

Process of Generating Three-Address Code

Source Code Analysis


  • Lexical Analysis: Converts source code into tokens.
  • Syntax Analysis: Checks grammatical structure and typically constructs a syntax tree or another structural representation.
  • Semantic Analysis: Checks semantic correctness, such as type compatibility and variable declarations.

Intermediate Code Generation

  • Traverses the syntax tree or another intermediate representation.
  • Breaks complex expressions into simpler instructions.
  • Uses temporary variables for intermediate results.
  • Generates instructions with at most three address-like operands.

Expression Translation

  • Arithmetic Expressions: Break complex expressions into simpler operations while preserving precedence.
  • Logical Expressions: May use conditional jumps or Boolean temporary values.
  • Assignments: Generate instructions to store computed results in variables.

Control Flow Translation

  • If Statements: Use conditional jumps and labels.
  • While Loops: Use labels and conditional/unconditional jumps.
  • For Loops: Translate initialization, condition, body, and increment into equivalent control-flow instructions.
  • Goto Statements: Represent unconditional jumps using labels.

Procedure and Function Translation

  • Parameter Passing: Generate instructions to pass arguments according to the calling convention.
  • Function Calls: Represent function invocation using call instructions.
  • Return Values: Generate return instructions with the required value.
Comment

Explore