0% found this document useful (0 votes)
70 views23 pages

006chapter 6 - Intermediate Code Generation

Uploaded by

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

006chapter 6 - Intermediate Code Generation

Uploaded by

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

Chapter- Six

Intermediate Code Generation


Topics to be Covered

 Introduction

 What is Intermediate code generation?

 Different Intermediate forms/Representation

 Different representation of Three address code


Introduction
 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?

 Let us see the reasons why we need an intermediate code.


Reasons why we need an Intermediate code
 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.
What is Intermediate Code generation?
 It is used to translate the source code into the machine code

 It lies between high-level language and low level language(machine language)

 In the analysis-synthesis model of a compiler the front end translates a source


program into an intermediate representation from which backend generates target
code.

i.e. Intermediate code is the interface between front end and back end in a compiler
What is Intermediate Code generation? …

 ICG receives input from its predecessors phase and semantic analyser phase.

 It takes input in the form of an annotated syntax tree.

 ICG- can be represented in two ways:

i. High-level IC-it can represented as source code

ii. Low level IC- it is close to the target machine


• it is used to machine dependent optimisation

 In this chapter we study intermediate representations, static type checking and


intermediate code generation
Different form of Intermediate code/Representation

i. Abstract Syntax tree and DAG (Directed Acyclic Graph )

ii. Polish Notation( Infix, prefix and postfix)

iii. Three Address code


i. Abstract Syntax Tree(AST) and DAG

 AST are more compact than a parse tree and can be easily used by compiler

 Nodes in a syntax tree represent constructs in the source program.

 the children of a node represent the meaningful components of a construct.

 A Directed Acyclic Graph (called a DAG) for an expression identifies the common

subexpressions (subexpressions that occur more than once) of the expression.


Directed Acyclic Graphs for Expressions
 It is sometimes beneficial to crate a DAG instead of parse tree for Expressions.

 This way we can easily show the common sub-expressions and then use that
knowledge during code generation.
 a DAG has leaves corresponding to atomic operands and interior codes
corresponding to operators.
 a node N in a DAG has more than one parent if N represents a common sub-
expression.
Example #1: DAG
 Shows the DAG for the expression.
ii. Polish Notation( Infix, prefix and postfix)
• Three types of polish notation:

• Infix-notation: operation between the operands e.g. a+b

• Prefix-notation: Operator before operands e.g. +ab

• Postfix-notation:

– It is also called suffix notation or reverse polish notation

– This is most natural way of representation in expression evaluation in compiler.

– In postfix notation the operands occurs first and then operators are arranged. E.g
ab+
Example #1: Postfix-Notation
• Consider the following string: a+b*c+d*e^f

• The postfix notation of the string is:


abc*+def^*+

Q. How to write post fix notation for any length of expression?


– to obtain the postfix notation evaluate the expression according to precedence of

operators
Example #1: Postfix-Notation- Solution
iii. Three address code
 Common intermediate representation of a program.

 Three address code is a sequence of statement of the general form a= b op c

 Where a,b,c are operands that can be names or constants and op stands for any
operator. Example #1: a=b+c*d
 Three address code for above expressions is:
t1=c*d
t2=b+t1
a=t2 where t1 and t2 are the temporary names generated by compiler
Note: In three-address code, there is at most one operator on the right side of an
instruction.( two for operands and one for result)
Three Address Code: Example #2

 Three-address code is a linearized representation of a syntax tree or a DAG in which


explicit names correspond to the interior nodes of the graph.
 For e.g. a+a*(b-c)+(b-c)*d
Forms of three address instructions

1. Assignment instructions of the form:

x = y op z

2. Assignments of the form:

x = op y, where op is a unary operation..

3. Copy instructions of the form:

x = y, where x is assigned the value of y.


Different Representation of three address codes

 There are three types of representation used for three address code:

i. Quadruples

ii. Triples

iii. Indirect triples


i. Quadruples
 A quadruple (or just "quad') has four fields, which we call op, arg1, arg2,and
result.
 The op field contains an internal code for the operator.

x=y+z placing + in op, y in arg1, z in arg2, and x


in result.

Note: Instructions with unary operators like x = minus y or x = y do not use arg2.

for a copy statement like x = y, op is =


Example: Quadruples
 Three-address code for the assignment a = b * - c + b * - c
ii. Triples

 A triple has only three fields, which we call op, arg1, and arg2

 we refer to the result of an operation x op y by its position, rather than by an

explicit temporary name.

 i.e. Temporaries are not used and instead references to instructions are made.
Example: Triples
 The syntax tree and triples in Fig.: correspond to the three-address code and
quadruples in Fig.:

 A benefit of quadruples over triples can be seen in an optimizing compiler, where


instructions are often moved around.
Indirect triples
 Indirect triples consist of a listing of pointers to triples, rather than a listing of triples
themselves.
 With indirect triples, an optimizing compiler can move an instruction by reordering
the instruction list, without affecting the triples themselves.
 Example #2: Assignment operators x=-a*b+-a+b

Figure : Indirect triples representation of three-address code


Readings Assignment

 Chapter 6 of the book

You might also like