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 -

Intermediate Code Generation in Compiler Design

In this article, we will learn more about Intermediate Code Generation in Compiler Design in detail.

Intermediate code generation

The above picture reflects the crucial segments of machine-independent and machine-dependent. All these 6 segments are very important in compiler design.

Table of Contents

Intermediate Code Generation in Compiler Design

Source code can change or translate into machine code. But, we also need intermediate code. Now the question here is, why do we need intermediate code? Or advantages of having intermediate code.

Video on Intermediate Code Generation in Compiler Design

Advantages of Intermediate Code

  • We need intermediate code because if we don’t have the option to create intermediate code, we require a native compiler for each new machine.
  • It becomes very easy to apply source code changes to enhance the implementation by optimising the intermediate code.
  • Intermediate code supports eliminating the requirement of a new complete compiler for every individual machine by upholding the same analysis part for all the compilers.
  • It becomes more effortless to use the source code transformations to enhance code implementation by using code optimisation strategies on the intermediate code.

Intermediate Representation

Intermediate codes can be defined in a combination of ways, and they have their perks.

    • High Level IR: The expression of high-level intermediate code is relatively identical to the source language. They are easy, uncomplicated and straightforward to build from source code. Also, we can efficiently update the code to improve execution.
  • Low Level IR – Low level IR is close to the target machine hence suitable for memory distribution, instruction group selection, and register. In short we can say that it is efficient for machine-dependent optimisation.

Three Address Code

Statements with no more than three references are considered to be three address statements. A set of three address claims is represented by three address codes. The addresses x, y, and z will be contained in three address entries of the type x = y op z. (memory location). A statement is still referred to as a three address statement even if it only has two or one reference.

Example – The three address code for the expression a + b * c + d:

T 1 = b * c

T 2 = a + T 1

T 3 = T 2 + d

T 1 , T 2 , T 3 are temporary variables.

You can explore more about three address codes to get a better understanding.

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. Consider the following intermediate program in three address code.

p = a – b

q = p * c

p = u * v

q = p + q

Q4 Which one of the following corresponds to a static single assignment form of the above code?

A) p1 = a – b

q1 = p1 * c

p1 = u * c

q1 = p1 + q1

B) p3 = a – b

q4 = p3 * c

p4 = u * c

q5 = p4 + q4

C) p1 = a – b

q1 = p2 * c

p3 = u * c

q2 = p4 + q3

D) p1 = a – b

q1 = p * c

p2 = u * c

q2 = p + q

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

Also Explore,