SlideShare a Scribd company logo
Dr. Shaukat Ali
Chapter # 11
Inermediate Code Generation
Dr. Shaukat Ali
Department of Computer Science
University of Peshawar
Compiler Overview
Introduction
 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 source code can directly be translated into its target machine
code
 Why at all we need to translate the source code into an intermediate
code which is then translated to its target code?
Why 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
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
Why Intermediate Code?
Why Intermediate Code?
 While generating machine code directly from source code is
possible, it entails problems
 With m languages and n target machines, we need to write m
front ends, m x n optimizers, and m x n code generators
 The code optimizer which is one of the largest and very-
 The code optimizer which is one of the largest and very-
difficult-to-write components of a compiler, cannot be reused
 By converting source code to an intermediate code, a
machine-independent code optimizer may be written
 This means just m front ends, n code generators and 1
optimizer
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
enhance performance. But for target machine optimization, it is less
preferred
 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).
Intermediate Code Generation
 Intermediate code must be easy to produce and easy to translate
to machine code
 A sort of universal assembly language
 Should not contain any machine-specific parameters (registers,
addresses, etc.)
 Intermediate code is represented in three-address space but the
 Intermediate code is represented in three-address space but the
type of intermediate code implementation is based on the
compiler designer
 Quadruples, triples, indirect triples are the classical forms used for
machine-independent optimizations and machine code generation
 Static SingleAssignment form (SSA) is a recent form and enables
more effective optimizations
Three-Address Code
 Instructions are very simple : LHS is the target and the
RHS has at most two sources and one operator
 RHS sources can be either variables or constants
 Examples: a = b + c, x = -y, if a > b
Examples: a = b + c, x = -y, if a > b
goto L1
 Three-address code is a generic form and can be
implemented as quadruples, triples, indirect triples
 Example:The three-address code for (a+b*c)-
(d/(b*c)) is below
IR Code is Made From
 (a+b*c)- (d/(b*c))
 Intermediate Code
t1 = b * c
t2 = a + t1
t3 = d / t1
t4 = t2 – t3
Example
 The intermediate code produced from DAG is more
compact as compared AST
 a = b * (minus c) + b * (minus c)
Instructions in 3 – Address Space (1)
 Assignment instructions: a = b biop c, a = uop b,
and a = b (copy)
 Where
 biop is any binary arithmetic, logical, or relational operator
 uop is any unary arithmetic (++, --, conversion) or logical operator (!)
 Conversion operators are useful for converting integers to floating point
numbers, etc.
numbers, etc.
 Jump instructions:
goto L (unconditional jump to L),
if t goto L (it t is true then jump to L),
if a relop b goto L (jump to L if a relop b is true),
 where
 L is the label of the next three-address instruction to be executed
 t is a boolean variable
 a and b are either variables or constants
Instructions in 3 – Address Space (2)
 Functions:
func begin <name> (beginning of the function),
func end (end of a function),
param p (place a value parameter p on stack),
refparam p (place a reference parameter p on stack),
call f, n (call a function f with n parameters),
call f, n (call a function f with n parameters),
return (return from a function),
return a (return from a function with a value a)
 Indexed copy instructions:
a = b[i] (a is set to contents(baseaddress(b)+offset(i)),
where b is (usually) the base address of an array
a[i] = b (ith location of array a is set to b)
Instructions in 3 – Address Space (3)
 Pointer assignments:
a = &b (a is set to the address of b, i.e., a points to b)
*a = b (contents(contents(a)) is set to contents(b))
a = *b (a is set to contents(contents(b)))
Intermediate Code – Example 1
Intermediate Code – Example 2
Intermediate Code – Example 3
Intermediate Code – Example 4
Intermediate Code – Example 5
Data Structures for 3 – Address Code
 Quadruples
 Has four fields: op, arg1, arg2 and result
 Temporaries are used
 Triples
 Temporaries are not used and instead references to instructions
 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
Quadruples
 In quadruples representation, there are four fields for each
instruction: op, arg1, arg2, result
 Binary ops have the obvious representation
 Unary ops do not use arg2
 Operators like param does not use either arg2 result
 Operators like param does not use either arg2 result
 Jumps put the target label into the result
 The quadruples implement the three address space for the
expression
a = b * (-c) + b * (-c)
Quadruples
Triples
 Triples has only three fields for each instruction: op, arg1,
arg2
 The result of an operation x op y is referred by its position
 Triples are equivlant to signature of nodes in DAG or syntax
tree
tree
 Triples and DAG are equivalent representations only for
expressions
 Ternary operation like X[i] =Y requires two entries in the
triple structure; similarly forY = X[i]
Triples
Indirect Triples
 These consist of a listing of pointers to triples; rather than a
listing of the triples themselves
 The triples consists of three fields: op, arg1, arg2
 The arg1 or arg2 could be pointers
Example
 a = 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
IndirectTriples
(4)
0
1
2
3
4
5
(0)
(1)
(2)
(3)
(4)
(5)
op
35
36
37
38
39
40
Example 2
 End of Chapter # 11

More Related Content

Similar to Chapter 11 - Intermediate Code Generation.pdf (20)

PPTX
Intermediate code
Vishal Agarwal
 
PPTX
Central processing unit pptx for computer engineering
mihirpatani5
 
PPT
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
PPTX
UNIT - III Compiler.pptx SYNTAX DIRECTED
KavithaNagendran1
 
PPTX
Chapter 6 - Intermediate Languages.pptxjfjgj
Shemse Shukre
 
PDF
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
venkatapranaykumarGa
 
PPT
Chapter 6 intermediate code generation
Vipul Naik
 
PDF
The New Yorker cartoon premium membership of the
shubhamgupta7133
 
PPTX
Presentation(intermediate code generation)
Sourov Kumar Ron
 
PPTX
Programming basic computer
Martial Kouadio
 
PPTX
3 address code ujjwal matoliya.pptx
ujjwalmatoliya
 
PPTX
Syntax directed definition and intermediate code generation
JananiRamannachetty1
 
PPT
Compreport
xdarlord
 
PDF
1588147798Begining_ABUAD1.pdf
SemsemSameer1
 
PPTX
Lecture 12 intermediate code generation
Iffat Anjum
 
PPT
Advanced+pointers
Rubal Bansal
 
PPTX
complier design unit 4 for helping students
aniketsugandhi1
 
PPTX
Code optimization
veena venugopal
 
PPTX
Code optimization
veena venugopal
 
PDF
Assignment12
Sunita Milind Dol
 
Intermediate code
Vishal Agarwal
 
Central processing unit pptx for computer engineering
mihirpatani5
 
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
UNIT - III Compiler.pptx SYNTAX DIRECTED
KavithaNagendran1
 
Chapter 6 - Intermediate Languages.pptxjfjgj
Shemse Shukre
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
venkatapranaykumarGa
 
Chapter 6 intermediate code generation
Vipul Naik
 
The New Yorker cartoon premium membership of the
shubhamgupta7133
 
Presentation(intermediate code generation)
Sourov Kumar Ron
 
Programming basic computer
Martial Kouadio
 
3 address code ujjwal matoliya.pptx
ujjwalmatoliya
 
Syntax directed definition and intermediate code generation
JananiRamannachetty1
 
Compreport
xdarlord
 
1588147798Begining_ABUAD1.pdf
SemsemSameer1
 
Lecture 12 intermediate code generation
Iffat Anjum
 
Advanced+pointers
Rubal Bansal
 
complier design unit 4 for helping students
aniketsugandhi1
 
Code optimization
veena venugopal
 
Code optimization
veena venugopal
 
Assignment12
Sunita Milind Dol
 

Recently uploaded (20)

PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Ad

Chapter 11 - Intermediate Code Generation.pdf

  • 1. Dr. Shaukat Ali Chapter # 11 Inermediate Code Generation Dr. Shaukat Ali Department of Computer Science University of Peshawar
  • 3. Introduction  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 source code can directly be translated into its target machine code  Why at all we need to translate the source code into an intermediate code which is then translated to its target code?
  • 4. Why 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 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. Why Intermediate Code?  While generating machine code directly from source code is possible, it entails problems  With m languages and n target machines, we need to write m front ends, m x n optimizers, and m x n code generators  The code optimizer which is one of the largest and very-  The code optimizer which is one of the largest and very- difficult-to-write components of a compiler, cannot be reused  By converting source code to an intermediate code, a machine-independent code optimizer may be written  This means just m front ends, n code generators and 1 optimizer
  • 7. 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 enhance performance. But for target machine optimization, it is less preferred  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. Intermediate Code Generation  Intermediate code must be easy to produce and easy to translate to machine code  A sort of universal assembly language  Should not contain any machine-specific parameters (registers, addresses, etc.)  Intermediate code is represented in three-address space but the  Intermediate code is represented in three-address space but the type of intermediate code implementation is based on the compiler designer  Quadruples, triples, indirect triples are the classical forms used for machine-independent optimizations and machine code generation  Static SingleAssignment form (SSA) is a recent form and enables more effective optimizations
  • 9. Three-Address Code  Instructions are very simple : LHS is the target and the RHS has at most two sources and one operator  RHS sources can be either variables or constants  Examples: a = b + c, x = -y, if a > b Examples: a = b + c, x = -y, if a > b goto L1  Three-address code is a generic form and can be implemented as quadruples, triples, indirect triples  Example:The three-address code for (a+b*c)- (d/(b*c)) is below
  • 10. IR Code is Made From  (a+b*c)- (d/(b*c))  Intermediate Code t1 = b * c t2 = a + t1 t3 = d / t1 t4 = t2 – t3
  • 11. Example  The intermediate code produced from DAG is more compact as compared AST  a = b * (minus c) + b * (minus c)
  • 12. Instructions in 3 – Address Space (1)  Assignment instructions: a = b biop c, a = uop b, and a = b (copy)  Where  biop is any binary arithmetic, logical, or relational operator  uop is any unary arithmetic (++, --, conversion) or logical operator (!)  Conversion operators are useful for converting integers to floating point numbers, etc. numbers, etc.  Jump instructions: goto L (unconditional jump to L), if t goto L (it t is true then jump to L), if a relop b goto L (jump to L if a relop b is true),  where  L is the label of the next three-address instruction to be executed  t is a boolean variable  a and b are either variables or constants
  • 13. Instructions in 3 – Address Space (2)  Functions: func begin <name> (beginning of the function), func end (end of a function), param p (place a value parameter p on stack), refparam p (place a reference parameter p on stack), call f, n (call a function f with n parameters), call f, n (call a function f with n parameters), return (return from a function), return a (return from a function with a value a)  Indexed copy instructions: a = b[i] (a is set to contents(baseaddress(b)+offset(i)), where b is (usually) the base address of an array a[i] = b (ith location of array a is set to b)
  • 14. Instructions in 3 – Address Space (3)  Pointer assignments: a = &b (a is set to the address of b, i.e., a points to b) *a = b (contents(contents(a)) is set to contents(b)) a = *b (a is set to contents(contents(b)))
  • 20. Data Structures for 3 – Address Code  Quadruples  Has four fields: op, arg1, arg2 and result  Temporaries are used  Triples  Temporaries are not used and instead references to instructions  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
  • 21. Quadruples  In quadruples representation, there are four fields for each instruction: op, arg1, arg2, result  Binary ops have the obvious representation  Unary ops do not use arg2  Operators like param does not use either arg2 result  Operators like param does not use either arg2 result  Jumps put the target label into the result  The quadruples implement the three address space for the expression a = b * (-c) + b * (-c)
  • 23. Triples  Triples has only three fields for each instruction: op, arg1, arg2  The result of an operation x op y is referred by its position  Triples are equivlant to signature of nodes in DAG or syntax tree tree  Triples and DAG are equivalent representations only for expressions  Ternary operation like X[i] =Y requires two entries in the triple structure; similarly forY = X[i]
  • 25. Indirect Triples  These consist of a listing of pointers to triples; rather than a listing of the triples themselves  The triples consists of three fields: op, arg1, arg2  The arg1 or arg2 could be pointers
  • 26. Example  a = 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 IndirectTriples (4) 0 1 2 3 4 5 (0) (1) (2) (3) (4) (5) op 35 36 37 38 39 40
  • 28.  End of Chapter # 11