31

Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

  • View
    227

  • Download
    5

Embed Size (px)

Citation preview

Page 1: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f
Page 2: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

Test Yourself #2

Page 3: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f
Page 4: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f
Page 5: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

Test Yourself #3

• Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

Page 6: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Algebraic optimization: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

Page 7: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Algebraic optimization: a := x * x b := 3 c := x d := c * c e := b << 1 f := a + d g := e * f

Page 8: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Copy propagation: a := x * x b := 3 c := x d := c * c e := b << 1 f := a + d g := e * f

Page 9: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Copy propagation: a := x * x b := 3 c := x d := x * x e := 3 << 1 f := a + d g := e * f

Page 10: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Constant folding: a := x * x b := 3 c := x d := x * x e := 3 << 1 f := a + d g := e * f

Page 11: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Constant folding: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f

Page 12: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Common subexpression elimination: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f

Page 13: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Common subexpression elimination: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f

Page 14: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f

Page 15: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f

Page 16: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Dead code elimination: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f

Page 17: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example

• Dead code elimination: a := x * x

f := a + a g := 6 * f

• This is the final form

Page 18: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

Test Yourself #4

Page 19: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

. תכנוה נורשת

Page 20: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

Int id1,id2

D

T L

INT L

ID1

ID2,

L.in

T.type

L.in

Page 21: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

DL

LL1,id {L.in = L1.in;

addtype(id.entery,L1.in);}

LT id {L.in = T.type;

addtype(id.entery,T.type);}

Tint | {T.type= integer;}

real {T.type= real;}

פתרון:

Page 22: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

DInt id1,id2

L

L1 , id1

T id2

Page 23: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

9תרגיל

Page 24: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

פתרון

E.true:

S1.next=E.false

S2.next=E.true

E.place++If(E.place=3)

Goto S.nextS1.code

E.place++If(E.place=3)

Goto S.nextS2.code

Goto E.true

E.false:

S.next:

E.trueE.place=0

E.code E.false

Page 25: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

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) }

Page 26: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

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’ _______) )}

Page 27: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

דוגמא:• S if a<b then start with a:=1 else start

with if true then b:=2100: E.place =0 /* Produced by R ε*/

101: if a < b goto______ /*Produced by Ea <b*/

102: goto_______ /* Produced by Ea <b */

103: E.place++; /*Produced by T1ε*/

104: if E.place ==3 goto________ /*Produced by T1ε*/

105: a:=1 /*Produces by S1a:=1*/

106: E.place++; /*Produced by T2ε*/107: if E.place ==3 goto________ /*Produced by T2ε*/

108: goto___ /*Produced by ETRUE*/

109: b:=2 /*Produced by b:=2*/

110: goto 103 /*Produces by S if E then start with ….*/

Page 28: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

דוגמא:• S if a<b then start with a:=1 else start

with if true then b:=2100: 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 /**/

Page 29: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

דוגמא:• S if a<b then start with a:=1 else start

with if true then b:=2100: 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 /**/

Page 30: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

דוגמא:• S if a<b then start with a:=1 else start

with if true then b:=2100: 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 /**/

Page 31: Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

Test yourself #5