compiler over 2
TRANSCRIPT
![Page 1: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/1.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 1/17
An Overview of a Compiler - Part 2
Y.N. Srikant
Department of Computer ScienceIndian Institute of Science
Bangalore 560 012
NPTEL Course on Compiler Design
Y.N. Srikant Compiler Overview
![Page 2: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/2.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 2/17
Outline of the Lecture
1 Compiler overview with block diagram
2 Lexical analysis with LEX
3 Parsing with YACC
4 Semantic analysis with attribute grammars5 Intermediate code generation with syntax-directed
translation
6 Code optimization examples
Topics 1 to 4 have been covered in Part I of the lecture
Y.N. Srikant Compiler Overview
![Page 3: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/3.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 3/17
Compiler Overview
Y.N. Srikant Compiler Overview
![Page 4: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/4.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 4/17
Translation Overview - Lexical Analysis
Y.N. Srikant Compiler Overview
![Page 5: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/5.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 5/17
Translation Overview - Syntax Analysis
Y.N. Srikant Compiler Overview
![Page 6: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/6.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 6/17
Translation Overview - Semantic Analysis
Y.N. Srikant Compiler Overview
O C G
![Page 7: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/7.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 7/17
Translation Overview - Intermediate Code Generation
Y.N. Srikant Compiler Overview
I di C d G i
![Page 8: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/8.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 8/17
Intermediate Code Generation
While generating machine code directly from source codeis possible, it entails two problems
With m languages and n target machines, we need to writem × n compilersThe code optimizer which is one of the largest andvery-difficult-to-write components of any compiler cannot bereused
By converting source code to an intermediate code, amachine-independent code optimizer may be written
Intermediate code must be easy to produce and easy totranslate to machine code
A sort of universal assembly languageShould not contain any machine-specific parameters(registers, addresses, etc.)
Usually produced during a traversal of the semantically
validated syntax tree
Y.N. Srikant Compiler Overview
Diff t T f I t di t C d
![Page 9: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/9.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 9/17
Different Types of Intermediate Code
The type of intermediate code deployed is based on theapplication
Quadruples, triples, indirect triples, abstract syntax trees
are the classical forms used for machine-independent
optimizations and machine code generation
Static Single Assignment form (SSA) is a recent form andenables more effective optimizations
Conditional constant propagation and global valuenumbering are more effective on SSA
Program Dependence Graph (PDG) is useful in automaticparallelization, instruction scheduling, and software
pipelining
Y.N. Srikant Compiler Overview
T l ti t d Q d l f E i
![Page 10: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/10.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 10/17
Translation to produce Quadruples for Expressions
1 S → id := E {idptr := search (id .name );if idptr = NULL then gen (idptr := E .result else error }
2 E → E 1 + E 2 {E .
result := gentemp ();gen (E .result := E 1.result + E 2.result )}
3 E → E 1 ∗ E 2 {E .result := gentemp ();gen (E .result := E 1.result ∗ E 2.result )}
4
E → −E 1 {E .
result := gentemp ();gen (E .result := uminus E 1.result )}5 E → ( E 1 ) {E .result := E 1.result }6 E → id {idptr := search (id .name );
if idptr = NULL then E .result := idptr else error }
Names are stored in a symbol table; the routine
search (id .name ) gets a pointer to the name id .name
gentemp () generates a temporary name, puts it in the
symbol table, and returns a pointer to it
Y.N. Srikant Compiler Overview
Q d l f E i A E l f
![Page 11: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/11.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 11/17
Quadruples for Expressions - An Example of
Translation
Y.N. Srikant Compiler Overview
Translation Overview Code Optimization
![Page 12: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/12.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 12/17
Translation Overview - Code Optimization
Y.N. Srikant Compiler Overview
Machine independent Code Optimization
![Page 13: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/13.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 13/17
Machine-independent Code Optimization
Intermediate code generation process introduces many
inefficienciesExtra copies of variables, using variables instead ofconstants, repeated evaluation of expressions, etc.
Code optimization removes such inefficiencies and
improves code
Improvement may be time, space, or power consumption
It changes the structure of programs, sometimes of beyondrecognition
Inlines functions, unrolls loops, eliminates someprogrammer-defined variables, etc.
Code optimization consists of a bunch of heuristics and
percentage of improvement depends on programs (may be
zero also)
Y.N. Srikant Compiler Overview
Examples of Machine Independant Optimizations
![Page 14: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/14.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 14/17
Examples of Machine-Independant Optimizations
Common sub-expression elimination
Copy propagation
Loop invariant code motion
Partial redundancy elimination
Induction variable elimination and strength reduction
Code opimization needs information about the program
which expressions are being recomputed in a function?which definitions reach a point?
All such information is gathered through data-flow analysis
Y.N. Srikant Compiler Overview
Translation Overview Code Generation
![Page 15: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/15.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 15/17
Translation Overview - Code Generation
Y.N. Srikant Compiler Overview
Code Generation
![Page 16: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/16.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 16/17
Code Generation
Converts intermediate code to machine codeEach intermediate code instruction may result in many
machine instructions or vice-cersa
Must handle all aspects of machine architecture
Registers, pipelining, cache, multiple function units, etc.Generating efficient code is an NP-complete problem
Tree pattern matching-based strategies are among the bestNeeds tree intermediate code
Storage allocation decisions are made here
Register allocation and assignment are the most importantproblems
Y.N. Srikant Compiler Overview
Machine-Dependent Optimizations
![Page 17: Compiler Over 2](https://reader031.vdocuments.site/reader031/viewer/2022021212/577d24ba1a28ab4e1e9d396d/html5/thumbnails/17.jpg)
8/3/2019 Compiler Over 2
http://slidepdf.com/reader/full/compiler-over-2 17/17
Machine-Dependent Optimizations
Peephole optimizations
Analyze sequence of instructions in a small window(peephole ) and using preset patterns, replace them with amore efficient sequenceRedundant instruction eliminatione.g., replace the sequence [LD A,R1][ST R1,A] by [LD
A,R1]Eliminate “jump to jump” instructionsUse machine idioms (use INC instead of LD and ADD)
Instruction scheduling (reordering) to eliminate pipeline
interlocks and to increase parallelism
Trace scheduling to increase the size of basic blocks andincrease parallelism
Software pipelining to increase parallelism in loops
Y.N. Srikant Compiler Overview