Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * 60 1. Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *

Similar presentations


Presentation on theme: "Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * 60 1. Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *"— Presentation transcript:

1 Dr. Hussien Sharaf Dr Emad Nabil

2 Dr. Hussien M. Sharaf 2 position := initial + rate * 60 1. Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 * 60 3. Semantic analyzer := id 1 + id 2 * id 3 60 := id 1 + id 2 * id 3 60 inttoreal 4. Intermediate code generator temp 1 := inttoreal(60) temp 2 := id 3 * temp 1 temp 3 := id 2 + temp 2 id 1 := temp3 5. Code optimizer temp 1 := id 3 * 60.0 id 1 := id 2 + temp 1 6. Code generator MOVF id 3, R 2 MULF #60.0, R 2 MOVF id 2, R 1 ADDF R 2, R 1 MOVF R 1, id 1

3  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. Dr. Hussien M. Sharaf 3

4  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; } Dr. Hussien M. Sharaf 4

5  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. Dr. Hussien M. Sharaf 5

6  The semantic analyzer does the following:  Checks the static semantics of the language.  Annotates the syntax tree with type information, as shown in example.  $ Dr. Hussien M. Sharaf 6 Real a, x; Int y; a := x + y * 2.5; := real id a real+ real id x real * real inttorealliteral 2.5 real id y integer Annotated syntax tree

7  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. Dr. Hussien M. Sharaf 7

8  Example:  A TAC instruction have at most one instruction per line and can have at most three operands. Dr. Hussien M. Sharaf 8 Three-address code temp1 := inttoreal(y) temp2 := temp1 * 2.5 temp3 := x + temp2 a := temp3 := real id a real+ real id x real * real inttorealliteral 2.5 real id y integer a := x + y * 2.5;

9  Code optimization can be applied to:  Intermediate code – independent of the target machine.  Target code – dependent on the target machine. Dr. Hussien M. Sharaf 9

10  Intermediate code optimization include: A. Constant folding. B. Elimination of common sub- expressions. C. Identification and elimination of unreachable code (called dead code). D. Improving loops. E. Improving function calls. Dr. Hussien M. Sharaf 10

11  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) Dr. Hussien M. Sharaf 11

12  Replace the common expressions with a single variable holding the computed value.  Example: Dr. Hussien M. Sharaf 12 a = b * c + g; d = b * c * e; tmp = b * c; a = tmp + g; d = tmp * e;

13  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; should be eliminated. return x * y; } Dr. Hussien M. Sharaf 13

14  There are a lot of strategies for improving loops.  A simple one is: Move loop invariants out of the loop  Example:  This link : http://www.aivosto.com/vbtips/loopopt.html illustrates all the strategies for interested readers.http://www.aivosto.com/vbtips/loopopt.html Dr. Hussien M. Sharaf 14 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

15  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; } Dr. Hussien M. Sharaf 15

16  Target Code optimization is done by improving the intermediate code, and removing the redundant code.  Example: temp 1 := id 3 * 60.0 id 1 := id 2 + temp 1 Dr. Hussien M. Sharaf 16 Temp1 := int-to-real(60) Temp2 := id3 * temp1 Temp3 := id2 + temp2 Id1 := temp3

17  Target code optimization include: a. Allocation and use of registers. b. Selection of better (safer) instructions and addressing modes. Dr. Hussien M. Sharaf 17

18  Generates code for the target machine.  Selects appropriate machine instructions.  Allocates memory locations for variables.  Allocates registers for intermediate computations. Dr. Hussien M. Sharaf 18 temp1 := int2real(y) temp2 := temp1 * 2.5 temp3 := x + temp2 a : = temp3 LOADIR1, y;; R1= y MOVFF1, R1;; F1= int2real(R1) MULFF2, F1, 2.5;; F2= F1 * 2.5 LOADFF3, x;; F3= x ADDFF4, F3, F2 ;; F4= F3 + F2 STORFa, F4;; a= F4 Three-address codeAssembly code

19  Generation target code example: temp := id3 * 60.0 id1 := id2 + temp Dr. Hussien M. Sharaf 19 MOVF R2, id3;; R2= id3 MULF R2, #60.0 ;; R2= R2 * 60.0 MOVF R1, id2 ;; R1= id2 ADDFR1, R2 ;; R1= R2 + R1 MOVF id1, R1;; id1= R1

20 Dr. Hussien M. Sharaf


Download ppt "Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * 60 1. Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *"

Similar presentations


Ads by Google