Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Three Address Code in Compiler Design

Three address code

Table of Content:

Three Address Code

Three address code is a sort of intermediate code that is simple to create and convert to machine code. It can only define an expression with three addresses and one operator. Basically, the three address codes help in determining the sequence in which operations are actioned by the compiler.

Pointers for Three Address Code

  • Three-address code is considered as an intermediate code and utilised by optimising compilers.
  • In the three-address code, the given expression is broken down into multiple guidelines. These instructions translate to assembly language with ease.
  • Three operands are required for each of the three address code instructions. It’s a binary operator and an assignment combined.

General Representation

p := (-r * q) + (-r * s)

Three-address code is as follows:

t1 := -r

t2 := q*t1

t3 := -r

t4 := s * t3

t5 := t2 + t4

p := t5

Here, t is used as registers in the target program.

Implementation of Three Address Code

There are 2 representations of three address codes, namely

  1. Quadruple
  2. Triples

1. Quadruple

To implement the three address codes, the quadruples have four fields. The name of the operator, the first source operand, the second source operand, and the result are all contained in the quadruple field.

Quadruples field

Operator
Source 1
Source 2
Destination

Example:

p := -q * r + s

Three-address code is as follows:

t1 := -q

t2 := r + s

t3 := t1 * t2

p := t3

2. Triples

To implement the three address codes, the triples have three fields. The name of the operator, the first source operand, and the second source operand are all contained in the field of triples.

Triples fields

Operator
Source 1
Source 2

Example – p := -q * r + s

Three address code is as follows:

t1 := -q t2 := r + sM t3 := t1 * t2 p := t3

Practice Problems

Q1. Consider the following code segment.

x = u – t;

y = x * v;

x = y + w;

y = t – z;

y = x * y;

The minimum number of total variables required to convert the above code segment to a static single assignment form is_________.

Q2. Consider the intermediate code given below.

(1) i = 1

(2) j = 1

(3) t1 = 5 * i

(4) t2 = t1+ j

(5) t3 = 4 * t2

(6) t4 = t3

(7) a[t4] = – 1

(8) j = j + 1

(9) if j < = 5 goto (3)

(10) i = i + 1

(11) if i < 5 goto (2)

The number of nodes and edges in the control-flow graph constructed for the above code, respectively, are

  • (A) 5 and 7
  • (B) 6 and 7
  • (C) 5 and 5
  • (D) 7 and 8

Q3. For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits.

t0 = i * 1024

t1 = j * 32

t2 = k * 4

t3 = t1 + t0

t4 = t3 + t2

t5 = X[t4]

Which one of the following statements about the source code for the C program is CORRECT?

(A) X is declared as “int X[32][32][8]”.

(B) X is declared as “int X[4][1024][32]”.

(C) X is declared as “char X[4][32][8]”.

(D) X is declared as “char X[32][16][2]”.

Also Explore,

Parsing in Compiler Design Notes for GATE | Difference Between Compiler and Interpreter | Runtime Environment in Compiler Design

Keep learning and stay tuned to get the latest updates on the GATE Exam along with Eligibility Criteria, GATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*