SlideShare a Scribd company logo
Chapter six
Intermediate Code Generation
Rift valley University
Harar Campus
Overview
 Intermediate code is the interface between
front end and back end in a compiler.
 Ideally the details of source language are
confined to the front end and the details of
target machines to the back end (a machine
model)
 This intermediate code serves as a bridge
between the high-level source code and
the final machine code or another target
language.
3
Overview
 In a compiler, the front end translates source
program into an intermediate representation,
 and the back end generates the target code from
this intermediate representation.
 The use of a machine independent intermediate
code (IC) is:
 retargeting to another machine is facilitated
 the optimization can be done on the machine independent
code
4
Overview
 Intermediate representations span the gap between
the source and target languages:
 closer to target language;
 (more or less) machine independent;
 allows many optimizations to be done in a machine-independent way.
 Implementable via syntax directed translation, so
can be folded into the parsing process.
Intermediate Representations
 Decisions in IR design affect the speed and
efficiency of the compiler
 Some important IR properties
 Ease of generation
 Ease of manipulation
 Procedure size
 Level of abstraction
 The importance of different properties varies
between compilers
 Selecting an appropriate IR for a compiler is
critical
5
6
Types of Intermediate Languages
 High Level Representations (e.g., syntax trees):
 closer to the source language
 easy to generate from an input program
 code optimizations may not be straightforward.
 Low Level Representations (e.g., 3-address
code)
 closer to the target machine;
 easier for optimizations, final code generation;
Intermediate Code Generation
 Intermediate language can be many different languages, and the
designer of the compiler decides this intermediate language.
 Syntax tree can be used as an intermediate language.
 Postfix notation can be used as an intermediate language.
 Three-address code (Quadraples) can be used as an
intermediate language
 We will use three address to discuss intermediate code
generation.
 Three address are close to machine instructions, but they
are not actual machine instructions.
 Some programming languages have well defined intermediate
languages.
7
8
Syntax Trees
A syntax tree shows the structure of a program by abstracting
away irrelevant details from a parse tree.
 Each node represents a computation to be performed;
 The children of the node represents what that computation is
performed on.
 Syntax trees decouple parsing from subsequent
processing.
9
Syntax Trees: Example
Grammar :
E  E + T | T
T  T * F | F
F  ( E ) | id
Input: id + id * id
Parse tree:
Syntax tree:
10
Syntax Trees: Structure
 Expressions:
 leaves: identifiers or constants;
 internal nodes are labeled with operators;
 the children of a node are its operands.
 Statements:
 a node’s label indicates what kind of
statement it is;
 the children correspond to the components
of the statement.
Syntax Tree
 While parsing the input, a syntax tree can be constructed.
 A syntax tree (abstract tree) is a condensed form of parse tree
useful for representing language constructs.
 For example, for the string a + b, the parse tree in (a) below can
be represented by the syntax tree shown in (b);
 the keywords (syntactic sugar) that existed in the parse tree will
no longer exist in the syntax tree.
11
E
E
E
+
a b
Parse
tree
+
a b
Abstract
tree
Three-Address Code
 A three address code is: x := y op z
where x, y and z are names, constants or compiler-
generated temporaries; op is any operator.
 But we may also use the following notation for three
address code (much better notation because it looks like
a machine code instruction)
 op y, z, x apply operator op to y and z, and store the
result in x.
 We use the term “three-address code” because each
statement usually contains three addresses (two for
operands, one for the result).
12
Three-Address Code
13
a:= b * -c + b * -c
t1 := - c
t2 := b * t1
t3 := - c
t4 := b * t3
t5 := t2 + t4
a := t5
t1 := - c
t2 := b * t1
t5 := t2 + t2
a := t5
Postfix Notation
 Postfix notation is a linear representation of a syntax tree.
 In the postfix notation, any expression can be written
unambiguously without parentheses
 In postfix notation, the operator appears after the operands,
i.e., the operator between operands is taken out & is attached
after operands.
 Example : Translate a ∗ d − (b + c) into Postfix form.
Solution
ad ∗ bc + −
Three-Address Code…
 In three-address code:
 Only one operator at the right side of the
assignment is possible, i.e. x + y * z is not
possible.
 Similar to postfix notation, the three address
code is a linear representation of a syntax tree.
 It has been given the name three-address code
because such an instruction usually contains
three addresses (the two operands and the result)
t1 = y * z
t2 = x + t1 15
Data structures for three address codes
 Quadruples
 Has four fields: op, arg1, arg2 and result
 Triples
 Temporaries are not used and instead references to
instructions are made
 Indirect triples
 In addition to triples we use a list of pointers to triples
Example-1
 b * minus c + b * minus c
t1 = minus c
t2 = b * t1
t3 = minus c
t4 = b * t3
t5 = t2 + t4
a = t5
Three address code
minus
*
minus c t3
*
+
=
c t1
b t2
t1
b t4
t3
t2 t5
t4
t5 a
arg1 result
arg2
op
Quadruples
minus
*
minus c
*
+
=
c
b (0)
b (2)
(1) (3)
a
arg1 arg2
op
Triples
(4)
0
1
2
3
4
5
minus
*
minus c
*
+
=
c
b (0)
b (2)
(1) (3)
a
arg1 arg2
op
Indirect Triples
(4)
0
1
2
3
4
5
(0)
(1)
(2)
(3)
(4)
(5)
op
35
36
37
38
39
40
Example continued…
 Construct quadruple and triple for the following
equation:(a+b) * ( c+ d) - ( a+ b+ c) three Address code:
 a. Quadruple
b. triples
op arg1 arg2 result
+ a b t1
+ c d t2
* t1 t2 t3
+ t1 c t4
- t3 t4 t5 op arg1 arg2
+ a b
+ c d
* (0) (1)
+ (0) c
- (2) (3)

More Related Content

PPT
CS8461 - Design and Analysis of Algorithms
PPTX
ASP.NET Core
PPTX
Translation of expression(copmiler construction)
PDF
Modelling and evaluation
PPTX
Data preprocessing in Machine learning
PDF
Java 8 Lambda Expressions
PPTX
PHP FUNCTIONS
PDF
Python programming : Strings
CS8461 - Design and Analysis of Algorithms
ASP.NET Core
Translation of expression(copmiler construction)
Modelling and evaluation
Data preprocessing in Machine learning
Java 8 Lambda Expressions
PHP FUNCTIONS
Python programming : Strings

What's hot (20)

PPTX
Dynamic programming - fundamentals review
PPTX
OOP concepts -in-Python programming language
PDF
COMPILER DESIGN.pdf
PPTX
MongoDB - Aggregation Pipeline
PDF
file handling c++
PPT
Intermediate code generation
DOCX
Oops practical file
PPTX
Operators and expressions in C++
PPTX
PDF
Bayesian networks
PDF
Estructura de Datos, Multilistas
PPTX
K-Nearest Neighbor(KNN)
PPTX
Python programing
PPT
PPTX
Asp.net file types
PPTX
Delegates and events in C#
PPTX
K-means Clustering
PPTX
PPT
File handling in C++
Dynamic programming - fundamentals review
OOP concepts -in-Python programming language
COMPILER DESIGN.pdf
MongoDB - Aggregation Pipeline
file handling c++
Intermediate code generation
Oops practical file
Operators and expressions in C++
Bayesian networks
Estructura de Datos, Multilistas
K-Nearest Neighbor(KNN)
Python programing
Asp.net file types
Delegates and events in C#
K-means Clustering
File handling in C++
Ad

Similar to Compiler chapter six .ppt course material (20)

PDF
Chapter 11 - Intermediate Code Generation.pdf
PPTX
Intermediate code- generation
PPT
Intermediate code generation (Compiler Design)
PDF
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
PPTX
Intermediate code generator
PPTX
UNIT - III Compiler.pptx power point presentation
PPTX
Three address code In Compiler Design
PDF
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
PDF
Intermediate code generation
PPTX
3 address code ujjwal matoliya.pptx
PPTX
Syntax directed definition and intermediate code generation
PDF
The New Yorker cartoon premium membership of the
PPT
Chapter 6 intermediate code generation
PPTX
Intermediate code representations
PPT
Chapter 6 Intermediate Code Generation
DOCX
Cs6660 compiler design may june 2016 Answer Key
DOC
Compiler notes--unit-iii
PDF
C programming introduction for beginners.pdf
PPT
Lecture 21 22
PDF
Assignment12
Chapter 11 - Intermediate Code Generation.pdf
Intermediate code- generation
Intermediate code generation (Compiler Design)
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
Intermediate code generator
UNIT - III Compiler.pptx power point presentation
Three address code In Compiler Design
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
Intermediate code generation
3 address code ujjwal matoliya.pptx
Syntax directed definition and intermediate code generation
The New Yorker cartoon premium membership of the
Chapter 6 intermediate code generation
Intermediate code representations
Chapter 6 Intermediate Code Generation
Cs6660 compiler design may june 2016 Answer Key
Compiler notes--unit-iii
C programming introduction for beginners.pdf
Lecture 21 22
Assignment12
Ad

More from gadisaAdamu (20)

PDF
Addis ababa of education plan.docxJOSY 10 C.pdf
PDF
Addis ababa college of education plan.docxjosy 10 A.pdf
PPT
Lecture -3 Classification(Decision Tree).ppt
PPT
Lecture -2 Classification (Machine Learning Basic and kNN).ppt
PPT
Lecture -8 Classification(AdaBoost) .ppt
PPT
Lecture -10 AI Reinforcement Learning.ppt
PPTX
Updated Lensa Research Proposal (1).pptx
PPTX
Lensa research presentation Powepoint.pptx
PPTX
Lensa Habtamu Updated one Powerpoint.pptx
PPTX
Updated Lensa Research Proposal (1).pptx
PPTX
Lensa Updated research presentation Powerpoint.pptx
PPTX
AI Chapter Two.pArtificial Intelligence Chapter One.pptxptx
PPTX
Artificial Intelligence Chapter One.pptx
PPTX
Introduction to Embeded System chapter 1 and 2.pptx
PPT
Chapter Five Synchonization distributed Sytem.ppt
PPTX
Introduction to Embeded System chapter one and 2.pptx
PPT
chapter distributed System chapter 3 3.ppt
PPTX
Chapter 2- distributed system Communication.pptx
PPTX
Chapter 1-Introduction to distributed system.pptx
PPTX
chapter AI 4 Kowledge Based Agent.pptx
Addis ababa of education plan.docxJOSY 10 C.pdf
Addis ababa college of education plan.docxjosy 10 A.pdf
Lecture -3 Classification(Decision Tree).ppt
Lecture -2 Classification (Machine Learning Basic and kNN).ppt
Lecture -8 Classification(AdaBoost) .ppt
Lecture -10 AI Reinforcement Learning.ppt
Updated Lensa Research Proposal (1).pptx
Lensa research presentation Powepoint.pptx
Lensa Habtamu Updated one Powerpoint.pptx
Updated Lensa Research Proposal (1).pptx
Lensa Updated research presentation Powerpoint.pptx
AI Chapter Two.pArtificial Intelligence Chapter One.pptxptx
Artificial Intelligence Chapter One.pptx
Introduction to Embeded System chapter 1 and 2.pptx
Chapter Five Synchonization distributed Sytem.ppt
Introduction to Embeded System chapter one and 2.pptx
chapter distributed System chapter 3 3.ppt
Chapter 2- distributed system Communication.pptx
Chapter 1-Introduction to distributed system.pptx
chapter AI 4 Kowledge Based Agent.pptx

Recently uploaded (20)

PDF
GREEN BUILDING MATERIALS FOR SUISTAINABLE ARCHITECTURE AND BUILDING STUDY
PPT
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt
PDF
Urban Design Final Project-Site Analysis
PDF
Urban Design Final Project-Context
PPTX
BSCS lesson 3.pptxnbbjbb mnbkjbkbbkbbkjb
PDF
Facade & Landscape Lighting Techniques and Trends.pptx.pdf
PDF
Phone away, tabs closed: No multitasking
PDF
Quality Control Management for RMG, Level- 4, Certificate
PDF
Wio LTE JP Version v1.3b- 4G, Cat.1, Espruino Compatible\202001935, PCBA;Wio ...
PPTX
Tenders & Contracts Works _ Services Afzal.pptx
PDF
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
PDF
High-frequency high-voltage transformer outline drawing
PPTX
mahatma gandhi bus terminal in india Case Study.pptx
PPTX
Implications Existing phase plan and its feasibility.pptx
PPTX
Fundamental Principles of Visual Graphic Design.pptx
PDF
Trusted Executive Protection Services in Ontario — Discreet & Professional.pdf
PPT
Machine printing techniques and plangi dyeing
PDF
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
PPT
unit 1 ppt.ppthhhhhhhhhhhhhhhhhhhhhhhhhh
PPTX
6- Architecture design complete (1).pptx
GREEN BUILDING MATERIALS FOR SUISTAINABLE ARCHITECTURE AND BUILDING STUDY
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt
Urban Design Final Project-Site Analysis
Urban Design Final Project-Context
BSCS lesson 3.pptxnbbjbb mnbkjbkbbkbbkjb
Facade & Landscape Lighting Techniques and Trends.pptx.pdf
Phone away, tabs closed: No multitasking
Quality Control Management for RMG, Level- 4, Certificate
Wio LTE JP Version v1.3b- 4G, Cat.1, Espruino Compatible\202001935, PCBA;Wio ...
Tenders & Contracts Works _ Services Afzal.pptx
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
High-frequency high-voltage transformer outline drawing
mahatma gandhi bus terminal in india Case Study.pptx
Implications Existing phase plan and its feasibility.pptx
Fundamental Principles of Visual Graphic Design.pptx
Trusted Executive Protection Services in Ontario — Discreet & Professional.pdf
Machine printing techniques and plangi dyeing
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
unit 1 ppt.ppthhhhhhhhhhhhhhhhhhhhhhhhhh
6- Architecture design complete (1).pptx

Compiler chapter six .ppt course material

  • 1. Chapter six Intermediate Code Generation Rift valley University Harar Campus
  • 2. Overview  Intermediate code is the interface between front end and back end in a compiler.  Ideally the details of source language are confined to the front end and the details of target machines to the back end (a machine model)  This intermediate code serves as a bridge between the high-level source code and the final machine code or another target language.
  • 3. 3 Overview  In a compiler, the front end translates source program into an intermediate representation,  and the back end generates the target code from this intermediate representation.  The use of a machine independent intermediate code (IC) is:  retargeting to another machine is facilitated  the optimization can be done on the machine independent code
  • 4. 4 Overview  Intermediate representations span the gap between the source and target languages:  closer to target language;  (more or less) machine independent;  allows many optimizations to be done in a machine-independent way.  Implementable via syntax directed translation, so can be folded into the parsing process.
  • 5. Intermediate Representations  Decisions in IR design affect the speed and efficiency of the compiler  Some important IR properties  Ease of generation  Ease of manipulation  Procedure size  Level of abstraction  The importance of different properties varies between compilers  Selecting an appropriate IR for a compiler is critical 5
  • 6. 6 Types of Intermediate Languages  High Level Representations (e.g., syntax trees):  closer to the source language  easy to generate from an input program  code optimizations may not be straightforward.  Low Level Representations (e.g., 3-address code)  closer to the target machine;  easier for optimizations, final code generation;
  • 7. Intermediate Code Generation  Intermediate language can be many different languages, and the designer of the compiler decides this intermediate language.  Syntax tree can be used as an intermediate language.  Postfix notation can be used as an intermediate language.  Three-address code (Quadraples) can be used as an intermediate language  We will use three address to discuss intermediate code generation.  Three address are close to machine instructions, but they are not actual machine instructions.  Some programming languages have well defined intermediate languages. 7
  • 8. 8 Syntax Trees A syntax tree shows the structure of a program by abstracting away irrelevant details from a parse tree.  Each node represents a computation to be performed;  The children of the node represents what that computation is performed on.  Syntax trees decouple parsing from subsequent processing.
  • 9. 9 Syntax Trees: Example Grammar : E  E + T | T T  T * F | F F  ( E ) | id Input: id + id * id Parse tree: Syntax tree:
  • 10. 10 Syntax Trees: Structure  Expressions:  leaves: identifiers or constants;  internal nodes are labeled with operators;  the children of a node are its operands.  Statements:  a node’s label indicates what kind of statement it is;  the children correspond to the components of the statement.
  • 11. Syntax Tree  While parsing the input, a syntax tree can be constructed.  A syntax tree (abstract tree) is a condensed form of parse tree useful for representing language constructs.  For example, for the string a + b, the parse tree in (a) below can be represented by the syntax tree shown in (b);  the keywords (syntactic sugar) that existed in the parse tree will no longer exist in the syntax tree. 11 E E E + a b Parse tree + a b Abstract tree
  • 12. Three-Address Code  A three address code is: x := y op z where x, y and z are names, constants or compiler- generated temporaries; op is any operator.  But we may also use the following notation for three address code (much better notation because it looks like a machine code instruction)  op y, z, x apply operator op to y and z, and store the result in x.  We use the term “three-address code” because each statement usually contains three addresses (two for operands, one for the result). 12
  • 13. Three-Address Code 13 a:= b * -c + b * -c t1 := - c t2 := b * t1 t3 := - c t4 := b * t3 t5 := t2 + t4 a := t5 t1 := - c t2 := b * t1 t5 := t2 + t2 a := t5
  • 14. Postfix Notation  Postfix notation is a linear representation of a syntax tree.  In the postfix notation, any expression can be written unambiguously without parentheses  In postfix notation, the operator appears after the operands, i.e., the operator between operands is taken out & is attached after operands.  Example : Translate a ∗ d − (b + c) into Postfix form. Solution ad ∗ bc + −
  • 15. Three-Address Code…  In three-address code:  Only one operator at the right side of the assignment is possible, i.e. x + y * z is not possible.  Similar to postfix notation, the three address code is a linear representation of a syntax tree.  It has been given the name three-address code because such an instruction usually contains three addresses (the two operands and the result) t1 = y * z t2 = x + t1 15
  • 16. Data structures for three address codes  Quadruples  Has four fields: op, arg1, arg2 and result  Triples  Temporaries are not used and instead references to instructions are made  Indirect triples  In addition to triples we use a list of pointers to triples
  • 17. Example-1  b * minus c + b * minus c t1 = minus c t2 = b * t1 t3 = minus c t4 = b * t3 t5 = t2 + t4 a = t5 Three address code minus * minus c t3 * + = c t1 b t2 t1 b t4 t3 t2 t5 t4 t5 a arg1 result arg2 op Quadruples minus * minus c * + = c b (0) b (2) (1) (3) a arg1 arg2 op Triples (4) 0 1 2 3 4 5 minus * minus c * + = c b (0) b (2) (1) (3) a arg1 arg2 op Indirect Triples (4) 0 1 2 3 4 5 (0) (1) (2) (3) (4) (5) op 35 36 37 38 39 40
  • 18. Example continued…  Construct quadruple and triple for the following equation:(a+b) * ( c+ d) - ( a+ b+ c) three Address code:  a. Quadruple b. triples op arg1 arg2 result + a b t1 + c d t2 * t1 t2 t3 + t1 c t4 - t3 t4 t5 op arg1 arg2 + a b + c d * (0) (1) + (0) c - (2) (3)

Editor's Notes