Download presentation
Presentation is loading. Please wait.
2
Test Yourself #2
5
Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f
6
An Example Algebraic optimization: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f
7
An Example Algebraic optimization: a := x * x b := 3 c := x d := c * c e := b << 1 f := a + d g := e * f
8
An Example Copy propagation: a := x * x b := 3 c := x d := c * c e := b << 1 f := a + d g := e * f
9
An Example Copy propagation: a := x * x b := 3 c := x d := x * x e := 3 << 1 f := a + d g := e * f
10
An Example Constant folding: a := x * x b := 3 c := x d := x * x e := 3 << 1 f := a + d g := e * f
11
An Example Constant folding: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f
12
An Example Common subexpression elimination: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f
13
An Example Common subexpression elimination: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f
14
An Example Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f
15
An Example Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f
16
An Example Dead code elimination: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f
17
An Example Dead code elimination: a := x * x f := a + a g := 6 * f This is the final form
18
Test Yourself #4
19
תכנוה נורשת.
20
Int id1,id2 D TL INT L ID1 ID2, L.in T.type L.in
21
D L L L1,id {L.in = L1.in; addtype(id.entery,L1.in);} L T id {L.in = T.type; addtype(id.entery,T.type);} T int | {T.type= integer;} real {T.type= real;} פתרון:
22
D Int id1,id2 L L1,id1 T id2
23
תרגיל 9
24
פתרון E.true: S1.next=E.false S2.next=E.true E.place++ If(E.place=3) Goto S.next S1.code E.place++ If(E.place=3) Goto S.next S2.code Goto E.true E.false: S.next: E.true E.place=0 E.code E.false
25
S if E then start with S1 else start with S2 {S.code=Gen(E.place’=0’) || E.code || gen(E.true ‘:’ E.place ‘++ if(’ E.place ‘=3) goto’ S.next ) || S1.code || gen(E.false ‘:’ E.place ‘++ if(’ E.place ‘=3) goto’ S.next ) || S2.code || gen(‘goto’ E.true) }
26
פתרון עם Backpatching S if R E then start with M1 T1 S1 else start with M2 T2 S2 {S.next_list= merge(T1.next_list,T2.next_list) backpatch (E.true_list, M1.quad) backpatch (E.false_list, M2.quad) backpatch (S1.next_list,M2.quad) backpatch (S2.next_list,M1_quad) Emit(‘goto’ M1.quad) } R ε {Emit(E.place=0(} T ε {Emit(E.place ‘++( T.next_list=makelist(nextquad); Emit(if(’ E.place ‘=3) goto’ _______) )}
27
דוגמא: S if a<b then start with a:=1 else start with if true then b:=2 100: E.place =0 /* Produced by R ε*/ 101: if a < b goto______ /*Produced by E a <b*/ 102: goto_______ /* Produced by E a <b */ 103: E.place++; /*Produced by T1 ε*/ 104: if E.place ==3 goto________ /*Produced by T1 ε*/ 105: a:=1 /*Produces by S1 a:=1*/ 106: E.place++; /*Produced by T2 ε*/ 107: if E.place ==3 goto________ /*Produced by T2 ε*/ 108: goto___ /*Produced by E TRUE*/ 109: b:=2 /*Produced by b:=2*/ 110: goto 103 /*Produces by S if E then start with ….*/
28
דוגמא: S if a<b then start with a:=1 else start with if true then b:=2 100: E.place =0 /* */ 101: if a < b goto__103 (M1.quad) /* Backpatched by S if E then start with…*/ 102: goto__106 (M2.quad) /* Backpatched by S if E then start with…*/ 103: E.place++; /**/ 104: if E.place ==3 goto________ /**/ 105: a:=1 /**/ 106: E.place++; /**/ 107: if E.place ==3 goto________ /**/ 108: goto___ /**/ 109: b:=2 /**/ 110: goto 103 /**/
29
דוגמא: S if a<b then start with a:=1 else start with if true then b:=2 100: E.place =0 /* */ 101: if a < b goto__103 (M1.quad) /* Backpatched by S if E then start with…*/ 102: goto__106 (M2.quad) /* Backpatched by S if E then start with…*/ 103: E.place++; /**/ 104: if E.place ==3 goto________ /**/ 105: a:=1 /**/ 106: E.place++; /**/ 107: if E.place ==3 goto________ /**/ 108: goto__109 /*Backpatched by S if E then S1*/ 109: b:=2 /**/ 110: goto 103 /**/
30
דוגמא: S if a<b then start with a:=1 else start with if true then b:=2 100: E.place =0 /* */ 101: if a < b goto__103 (M1.quad) /* Backpatched by S if E then start with…*/ 102: goto__106 (M2.quad) /* Backpatched by S if E then start with…*/ 103: E.place++; /**/ 104: if E.place ==3 goto________ /* Can’t BAckpatched but S.next_list 104*/ 105: a:=1 /**/ 106: E.place++; /**/ 107: if E.place ==3 goto________ /*Can’t BAckpatched but S.next_list 107*/ 108: goto__109 /*Backpatched by S if E then S1*/ 109: b:=2 /**/ 110: goto 103 /**/
31
Test yourself #5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.