SlideShare uma empresa Scribd logo
1 de 20
WELCOME TO A
JOURNEY TO
CS419

Dr. Hussien Sharaf
Computer Science Department

dr.sharaf@from-masr.com
Dr. Hussien M. Sharaf

PHASES OF COMPILER
position := initial + rate * 60
1. Lexical analyzer
id1 := id2 + id3 * 60
2. Syntax analyzer

4. Intermediate code
generator
temp1:= inttoreal(60)
temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3

:=

5. Code optimizer

+

id1

*

id2
id3

60

3. Semantic analyzer
:=
+

id1

*

id2
id3

inttoreal

temp1 := id3 * 60.0
id1 := id2 + temp1

6. Code generator
MOVF id3 , R2
MULF #60.0 , R2
MOVF id2 , R1
ADDF R2, R1
MOVF R1, id1

60
2
Dr. Hussien M. Sharaf

3. SEMANTIC ANALYZER
The semantics of a program are its meaning
as opposed to syntax or structure.
 The semantics consist of:
 Runtime semantics: behavior of program
at run time.
 Static semantics: checked by the
compiler.


3
Dr. Hussien M. Sharaf

STATIC SEMANTICS


Static semantics include:
 Declaration of variables and constants
before use. i.e.
int x;
x = 3;

 Calling

functions that exist (predefined in a library
or defined by the user) i.e.
n = Max(4,7);
int Max(int x, int y)
{
int z;
if(x > y)
z = x;
else
z = y;
return z;
}
4
Dr. Hussien M. Sharaf

STATIC SEMANTICS
 Passing

parameters properly.
 Type checking, i.e.
int x, y;
x = 3;
y = 2.5; performs an error, cause 2.5 is not int it is float data type.



Static semantics can not be checked by the
parser.

5
Dr. Hussien M. Sharaf

SEMANTIC ANALYZER (CONT.)


The semantic analyzer does the following:
 Checks

the static semantics of the language.
 Annotates the syntax tree with type information,
as shown in example. := x + y * 2.5;
a
:= real
id a real

+ real

id x real

Annotated syntax tree

* real

inttoreal

id y integer

literal 2.5 real
6
Dr. Hussien M. Sharaf

4. INTERMEDIATE CODE GENERATOR
Intermediate language called “Three-address
code”.
 Should have two important properties:
 Should be easy to produce.
 Should be easy to translate to the target
language.
 A TAC instruction have at most one instruction
per line and can have at most three operands.


7
Dr. Hussien M. Sharaf

INTERMEDIATE CODE GENERATOR


Example:

a := x + y * 2.5;

:= real

Three-address code
id a real

+ real

id x real

* real

inttoreal

temp1 := inttoreal(y)
temp2 := temp1 real* 2.5
temp3 := x real+ temp2
a := temp3

literal 2.5 real

id y integer



A TAC instruction have at most one instruction per
line and can have at most three operands.
8
Dr. Hussien M. Sharaf

5. CODE OPTIMIZER
 Code optimization can be applied to:
code – independent of the
target machine.
 Target code – dependent on the target
machine.
 Intermediate

9
Dr. Hussien M. Sharaf

INTERMEDIATE CODE OPTIMIZATION


Intermediate code optimization include:
A.

B.
C.

D.
E.

Constant folding.
Elimination of common sub- expressions.
Identification and elimination of unreachable
code (called dead code).
Improving loops.
Improving function calls.

10
Dr. Hussien M. Sharaf

A. CONSTANT FOLDING
Simplifying constant expressions at compile
time.
 Example:
i = 320 * 200 * 32
In this example modern compilers identify
constructs, and substitute the computed
values at compile time (in this case –
2,048,000)


11
Dr. Hussien M. Sharaf

B. ELIMINATION OF COMMON SUB- EXPRESSIONS

Replace the common expressions with a
single variable holding the computed value.
 Example:


a = b * c + g;
d = b * c * e;

tmp = b * c;
a = tmp + g;
d = tmp * e;

12
Dr. Hussien M. Sharaf

C. IDENTIFICATION AND ELIMINATION OF DEAD CODE

Dead code is the code in a program which is
executed but whose result is never used in
any other computation.
 Dead code wastes computation time.
 Example:


int f (int x, int y)
{
int z = x + y;
return x * y;
}

should be eliminated.

13
Dr. Hussien M. Sharaf

D. IMPROVING LOOPS





There are a lot of strategies for improving loops.
A simple one is: Move loop invariants out of the loop
Example:
For y = 0 to Height-1
For x = 0 to Width-1
' y*Width is invariant
i = y*Width + x
Process i
Next x
Next y



For y = 0 to Height-1
temp = y*Width
For x = 0 to Width-1
i = temp + x
Process i
Next x
Next y

This link : http://www.aivosto.com/vbtips/loopopt.html
illustrates all the strategies for interested readers. 14
Dr. Hussien M. Sharaf

E. IMPROVING FUNCTION CALLS





One strategy is by Removing recursion (function that calls itself).
Example of recursive function:
unsigned int factorial(unsigned int n)
{
if (n == 0) { return 1; }
else { return n * factorial(n - 1); }
}
After removing recursion and using loops instead it will be:
unsigned int factorial(unsigned int n)
{
int result = 1;
if (n == 0) { return 1; }
else {
for (i = n; i>=1; i--)
{
result = result * i;
}
return result;
}
}
15
Dr. Hussien M. Sharaf

TARGET CODE OPTIMIZATION


Target Code optimization is done by
improving the intermediate code, and
removing the redundant code.



Example:
Temp1 := int-to-real(60)
Temp2 := id3 * temp1
Temp3 := id2 + temp2
Id1 := temp3

temp1 := id3 * 60.0
id1 := id2 + temp1

16
Dr. Hussien M. Sharaf

TARGET CODE OPTIMIZATION


Target code optimization include:
a.

b.

Allocation and use of registers.
Selection of better (safer) instructions and
addressing modes.

17
Dr. Hussien M. Sharaf

6. CODE GENERATOR
Generates code for the target machine.
 Selects appropriate machine instructions.
 Allocates memory locations for variables.
 Allocates registers for intermediate
computations.


Three-address code
temp1 := int2real(y)
temp2 := temp1 * 2.5
temp3 := x + temp2
a : = temp3

Assembly code
LOADI
MOVF
MULF
LOADF
ADDF
STORF

R1 , y
F1 , R1
F2 , F1, 2.5
F3 , x
F4 , F3, F2
a, F4

;; R1
;; F1
;; F2
;; F3
;; F4
;; a

y
int2real(R1)
F1 * 2.5
x
F3 + F2
F4
18
Dr. Hussien M. Sharaf

CODE GENERATOR


Generation target code example:
temp := id3 * 60.0

id1 := id2 + temp

MOVF
MULF
MOVF
ADDF
MOVF

id3 , R2
#60.0, R2
id2 , R1
R2 , R1
R1, id1

;; id3 R2
;; R2 * 60.0
;; id2 R1
;; R2 + R1
;; R1
id1

R2
R1

19
Dr. Hussien M. Sharaf

THANK YOU

Mais conteúdo relacionado

Mais procurados

Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONAnil Pokhrel
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Dushmanta Nath
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representationsahmed51236
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationAkshaya Arunan
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)bolovv
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshopBAINIDA
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 

Mais procurados (20)

Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTION
 
Three Address code
Three Address code Three Address code
Three Address code
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
C programming part4
C programming part4C programming part4
C programming part4
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representations
 
Code generator
Code generatorCode generator
Code generator
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Network Security CS3-4
Network Security CS3-4 Network Security CS3-4
Network Security CS3-4
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 

Destaque

Theory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemTheory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemKaran Thakkar
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Animesh Chaturvedi
 

Destaque (20)

Theory of computation Lec6
Theory of computation Lec6Theory of computation Lec6
Theory of computation Lec6
 
Cs419 lec3 lexical analysis using re
Cs419 lec3   lexical analysis using reCs419 lec3   lexical analysis using re
Cs419 lec3 lexical analysis using re
 
Cs419 lec4 lexical analysis using re
Cs419 lec4   lexical analysis using reCs419 lec4   lexical analysis using re
Cs419 lec4 lexical analysis using re
 
Lec4
Lec4Lec4
Lec4
 
Cs419 lec5 lexical analysis using dfa
Cs419 lec5   lexical analysis using dfaCs419 lec5   lexical analysis using dfa
Cs419 lec5 lexical analysis using dfa
 
Cs419 lec7 cfg
Cs419 lec7   cfgCs419 lec7   cfg
Cs419 lec7 cfg
 
Cs419 lec6 lexical analysis using nfa
Cs419 lec6   lexical analysis using nfaCs419 lec6   lexical analysis using nfa
Cs419 lec6 lexical analysis using nfa
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
Theory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemTheory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence Problem
 
Infos2014
Infos2014Infos2014
Infos2014
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Minimizing DFA
Minimizing DFAMinimizing DFA
Minimizing DFA
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Theory of computation Lec7 pda
Theory of computation Lec7 pdaTheory of computation Lec7 pda
Theory of computation Lec7 pda
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 

Semelhante a CS419 Compiler Phases Journey

Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankarDipankar Nalui
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languagesAnkit Pandey
 
Code transformation by direct transformation of ASTs
Code transformation by direct transformation of ASTsCode transformation by direct transformation of ASTs
Code transformation by direct transformation of ASTsMarkRizun
 
Code Transformation by Direct Transformation of ASTs
Code Transformation by Direct Transformation of ASTsCode Transformation by Direct Transformation of ASTs
Code Transformation by Direct Transformation of ASTsESUG
 
Public-Key Cryptography.pdfWrite the result of the following operation with t...
Public-Key Cryptography.pdfWrite the result of the following operation with t...Public-Key Cryptography.pdfWrite the result of the following operation with t...
Public-Key Cryptography.pdfWrite the result of the following operation with t...FahmiOlayah
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2Zaibi Gondal
 
5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constantsCtOlaf
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10alish sha
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded cBenux Wei
 

Semelhante a CS419 Compiler Phases Journey (20)

Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankar
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
functions
functionsfunctions
functions
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
Code transformation by direct transformation of ASTs
Code transformation by direct transformation of ASTsCode transformation by direct transformation of ASTs
Code transformation by direct transformation of ASTs
 
Code Transformation by Direct Transformation of ASTs
Code Transformation by Direct Transformation of ASTsCode Transformation by Direct Transformation of ASTs
Code Transformation by Direct Transformation of ASTs
 
Public-Key Cryptography.pdfWrite the result of the following operation with t...
Public-Key Cryptography.pdfWrite the result of the following operation with t...Public-Key Cryptography.pdfWrite the result of the following operation with t...
Public-Key Cryptography.pdfWrite the result of the following operation with t...
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constants
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Introductionof c
Introductionof cIntroductionof c
Introductionof c
 
20BCE1734.pdf
20BCE1734.pdf20BCE1734.pdf
20BCE1734.pdf
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded c
 
Bc0037
Bc0037Bc0037
Bc0037
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Ch3
Ch3Ch3
Ch3
 
Apclass
ApclassApclass
Apclass
 

Mais de Arab Open University and Cairo University

Mais de Arab Open University and Cairo University (14)

File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013
 
Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Setup python with eclipse
Setup python with eclipseSetup python with eclipse
Setup python with eclipse
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
Compilers midterm spring 2013 model answer
Compilers midterm spring 2013   model answerCompilers midterm spring 2013   model answer
Compilers midterm spring 2013 model answer
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
CS215 - Lec 8 searching records
CS215 - Lec 8  searching recordsCS215 - Lec 8  searching records
CS215 - Lec 8 searching records
 
CS215 - Lec 7 managing records collection
CS215 - Lec 7  managing records collectionCS215 - Lec 7  managing records collection
CS215 - Lec 7 managing records collection
 
CS215 - Lec 6 record index
CS215 - Lec 6  record indexCS215 - Lec 6  record index
CS215 - Lec 6 record index
 
CS215 - Lec 5 record organization
CS215 - Lec 5  record organizationCS215 - Lec 5  record organization
CS215 - Lec 5 record organization
 
CS215 - Lec 4 single record organization
CS215 - Lec 4  single record organizationCS215 - Lec 4  single record organization
CS215 - Lec 4 single record organization
 
CS215 - Lec 2 file organization
CS215 - Lec 2   file organizationCS215 - Lec 2   file organization
CS215 - Lec 2 file organization
 

Último

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
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
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

Último (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
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
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

CS419 Compiler Phases Journey

  • 1. WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 2. Dr. Hussien M. Sharaf PHASES OF COMPILER position := initial + rate * 60 1. Lexical analyzer id1 := id2 + id3 * 60 2. Syntax analyzer 4. Intermediate code generator temp1:= inttoreal(60) temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3 := 5. Code optimizer + id1 * id2 id3 60 3. Semantic analyzer := + id1 * id2 id3 inttoreal temp1 := id3 * 60.0 id1 := id2 + temp1 6. Code generator MOVF id3 , R2 MULF #60.0 , R2 MOVF id2 , R1 ADDF R2, R1 MOVF R1, id1 60 2
  • 3. Dr. Hussien M. Sharaf 3. SEMANTIC ANALYZER The semantics of a program are its meaning as opposed to syntax or structure.  The semantics consist of:  Runtime semantics: behavior of program at run time.  Static semantics: checked by the compiler.  3
  • 4. Dr. Hussien M. Sharaf STATIC SEMANTICS  Static semantics include:  Declaration of variables and constants before use. i.e. int x; x = 3;  Calling functions that exist (predefined in a library or defined by the user) i.e. n = Max(4,7); int Max(int x, int y) { int z; if(x > y) z = x; else z = y; return z; } 4
  • 5. Dr. Hussien M. Sharaf STATIC SEMANTICS  Passing parameters properly.  Type checking, i.e. int x, y; x = 3; y = 2.5; performs an error, cause 2.5 is not int it is float data type.  Static semantics can not be checked by the parser. 5
  • 6. Dr. Hussien M. Sharaf SEMANTIC ANALYZER (CONT.)  The semantic analyzer does the following:  Checks the static semantics of the language.  Annotates the syntax tree with type information, as shown in example. := x + y * 2.5; a := real id a real + real id x real Annotated syntax tree * real inttoreal id y integer literal 2.5 real 6
  • 7. Dr. Hussien M. Sharaf 4. INTERMEDIATE CODE GENERATOR Intermediate language called “Three-address code”.  Should have two important properties:  Should be easy to produce.  Should be easy to translate to the target language.  A TAC instruction have at most one instruction per line and can have at most three operands.  7
  • 8. Dr. Hussien M. Sharaf INTERMEDIATE CODE GENERATOR  Example: a := x + y * 2.5; := real Three-address code id a real + real id x real * real inttoreal temp1 := inttoreal(y) temp2 := temp1 real* 2.5 temp3 := x real+ temp2 a := temp3 literal 2.5 real id y integer  A TAC instruction have at most one instruction per line and can have at most three operands. 8
  • 9. Dr. Hussien M. Sharaf 5. CODE OPTIMIZER  Code optimization can be applied to: code – independent of the target machine.  Target code – dependent on the target machine.  Intermediate 9
  • 10. Dr. Hussien M. Sharaf INTERMEDIATE CODE OPTIMIZATION  Intermediate code optimization include: A. B. C. D. E. Constant folding. Elimination of common sub- expressions. Identification and elimination of unreachable code (called dead code). Improving loops. Improving function calls. 10
  • 11. Dr. Hussien M. Sharaf A. CONSTANT FOLDING Simplifying constant expressions at compile time.  Example: i = 320 * 200 * 32 In this example modern compilers identify constructs, and substitute the computed values at compile time (in this case – 2,048,000)  11
  • 12. Dr. Hussien M. Sharaf B. ELIMINATION OF COMMON SUB- EXPRESSIONS Replace the common expressions with a single variable holding the computed value.  Example:  a = b * c + g; d = b * c * e; tmp = b * c; a = tmp + g; d = tmp * e; 12
  • 13. Dr. Hussien M. Sharaf C. IDENTIFICATION AND ELIMINATION OF DEAD CODE Dead code is the code in a program which is executed but whose result is never used in any other computation.  Dead code wastes computation time.  Example:  int f (int x, int y) { int z = x + y; return x * y; } should be eliminated. 13
  • 14. Dr. Hussien M. Sharaf D. IMPROVING LOOPS    There are a lot of strategies for improving loops. A simple one is: Move loop invariants out of the loop Example: For y = 0 to Height-1 For x = 0 to Width-1 ' y*Width is invariant i = y*Width + x Process i Next x Next y  For y = 0 to Height-1 temp = y*Width For x = 0 to Width-1 i = temp + x Process i Next x Next y This link : http://www.aivosto.com/vbtips/loopopt.html illustrates all the strategies for interested readers. 14
  • 15. Dr. Hussien M. Sharaf E. IMPROVING FUNCTION CALLS    One strategy is by Removing recursion (function that calls itself). Example of recursive function: unsigned int factorial(unsigned int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } After removing recursion and using loops instead it will be: unsigned int factorial(unsigned int n) { int result = 1; if (n == 0) { return 1; } else { for (i = n; i>=1; i--) { result = result * i; } return result; } } 15
  • 16. Dr. Hussien M. Sharaf TARGET CODE OPTIMIZATION  Target Code optimization is done by improving the intermediate code, and removing the redundant code.  Example: Temp1 := int-to-real(60) Temp2 := id3 * temp1 Temp3 := id2 + temp2 Id1 := temp3 temp1 := id3 * 60.0 id1 := id2 + temp1 16
  • 17. Dr. Hussien M. Sharaf TARGET CODE OPTIMIZATION  Target code optimization include: a. b. Allocation and use of registers. Selection of better (safer) instructions and addressing modes. 17
  • 18. Dr. Hussien M. Sharaf 6. CODE GENERATOR Generates code for the target machine.  Selects appropriate machine instructions.  Allocates memory locations for variables.  Allocates registers for intermediate computations.  Three-address code temp1 := int2real(y) temp2 := temp1 * 2.5 temp3 := x + temp2 a : = temp3 Assembly code LOADI MOVF MULF LOADF ADDF STORF R1 , y F1 , R1 F2 , F1, 2.5 F3 , x F4 , F3, F2 a, F4 ;; R1 ;; F1 ;; F2 ;; F3 ;; F4 ;; a y int2real(R1) F1 * 2.5 x F3 + F2 F4 18
  • 19. Dr. Hussien M. Sharaf CODE GENERATOR  Generation target code example: temp := id3 * 60.0 id1 := id2 + temp MOVF MULF MOVF ADDF MOVF id3 , R2 #60.0, R2 id2 , R1 R2 , R1 R1, id1 ;; id3 R2 ;; R2 * 60.0 ;; id2 R1 ;; R2 + R1 ;; R1 id1 R2 R1 19
  • 20. Dr. Hussien M. Sharaf THANK YOU