SlideShare a Scribd company logo
1 of 42
Chapter 6 
Intermediate Code Generation
Outline 
ī‚—Variants of Syntax Trees 
ī‚—Three-address code 
ī‚—Types and declarations 
ī‚—Translation of expressions 
ī‚—Type checking 
ī‚—Control flow 
ī‚—Backpatching
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 m*n model) 
ī‚—In this chapter we study intermediate 
representations, static type checking and 
intermediate code generation 
Parser Static 
Checker 
Intermediate 
Code Generator 
Code 
Generator 
Front end Back end
Variants of syntax trees 
ī‚—It is sometimes beneficial to crate a DAG instead of 
tree for Expressions. 
ī‚—This way we can easily show the common sub-expressions 
and then use that knowledge during code 
generation 
ī‚—Example: a+a*(b-c)+(b-c)*d 
+ 
+ * 
* 
- 
b c 
a 
d
SDD for creating DAG’s 
Production Semantic Rules 
1) E -> E1+T 
2) E -> E1-T 
3) E -> T 
4) T -> (E) 
5) T -> id 
6) T -> num 
E.node= new Node(‘+’, E1.node,T.node) 
E.node= new Node(‘-’, E1.node,T.node) 
E.node = T.node 
T.node = E.node 
T.node = new Leaf(id, id.entry) 
T.node = new Leaf(num, num.val) 
Example: 
1)p1=Leaf(id, entry-a) 
2)P2=Leaf(id, entry-a)=p1 
3)p3=Leaf(id, entry-b) 
4)p4=Leaf(id, entry-c) 
5)p5=Node(‘-’,p3,p4) 
6)p6=Node(‘*’,p1,p5) 
7)p7=Node(‘+’,p1,p6) 
8) p8=Leaf(id,entry-b)=p3 
9) p9=Leaf(id,entry-c)=p4 
10) p10=Node(‘-’,p3,p4)=p5 
11) p11=Leaf(id,entry-d) 
12) p12=Node(‘*’,p5,p11) 
13) p13=Node(‘+’,p7,p12)
Value-number method for 
constructing DAG’s 
= 
+ 
i 10 
ī‚—Algorithm 
ī‚—Search the array for a node M with label op, left child l 
and right child r 
ī‚—If there is such a node, return the value number M 
ī‚—If not create in the array a new node N with label op, 
left child l, and right child r and return its value 
ī‚—We may use a hash table 
id To entry for i 
num 10 
+ 1 2 
3 1 3
Three address code 
ī‚—In a three address code there is at most one operator 
at the right side of an instruction 
ī‚—Example: 
+ 
+ * 
* 
- 
b c 
a 
d 
t1 = b – c 
t2 = a * t1 
t3 = a + t2 
t4 = t1 * d 
t5 = t3 + t4
Forms of three address 
instructions 
ī‚—x = y op z 
ī‚—x = op y 
ī‚—x = y 
ī‚—goto L 
ī‚—if x goto L and ifFalse x goto L 
ī‚—if x relop y goto L 
ī‚—Procedure calls using: 
ī‚— param x 
ī‚— call p,n 
ī‚— y = call p,n 
ī‚—x = y[i] and x[i] = y 
ī‚—x = &y and x = *y and *x =y
Example 
ī‚—do i = i+1; while (a[i] < v); 
L: t1 = i + 1 
i = t1 
t2 = i * 8 
t3 = a[t2] 
if t3 < v goto L 
Symbolic labels 
100: t1 = i + 1 
101: i = t1 
102: t2 = i * 8 
103: t3 = a[t2] 
104: if t3 < v goto 100 
Position numbers
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 
ī‚—b * minus c + b * minus c 
Three address code 
t1 = minus c 
t2 = b * t1 
t3 = minus c 
t4 = b * t3 
t5 = t2 + t4 
a = t5 
Quadruples 
op arg1 arg2 result 
minus 
* minus c t3 
* 
+ 
= 
c t1 
b t1 t2 
b t3 t4 
t2 t4 t5 
t5 a 
Triples 
op arg1 arg2 
minus 
* minus c 
* 
+ 
= 
c 
b (0) 
b (2) 
(1) (3) 
a 
(4) 
012 
345 
Indirect Triples 
op arg1 arg2 
minus 
* minus c 
* 
+ 
= 
c 
b (0) 
b (2) 
(1) (3) 
a 
(4) 
012 
345 
op 
(0) 
(1) 
(2) 
(3) 
(4) 
(5) 
35 
36 
37 
38 
39 
40
Type Expressions 
Example: int[2][3] 
array(2,array(3,integer)) 
ī‚— A basic type is a type expression 
ī‚— A type name is a type expression 
ī‚— A type expression can be formed by applying the array type 
constructor to a number and a type expression. 
ī‚— A record is a data structure with named field 
ī‚— A type expression can be formed by using the type constructor ī§ for 
function types 
ī‚— If s and t are type expressions, then their Cartesian product s*t is a 
type expression 
ī‚— Type expressions may contain variables whose values are type 
expressions
Type Equivalence 
ī‚—They are the same basic type. 
ī‚—They are formed by applying the same constructor to 
structurally equivalent types. 
ī‚—One is a type name that denotes the other.
Declarations
Storage Layout for Local Names 
ī‚—Computing types and their widths
Storage Layout for Local Names 
ī‚—Syntax-directed translation of array types
Sequences of Declarations 
ī‚— 
ī‚—Actions at the end: 
ī‚—
Fields in Records and Classes 
ī‚— 
ī‚—
Translation of Expressions and 
Statements 
ī‚—We discussed how to find the types and offset of 
variables 
ī‚—We have therefore necessary preparations to discuss 
about translation to intermediate code 
ī‚—We also discuss the type checking
Three-address code for expressions
Incremental Translation
Addressing Array Elements 
ī‚—Layouts for a two-dimensional array:
Semantic actions for array reference
Translation of Array References 
Nonterminal L has three synthesized 
attributes: 
ī‚—L.addr 
ī‚—L.array 
ī‚—L.type
Conversions between primitive 
types in Java
Introducing type conversions into 
expression evaluation
Abstract syntax tree for the 
function definition 
fun length(x) = 
if null(x) then 0 else length(tl(x)+1) 
This is a polymorphic function 
in ML language
Inferring a type for the function length
Algorithm for Unification
Unification algorithm 
boolean unify (Node m, Node n) { 
s = find(m); t = find(n); 
if ( s = t ) return true; 
else if ( nodes s and t represent the same basic type ) return true; 
else if (s is an op-node with children s1 and s2 and 
t is an op-node with children t1 and t2) { 
union(s , t) ; 
return unify(s1, t1) and unify(s2, t2); 
} 
else if s or t represents a variable { 
union(s, t) ; 
return true; 
} 
else return false; 
}
Control Flow 
boolean expressions are often used to: 
ī‚—Alter the flow of control. 
ī‚—Compute logical values.
Short-Circuit Code 
ī‚— 
ī‚—
Flow-of-Control Statements
Syntax-directed definition
Generating three-address code for booleans
translation of a simple if-statement 
ī‚— 
ī‚—
Backpatching 
ī‚—Previous codes for Boolean expressions insert symbolic labels for 
jumps 
ī‚—It therefore needs a separate pass to set them to appropriate addresses 
ī‚—We can use a technique named backpatching to avoid this 
ī‚—We assume we save instructions into an array and labels will be 
indices in the array 
ī‚—For nonterminal B we use two attributes B.truelist and B.falselist 
together with following functions: 
ī‚—makelist(i): create a new list containing only I, an index into the array 
of instructions 
ī‚—Merge(p1,p2): concatenates the lists pointed by p1 and p2 and returns a 
pointer to the concatenated list 
ī‚—Backpatch(p,i): inserts i as the target label for each of the instruction 
on the list pointed to by p
Backpatching for Boolean Expressions 
ī‚— 
ī‚—
Backpatching for Boolean Expressions 
ī‚—Annotated parse tree for x < 100 || x > 200 && x ! = y
Flow-of-Control Statements
Translation of a switch-statement
Readings 
ī‚—Chapter 6 of the book

More Related Content

What's hot

Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design Dr. C.V. Suresh Babu
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphsTilakpoudel2
 
Code generation
Code generationCode generation
Code generationAparna Nayak
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeUnited International University
 
Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assemblerBansari Shah
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGJothi Lakshmi
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Operator precedence
Operator precedenceOperator precedence
Operator precedenceAkshaya Arunan
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTanzeela_Hussain
 
Intermediate code
Intermediate codeIntermediate code
Intermediate codeVishal Agarwal
 

What's hot (20)

Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Parsing
ParsingParsing
Parsing
 
Three Address code
Three Address code Three Address code
Three Address code
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphs
 
Code generation
Code generationCode generation
Code generation
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assembler
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Compiler design
Compiler designCompiler design
Compiler design
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Run time storage
Run time storageRun time storage
Run time storage
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Intermediate code
Intermediate codeIntermediate code
Intermediate code
 

Similar to Chapter 6 intermediate code generation

Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code GenerationRadhakrishnan Chinnusamy
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...venkatapranaykumarGa
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generatorsanchi29
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representationsahmed51236
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationAkshaya Arunan
 
Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course materialgadisaAdamu
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)Sourov Kumar Ron
 
Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfRAnwarpasha
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Saikrishna Tanguturu
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)bolovv
 
In the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxIn the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxmecklenburgstrelitzh
 
ch08.ppt
ch08.pptch08.ppt
ch08.pptNewsMogul
 

Similar to Chapter 6 intermediate code generation (20)

Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Assignment12
Assignment12Assignment12
Assignment12
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representations
 
Compiler notes--unit-iii
Compiler notes--unit-iiiCompiler notes--unit-iii
Compiler notes--unit-iii
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course material
 
Python
PythonPython
Python
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Ch8a
Ch8aCh8a
Ch8a
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)
 
Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdf
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
 
Python PPT2
Python PPT2Python PPT2
Python PPT2
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
 
In the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxIn the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docx
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
C pointers and references
C pointers and referencesC pointers and references
C pointers and references
 

More from Vipul Naik

Servlet viva questions
Servlet viva questionsServlet viva questions
Servlet viva questionsVipul Naik
 
Jsp viva questions
Jsp viva questionsJsp viva questions
Jsp viva questionsVipul Naik
 
Javascript viva questions
Javascript viva questionsJavascript viva questions
Javascript viva questionsVipul Naik
 
Xml viva questions
Xml viva questionsXml viva questions
Xml viva questionsVipul Naik
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for vivaVipul Naik
 
Html viva questions
Html viva questionsHtml viva questions
Html viva questionsVipul Naik
 

More from Vipul Naik (6)

Servlet viva questions
Servlet viva questionsServlet viva questions
Servlet viva questions
 
Jsp viva questions
Jsp viva questionsJsp viva questions
Jsp viva questions
 
Javascript viva questions
Javascript viva questionsJavascript viva questions
Javascript viva questions
 
Xml viva questions
Xml viva questionsXml viva questions
Xml viva questions
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for viva
 
Html viva questions
Html viva questionsHtml viva questions
Html viva questions
 

Recently uploaded

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 

Recently uploaded (20)

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 

Chapter 6 intermediate code generation

  • 1. Chapter 6 Intermediate Code Generation
  • 2. Outline ī‚—Variants of Syntax Trees ī‚—Three-address code ī‚—Types and declarations ī‚—Translation of expressions ī‚—Type checking ī‚—Control flow ī‚—Backpatching
  • 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 m*n model) ī‚—In this chapter we study intermediate representations, static type checking and intermediate code generation Parser Static Checker Intermediate Code Generator Code Generator Front end Back end
  • 4. Variants of syntax trees ī‚—It is sometimes beneficial to crate a DAG instead of tree for Expressions. ī‚—This way we can easily show the common sub-expressions and then use that knowledge during code generation ī‚—Example: a+a*(b-c)+(b-c)*d + + * * - b c a d
  • 5. SDD for creating DAG’s Production Semantic Rules 1) E -> E1+T 2) E -> E1-T 3) E -> T 4) T -> (E) 5) T -> id 6) T -> num E.node= new Node(‘+’, E1.node,T.node) E.node= new Node(‘-’, E1.node,T.node) E.node = T.node T.node = E.node T.node = new Leaf(id, id.entry) T.node = new Leaf(num, num.val) Example: 1)p1=Leaf(id, entry-a) 2)P2=Leaf(id, entry-a)=p1 3)p3=Leaf(id, entry-b) 4)p4=Leaf(id, entry-c) 5)p5=Node(‘-’,p3,p4) 6)p6=Node(‘*’,p1,p5) 7)p7=Node(‘+’,p1,p6) 8) p8=Leaf(id,entry-b)=p3 9) p9=Leaf(id,entry-c)=p4 10) p10=Node(‘-’,p3,p4)=p5 11) p11=Leaf(id,entry-d) 12) p12=Node(‘*’,p5,p11) 13) p13=Node(‘+’,p7,p12)
  • 6. Value-number method for constructing DAG’s = + i 10 ī‚—Algorithm ī‚—Search the array for a node M with label op, left child l and right child r ī‚—If there is such a node, return the value number M ī‚—If not create in the array a new node N with label op, left child l, and right child r and return its value ī‚—We may use a hash table id To entry for i num 10 + 1 2 3 1 3
  • 7. Three address code ī‚—In a three address code there is at most one operator at the right side of an instruction ī‚—Example: + + * * - b c a d t1 = b – c t2 = a * t1 t3 = a + t2 t4 = t1 * d t5 = t3 + t4
  • 8. Forms of three address instructions ī‚—x = y op z ī‚—x = op y ī‚—x = y ī‚—goto L ī‚—if x goto L and ifFalse x goto L ī‚—if x relop y goto L ī‚—Procedure calls using: ī‚— param x ī‚— call p,n ī‚— y = call p,n ī‚—x = y[i] and x[i] = y ī‚—x = &y and x = *y and *x =y
  • 9. Example ī‚—do i = i+1; while (a[i] < v); L: t1 = i + 1 i = t1 t2 = i * 8 t3 = a[t2] if t3 < v goto L Symbolic labels 100: t1 = i + 1 101: i = t1 102: t2 = i * 8 103: t3 = a[t2] 104: if t3 < v goto 100 Position numbers
  • 10. 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
  • 11. Example ī‚—b * minus c + b * minus c Three address code t1 = minus c t2 = b * t1 t3 = minus c t4 = b * t3 t5 = t2 + t4 a = t5 Quadruples op arg1 arg2 result minus * minus c t3 * + = c t1 b t1 t2 b t3 t4 t2 t4 t5 t5 a Triples op arg1 arg2 minus * minus c * + = c b (0) b (2) (1) (3) a (4) 012 345 Indirect Triples op arg1 arg2 minus * minus c * + = c b (0) b (2) (1) (3) a (4) 012 345 op (0) (1) (2) (3) (4) (5) 35 36 37 38 39 40
  • 12. Type Expressions Example: int[2][3] array(2,array(3,integer)) ī‚— A basic type is a type expression ī‚— A type name is a type expression ī‚— A type expression can be formed by applying the array type constructor to a number and a type expression. ī‚— A record is a data structure with named field ī‚— A type expression can be formed by using the type constructor ī§ for function types ī‚— If s and t are type expressions, then their Cartesian product s*t is a type expression ī‚— Type expressions may contain variables whose values are type expressions
  • 13. Type Equivalence ī‚—They are the same basic type. ī‚—They are formed by applying the same constructor to structurally equivalent types. ī‚—One is a type name that denotes the other.
  • 15. Storage Layout for Local Names ī‚—Computing types and their widths
  • 16. Storage Layout for Local Names ī‚—Syntax-directed translation of array types
  • 17. Sequences of Declarations ī‚— ī‚—Actions at the end: ī‚—
  • 18. Fields in Records and Classes ī‚— ī‚—
  • 19. Translation of Expressions and Statements ī‚—We discussed how to find the types and offset of variables ī‚—We have therefore necessary preparations to discuss about translation to intermediate code ī‚—We also discuss the type checking
  • 20. Three-address code for expressions
  • 22. Addressing Array Elements ī‚—Layouts for a two-dimensional array:
  • 23. Semantic actions for array reference
  • 24. Translation of Array References Nonterminal L has three synthesized attributes: ī‚—L.addr ī‚—L.array ī‚—L.type
  • 26. Introducing type conversions into expression evaluation
  • 27. Abstract syntax tree for the function definition fun length(x) = if null(x) then 0 else length(tl(x)+1) This is a polymorphic function in ML language
  • 28. Inferring a type for the function length
  • 30. Unification algorithm boolean unify (Node m, Node n) { s = find(m); t = find(n); if ( s = t ) return true; else if ( nodes s and t represent the same basic type ) return true; else if (s is an op-node with children s1 and s2 and t is an op-node with children t1 and t2) { union(s , t) ; return unify(s1, t1) and unify(s2, t2); } else if s or t represents a variable { union(s, t) ; return true; } else return false; }
  • 31. Control Flow boolean expressions are often used to: ī‚—Alter the flow of control. ī‚—Compute logical values.
  • 36. translation of a simple if-statement ī‚— ī‚—
  • 37. Backpatching ī‚—Previous codes for Boolean expressions insert symbolic labels for jumps ī‚—It therefore needs a separate pass to set them to appropriate addresses ī‚—We can use a technique named backpatching to avoid this ī‚—We assume we save instructions into an array and labels will be indices in the array ī‚—For nonterminal B we use two attributes B.truelist and B.falselist together with following functions: ī‚—makelist(i): create a new list containing only I, an index into the array of instructions ī‚—Merge(p1,p2): concatenates the lists pointed by p1 and p2 and returns a pointer to the concatenated list ī‚—Backpatch(p,i): inserts i as the target label for each of the instruction on the list pointed to by p
  • 38. Backpatching for Boolean Expressions ī‚— ī‚—
  • 39. Backpatching for Boolean Expressions ī‚—Annotated parse tree for x < 100 || x > 200 && x ! = y
  • 41. Translation of a switch-statement