Download presentation
1
Compiler Designs and Constructions
Chapter 11: Intermediate Code Generation(part II) Compiler Designs and Constructions Chapter 11: Intermediate Code Generation(part II) Objectives: Dr. Mohsen Chitsaz Chapter 11: Intermediate Code Generation (part II) COSC 470
2
Method II Flow of Control
Translation of Boolean Expression (Position Reached) True (Exit) False (Exit) Chapter 11: Intermediate Code Generation (part II)
3
Method II Flow of Control
Example: IF (a<b OR c>d) THEN N = M+K; IF a<b GOTO 4 IF c>d GOTO 4 GOTO 6 T1 = M + K N = T1 True 4 False 6 Chapter 11: Intermediate Code Generation (part II)
4
Chapter 11: Intermediate Code Generation (part II)
Address of Jump? Functions: Backpatch (P, I) Quadruple P takes I as a target Makelist (i) Create a new list with element I, (an index into the array of quadruples) Makelist returns a pointer Merge (P1, P2) Concatenate pointer P1 and P2 into one list, returns a pointer to the new list Chapter 11: Intermediate Code Generation (part II)
5
Chapter 11: Intermediate Code Generation (part II)
Example WHILE (a=b AND a=c) DO { N = a+b; c = N; } IF a=b GOTO __ GOTO __ IF a=c GOTO __ T1 = a+b N = T1 C=N Marker1 (True)=1-> 3 Marker2 (True)=3-> 5 Marker3 (False)=2-> 9 Marker4 (False)=4-> 9 How do I know the address? Chapter 11: Intermediate Code Generation (part II)
6
Chapter 11: Intermediate Code Generation (part II)
E. True Target Location (Jump) (MARKER) E.False E E1 AND E2 To find the jump address, we need to check E2 Modify the Grammar: E E1 OR M E2 E E1 AND M E2 E Not E1 E (E1) E id E id1 Relop id2 M Chapter 11: Intermediate Code Generation (part II)
7
Chapter 11: Intermediate Code Generation (part II)
M.Quad = NextQuad E id1 Relap id2 E.True = Makelist (NextQuad) E.False = Makelist (NextQuad+1) Gen (IF id1.Place Relap id2.Place GOTO ) Gen (GOTO ) E id Gen (IF id1.Place GOTO ) Gen (GOTO ) Chapter 11: Intermediate Code Generation (part II)
8
Chapter 11: Intermediate Code Generation (part II)
E (E1) E.True = E1.True E.False = E1.False E Not E1 E.True = E1.False E.False=E1.True E E1 AND M E2 Backpatch (E1.True, M.Quad) E.True := E2.True E.False := Merge (E1.False, E2.False) Chapter 11: Intermediate Code Generation (part II)
9
Chapter 11: Intermediate Code Generation (part II)
E E1 OR M E2 Backpatch (E1.False, M.Quad) E.True := Merge (E1.True, E2.True) E.False := E2.False Chapter 11: Intermediate Code Generation (part II)
10
Chapter 11: Intermediate Code Generation (part II)
Practice: 1- Build the Parse Tree 2- Add attributes a<b OR c<d AND e>f Chapter 11: Intermediate Code Generation (part II)
11
Chapter 11: Intermediate Code Generation (part II)
0. IF a<b GOTO___ True GOTO 2 IF c<d GOTO 4 GOTO___ False IF c>f GOTO ___ True GOTO ___ False E E1 AND M E Backpatch(2,4) E E1 OR M E2 Backpatch (1,2) Chapter 11: Intermediate Code Generation (part II)
12
Chapter 11: Intermediate Code Generation (part II)
Flow of Control Statement IF (exp) Statement ELSE Statement WHILE (exp) Statement {State_list} Statement Assignment_Statement State_list State_list; Statement State_list Statement Chapter 11: Intermediate Code Generation (part II)
13
Chapter 11: Intermediate Code Generation (part II)
Flow of Control What should we do with nested WHILE loop? WHILE { WHILE . { . . . } } Chapter 11: Intermediate Code Generation (part II)
14
Chapter 11: Intermediate Code Generation (part II)
Method 1: Using Stack Method 2: Using Markers Statement WHILE M1(exp) M2 Statement M Example: WHILE (a>b) WHILE (c>d) .. END Chapter 11: Intermediate Code Generation (part II)
15
Chapter 11: Intermediate Code Generation (part II)
Method 1 0. IF a> b GOTO 2 GOTO ___ 2. IF c<d GOTO 4 GOTO __ GOTO 2 False2: GOTO 0 False1: Chapter 11: Intermediate Code Generation (part II)
16
Chapter 11: Intermediate Code Generation (part II)
Method 2 Statement WHILE M1(exp) M2 Statement Chapter 11: Intermediate Code Generation (part II)
17
Chapter 11: Intermediate Code Generation (part II)
Grammar Statement IF (exp) M1 Statement1 N ELSE M2 Statement2 Backpatch(E.True, M1.Quad) Backpatch (E.False, M2.Quad) Statement.Next: Merge (S1.Next, N.Next, S2.Next) { Next is the next statement to Jump} N N.Next = Makelist (NextQuad) Gen (GOTO) Chapter 11: Intermediate Code Generation (part II)
18
Chapter 11: Intermediate Code Generation (part II)
Grammar M M.Quad = NextQuad Statement IF (exp) M Statement Backpatch (E.True, M.Quad); S.Next = Merge (E.False, S.Next) Statement WHILE M1(exp) M2 Statement Gen (GOTO M1.Quad) Backpatch(S.Next, M1.Quad) Backpatch(E.True, M2.Quad) S.Next = E.False Chapter 11: Intermediate Code Generation (part II)
19
Chapter 11: Intermediate Code Generation (part II)
Grammar Statement { State_list } S.Next = L.Next Statement Assignment_Statement S.Next = Makelist (NextQuad); State_list State_List1; M Statement Backpatch (L1.Next, M.Quad) L.Next := S.Next State_List Statement Chapter 11: Intermediate Code Generation (part II)
20
Chapter 11: Intermediate Code Generation (part II)
Practice: 1- Build parse tree 2- Add attributes WHILE (a< b) IF (c > d) a = b + c; Chapter 11: Intermediate Code Generation (part II)
21
Chapter 11: Intermediate Code Generation (part II)
WHILE (a< b) DO IF (c > d) THEN a = b + c; 0. IF a< b GOTO 2 GOTO 7 IF c>d GOTO 4 GOTO 0 T1 = b+c a = T1 GOTO Chapter 11: Intermediate Code Generation (part II)
22
Chapter 11: Intermediate Code Generation (part II)
Static Array List: Array [1..20, 1..30] of datatype; How much space do we need? How do we store this array? Row Major Column Major Where is List [20,30] started? List [I,j] is stored at = +((I-1) * 30) * bpw + (j-1) * bpw = +30i + j – 31 (bpw)? Base Register –31 Compiled Time (Fixed) Index Register 30i+j Calculated Chapter 11: Intermediate Code Generation (part II)
23
Chapter 11: Intermediate Code Generation (part II)
Example B = List [I,j] 0- T1 = i+*30 1- T1 = T1 + j Index Register 2- T2 = -31 * 4 Assume bpw = 4 3- T3 = 4* T1 4- T4 = T2 [T3] 5- B = T4 Chapter 11: Intermediate Code Generation (part II)
24
Chapter 11: Intermediate Code Generation (part II)
Array Code for Array []= A i T1 T1 = A[i] = B T1 T1 = B For K dimensional array: (Row Major) A [i1,i2,…ik] =(i1-1) d2,d3…dk+(i2-1)d3,d4…dk + …+(ik-1-1)dk+(ik-1) = i1d2d3…dk+i2d3d4…dk+…+dk+1 =(d2d3…dk+d3d4…dk+…+dk+1) Fixed = D If Address of A is THEN D is computed at Compile Time Chapter 11: Intermediate Code Generation (part II)
25
Chapter 11: Intermediate Code Generation (part II)
Array A L=E E E+E E (E) E L E elist ] L id elist elist, E elist id [ E Chapter 11: Intermediate Code Generation (part II)
26
Chapter 11: Intermediate Code Generation (part II)
Array elist id [ E elist.Place = E.Place elist.Ndim = 1; elist.Array = id.Place elist elist1, E T=NewTemp(); M=elist.Ndim + 1; Gen (t ‘=‘ elist1.Place ‘*’ Limit (elist1.Array, M)) Gen (t ‘=‘ t ‘+’ E.Place); elist.Array = elist1.Array elist.Place = t elist.Ndim = M Chapter 11: Intermediate Code Generation (part II)
27
Chapter 11: Intermediate Code Generation (part II)
Array L id L.Place = id.Place L.offset = Null L elist ] L.Place = NewTemp(); L.offset = NewTemp(); Gen (L.offset ‘=‘ ‘bpw’ ‘*’ elist.Place) E L IF (L.offset = Null) E.Place = L.Place ELSE { T = NewTemp(); Gen (T ‘=‘ L.Place ‘[‘ L.offset ‘]’); E.Place = T } Chapter 11: Intermediate Code Generation (part II)
28
Chapter 11: Intermediate Code Generation (part II)
Array E (E1) E.Place = E1.Place E E1 + E2 T = NewTemp() Gen (T ‘=‘ E1.Place + E2.Place); E.Place = T A L = E IF L.offset = Null Gen(L.Place ‘=‘ E.Place) ELSE Gen (L.Place ‘[‘ L.offset ‘]’ := E.Place) Chapter 11: Intermediate Code Generation (part II)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.