معالجات دقيقة - ج
معالجات دقيقة - ج
معالجات دقيقة - ج
z = x + y*17 ;
y = x/y + x ;
Symbol Table
Line Lexeme Token Attributes Values
1 { 1 Keyword
2 for 5 Keyword
3 ( 51 Punctuation
4 x 80 Identifier
5 = 29 Operator
6 1 90 Number 00000001
7 ; 53 Punctuation
8 <= 32 Operator
9 10 90 Number 00001010
10 ++ 27 Operator
11 ) 52 Punctuation
12 y 80 Identifier
13 20 90 Number 00010100
14 >= 34 Operator
15 5 90 Number 00000101
16 -- 28 Operator
17 z 80 Identifier 000010001
18 + 25 Operator
19 * 22 Operator
20 17 90 Number
21 / 23 Operator
22 } 2 Keyword
Token File
1 1 5 80 4
2 29 90 80 4
3 32 90 53 80
4 4 27 52 5
5 51 80 12 29
6 90 53 80 12
7 34 90 53 80
8 12 28 52 80
9 17 29 80 4
10 25 80 12 22
11 90 53 80 12
12 29 80 4 23
13 80 12 25 80
14 4 53 2 $
15
Syntax Analyzer:
format.
For
/\
/ \
x For
/\
/ \
y Assign
/\
z +
/\
x *
/\
y 17
look like:
1: x = 1
2: label_start
4: y = 20
5: loop
6: z = x + y * 17
7: y = x / y + x
8: y = y - 1
10: x = x + 1
12: label_end
L1 x =1
L2 IF x < 10
L3 goto L6
L4 T1 =0
L5 goto L7
L6 T1 =1
L7 IF x ==10
L8 goto L11
L9 T2 =0
L11 T2=1
L12 T3 = T1 || T2
L13 IF T3 == 1
L16 y==20
L7 IF y > 5
L19 T1 =0
L21 T1 =1
L22 IF y ==5
L23 goto L25
L24 T2 =0
L26 T2=1
L27 T3 = T1 || T2
L28 IF T3 == 1
L31 M1 = x + y*17
L32 z = M1
L33 goto L2
L34 m2 =x/y+x
L35 y=m2
L36 goto L2
To optimize the given source code segment, we can make the following
improvements:
1. Constant Folding: Evaluate constant expressions at compile time.
alternatives.
int temp2 = x / x;
z = x + y * temp1;
y = temp2 + x;
loop.
Code generation phase.
Assuming the compiler applies the common subexpression elimination
Target Architecture:
instructions.
Generated Code:
assembly
LOAD_CONST 1
STORE x
LOAD x
STORE temp
L1:
L2:
LOAD x
LOAD y
LOAD_CONST 17
MUL
ADD
STORE z
LOAD temp
LOAD x
ADD
STORE y
// Decrement y
LOAD y
LOAD_CONST 1
SUB
STORE y
LOAD y
LOAD_CONST 5
// Increment x
LOAD x
LOAD_CONST 1
ADD
STORE x
LOAD x
LOAD_CONST 10
// Program ends
```
Explanation:
3. Outer loop: Label `L1` marks the start of the outer loop.
4. Inner loop:Label `L2` marks the start of the inner loop.
8. Inner loop condition: Branch back to `L2` (continue inner loop) if `y`
10. Outer loop condition:Branch back to `L1` (continue outer loop) if `x`
11.Program end: The program execution ends here if the outer loop
completes.