Compi Desi CHP 05
Compi Desi CHP 05
Compiler Design
1
CHAPTER 5
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
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
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
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:
* 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.
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
18
End of Chapter
5
19