SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler offline
COMPILER DESIGN
UNIT-V
ANKUR SRIVASTAVA
ASSISTANT PROF.(CSE)
JETGI
31-Dec-16 1ANKUR SRIVASTAVA (CSE) JETGI
CONTENTS
Code Generation Issues
Target language Issues
Addresses in Target Code
Basic Blocks and Flow Graphs
Optimizations of Basic Blocks
A Simple Code Generator
Peephole optimization
Register allocation and assignment
Instruction selection by tree rewriting
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 2
INTRODUCTION
The final phase of a compiler is code generator
It receives an intermediate representation (IR) with supplementary information
in symbol table
Produces a semantically equivalent target program
Code generator main tasks:
Instruction selection
Register allocation and assignment
Instruction ordering
Front end Code optimizer
Code
Generator
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 3
ISSUES IN THE DESIGN OF CODE GENERATOR
The most important criterion is that it produces correct code
Input to the code generator
IR + Symbol table
We assume front end produces low-level IR, i.e. values of names in it can
be directly manipulated by the machine instructions.
Syntactic and semantic errors have been already detected
The target program
Common target architectures are: RISC, CISC and Stack based machines
In this chapter we use a very simple RISC-like computer with addition of
some CISC-like addressing modes
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 4
COMPLEXITY OF MAPPING
the level of the IR
the nature of the instruction-set architecture
the desired quality of the generated code.
x=y+z
LD R0, y
ADD R0, R0, z
ST x, R0
a=b+c
d=a+e
LD R0, b
ADD R0, R0, c
ST a, R0
LD R0, a
ADD R0, R0, e
ST d, R0
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 5
REGISTER ALLOCATION
Two sub-problems
Register allocation: selecting the set of variables that will reside in
registers at each point in the program
Resister assignment: selecting specific register that a variable reside in
Complications imposed by the hardware architecture
Example: register pairs for multiplication and division
t=a+b
t=t*c
T=t/d
t=a+b
t=t+c
T=t/d
L R1, a
A R1, b
M R0, c
D R0, d
ST R1, t
L R0, a
A R0, b
M R0, c
SRDA R0, 32
D R0, d
ST R1, t
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 6
A SIMPLE TARGET MACHINE MODEL
Load operations: LD r,x and LD r1, r2
Store operations: ST x,r
Computation operations: OP dst, src1, src2
Unconditional jumps: BR L
Conditional jumps: Bcond r, L like BLTZ r, L
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 7
ADDRESSING MODES
variable name: x
indexed address: a(r) like LD R1, a(R2) means R1=contents(a+contents(R2))
integer indexed by a register : like LD R1, 100(R2)
Indirect addressing mode: *r and *100(r)
immediate constant addressing mode: like LD R1, #100
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 8
ADDRESSES IN THE TARGET CODE
A statically determined area Code
A statically determined data area Static
A dynamically managed area Heap
A dynamically managed area Stack
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 9
THREE-ADDRESS STATEMENTS FOR PROCEDURE
CALLS AND RETURNS
call callee
Return
Halt
action
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 10
BASIC BLOCKS AND FLOW GRAPHS
Partition the intermediate code into basic blocks
The flow of control can only enter the basic block through the first
instruction in the block. That is, there are no jumps into the middle of the
block.
Control will leave the block without halting or branching, except possibly
at the last instruction in the block.
The basic blocks become the nodes of a flow graph
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 11
RULES FOR FINDING LEADERS
The first three-address instruction in the intermediate code is a leader.
Any instruction that is the target of a conditional or unconditional jump is a
leader.
Any instruction that immediately follows a conditional or unconditional jump
is a leader.
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 12
DAG REPRESENTATION OF BASIC BLOCKS
There is a node in the DAG for each of the initial values of the variables
appearing in the basic block.
There is a node N associated with each statement s within the block. The
children of N are those nodes corresponding to statements that are the last
definitions, prior to s, of the operands used by s.
Node N is labeled by the operator applied at s, and also attached to N is the list
of variables for which it is the last definition within the block.
Certain nodes are designated output nodes. These are the nodes whose
variables are live on exit from the block.
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 13
CODE IMPROVING TRANSFORMATIONS
We can eliminate local common subexpressions, that is, instructions that
compute a value that has already been computed.
We can eliminate dead code, that is, instructions that compute a value that is
never used.
We can reorder statements that do not depend on one another; such reordering
may reduce the time a temporary value needs to be preserved in a register.
We can apply algebraic laws to reorder operands of three-address instructions,
and sometimes t hereby simplify t he computation.
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 14
DAG FOR BASIC BLOCK
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 15
DAG for basic block
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 16
ARRAY ACCESSES IN A DAG
An assignment from an array, like x = a [i], is represented by creating a node
with operator =[] and two children representing the initial value of the array, a0
in this case, and the index i. Variable x becomes a label of this new node.
An assignment to an array, like a [j] = y, is represented by a new node with
operator []= and three children representing a0, j and y. There is no variable
labeling this node. What is different is that the creation of this node kills all
currently constructed nodes whose value depends on a0. A node that has been
killed cannot receive any more labels; that is, it cannot become a common
subexpression.
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 17
RULES FOR RECONSTRUCTING THE BASIC BLOCK
FROM A DAG
The order of instructions must respect the order of nodes in the DAG. That is,
we cannot compute a node's value until we have computed a value for each of
its children.
Assignments to an array must follow all previous assignments to, or
evaluations from, the same array, according to the order of these instructions in
the original basic block.
Evaluations of array elements must follow any previous (according to the
original block) assignments to the same array. The only permutation allowed is
that two evaluations from the same array may be done in either order, as long
as neither crosses over an assignment to that array.
Any use of a variable must follow all previous (according to the original
block) procedure calls or indirect assignments through a pointer.
Any procedure call or indirect assignment through a pointer must follow all
previous (according to the original block) evaluations of any variable.
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 18
PRINCIPAL USES OF REGISTERS
In most machine architectures, some or all of the operands of an operation
must be in registers in order to perform the operation.
Registers make good temporaries - places to hold the result of a subexpression
while a larger expression is being evaluated, or more generally, a place to hold
a variable that is used only within a single basic block.
Registers are often used to help with run-time storage management, for
example, to manage the run-time stack, including the maintenance of stack
pointers and possibly the top elements of the stack itself.
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 19
DESCRIPTORS FOR DATA STRUCTURE
For each available register, a register descriptor keeps track of the variable
names whose current value is in that register. Since we shall use only those
registers that are available for local use within a basic block, we assume that
initially, all register descriptors are empty. As the code generation progresses,
each register will hold the value of zero or more names.
For each program variable, an address descriptor keeps track of the location or
locations where the current value of that variable can be found. The location
might be a register, a memory address, a stack location, or some set of more
than one of these. The information can be stored in the symbol-table entry for
that variable name.
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 20
MACHINE INSTRUCTIONS FOR OPERATIONS
• Use getReg(x = y + z) to select registers for x, y, and z. Call
these Rx, Ry and Rz.
• If y is not in Ry (according to the register descriptor for Ry),
then issue an instruction LD Ry, y', where y' is one of the
memory locations for y (according to the address descriptor
for y).
• Similarly, if z is not in Rz, issue and instruction LD Rz, z',
where z' is a location for x .
• Issue the instruction ADD Rx , Ry, Rz.
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 21
Rules for updating the register and address descriptors
For the instruction LD R, x
Change the register descriptor for register R so it holds only x.
Change the address descriptor for x by adding register R as an additional
location.
For the instruction ST x, R, change the address descriptor for x to include
its own memory location.
For an operation such as ADD Rx, Ry, Rz implementing a three-address
instruction x = y + x
Change the register descriptor for Rx so that it holds only x.
Change the address descriptor for x so that its only location is Rx. Note
that the memory location for x is not now in the address descriptor for x.
Remove Rx from the address descriptor of any variable other than x.
When we process a copy statement x = y, after generating the load for y
into register Ry, if needed, and after managing descriptors as for all load
statements (per rule I):
Add x to the register descriptor for Ry.
Change the address descriptor for x so that its only location is Ry .
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 22
CHARACTERISTIC OF PEEPHOLE OPTIMIZATIONS
• Redundant-instruction elimination
• Flow-of-control optimizations
• Algebraic simplifications
• Use of machine idioms
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 23
REDUNDANT-INSTRUCTION ELIMINATION
• LD a, R0
ST R0, a
• if debug == 1 goto L1
goto L2
L I : print debugging information
L2:
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 24
FLOW-OF-CONTROL OPTIMIZATIONS
goto L1
...
Ll: goto L2
Can be replaced by:
goto L2
...
Ll: goto L2
if a<b goto L1
...
Ll: goto L2
Can be replaced by:
if a<b goto L2
...
Ll: goto L2
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 25
ALGEBRAIC SIMPLIFICATIONS
• x=x+0
• x=x*1
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 26
REGISTER ALLOCATION AND ASSIGNMENT
• Global Register Allocation
• Usage Counts
• Register Assignment for Outer Loops
• Register Allocation by Graph Coloring
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 27
GLOBAL REGISTER ALLOCATION
This resulted that all live variables be stored at the end of block
To save some of these stores and their corresponding loads, we might arrange
to assign registers to frequently used variables and keep these registers
consistent across block boundaries (globally)
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 28
DYNAMIC PROGRAMMING ALGORITHM
Compute bottom-up for each node n of the expression tree T an array C of
costs, in which the ith component C[i] is the optimal cost of computing the
subtree S rooted at n into a register, assuming i registers are available for the
computation, for
Traverse T, using the cost vectors to determine which subtrees of T must be
computed into memory.
Traverse each tree using the cost vectors and associated instructions to
generate the final target code. The code for the subtrees computed into
memory locations is generated first.
31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 29

Mais conteúdo relacionado

Mais procurados

Distributed operating system(os)
Distributed operating system(os)Distributed operating system(os)
Distributed operating system(os)Dinesh Modak
 
Ch1 language design issue
Ch1 language design issueCh1 language design issue
Ch1 language design issueJigisha Pandya
 
Lecture 1 introduction to parallel and distributed computing
Lecture 1   introduction to parallel and distributed computingLecture 1   introduction to parallel and distributed computing
Lecture 1 introduction to parallel and distributed computingVajira Thambawita
 
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating SystemsMukesh Chinta
 
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...Gyanmanjari Institute Of Technology
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and DynamicSneh Pahilwani
 
program flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architectureprogram flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architecturePankaj Kumar Jain
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structuresMukesh Chinta
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
13. Query Processing in DBMS
13. Query Processing in DBMS13. Query Processing in DBMS
13. Query Processing in DBMSkoolkampus
 

Mais procurados (20)

Distributed operating system(os)
Distributed operating system(os)Distributed operating system(os)
Distributed operating system(os)
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Parallel computing persentation
Parallel computing persentationParallel computing persentation
Parallel computing persentation
 
Ch1 language design issue
Ch1 language design issueCh1 language design issue
Ch1 language design issue
 
Lecture 1 introduction to parallel and distributed computing
Lecture 1   introduction to parallel and distributed computingLecture 1   introduction to parallel and distributed computing
Lecture 1 introduction to parallel and distributed computing
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating Systems
 
Distributed Operating System_1
Distributed Operating System_1Distributed Operating System_1
Distributed Operating System_1
 
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
 
Mobile computing (Wireless) Medium Access Control (MAC)
Mobile computing (Wireless) Medium Access Control (MAC)Mobile computing (Wireless) Medium Access Control (MAC)
Mobile computing (Wireless) Medium Access Control (MAC)
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Parallel processing
Parallel processingParallel processing
Parallel processing
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
 
program flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architectureprogram flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architecture
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structures
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
13. Query Processing in DBMS
13. Query Processing in DBMS13. Query Processing in DBMS
13. Query Processing in DBMS
 
Chpt7
Chpt7Chpt7
Chpt7
 

Destaque

Code Optimization
Code OptimizationCode Optimization
Code Optimizationguest9f8315
 
Spr ch-05-compilers
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilersVasim Pathan
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyCs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyappasami
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)bolovv
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit IIIManoj Patil
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
code optimization
code optimization code optimization
code optimization Sanjeev Raaz
 
Software Engineering unit 5
Software Engineering unit 5Software Engineering unit 5
Software Engineering unit 5Abhimanyu Mishra
 

Destaque (20)

Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Ch9b
Ch9bCh9b
Ch9b
 
Spr ch-05-compilers
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilers
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyCs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer key
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
Code generator
Code generatorCode generator
Code generator
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
code optimization
code optimization code optimization
code optimization
 
Code generation
Code generationCode generation
Code generation
 
Software Engineering unit 5
Software Engineering unit 5Software Engineering unit 5
Software Engineering unit 5
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Module 11
Module 11Module 11
Module 11
 
Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 

Semelhante a Compiler unit 5

COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptCOMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptssuserebb9821
 
COMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxCOMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxssuserebb9821
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
Chap 3_2.ppt
Chap 3_2.pptChap 3_2.ppt
Chap 3_2.pptinian2
 
unit2-8085-programminG.pptx
unit2-8085-programminG.pptxunit2-8085-programminG.pptx
unit2-8085-programminG.pptxRajaSekhar533255
 
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PRESENTATION ON DATA STRUCTURE AND THEIR TYPEPRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PRESENTATION ON DATA STRUCTURE AND THEIR TYPEnikhilcse1
 
456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).ppt456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).pptMohibKhan79
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 80869840596838
 
Arm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.pptArm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.pptManju Badiger
 
Instruction set-of-8086
Instruction set-of-8086Instruction set-of-8086
Instruction set-of-8086mudulin
 
Relational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptRelational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptAnkush138
 
Saumya Debray The University of Arizona Tucson
Saumya Debray The University of Arizona TucsonSaumya Debray The University of Arizona Tucson
Saumya Debray The University of Arizona Tucsonjeronimored
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica"FENG "GEORGE"" YU
 
EECS 214395--‐Data Structures and Data Mana.docx
EECS  214395--‐Data    Structures    and    Data   Mana.docxEECS  214395--‐Data    Structures    and    Data   Mana.docx
EECS 214395--‐Data Structures and Data Mana.docxjack60216
 

Semelhante a Compiler unit 5 (20)

COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptCOMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.ppt
 
COMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxCOMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptx
 
Instruction set
Instruction setInstruction set
Instruction set
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Chap 3_2.ppt
Chap 3_2.pptChap 3_2.ppt
Chap 3_2.ppt
 
unit2-8085-programminG.pptx
unit2-8085-programminG.pptxunit2-8085-programminG.pptx
unit2-8085-programminG.pptx
 
Chap03[1]
Chap03[1]Chap03[1]
Chap03[1]
 
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PRESENTATION ON DATA STRUCTURE AND THEIR TYPEPRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
 
456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).ppt456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).ppt
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
X86 operation types
X86 operation typesX86 operation types
X86 operation types
 
Arm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.pptArm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.ppt
 
Instruction set-of-8086
Instruction set-of-8086Instruction set-of-8086
Instruction set-of-8086
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Relational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptRelational Algebra and Calculus.ppt
Relational Algebra and Calculus.ppt
 
ARM Fundamentals
ARM FundamentalsARM Fundamentals
ARM Fundamentals
 
Saumya Debray The University of Arizona Tucson
Saumya Debray The University of Arizona TucsonSaumya Debray The University of Arizona Tucson
Saumya Debray The University of Arizona Tucson
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica
 
It3416071612
It3416071612It3416071612
It3416071612
 
EECS 214395--‐Data Structures and Data Mana.docx
EECS  214395--‐Data    Structures    and    Data   Mana.docxEECS  214395--‐Data    Structures    and    Data   Mana.docx
EECS 214395--‐Data Structures and Data Mana.docx
 

Mais de BBDITM LUCKNOW (18)

Unit 5 cspc
Unit 5 cspcUnit 5 cspc
Unit 5 cspc
 
Unit 4 cspc
Unit 4 cspcUnit 4 cspc
Unit 4 cspc
 
Unit3 cspc
Unit3 cspcUnit3 cspc
Unit3 cspc
 
Cse ppt 2018
Cse ppt 2018Cse ppt 2018
Cse ppt 2018
 
Binary system ppt
Binary system pptBinary system ppt
Binary system ppt
 
Unit 4 ca-input-output
Unit 4 ca-input-outputUnit 4 ca-input-output
Unit 4 ca-input-output
 
Unit 3 ca-memory
Unit 3 ca-memoryUnit 3 ca-memory
Unit 3 ca-memory
 
Unit 2 ca- control unit
Unit 2 ca- control unitUnit 2 ca- control unit
Unit 2 ca- control unit
 
Unit 1 ca-introduction
Unit 1 ca-introductionUnit 1 ca-introduction
Unit 1 ca-introduction
 
Yacc
YaccYacc
Yacc
 
Bnf and ambiquity
Bnf and ambiquityBnf and ambiquity
Bnf and ambiquity
 
Lex
LexLex
Lex
 
Minimization of dfa
Minimization of dfaMinimization of dfa
Minimization of dfa
 
Passescd
PassescdPassescd
Passescd
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Cspc final
Cspc finalCspc final
Cspc final
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Validation based protocol
Validation based protocolValidation based protocol
Validation based protocol
 

Último

CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
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
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
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
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 

Último (20)

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
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
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...
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 

Compiler unit 5

  • 1. COMPILER DESIGN UNIT-V ANKUR SRIVASTAVA ASSISTANT PROF.(CSE) JETGI 31-Dec-16 1ANKUR SRIVASTAVA (CSE) JETGI
  • 2. CONTENTS Code Generation Issues Target language Issues Addresses in Target Code Basic Blocks and Flow Graphs Optimizations of Basic Blocks A Simple Code Generator Peephole optimization Register allocation and assignment Instruction selection by tree rewriting 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 2
  • 3. INTRODUCTION The final phase of a compiler is code generator It receives an intermediate representation (IR) with supplementary information in symbol table Produces a semantically equivalent target program Code generator main tasks: Instruction selection Register allocation and assignment Instruction ordering Front end Code optimizer Code Generator 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 3
  • 4. ISSUES IN THE DESIGN OF CODE GENERATOR The most important criterion is that it produces correct code Input to the code generator IR + Symbol table We assume front end produces low-level IR, i.e. values of names in it can be directly manipulated by the machine instructions. Syntactic and semantic errors have been already detected The target program Common target architectures are: RISC, CISC and Stack based machines In this chapter we use a very simple RISC-like computer with addition of some CISC-like addressing modes 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 4
  • 5. COMPLEXITY OF MAPPING the level of the IR the nature of the instruction-set architecture the desired quality of the generated code. x=y+z LD R0, y ADD R0, R0, z ST x, R0 a=b+c d=a+e LD R0, b ADD R0, R0, c ST a, R0 LD R0, a ADD R0, R0, e ST d, R0 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 5
  • 6. REGISTER ALLOCATION Two sub-problems Register allocation: selecting the set of variables that will reside in registers at each point in the program Resister assignment: selecting specific register that a variable reside in Complications imposed by the hardware architecture Example: register pairs for multiplication and division t=a+b t=t*c T=t/d t=a+b t=t+c T=t/d L R1, a A R1, b M R0, c D R0, d ST R1, t L R0, a A R0, b M R0, c SRDA R0, 32 D R0, d ST R1, t 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 6
  • 7. A SIMPLE TARGET MACHINE MODEL Load operations: LD r,x and LD r1, r2 Store operations: ST x,r Computation operations: OP dst, src1, src2 Unconditional jumps: BR L Conditional jumps: Bcond r, L like BLTZ r, L 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 7
  • 8. ADDRESSING MODES variable name: x indexed address: a(r) like LD R1, a(R2) means R1=contents(a+contents(R2)) integer indexed by a register : like LD R1, 100(R2) Indirect addressing mode: *r and *100(r) immediate constant addressing mode: like LD R1, #100 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 8
  • 9. ADDRESSES IN THE TARGET CODE A statically determined area Code A statically determined data area Static A dynamically managed area Heap A dynamically managed area Stack 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 9
  • 10. THREE-ADDRESS STATEMENTS FOR PROCEDURE CALLS AND RETURNS call callee Return Halt action 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 10
  • 11. BASIC BLOCKS AND FLOW GRAPHS Partition the intermediate code into basic blocks The flow of control can only enter the basic block through the first instruction in the block. That is, there are no jumps into the middle of the block. Control will leave the block without halting or branching, except possibly at the last instruction in the block. The basic blocks become the nodes of a flow graph 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 11
  • 12. RULES FOR FINDING LEADERS The first three-address instruction in the intermediate code is a leader. Any instruction that is the target of a conditional or unconditional jump is a leader. Any instruction that immediately follows a conditional or unconditional jump is a leader. 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 12
  • 13. DAG REPRESENTATION OF BASIC BLOCKS There is a node in the DAG for each of the initial values of the variables appearing in the basic block. There is a node N associated with each statement s within the block. The children of N are those nodes corresponding to statements that are the last definitions, prior to s, of the operands used by s. Node N is labeled by the operator applied at s, and also attached to N is the list of variables for which it is the last definition within the block. Certain nodes are designated output nodes. These are the nodes whose variables are live on exit from the block. 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 13
  • 14. CODE IMPROVING TRANSFORMATIONS We can eliminate local common subexpressions, that is, instructions that compute a value that has already been computed. We can eliminate dead code, that is, instructions that compute a value that is never used. We can reorder statements that do not depend on one another; such reordering may reduce the time a temporary value needs to be preserved in a register. We can apply algebraic laws to reorder operands of three-address instructions, and sometimes t hereby simplify t he computation. 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 14
  • 15. DAG FOR BASIC BLOCK 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 15
  • 16. DAG for basic block 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 16
  • 17. ARRAY ACCESSES IN A DAG An assignment from an array, like x = a [i], is represented by creating a node with operator =[] and two children representing the initial value of the array, a0 in this case, and the index i. Variable x becomes a label of this new node. An assignment to an array, like a [j] = y, is represented by a new node with operator []= and three children representing a0, j and y. There is no variable labeling this node. What is different is that the creation of this node kills all currently constructed nodes whose value depends on a0. A node that has been killed cannot receive any more labels; that is, it cannot become a common subexpression. 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 17
  • 18. RULES FOR RECONSTRUCTING THE BASIC BLOCK FROM A DAG The order of instructions must respect the order of nodes in the DAG. That is, we cannot compute a node's value until we have computed a value for each of its children. Assignments to an array must follow all previous assignments to, or evaluations from, the same array, according to the order of these instructions in the original basic block. Evaluations of array elements must follow any previous (according to the original block) assignments to the same array. The only permutation allowed is that two evaluations from the same array may be done in either order, as long as neither crosses over an assignment to that array. Any use of a variable must follow all previous (according to the original block) procedure calls or indirect assignments through a pointer. Any procedure call or indirect assignment through a pointer must follow all previous (according to the original block) evaluations of any variable. 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 18
  • 19. PRINCIPAL USES OF REGISTERS In most machine architectures, some or all of the operands of an operation must be in registers in order to perform the operation. Registers make good temporaries - places to hold the result of a subexpression while a larger expression is being evaluated, or more generally, a place to hold a variable that is used only within a single basic block. Registers are often used to help with run-time storage management, for example, to manage the run-time stack, including the maintenance of stack pointers and possibly the top elements of the stack itself. 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 19
  • 20. DESCRIPTORS FOR DATA STRUCTURE For each available register, a register descriptor keeps track of the variable names whose current value is in that register. Since we shall use only those registers that are available for local use within a basic block, we assume that initially, all register descriptors are empty. As the code generation progresses, each register will hold the value of zero or more names. For each program variable, an address descriptor keeps track of the location or locations where the current value of that variable can be found. The location might be a register, a memory address, a stack location, or some set of more than one of these. The information can be stored in the symbol-table entry for that variable name. 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 20
  • 21. MACHINE INSTRUCTIONS FOR OPERATIONS • Use getReg(x = y + z) to select registers for x, y, and z. Call these Rx, Ry and Rz. • If y is not in Ry (according to the register descriptor for Ry), then issue an instruction LD Ry, y', where y' is one of the memory locations for y (according to the address descriptor for y). • Similarly, if z is not in Rz, issue and instruction LD Rz, z', where z' is a location for x . • Issue the instruction ADD Rx , Ry, Rz. 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 21
  • 22. Rules for updating the register and address descriptors For the instruction LD R, x Change the register descriptor for register R so it holds only x. Change the address descriptor for x by adding register R as an additional location. For the instruction ST x, R, change the address descriptor for x to include its own memory location. For an operation such as ADD Rx, Ry, Rz implementing a three-address instruction x = y + x Change the register descriptor for Rx so that it holds only x. Change the address descriptor for x so that its only location is Rx. Note that the memory location for x is not now in the address descriptor for x. Remove Rx from the address descriptor of any variable other than x. When we process a copy statement x = y, after generating the load for y into register Ry, if needed, and after managing descriptors as for all load statements (per rule I): Add x to the register descriptor for Ry. Change the address descriptor for x so that its only location is Ry . 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 22
  • 23. CHARACTERISTIC OF PEEPHOLE OPTIMIZATIONS • Redundant-instruction elimination • Flow-of-control optimizations • Algebraic simplifications • Use of machine idioms 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 23
  • 24. REDUNDANT-INSTRUCTION ELIMINATION • LD a, R0 ST R0, a • if debug == 1 goto L1 goto L2 L I : print debugging information L2: 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 24
  • 25. FLOW-OF-CONTROL OPTIMIZATIONS goto L1 ... Ll: goto L2 Can be replaced by: goto L2 ... Ll: goto L2 if a<b goto L1 ... Ll: goto L2 Can be replaced by: if a<b goto L2 ... Ll: goto L2 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 25
  • 26. ALGEBRAIC SIMPLIFICATIONS • x=x+0 • x=x*1 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 26
  • 27. REGISTER ALLOCATION AND ASSIGNMENT • Global Register Allocation • Usage Counts • Register Assignment for Outer Loops • Register Allocation by Graph Coloring 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 27
  • 28. GLOBAL REGISTER ALLOCATION This resulted that all live variables be stored at the end of block To save some of these stores and their corresponding loads, we might arrange to assign registers to frequently used variables and keep these registers consistent across block boundaries (globally) 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 28
  • 29. DYNAMIC PROGRAMMING ALGORITHM Compute bottom-up for each node n of the expression tree T an array C of costs, in which the ith component C[i] is the optimal cost of computing the subtree S rooted at n into a register, assuming i registers are available for the computation, for Traverse T, using the cost vectors to determine which subtrees of T must be computed into memory. Traverse each tree using the cost vectors and associated instructions to generate the final target code. The code for the subtrees computed into memory locations is generated first. 31-Dec-16 ANKUR SRIVASTAVA (CSE) JETGI 29