Upload
felix-cobb
View
222
Download
0
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
Fall 2015-2016 Compiler PrinciplesLecture 7: Lowering Correctness
Roman ManevichBen-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
3
Previously• The role of intermediate representations• Two example languages– A high-level language– An intermediate language
• Lowering
• Correctness– Formal meaning of programs
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
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
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
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
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
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
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;
11
agenda• Operational semantics of While• Operational semantics of IL• Formalizing the correctness of lowering• Proving correctness
12
Correctness
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
14
Formal semantics
15
Operational semanticsof while
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
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]
18
Semantics of expressions
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
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
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
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
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
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
25
Executing the semantics
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]
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
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]
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]
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
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]
32
Properties of natural semantics
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
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
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)
36
Equivalence of {S1; S2}; S3 and S1; {S2; S3}
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.
38
Deterministic semantics for While• Theorem: for all statements S and states 1, 2
if S, 1 and S, 2 then 1= 2
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
40
Operational semanticsof IL
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
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
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’
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
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]
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’
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
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] ?
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]
50
Proof methodology
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
52
Defining and Proving equivalence for expressions
53
Exercise 1
• Are the following equivalent in your opinion?
x := 137 1: t1 := 1372: x := t1
ILWhile
54
Exercise 2
• Are the following equivalent in your opinion?
x := 137 1: y := 1372: x := y
ILWhile
55
Exercise 3
• Are the following equivalent in your opinion?
x := 137 2: t2 := 1383: t1 := 1374: x := t1
ILWhile
56
Exercise 4
• Are the following equivalent in your opinion?
x := 137 2: t2 := 1383: t1 := 1374: x := t15: t1 := 138
ILWhile
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
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:
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
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
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.
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) =
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
64
Conclusion
• Missing: proof for Boolean expression(exercise for home)
• Theorem 1: for each While expression e we have that cgen(e) e
65
Defining and Proving equivalence for statements
66
State equivalence• Define m iff =m|Var
– That is, for each xVar (x)=m(x)
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
68
Defining equivalence for statements
ILWhile
PScgen
P mS
PS if m, : m(pc)=label(P) and
m
Definition:
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
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
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
72
Natural semantics for sequencing
S1, ’, S2, ’ ’’S1; S2, ’’ [compns]
Lemma 6: S1; S2 = S2 (S1 )
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
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
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
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
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
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
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
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)
81
Case 1: while b S terminates
82
Case 2: while b S loops
Next lecture:Dataflow-based Optimization