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

Preview:

Citation preview

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

An Example

• Algebraic optimization: a := x * x b := 3 c := x d := c * c e := b << 1 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

An Example

• Copy propagation: a := x * x b := 3 c := x d := x * x e := 3 << 1 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

An Example

• Constant folding: a := x * x b := 3 c := x d := x * x e := 6 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

An Example

• Common subexpression elimination: a := x * x b := 3 c := x d := a e := 6 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

An Example

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

An Example

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

An Example

• Dead code elimination: a := x * x

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

• This is the final form

Test Yourself #4

. תכנוה נורשת

Int id1,id2

D

T L

INT L

ID1

ID2,

L.in

T.type

L.in

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

פתרון:

DInt id1,id2

L

L1 , id1

T id2

9תרגיל

פתרון

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

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

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

דוגמא:• 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 ….*/

דוגמא:• 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 /**/

דוגמא:• 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 /**/

דוגמא:• 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 /**/

Test yourself #5