83
Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

Embed Size (px)

DESCRIPTION

Previously The role of intermediate representations Two example languages – A high-level language – An intermediate language Lowering Correctness – Formal meaning of programs 3

Citation preview

Page 1: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

Fall 2015-2016 Compiler PrinciplesLecture 7: Lowering Correctness

Roman ManevichBen-Gurion University of the Negev

Page 2: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

2

Tentative syllabus

FrontEnd

Scanning

Top-downParsing (LL)

Bottom-upParsing (LR)

IntermediateRepresentatio

n

Lowering

Lowering Correctness

Optimizations

DataflowAnalysis

LoopOptimization

s

Code Generation

RegisterAllocation

InstructionSelection

Page 3: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

3

Previously• The role of intermediate representations• Two example languages– A high-level language– An intermediate language

• Lowering

• Correctness– Formal meaning of programs

Page 4: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

4

While syntax

A n | x | A ArithOp A | (A)ArithOp - | + | * | /B true | false

| A = A | A A | B | B B | (B)S x := A | skip | S; S | { S }

| if B then S else S| while B S

n Num numeralsx Var program variables

Page 5: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

5

IL syntax

V n | xR V Op V | VOp - | + | * | / | = | | << | >> | …C l: skip

| l: x := R| l: Goto l’| l: IfZ x Goto l’| l: IfNZ x Goto l’

IR C+

n Num Numeralsl Num Labelsx Temp Var Temporaries and variables

Page 6: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

6

Translation rules for expressionscgen(n) = (l: t:=n, t) where l and t are fresh

cgen(x) = (l: t:=x, t) where l and t are fresh

cgen(e1) = (P1, t1) cgen(e2) = (P2, t2)cgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)

where l and t are fresh

Page 7: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

7

Translation rules for statements

cgen(e) = (P, t)cgen(x := e) = P · l: x :=t where l is fresh

cgen(S1) = P1, cgen(S2) = P2

cgen(S1; S2) = P1 · P2

cgen(skip) = l: skip where l is fresh

Page 8: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

8

Translation rules for conditions

cgen(b) = (Pb, t), cgen(S1) = P1, cgen(S2) = P2

cgen(if b then S1 else S2) = Pb lb: IfZ t Goto label(P2)

P1

lfinish: Goto Lafter

P2

lafter: skip

where lb, lfinish, lafter are fresh

Page 9: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

9

Translation rules for loopscgen(b) = (Pb, t), cgen(S) = P cgen(while b S) =

lbefore: skip Pb

IfZ t Goto lafter

P

lloop: Goto Lbefore

lafter: skip

where lafter, lbefore, lloop are fresh

Page 10: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

10

Translation example

1: t1 := 1372: t2 := 33: t3 := t1 + t24: y := t35: t4 := x6: t5 := 07: t6 := t4=t58: IfZ t6 Goto 129: t7 := y10: z := t711: Goto 1412: t8 := y13: x := t814: skip

y := 137+3;if x=0 z := y;else x := y;

Page 11: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

11

agenda• Operational semantics of While• Operational semantics of IL• Formalizing the correctness of lowering• Proving correctness

Page 12: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

12

Correctness

Page 13: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

13

Compiler correctness

• Intuitively, a compiler translates programs in one language (usually high) to another language (usually lower) such that they are bot equivalent

• Our goal is to formally define the meaning of this equivalence

• But first, we must define the meaning of a programming language

Page 14: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

14

Formal semantics

Page 15: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

15

Operational semanticsof while

Page 16: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

16

While syntax reminder

A n | x | A ArithOp A | (A)ArithOp - | + | * | /B true | false

| A = A | A A | B | B B | (B)S x := A | skip | S; S | { S }

| if B then S else S| while B S

n Num numeralsx Var program variables

Page 17: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

17

Semantic categories

Z Integers {0, 1, -1, 2, -2, …}TTruth values {ff, tt}State Var Z

Example state: =[x5, y7, z0]Lookup: (x) = 5Update: [x6] = [x6, y7, z0]

Page 18: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

18

Semantics of expressions

Page 19: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

19

Semantics of arithmetic expressions• Semantic function A : State Z• Defined by induction on the syntax tree

n = n x = (x) a1 + a2 = a1 + a2 a1 - a2 = a1 - a2 a1 * a2 = a1 a2 (a1) = a1 --- not needed - a = 0 - a1

• Compositional• Expressions in While are side-effect free

Page 20: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

20

Semantics of boolean expressions• Semantic function B : State T• Defined by induction on the syntax tree

true = tt false = ff a1 = a2 = a1 a2 = b1 b2 = ß b =

• Compositional• Expressions in While are side-effect free

Page 21: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

21

Natural operating semantics

• Developed by Gilles Kahn [STACS 1987]

• ConfigurationsS, Statement S is about to execute on state Terminal (final) state

• TransitionsS, ’ Execution of S from will terminate

with the result state ’– Ignores non-terminating computations

Page 22: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

22

Natural operating semantics

• defined by rules of the form

• The meaning of compound statements is defined using the meaning immediate constituent statements

S1, 1 1’, … , Sn, n n’ S, ’

if…

premise

conclusion

side condition

Page 23: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

23

Natural semantics for While x := a, [x a][assns]

skip, [skipns]

S1, ’, S2, ’ ’’S1; S2, ’’ [compns]

S1, ’ if b then S1 else S2, ’

if b = tt[ifttns]

S2, ’ if b then S1 else S2, ’

if b = ff[ifffns]

axioms

Page 24: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

24

Natural semantics for While

S, ’, while b S, ’ ’’while b S, ’’

if b = tt[whilettns]

while b S, if b = ff[whileffns]

Non-compositional

Page 25: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

25

Executing the semantics

Page 26: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

26

Example

• Let 0 be the state which assigns zero to all program variables

x:=x+1, 0

skip, 0 0

0[x1]

skip, 0 0, x:=x+1, 0 0[x1] skip; x:=x+1, 0 0[x1]

x:=x+1, 0 0[x1]if x=0 then x:=x+1 else skip, 0 0[x1]

Page 27: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

27

Derivation trees

• Using axioms and rules to derive a transition S, ’ gives a derivation tree– Root: S, ’– Leaves: axioms– Internal nodes: conclusions of rules• Immediate children: matching rule premises

Page 28: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

28

Derivation tree example 1• Assume 0=[x5, y7, z0]

1=[x5, y7, z5]2=[x7, y7, z5]3=[x7, y5, z5]

(z:=x; x:=y); y:=z, 0 3

(z:=x; x:=y), 0 2 y:=z, 2 3

z:=x, 0 1 x:=y, 1 2

[assns] [assns]

[assns][compns]

[compns]

Page 29: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

29

Derivation tree example 1• Assume 0=[x5, y7, z0]

1=[x5, y7, z5]2=[x7, y7, z5]3=[x7, y5, z5]

(z:=x; x:=y); y:=z, 0 3

(z:=x; x:=y), 0 2 y:=z, 2 3

z:=x, 0 1 x:=y, 1 2

[assns] [assns]

[assns][compns]

[compns]

Page 30: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

30

Top-down evaluation via derivation trees

• Given a statement S and an input state find an output state ’ such that S, ’

• Start with the root and repeatedly apply rules until the axioms are reached– Inspect different alternatives in order

• Theorem: In While, ’ and the derivation tree are unique

Page 31: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

31

Top-down evaluation example

• Factorial program with x = 2• Shorthand: W=while (x=1) { y:=y*x; x:=x-1 }

y:=1; while (x=1) { y:=y*x; x:=x-1 }, [y 2][x1]

y:=1, [y 1] W, [y 1] [y 2, x 1]

y:=y*x; x:=x-1, [y 1] [y 2][x1] W, [y 2][x1] [y 2, x 1]

y:=y*x, [y 1] [y 2] x:=x-1, [y 2] [y 2][x1][assns]

[compns]

[assns]

[compns]

[whileffns]

[whilettns]

[assns]

Page 32: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

32

Properties of natural semantics

Page 33: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

33

Program termination

• Given a statement S and input – S terminates on if there exists a state ’ such

that S, ’– S loops on if there is no state ’ such that

S, ’• Given a statement S– S always terminates if

for every input state , S terminates on – S always loops if

for every input state , S loops on

Page 34: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

34

Semantic equivalence

• S1 and S2 are semantically equivalent if for all and ’ S1, ’ if and only if S2, ’

• Simple examplewhile b do Sis semantically equivalent to:if b then (S; while b S) else skip– Read proof in pages 26-27

Page 35: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

35

Properties of natural semantics

• Equivalence of program constructs– skip; skip is semantically equivalent to skip – ((S1; S2); S3) is semantically equivalent to

(S1; (S2; S3))– (x:=5; y:=x*8) is semantically equivalent to (x:=5; y:=40)

Page 36: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

36

Equivalence of {S1; S2}; S3 and S1; {S2; S3}

Page 37: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

37

Equivalence of {S1; S2}; S3 and S1; {S2; S3}

(S1; S2), 12, S3, 12 ’ {S1; S2}; S3, ’

S1, 1, S2, 1 12

S1, 1, {S2; S3}, 1 ’ S1; {S2; S3}, ’

S2, 1 12, S3, 12 ’

Assume (S1; S2); S3, ’ then the following unique derivation tree exists:

Using the rule applications above, we can construct the following derivation tree:

And vice versa.

Page 38: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

38

Deterministic semantics for While• Theorem: for all statements S and states 1, 2

if S, 1 and S, 2 then 1= 2

Page 39: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

39

The semantics of statements

• The meaning of a statement S is defined as

• Examples:skip = x:=1 = [x 1]while true do skip = undefined

S = ’ if S, ’ else

Page 40: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

40

Operational semanticsof IL

Page 41: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

41

IL syntax reminder

V n | xR V Op V |VOp - | + | * | / | = | | << | >> | …C l: skip

| l: x := R| l: Goto l’| l: IfZ x Goto l’

IR C+

n Num Numeralsl Num Labelsx Var Temp Variables and temporaries

Page 42: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

42

Intermediate program states

Z Integers {0, 1, -1, 2, -2, …}

IState (Var Temp {pc}) Z– Var, Temp, and {pc} are all disjoint– For every state m and program

P=1:c1,…,n:cn we have that 1 m(pc) n+1• We can check that the labels used in P are within

the range 1..n

Page 43: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

43

Rules for executing commands

• We will use rules of the following form

• Here m is the pre-state, which is scheduled to be executed as the program counter indicates, and m’ is the post-state

• The rules specialize for the particular type of command C and possibly other conditions

m(pc) = l P(l) = Cm m’

Page 44: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

44

Rules for executing commandsm(pc) = l P(l) = skip

m m[pcl+1]

m(pc) = l P(l) = Goto l’

m m[pcl’]

m(pc) = l P(l) = x:=v1 op v2m m[pcl+1, xM(v1) op

M(v2)]

M(v)= m(v) v VarTemp

v else

m(pc) = l P(l) = x:=v

m m[pcl+1, xM(v)]

M(v)= m(v) v VarTemp v else

Page 45: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

45

Rules for executing commandsm(pc) = l P(l) = IfZ x Goto l’

m(x)=0m m[pcl’]

m(pc) = l P(l) = IfZ x Goto l’m(x)0

m m[pcl+1] m(pc) = l P(l) = IfNZ x Goto l’m(x)

0m m[pcl’]

m(pc) = l P(l) = IfNZ x Goto l’m(x)=0m m[pcl+1]

Page 46: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

46

Executing programs

• For a program P=1:c1,…,n:cn

we define executions as finite or infinite sequences m1 m2 … mn …

• We write m * m’ if there is a finite execution starting at m and ending at m’:

m = m1 m2 … mn = m’

Page 47: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

47

Semantics of a program

• For a program P=1:c1,…,n:cn

and a state m s.t. m(pc)=1we define the result of executing P on m as

• Lemma: the function is well-defined(i.e., at most one output state)

P m = m’ if m * m’ and m’(pc)=n+1 else

Page 48: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

48

Execution example

• Execute the following intermediate language program on a state where all variables evaluate to 0

1: t1 := 1372: y := t1 + 33: IfZ x Goto 74: t2 := y5: z := t26: Goto 97: t3 := y8: x := t39: skip

m = [pc1, t10 , t20 , t30 , x0 , y0] ?

Page 49: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

49

Execution example

• Execute the following intermediate language program on a state where all variables evaluate to 0

1: t1 := 1372: y := t1 + 33: IfZ x Goto 74: t2 := y5: z := t26: Goto 97: t3 := y8: x := t39: skip

m = [pc1, t10 , t20 , t30 , x0 , y0] m[pc2, t1137] m[pc3, t1137, y140] m[pc7, t1137, y140] m[pc8, t1137, t3140, y140] m[pc9, t1137, t3140, , x140, y140] m[pc10, t1137, t3140, , x140, y140]

Page 50: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

50

Proof methodology

Page 51: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

51

Structural induction

• To prove a property of a derivation tree– Prove property holds for leaves– Assume property holds on all sub-trees of a given

node and establish that it holds for the node• Conclude that the property holds for every

derivation tree

Page 52: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

52

Defining and Proving equivalence for expressions

Page 53: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

53

Exercise 1

• Are the following equivalent in your opinion?

x := 137 1: t1 := 1372: x := t1

ILWhile

Page 54: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

54

Exercise 2

• Are the following equivalent in your opinion?

x := 137 1: y := 1372: x := y

ILWhile

Page 55: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

55

Exercise 3

• Are the following equivalent in your opinion?

x := 137 2: t2 := 1383: t1 := 1374: x := t1

ILWhile

Page 56: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

56

Exercise 4

• Are the following equivalent in your opinion?

x := 137 2: t2 := 1383: t1 := 1374: x := t15: t1 := 138

ILWhile

Page 57: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

57

Equivalence of arithmetic expressions

• Define TVar = Var Temp• While state: Var Z• IL state: m (Var Temp {pc}) Z• An arithmetic While expression a is equivalent to an

IL program P and xTVar iff for every input state m such that m(pc)=label(P):

a m|Var = (P m) x

Page 58: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

58

Defining equivalence for expressions

ILWhile

(P, t)a

(P m) t

a m|Var

cgen

=

(P, t)a if m: m(pc)=label(P)

Definition:

Page 59: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

59

Equivalence of Boolean expressions• A Boolean While expression b is equivalent to an IL

program P and xTVar iff for every input state m such that m(pc)=label(P): b m|Var = tt and (P m) x = 1 orb m|Var = ff and (P m) x = 0

Page 60: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

60

Equivalence of atomic expressionscgen(n) = (l: t:=n, t) where l and t are fresh

cgen(x) = (l: t:=x, t) where l and t are fresh

Claim: n (l: t:=n, t)Proof: choose w.l.o.g m such that m(pc)=ln m|Var = n(l: t:=n m) t = m[pcl+1, tn] t = nQ.E.D

Claim: n (l: t:=x, t)Proof: choose w.l.o.g m such that m(pc)=lx m|Var = m(x) since xVar (l: t:=x m) t = m[pcl+1, tm(x)] t = m(x)Q.E.D

Page 61: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

61

Lemmas for expressionscgen(e1) = (P1, t1) cgen(e2) = (P2, t2)cgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)

where l and t are fresh

Lemma 1: Let cgen(e1 op e2)=(P, t)then P always terminates

Lemma 2 (sequential execution): Let Pe and P be two IL such that (Pe, t)=cgen(e) and labels(Pe) labels(P) = andTemps(Pe) Temps(P) = {t}Then for every m, such that m(pc)=label(P1)(Pe m) = m’ such that m’(pc) = label(P).

Moreover, let =m|Var

then Pe · P m|Var = (P m[te])|Var.

Page 62: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

62

Lemmas for expressionscgen(e1) = (P1, t1) cgen(e2) = (P2, t2)cgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)

where l and t are fresh

Lemma 4: Let P1 and P2 be two IL programs such thatcgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)Then for every m1, m2 such that m1(pc)=label(P1) and m2(pc)=label(P2)(P1· P2 m1) t1 = (P1 m1) t1

(P1· P2 m1) t2 = (P2 m1) t2

Lemma 3: Let Temps(P) stand for the set of temporaries appearing in P.If cgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)Then Temps(P1) Temps(P2) =

Page 63: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

63

Equivalence of compound expressionscgen(e1) = (P1, t1) cgen(e2) = (P2, t2)cgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)

where l and t are fresh

Claim: Let P= P1· P2· l: t:=t1 op t2 then e1 op e2 (P, t)Proof: choose w.l.o.g m such that m(pc)=label(P1)let =m|Var

e1 + e2 = e1 + e2 by the induction hypothesis: e1 (P1, t1) and e2 (P2, t2)Denote m’ = P1· P2 mBy Lemma 4, we have that(P1· P2 m) t1 = (P1 m) t1 = e1 = m’ t1

(P1· P2 m) t2 = (P2 m) t2 = e2 = m’ t2

Therefore, by Lemma 2(P m) t = (l: t:=t1 op t2 m’) t = m’[pcl+1, tm’(t1) op m’(t2) m) t = e1 + e2 = e1 + e2 Q.E.D

Page 64: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

64

Conclusion

• Missing: proof for Boolean expression(exercise for home)

• Theorem 1: for each While expression e we have that cgen(e) e

Page 65: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

65

Defining and Proving equivalence for statements

Page 66: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

66

State equivalence• Define m iff =m|Var

– That is, for each xVar (x)=m(x)

Page 67: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

67

Statement equivalence• A While statement S is equivalent to an IL program P

iff for every input state m such that m(pc)=label(P):S m|Var (P )|Var

Page 68: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

68

Defining equivalence for statements

ILWhile

PScgen

P mS

PS if m, : m(pc)=label(P) and

m

Definition:

Page 69: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

69

Equivalence of skip cgen(skip) = l: skip where l is fresh Claim: skip l: skipProof: choose w.l.o.g m such that m(pc)=land =m|Var

skip m|Var= m|Var

(l: skip m)|Var = m[pcl+1]|Var = m|Var

Q.E.D

Page 70: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

70

Equivalence of assignments

Lemma 5: let DefVars(P) denote the variables being assigned-to in P.If DefVars(P)=Then for every m such that m(pc)=label(P)(P m)|Var = m|Var

cgen(a) = (P, t)cgen(x := a) = P · l: x := t where l is fresh

Page 71: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

71

Equivalence of assignments

Claim: x := a P · l: x := tProof: choose w.l.o.g m such that m(pc)=land =m|Var

x := a = [x a]Let m’ = P m. Then from Theorem 1: a (P,t). That is, a = m’(t)From Lemma 2: P · l: x := t m = l: x := t m’ = m’[pcl+1, xm’(t)] = m’[pcl+1, xa]Now, since DefVars(P)= by Lemma 5, we have that m’|Var = m|Var

Therefore, [x a]|Var = P · l: x := t|Var

Q.E.D

cgen(a) = (P, t)cgen(x := a) = P · l: x := t where l is fresh

Page 72: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

72

Natural semantics for sequencing

S1, ’, S2, ’ ’’S1; S2, ’’ [compns]

Lemma 6: S1; S2 = S2 (S1 )

Page 73: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

73

Helper lemmas for sequencing

Lemma 7: Let P1 and P1 be two IL programs such thatsuch that cgen(S1; S2) = P1 · P2

Then labels(P1) labels(P2) = andTemps(P1) Temps(P2) = and for every m, such that m(pc)=label(P1)(P1 m) = m’ such that m’(pc) = label(P2).

Moreover, P1 · P2 m = P2 m’

cgen(S1) = P1, cgen(S2) = P2

cgen(S1; S2) = P1 · P2

Page 74: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

74

Equivalence of sequencing

Claim: assume S1 P1 and S2 P2

then S1; S2 P1 · P2

Proof: choose w.l.o.g m such that m(pc)=land =m|Var

By Lemma 7, we have that(P1 m) = m’ such that m’(pc) = label(P2)By the induction hypothesis m’|Var= S1 .By Lemma 7, we have that P1 · P2 m = P2 m’Let S1 = ’.By the induction hypothesis, since m’ ’, we have thatP2 m’ S2 ’By the definition of the natural semantics (lemma 6), we have thatS1; S2 P1 · P2 mQ.E.D

cgen(S1) = P1, cgen(S2) = P2

cgen(S1; S2) = P1 · P2

Page 75: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

75

Equivalence of conditionscgen(b) = (Pb, t), cgen(S1) = P1, cgen(S2) = P2

cgen(if b then S1 else S2) = Pb lb: IfZ t Goto label(P2)

P1

lfinish: Goto Lafter

P2

lafter: skip

where lb, lfinish, lafter are fresh

Lemma 8: for all states we have thatIf b = tt then, if b then S1 else S2 = S1 If b = ff then, if b then S1 else S2 = S2

Page 76: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

76

Helper lemmascgen(b) = (Pb, t), cgen(S1) = P1, cgen(S2) = P2

cgen(if b then S1 else S2) = Pb lb: IfZ t Goto label(P2)

P1

lfinish: Goto Lafter

P2

lafter: skip

where lb, lfinish, lafter are fresh

Page 77: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

77

Equivalence of conditions: ffClaim: Let P=cgen(if b then S1 else S2)if b then S1 else S2 PProof: choose w.l.o.g m such that m(pc)=land =m|Var

From Theorem 1: b (P,t) therefore Pb m t = 0Case ff: assume b = ff. By Lemma 8: if b then S1 else S2 = S2 = 2 By Lemma 2 Pb · lb: IfZ t Goto label(P2) · P1 · lfinish: Goto Lafter · P2 · lafter: skip m IfZ t Goto label(P2) · P1 · lfinish: Goto Lafter · P2 · lafter: skip(m[t0]) IfZ t Goto label(P2) · P1 · lfinish: Goto Lafter · P2 · lafter: skip(m[pclabel(P2)]) By the induction hypothesis and lemma 2:Let P2 m[pclabel(P2)] = m2 then m2 2

IfZ t Goto label(P2) · P1 · lfinish: Goto Lafter · P2 · lafter: skip(m2[pclafter]) = m2[pclafter+1] m2

Page 78: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

78

Equivalence of conditions: tt Claim: Let P=cgen(if b then S1 else S2)if b then S1 else S2 PProof: choose w.l.o.g m such that m(pc)=land =m|Var

From Theorem 1: b (P,t) therefore Pb m t = 0Case ff: assume b = tt. By Lemma 8: if b then S1 else S2 = S1 = 1 By Lemma 2 Pb · lb: IfZ t Goto label(P2) · P1 · lfinish: Goto Lafter · P2 · lafter: skip m IfZ t Goto label(P2) · P1 · lfinish: Goto Lafter · P2 · lafter: skip(m[t1]) IfZ t Goto label(P2) · P1 · lfinish: Goto Lafter · P2 · lafter: skip(m[pclabel(P1)]) By the induction hypothesis and lemma 2:Let P1 m[pclabel(P1)] = m1 then m1 1

IfZ t Goto label(P2) · P1 · lfinish: Goto Lafter · P2 · lafter: skip(m1[pclfinish]) = m1[pclafter] = m1[pclafter+1] m1

Page 79: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

79

Equivalence for loopscgen(b) = (Pb, t), cgen(S) = P cgen(while b S) =

lbefore: skip Pb

IfZ t Goto lafter

P

lloop: Goto Lbefore

lafter: skip

where lafter, lbefore, lloop are fresh

Page 80: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

80

Proof outline

• Let be a state• We will split the proof into two cases:

1. while b S terminates(there is a derivation tree)

2. while b S loops(no derivation tree)

Page 81: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

81

Case 1: while b S terminates

Page 82: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

82

Case 2: while b S loops

Page 83: Compiler Principles Fall 2015-2016 Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev

Next lecture:Dataflow-based Optimization