SlideShare a Scribd company logo
1 of 34
Download to read offline
INTERMEDIATE CODE
GENERATION
1
2Structure of a Compiler
 Front end of a compiler is efficient and can be
automated
 Back end is generally hard to automate and finding the
optimum solution requires exponential time
 Intermediate code generation can affect the
performance of the back end
Instruction
Selection
Instruction
Scheduling
Register
Allocation
Scanner Parser
Semantic
Analysis
Code
Optimization
Intermediate
Code
Generation
IR
Overview
 Goal: Generate a Machine Independent
Intermediate Form that is Suitable for Optimization
and Portability
 Facilitates retargeting: enables attaching a back
end for the new machine to an existing front end
 Enables machine-independent code optimization
3
4
Motivation
 What we have so far...
 A Parser tree
 With all the program information
 Known to be correct
 Well-typed
 Nothing missing
 No ambiguities
 What we need...
 Something “Executable”
 Closer to
 An operations schedule
 Actual machine level
5
What We Want
 A Representation that
 Is closer to actual machine
 Is easy to manipulate
 Is target neutral (hardware
independent)
 Can be interpreted
6
Intermediate Languages
 Syntax Tree.
 A syntax tree heiarachical structure of the
source program
 DAG more compact.
 Postfix Notation
 Linearized representation of a syntax tree.
 Edges do not appear explicitly. They can be
recovered.
 Three address code
 Control Flow Graphs (CFGs)
7
Recall ASTs and DAGs
 Intermediate Forms of a Program:
 ASTs: Abstract Syntax Trees
 DAGs: Directed Acyclic Graphs
 What is the Expression?
assign
a +
* *
b
c
uminusb
c
uminus
assign
a +
*
b
c
uminus
8
Representation
 Two Different Forms:
 Linked Data Structure
 Multi-Dimensional Array
9
10Abstract Syntax Trees (ASTs)
if (x < y)
x = 5*y + 5*y/3;
else
y = 5;
x = x+y;
Statements
<
AssignStmt
+
*
x
IfStmt
AssignStmt AssignStmt
x x y+ yxy
/
5 y 3*
5 y
5
11Directed Acyclic Graphs
(DAGs)
 Use directed acyclic graphs to represent expressions
 Use a unique node for each n
if (x < y)
x = 5*y + 5*y/3;
else
y = 5;
x = x+y;
Statements
<
AssignStmt
*
IfStmt
AssignStmt AssignStmt
x +y
/
5
3
12
Control Flow Graphs (CFGs)
 Nodes in the control flow graph are basic blocks
 A basic block is a sequence of statements
always entered at the beginning of the
block and exited at the end
 Edges in the control flow graph represent the control flow
CFG
if (x < y)
x = 5*y + 5*y/3;
else
y = 5;
x = x+y;
B1
if (x < y) goto B1 else goto B2
x = 5*y + 5*y/3 y = 5
x = x+y
B2
B0
B3
• Each block has a sequence of statements
• No jump from or to the middle of the block
• Once a block starts executing, it will execute till the end
13
Objective
 Directly Generate Code From AST or DAG as a Side Effect of Parsing
Process.
 Consider Code Below:
14
Each is Referred to as “3 Address Coding (3AC)”
since there are at Most 3 Addresses per Statement
One for Result and At Most 2 for Operands
15
The Intermediate Code
Generation Machine
 A Machine with
 Infinite number of temporaries
 Simple instructions
 3-operands
 Branching
 Calls with simple calling convention
 Simple code structure
 Array of instructions
 Labels to define targets of branches.
16
Temporaries
 The machine has an infinite number of temporaries
 Call them t0, t1, t2, ....
 Temporaries can hold values of any type
 The type of the temporary is derived from the
generation
 Temporaries go out of scope with the function
they are in
17
What is Three-Address
Coding?
 A simple type of instruction
 3 / 2 Operands x,y,z
 Each operand could be
 A literal
 A variable
 A temporary
 Example
x := y op z
x + y * z
t0 := y * z
t1 := x + t0
x := op z
18
Types of Three Address
Statements
 Assignment Statements of Form:
 X := Y op Z
 op is a Binary Arithmetic or Logical Operation
 Assignment Instructions of Form:
 X := op Y
 op is Unary Operation such as Unary Minus, Logical
Negative, Shift/Conversion Operations
 Copy Statements of Form:
 X := Y where value of Y assigned to X
 Unconditional Jump of Form:
 goto L which goes to a three address statement labeled
with L
19
Types of Three Address
Statements
 Conditional Jumps of Form:
 if x relop y goto L
 with relop as relational operators and the goto executed if
the x relop y is true
 Parameter Operations of Form:
 param a (a parameter of function)
 call p, n (call function p with n parameters)
 return y (return value y from function – optional)
 param a
 param b
 param c
 call p, 3
20
Types of Three Address
Statements
 Indexed Assignments of Form:
 X := Y[i] (Set X to i-th memory location of Y)
 X[i] := Y (Set i-th memory location of X to Y)
 Note the limit of 3 Addresses (X, Y, i)
 Cannot do: x[i] := y[j]; (4 addresses!)
 Address and Pointer Assignments of Form:
 X := & Y (X set to the Address of Y)
 X := * Y (X set to the contents pointed to by Y)
 * X := Y (Contents of X set to Value of Y)
21
Three Address Code
Representations
 Data structures for representation of TAC can be objects or
records with fields for operator and operands.
Representations include quadruples, triples and indirect
triples.
22
Quadruples
 In the quadruple representation, there are four
fields for each instruction: op, arg1, arg2, result
 Binary ops have the obvious representation
 Unary ops don’t use arg2
 Operators like param don’t use either arg2 or
result
 Jumps put the target label into result
 The quadruples in Fig (b) implement the three-
address code in (a) for the expression a = b * - c +
b * - c
23
Quadruples 24
Triples
 A triple has only three fields for each instruction:op,arg1,
arg2
 The result of an operation x op y is referred to by its
position.
 Triples are equivalent to signatures of nodes in DAG or
syntax trees.
 Triples and DAGs are equivalent representations only for
expressions; they are not equivalent for control flow.
 Ternary operations like x[i] = y requires two entries in the
triple structure, similarly for x = y[i].
 Moving around an instruction during optimization is a
problem
25
Representations of a = (b* - c) +
(b* – c)
26
Indirect Triples
 These consist of a listing of pointers to triples, rather than a listing of
the triples themselves.
 An optimizing compiler can move an instruction by reordering the
instruction list, without affecting the triples themselves.
27
28
Attribute Grammar for
Assignments
 Concepts:
 Need to Introduce Temporary Variables as
Necessary to Decompose Assignment Statement
 Every Generated Line of Code Must have at Most
3 Addresses!
29
Declarations
 Stack Utilized during Procedure/Function Calls to
 Allocate Space for Variables
 This now Includes Temporaries for 3AC
 We need to Track
 Name
 Type (Int, real, boolean, etc.)
 Offset (with respect to some relative address)
 Function
 enter (name, type, offset) creates symbol table entry
 offset global initially 0
30
Storage Layout for Local
Names
 The type and relative address are saved in the symbol-table
entry for the name .
 The width of a type is the number of storage units needed for
objects of that type.
 type
31
32Code Generation for Boolean
Expressions
 Two approaches
 Numerical representation
 Implicit representation
 Numerical representation
 Use 1 to represent true, use 0 to represent
false
 For three-address code store this result in a
temporary
 For stack machine code store this result in the
stack
Code Generation for Boolean
Expressions
 Implicit representation
 For the boolean expressions which are used
in flow-of-control statements (such as if-
statements, while-statements etc.) boolean
expressions do not have to explicitly
compute a value, they just need to branch
to the right instruction
 Generate code for boolean expressions
which branch to the appropriate instruction
based on the result of the boolean
expression
33
Generated Code
 Consider: a < b or c <
d and e < f
100: if a< b goto 103
101: t1:=0
102: goto 104
103: t1:=1
104: if c< d goto 107
105: t2:=0
106: goto 108
107: t2 := 1
108: if e< f goto 111
109: t3 := 0
110: goto 112
111: t3:=1
112: t4:=t2 and t3
113: t5:=t1 or t4
34

More Related Content

What's hot

Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Code Optimization
Code OptimizationCode Optimization
Code Optimizationguest9f8315
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler designhmnasim15
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environmentIffat Anjum
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocksJothi Lakshmi
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler DesignKuppusamy P
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 

What's hot (20)

Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocks
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Loop optimization
Loop optimizationLoop optimization
Loop optimization
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 

Similar to 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 materialgadisaAdamu
 
Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfRAnwarpasha
 
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
 
Three address code generation
Three address code generationThree address code generation
Three address code generationRabin BK
 
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
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1AmIt Prasad
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representationsahmed51236
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxLikhil181
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxKrishanPalSingh39
 
Microcontroller lec 3
Microcontroller  lec 3Microcontroller  lec 3
Microcontroller lec 3Ibrahim Reda
 
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptxganeshkarthy
 

Similar to Intermediate code generation (20)

Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course material
 
Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
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...
 
Three address code generation
Three address code generationThree address code generation
Three address code generation
 
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...
 
Compiler notes--unit-iii
Compiler notes--unit-iiiCompiler notes--unit-iii
Compiler notes--unit-iii
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
Lecture 21 22
Lecture 21 22Lecture 21 22
Lecture 21 22
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Ch02
Ch02Ch02
Ch02
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representations
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
Microcontroller lec 3
Microcontroller  lec 3Microcontroller  lec 3
Microcontroller lec 3
 
Programming
ProgrammingProgramming
Programming
 
ICP - Lecture 6
ICP - Lecture 6ICP - Lecture 6
ICP - Lecture 6
 
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
 

More from Akshaya Arunan

Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined NetworkTraffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined NetworkAkshaya Arunan
 
Enhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDNEnhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDNAkshaya Arunan
 
OpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingOpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingAkshaya Arunan
 
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSISA TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSISAkshaya Arunan
 

More from Akshaya Arunan (9)

Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined NetworkTraffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
 
Enhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDNEnhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDN
 
Akshayappt
AkshayapptAkshayappt
Akshayappt
 
OpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingOpenSec Policy-Based Security Using
OpenSec Policy-Based Security Using
 
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSISA TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Recently uploaded (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Intermediate code generation

  • 2. 2Structure of a Compiler  Front end of a compiler is efficient and can be automated  Back end is generally hard to automate and finding the optimum solution requires exponential time  Intermediate code generation can affect the performance of the back end Instruction Selection Instruction Scheduling Register Allocation Scanner Parser Semantic Analysis Code Optimization Intermediate Code Generation IR
  • 3. Overview  Goal: Generate a Machine Independent Intermediate Form that is Suitable for Optimization and Portability  Facilitates retargeting: enables attaching a back end for the new machine to an existing front end  Enables machine-independent code optimization 3
  • 4. 4
  • 5. Motivation  What we have so far...  A Parser tree  With all the program information  Known to be correct  Well-typed  Nothing missing  No ambiguities  What we need...  Something “Executable”  Closer to  An operations schedule  Actual machine level 5
  • 6. What We Want  A Representation that  Is closer to actual machine  Is easy to manipulate  Is target neutral (hardware independent)  Can be interpreted 6
  • 7. Intermediate Languages  Syntax Tree.  A syntax tree heiarachical structure of the source program  DAG more compact.  Postfix Notation  Linearized representation of a syntax tree.  Edges do not appear explicitly. They can be recovered.  Three address code  Control Flow Graphs (CFGs) 7
  • 8. Recall ASTs and DAGs  Intermediate Forms of a Program:  ASTs: Abstract Syntax Trees  DAGs: Directed Acyclic Graphs  What is the Expression? assign a + * * b c uminusb c uminus assign a + * b c uminus 8
  • 9. Representation  Two Different Forms:  Linked Data Structure  Multi-Dimensional Array 9
  • 10. 10Abstract Syntax Trees (ASTs) if (x < y) x = 5*y + 5*y/3; else y = 5; x = x+y; Statements < AssignStmt + * x IfStmt AssignStmt AssignStmt x x y+ yxy / 5 y 3* 5 y 5
  • 11. 11Directed Acyclic Graphs (DAGs)  Use directed acyclic graphs to represent expressions  Use a unique node for each n if (x < y) x = 5*y + 5*y/3; else y = 5; x = x+y; Statements < AssignStmt * IfStmt AssignStmt AssignStmt x +y / 5 3
  • 12. 12 Control Flow Graphs (CFGs)  Nodes in the control flow graph are basic blocks  A basic block is a sequence of statements always entered at the beginning of the block and exited at the end  Edges in the control flow graph represent the control flow
  • 13. CFG if (x < y) x = 5*y + 5*y/3; else y = 5; x = x+y; B1 if (x < y) goto B1 else goto B2 x = 5*y + 5*y/3 y = 5 x = x+y B2 B0 B3 • Each block has a sequence of statements • No jump from or to the middle of the block • Once a block starts executing, it will execute till the end 13
  • 14. Objective  Directly Generate Code From AST or DAG as a Side Effect of Parsing Process.  Consider Code Below: 14
  • 15. Each is Referred to as “3 Address Coding (3AC)” since there are at Most 3 Addresses per Statement One for Result and At Most 2 for Operands 15
  • 16. The Intermediate Code Generation Machine  A Machine with  Infinite number of temporaries  Simple instructions  3-operands  Branching  Calls with simple calling convention  Simple code structure  Array of instructions  Labels to define targets of branches. 16
  • 17. Temporaries  The machine has an infinite number of temporaries  Call them t0, t1, t2, ....  Temporaries can hold values of any type  The type of the temporary is derived from the generation  Temporaries go out of scope with the function they are in 17
  • 18. What is Three-Address Coding?  A simple type of instruction  3 / 2 Operands x,y,z  Each operand could be  A literal  A variable  A temporary  Example x := y op z x + y * z t0 := y * z t1 := x + t0 x := op z 18
  • 19. Types of Three Address Statements  Assignment Statements of Form:  X := Y op Z  op is a Binary Arithmetic or Logical Operation  Assignment Instructions of Form:  X := op Y  op is Unary Operation such as Unary Minus, Logical Negative, Shift/Conversion Operations  Copy Statements of Form:  X := Y where value of Y assigned to X  Unconditional Jump of Form:  goto L which goes to a three address statement labeled with L 19
  • 20. Types of Three Address Statements  Conditional Jumps of Form:  if x relop y goto L  with relop as relational operators and the goto executed if the x relop y is true  Parameter Operations of Form:  param a (a parameter of function)  call p, n (call function p with n parameters)  return y (return value y from function – optional)  param a  param b  param c  call p, 3 20
  • 21. Types of Three Address Statements  Indexed Assignments of Form:  X := Y[i] (Set X to i-th memory location of Y)  X[i] := Y (Set i-th memory location of X to Y)  Note the limit of 3 Addresses (X, Y, i)  Cannot do: x[i] := y[j]; (4 addresses!)  Address and Pointer Assignments of Form:  X := & Y (X set to the Address of Y)  X := * Y (X set to the contents pointed to by Y)  * X := Y (Contents of X set to Value of Y) 21
  • 22. Three Address Code Representations  Data structures for representation of TAC can be objects or records with fields for operator and operands. Representations include quadruples, triples and indirect triples. 22
  • 23. Quadruples  In the quadruple representation, there are four fields for each instruction: op, arg1, arg2, result  Binary ops have the obvious representation  Unary ops don’t use arg2  Operators like param don’t use either arg2 or result  Jumps put the target label into result  The quadruples in Fig (b) implement the three- address code in (a) for the expression a = b * - c + b * - c 23
  • 25. Triples  A triple has only three fields for each instruction:op,arg1, arg2  The result of an operation x op y is referred to by its position.  Triples are equivalent to signatures of nodes in DAG or syntax trees.  Triples and DAGs are equivalent representations only for expressions; they are not equivalent for control flow.  Ternary operations like x[i] = y requires two entries in the triple structure, similarly for x = y[i].  Moving around an instruction during optimization is a problem 25
  • 26. Representations of a = (b* - c) + (b* – c) 26
  • 27. Indirect Triples  These consist of a listing of pointers to triples, rather than a listing of the triples themselves.  An optimizing compiler can move an instruction by reordering the instruction list, without affecting the triples themselves. 27
  • 28. 28
  • 29. Attribute Grammar for Assignments  Concepts:  Need to Introduce Temporary Variables as Necessary to Decompose Assignment Statement  Every Generated Line of Code Must have at Most 3 Addresses! 29
  • 30. Declarations  Stack Utilized during Procedure/Function Calls to  Allocate Space for Variables  This now Includes Temporaries for 3AC  We need to Track  Name  Type (Int, real, boolean, etc.)  Offset (with respect to some relative address)  Function  enter (name, type, offset) creates symbol table entry  offset global initially 0 30
  • 31. Storage Layout for Local Names  The type and relative address are saved in the symbol-table entry for the name .  The width of a type is the number of storage units needed for objects of that type.  type 31
  • 32. 32Code Generation for Boolean Expressions  Two approaches  Numerical representation  Implicit representation  Numerical representation  Use 1 to represent true, use 0 to represent false  For three-address code store this result in a temporary  For stack machine code store this result in the stack
  • 33. Code Generation for Boolean Expressions  Implicit representation  For the boolean expressions which are used in flow-of-control statements (such as if- statements, while-statements etc.) boolean expressions do not have to explicitly compute a value, they just need to branch to the right instruction  Generate code for boolean expressions which branch to the appropriate instruction based on the result of the boolean expression 33
  • 34. Generated Code  Consider: a < b or c < d and e < f 100: if a< b goto 103 101: t1:=0 102: goto 104 103: t1:=1 104: if c< d goto 107 105: t2:=0 106: goto 108 107: t2 := 1 108: if e< f goto 111 109: t3 := 0 110: goto 112 111: t3:=1 112: t4:=t2 and t3 113: t5:=t1 or t4 34