0% found this document useful (0 votes)
16 views19 pages

Compi Desi CHP 05

Chapter 5 of the Compiler Design course discusses Intermediate Code Generation, focusing on the role of Intermediate Representation (IR) in compilers. It explains the necessity of IR for optimizing code and facilitating machine independence, detailing types such as High-Level and Low-Level IR, as well as Three-Address Code. The chapter also covers Quadruples and Triples representations, along with the concept of Backpatching for managing label addresses in code generation.

Uploaded by

ephremd406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views19 pages

Compi Desi CHP 05

Chapter 5 of the Compiler Design course discusses Intermediate Code Generation, focusing on the role of Intermediate Representation (IR) in compilers. It explains the necessity of IR for optimizing code and facilitating machine independence, detailing types such as High-Level and Low-Level IR, as well as Three-Address Code. The chapter also covers Quadruples and Triples representations, along with the concept of Backpatching for managing label addresses in code generation.

Uploaded by

ephremd406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Course Name:

Compiler Design

1
CHAPTER 5

Intermediate – Code Generation

2
Intermediate Language
An intermediate representation (IR) is the data structure
or code used internally by a compiler or virtual machine
to represent source code.
An IR is designed to be conducive for further
processing, such as optimization and translation.
A language that is generated from programming source
code, but that cannot be directly executed by the CPU.
Also called "bytecode," "p-code," "pseudocode" or
"pseudo language”, the intermediate language (IL) is
platform independent.
It can be run in any computer environment that has a
runtime engine for the language.
3
Cont’d

4
Cont’d

A source code can directly be translated into its target


machine code, then why at all we need to translate the
source code into an intermediate code which is then
translated to its target code?

5
Cont’d
Let us see the reasons why we need an intermediate code:
 If a compiler translates the source language to its target machine language
without having the option for generating intermediate code, then for each
new machine, a full native compiler is required.
 Intermediate code eliminates the need of a new full compiler for every
unique machine by keeping the analysis portion same for all the compilers.
 The second part of compiler, synthesis, is changed according to the target
machine.
 It becomes easier to apply the source code modifications to improve code
performance by applying code optimization techniques on the intermediate
code.

6
Intermediate Representation
Intermediate codes can be represented in a variety of ways and
they have their own benefits.
 High Level IR:
 High-level intermediate code representation is very close to
the source language itself.
 They can be easily generated from the source code and we
can easily apply code modifications to enhance
performance.
 But for target machine optimization, it is less preferred.

7
Cont’d

 Low Level IR:


 This one is close to the target machine, which makes it
suitable for register and memory allocation, instruction set
selection, etc.
 It is good for machine-dependent optimizations.

 Intermediate code can be either language specific


(e.g., Byte Code for Java) or language independent
(three-address code).

8
Three-Address Code
 Intermediate code generator receives input from its
predecessor phase, semantic analyzer, in the form of an
annotated syntax tree.
 That syntax tree then can be converted into a linear
representation.
 e.g., postfix notation. Intermediate code tends to be machine
independent code.
 Therefore, code generator assumes to have unlimited
number of memory storage (register) to generate code.

9
Cont’d
 For example:
o a = b + c * d;
 The intermediate code generator will try to divide
this expression into sub-expressions and then
generate the corresponding code.
o r1 = c * d;
o r2 = b + r1;
o a = r2
 r being used as registers in the target program.

10
Three-Address Code

 A Three-address code has at most three


address locations to calculate the expression.
 A three-address code can be represented in two
forms:
1. Quadruples and
2. Triples.

11
1. Quadruples
 Each instruction in quadruples presentation is divided
into four fields: operator, arg1, arg2, and result.
 The above example(a = b + c * d) is represented below in
quadruples format:

Op arg1 arg2 result

* c d r1

+ b r1 r2

+ r2 r1 r3

= r3 a

12
…1. Quadruples
Advantage
 Easy to rearrange code for global optimization.
 One can quickly access value of temporary variables
using symbol table.
Disadvantage
 Contain lot of temporaries.
 Temporary variable creation increases time and space
complexity.

13
2. Triples
 Each instruction in triples presentation has three
fields:
 op,
 arg1, and
 arg2.
 The results of respective sub-expressions are denoted by
the position of expression.

? How (a = b + c * d) is represented in triples format?

14
Declarations
A variable or procedure has to be declared
before it can be used.
Declaration involves allocation of space in
memory and entry of type and name in the symbol
table.
A program may be coded and designed keeping
the target machine structure in mind, but it may
not always be possible to accurately convert a
source code to its target language.

15
Backpatching
What is Backpatching?
While generating three address codes for the
given expression, it can specify the address of the
Label in goto statements.
It is very difficult to assign locations of these label
statements in one pass so, two passes are used.
 In the first pass, it can leave these addresses unspecified and
 In the next pass it can fill these addresses.
 Therefore filling of incomplete transformation is called
Backpatching.
 Generally, Backpatching is a process of fulfilling
unspecified information. 16
Need for Backpatching

1. Boolean expression
2. Flow of control statements
3. Labels and goto statements

17
Applications of Backpatching

 Used to translate flow-of-control statements in one


pass itself.
 Used for producing quadruples for Boolean
expressions during bottom-up parsing.
 Used for filling up unspecified information of labels
during the code generation process.
 Used for to resolve forward branches that have been
planted in the code.

18
End of Chapter
5
19

You might also like