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

What's hot (20)

PPT
Memory Addressing
chauhankapil
 
DOCX
Features of c language 1
srmohan06
 
PPTX
Basic blocks and control flow graphs
Tilakpoudel2
 
PPT
1 - Introduction to Compilers.ppt
Rakesh Kumar
 
PPTX
Mathematical Analysis of Non-Recursive Algorithm.
mohanrathod18
 
PPT
Lecture 1 - Lexical Analysis.ppt
NderituGichuki1
 
PPTX
Bootstrapping in Compiler
Akhil Kaushik
 
PPTX
Syntax Analysis in Compiler Design
MAHASREEM
 
PPT
Bitwise operators
Puneet Rajput
 
PDF
Infix to postfix expression in ds
Rohini Mahajan
 
PPTX
Lexical analyzer generator lex
Anusuya123
 
PDF
hardwired control unit ppt
SushmithaAcharya7
 
PPTX
Chapter 02 instructions language of the computer
Bảo Hoang
 
PPT
Assembler design options
Mohd Arif
 
DOCX
Cs6660 compiler design may june 2017 answer key
appasami
 
PPTX
Unit 1 computer architecture (1)
DevaKumari Vijay
 
PPTX
1.3 uso de tipos de datos abstractos
erwin_alexander
 
PPT
Introduction to Compiler
Radhakrishnan Chinnusamy
 
Memory Addressing
chauhankapil
 
Features of c language 1
srmohan06
 
Basic blocks and control flow graphs
Tilakpoudel2
 
1 - Introduction to Compilers.ppt
Rakesh Kumar
 
Mathematical Analysis of Non-Recursive Algorithm.
mohanrathod18
 
Lecture 1 - Lexical Analysis.ppt
NderituGichuki1
 
Bootstrapping in Compiler
Akhil Kaushik
 
Syntax Analysis in Compiler Design
MAHASREEM
 
Bitwise operators
Puneet Rajput
 
Infix to postfix expression in ds
Rohini Mahajan
 
Lexical analyzer generator lex
Anusuya123
 
hardwired control unit ppt
SushmithaAcharya7
 
Chapter 02 instructions language of the computer
Bảo Hoang
 
Assembler design options
Mohd Arif
 
Cs6660 compiler design may june 2017 answer key
appasami
 
Unit 1 computer architecture (1)
DevaKumari Vijay
 
1.3 uso de tipos de datos abstractos
erwin_alexander
 
Introduction to Compiler
Radhakrishnan Chinnusamy
 

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

PDF
Intermediate code generation in Compiler Design
Kuppusamy P
 
PDF
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
venkatapranaykumarGa
 
PPT
Intermediate code generation
Dr.DHANALAKSHMI SENTHILKUMAR
 
PPTX
UNIT - III Compiler.pptx power point presentation
KowsalyaG17
 
PPTX
Intermediate code
Vishal Agarwal
 
PPT
CC Week 11.ppt
KamranAli649587
 
PPTX
complier design unit 4 for helping students
aniketsugandhi1
 
PPTX
Intermediate code generator
sanchi29
 
PPTX
UNIT - III Compiler.pptx SYNTAX DIRECTED
KavithaNagendran1
 
DOC
Compiler notes--unit-iii
Sumathi Gnanasekaran
 
PPT
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
PPTX
Intermediate code- generation
rawan_z
 
PPTX
Chapter 6 - Intermediate Languages.pptxjfjgj
Shemse Shukre
 
PDF
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
Ranjeet Reddy
 
PDF
Intermediate code generation
Akshaya Arunan
 
PPT
Interm codegen
Anshul Sharma
 
PPTX
Intermediate code generator.pptx
gaurav728089
 
PPT
5_IntermediateCodeGeneration.ppt
HARIHARANMRA20110030
 
PPT
Lecture 21 22
Najmul Hassan
 
PPTX
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
ZulukhaniniTijani
 
Intermediate code generation in Compiler Design
Kuppusamy P
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
venkatapranaykumarGa
 
Intermediate code generation
Dr.DHANALAKSHMI SENTHILKUMAR
 
UNIT - III Compiler.pptx power point presentation
KowsalyaG17
 
Intermediate code
Vishal Agarwal
 
CC Week 11.ppt
KamranAli649587
 
complier design unit 4 for helping students
aniketsugandhi1
 
Intermediate code generator
sanchi29
 
UNIT - III Compiler.pptx SYNTAX DIRECTED
KavithaNagendran1
 
Compiler notes--unit-iii
Sumathi Gnanasekaran
 
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
Intermediate code- generation
rawan_z
 
Chapter 6 - Intermediate Languages.pptxjfjgj
Shemse Shukre
 
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
Ranjeet Reddy
 
Intermediate code generation
Akshaya Arunan
 
Interm codegen
Anshul Sharma
 
Intermediate code generator.pptx
gaurav728089
 
5_IntermediateCodeGeneration.ppt
HARIHARANMRA20110030
 
Lecture 21 22
Najmul Hassan
 
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
ZulukhaniniTijani
 
Ad

More from gadisaAdamu (20)

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

Recently uploaded (20)

PPTX
hall ppt 1 it for basic tamolet .pptx
ashishbehera64
 
PPTX
Elevating Elegance: Lobby Design Ideas by Germany’s Top Building Rendering Se...
Yantram Animation Studio Corporation
 
PDF
Uber Driver Hackday Sprint Solving Ride Cancellations
YellowSlice1
 
PDF
cs603 ppts .pdf 222222222222222222222222
RabiaNazneen1
 
PPT
1744066yaar kya hai ye bolo not nice 4.ppt
preethikavarsha
 
PPTX
Exploring Types of Rocks Educational Presentation rock forming james harold r...
jamescarllfelomino6
 
PPTX
DEVELOPING-PARAGRAPHS.pptx-developing...
rania680036
 
PDF
EY-emeia-fso-assurance-viewpoint-technology (1).pdf
INKPPT
 
PDF
Empowering Artisans Through Technology Karmakar App Concept
yellowslice2
 
PDF
ARC-101-B-4.pdfxxxxxxxxxxxxxxxxxxxxxxxxx
IzzyBaniquedBusto
 
PPTX
Bldg Mtc 8 Maintance documentation and audits - 25 (2).pptx
MwanamomoMpamba
 
PPTX
DSA_Algorithms_Prtestttttttttttttesentation.pptx
Kanchalkumar1
 
PPTX
Adobe Creative Cloud Cleaner Tool Crack Free Download Latest Version 2025
Slideshare
 
PDF
Presentation - Interior Design Concepts (2).pdf
vrindagrawal456
 
PPTX
Urban design is a huge concept when it comes to planning.
IshikaPanchal11
 
PPTX
Drjjdhdhhdjsjsjshshshhshshslecture 28.pptxfg
noorqaq25
 
PDF
AI Intervention in Design & Content Creation
YellowSlice1
 
PDF
🔴BUKTI KEMENANGAN HARI INI SELASA 08 JULI 2025 !!!🔴
GRAB
 
PPTX
unit 6 mgt.pptx on researchtoics can find
Arpit953319
 
PPTX
7psofmarketingandbranding-250114091831-cba08a7c (1).pptx
jamescarllfelomino6
 
hall ppt 1 it for basic tamolet .pptx
ashishbehera64
 
Elevating Elegance: Lobby Design Ideas by Germany’s Top Building Rendering Se...
Yantram Animation Studio Corporation
 
Uber Driver Hackday Sprint Solving Ride Cancellations
YellowSlice1
 
cs603 ppts .pdf 222222222222222222222222
RabiaNazneen1
 
1744066yaar kya hai ye bolo not nice 4.ppt
preethikavarsha
 
Exploring Types of Rocks Educational Presentation rock forming james harold r...
jamescarllfelomino6
 
DEVELOPING-PARAGRAPHS.pptx-developing...
rania680036
 
EY-emeia-fso-assurance-viewpoint-technology (1).pdf
INKPPT
 
Empowering Artisans Through Technology Karmakar App Concept
yellowslice2
 
ARC-101-B-4.pdfxxxxxxxxxxxxxxxxxxxxxxxxx
IzzyBaniquedBusto
 
Bldg Mtc 8 Maintance documentation and audits - 25 (2).pptx
MwanamomoMpamba
 
DSA_Algorithms_Prtestttttttttttttesentation.pptx
Kanchalkumar1
 
Adobe Creative Cloud Cleaner Tool Crack Free Download Latest Version 2025
Slideshare
 
Presentation - Interior Design Concepts (2).pdf
vrindagrawal456
 
Urban design is a huge concept when it comes to planning.
IshikaPanchal11
 
Drjjdhdhhdjsjsjshshshhshshslecture 28.pptxfg
noorqaq25
 
AI Intervention in Design & Content Creation
YellowSlice1
 
🔴BUKTI KEMENANGAN HARI INI SELASA 08 JULI 2025 !!!🔴
GRAB
 
unit 6 mgt.pptx on researchtoics can find
Arpit953319
 
7psofmarketingandbranding-250114091831-cba08a7c (1).pptx
jamescarllfelomino6
 

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