Fall 2015-2016 Compiler Principles
Lecture 7: Lowering Correctness
Roman Manevich
Ben-Gurion University of the Negev
Tentative syllabus
Front
End
Intermediate
Representation
Optimizations
Code
Generation
Scanning
Lowering
Dataflow
Analysis
Register
Allocation
Top-down
Parsing (LL)
Lowering
Correctness
Loop
Optimizations
Instruction
Selection
Bottom-up
Parsing (LR)
2
Previously
• The role of intermediate representations
• Two example languages
– A high-level language
– An intermediate language
• Lowering
• Correctness
– Formal meaning of programs
3
While syntax
n
x
Num
Var
numerals
program variables
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
4
IL syntax
n
l
x
Num
Num
Temp
Var
Vn|x
R V Op V | V
Op - | + | * | / | = |
C l: skip
| l: x := R
| l: Goto l’
| l: IfZ x Goto l’
| l: IfNZ x Goto l’
IR C+
Numerals
Labels
Temporaries and variables
| << | >> | …
5
Translation rules for expressions
cgen(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)
where l and t are fresh
cgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)
6
Translation rules for statements
cgen(skip) = l: skip
where l is fresh
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
7
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)
where lb, lfinish, lafter
P1
are fresh
lfinish: Goto Lafter
P2
lafter: skip
8
Translation rules for loops
cgen(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
9
Translation example
y := 137+3;
if x=0
z := y;
else
x := y;
1: t1 := 137
2: t2 := 3
3: t3 := t1 + t2
4: y := t3
5: t4 := x
6: t5 := 0
7: t6 := t4=t5
8: IfZ t6 Goto 12
9: t7 := y
10: z := t7
11: Goto 14
12: t8 := y
13: x := t8
14: skip
10
agenda
•
•
•
•
Operational semantics of While
Operational semantics of IL
Formalizing the correctness of lowering
Proving correctness
11
Correctness
12
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
13
Formal semantics
14
Operational semantics
of while
15
While syntax reminder
n
x
Num
Var
numerals
program variables
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
16
Semantic categories
Z
T
State
Integers {0, 1, -1, 2, -2, …}
Truth values {ff, tt}
Var Z
Example state:
Lookup:
Update:
=[x 5, y 7, z 0]
(x) = 5
[x 6] = [x 6, y 7, z 0]
17
Semantics of expressions
18
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 - a 2
a1 * a2 = a1 a2
(a1) = a1 --- not needed
- a = 0 - a1
• Compositional
• Expressions in While are side-effect free
19
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
20
Natural operating semantics
• Developed by Gilles Kahn [STACS 1987]
• Configurations
S,
Statement S is about to execute on state
Terminal (final) state
• Transitions
S,
’
Execution of S from will terminate
with the result state ’
– Ignores non-terminating computations
21
Natural operating semantics
•
defined by rules of the form
side condition
premise
S1, 1
1’, … , Sn, n
S, ’
n’
if…
conclusion
• The meaning of compound statements is
defined using the meaning immediate
constituent statements
22
Natural semantics for While
[assns]
x := a,
[x a]
[skipns]
skip,
[compns]
S1, ’, S2, ’ ’’
S1; S2, ’’
[ifttns]
S1, ’
if b then S1 else S2,
[ifffns]
S2, ’
if b then S1 else S2,
axioms
’
’
if b = tt
if b = ff
23
Natural semantics for While
[whileffns]
while b S,
if b = ff
Non-compositional
[whilettns]
S,
’, while b S, ’
while b S, ’’
’’
if b = tt
24
Executing the semantics
25
Example
• Let 0 be the state which assigns zero to all
program variables
x:=x+1, 0
skip, 0
0[x 1]
0
skip, 0 0, x:=x+1, 0 0[x 1]
skip; x:=x+1, 0 0[x 1]
x:=x+1, 0 0[x 1]
if x=0 then x:=x+1 else skip, 0
0[x 1]
26
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
27
Derivation tree example 1
• Assume 0=[x
1=[x
2=[x
3=[x
5, y
5, y
7, y
7, y
7, z
7, z
7, z
5, z
0]
5]
5]
5]
[assns]
[assns]
z:=x, 0 1 x:=y, 1 2
[compns]
(z:=x; x:=y), 0 2
[assns]
y:=z, 2 3
[compns]
(z:=x; x:=y); y:=z, 0 3
28
Derivation tree example 1
• Assume 0=[x
1=[x
2=[x
3=[x
5, y
5, y
7, y
7, y
7, z
7, z
7, z
5, z
0]
5]
5]
5]
[assns]
[assns]
z:=x, 0 1 x:=y, 1 2
[compns]
(z:=x; x:=y), 0 2
[assns]
y:=z, 2 3
[compns]
(z:=x; x:=y); y:=z, 0 3
29
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
30
Top-down evaluation example
• Factorial program with x = 2
• Shorthand: W=while (x=1) { y:=y*x;
[assns]
y:=y*x, [y 1] [y
2]
[compns]
[assns]
x:=x-1, [y 2] [y
2][x1]
y:=y*x; x:=x-1, [y 1] [y
2][x1]
[assns]
y:=1, [y
1]
x:=x-1 }
[whileffns]
W, [y 2][x1] [y 2, x
1]
[whilettns]
W, [y 1] [y 2, x
1]
[compns]
y:=1; while (x=1) { y:=y*x; x:=x-1 }, [y
2][x1]
31
Properties of natural
semantics
32
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
33
Semantic equivalence
• S1 and S2 are semantically equivalent if
for all and ’
S1, ’ if and only if S2, ’
• Simple example
while b do S
is semantically equivalent to:
if b then (S; while b S) else skip
– Read proof in pages 26-27
34
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)
35
Equivalence of {S1; S2}; S3 and S1; {S2; S3}
36
Equivalence of {S1; S2}; S3 and S1; {S2; S3}
Assume (S1; S2); S3,
’ then the following unique derivation tree exists:
S1, 1, S2, 1 12
(S1; S2), 12,
{S1; S2}; S3,
’
S3, 12
’
Using the rule applications above, we can construct the following derivation tree:
S1,
S2, 1 12, S3, 12 ’
1,
{S2; S3}, 1 ’
S1; {S2; S3}, ’
And vice versa.
37
Deterministic semantics for While
• Theorem: for all statements S and states 1, 2
if S, 1 and S, 2 then 1= 2
38
The semantics of statements
• The meaning of a statement S is defined as
S =
’
else
if S, ’
• Examples:
skip =
x:=1 = [x 1]
while true do skip = undefined
39
Operational semantics
of IL
40
IL syntax reminder
n
l
x
Num
Num
Var Temp
Vn|x
R V Op V |V
Op - | + | * | / | = |
C l: skip
| l: x := R
| l: Goto l’
| l: IfZ x Goto l’
IR C+
Numerals
Labels
Variables and temporaries
| << | >> | …
41
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
42
Rules for executing commands
• We will use rules of the following form
m(pc) = l
P(l) = C
m m’
• 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
43
Rules for executing commands
m(pc) = l
P(l) = skip
m m[pc l+1]
m(pc) = l
P(l) = x:=v
m m[pc l+1,
x M(v)]
M(v)=
m(v) v VarTemp
v
else
m(pc) = l
P(l) = x:=v1 op v2
m m[pc l+1, x M(v1) op
M(v2)]
M(v)=
m(v) v
VarTemp
v
else
m(pc) = l
P(l) = Goto l’
m m[pc l’]
44
Rules for executing commands
m(pc) = l
P(l) = IfZ x Goto l’
m m[pc l’]
m(x)=0
m(pc) = l
P(l) = IfZ x Goto l’
m m[pc l+1]
m(x)0
m(pc) = l
P(l) = IfNZ x Goto l’ m(x) 0
m m[pc l’]
m(pc) = l
P(l) = IfNZ x Goto l’ m(x)=0
m m[pc l+1]
45
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’
46
Semantics of a program
• For a program P=1:c1,…,n:cn
and a state m s.t. m(pc)=1
we define the result of executing P on m as
P m
=
m’
if m * m’ and m’(pc)=n+1
else
• Lemma: the function is well-defined
(i.e., at most one output state)
47
Execution example
• Execute the following intermediate language
program on a state where all variables
evaluate to 0
1:
2:
3:
4:
5:
6:
7:
8:
9:
t1 := 137
y := t1 + 3
IfZ x Goto 7
t2 := y
z := t2
Goto 9
t3 := y
x := t3
skip
m = [pc 1, t1 0 , t2 0 , t3 0 , x 0 , y 0] ?
48
Execution example
• Execute the following intermediate language
program on a state where all variables
evaluate to 0
1:
2:
3:
4:
5:
6:
7:
8:
9:
t1 := 137
y := t1 + 3
IfZ x Goto 7
t2 := y
z := t2
Goto 9
t3 := y
x := t3
skip
m = [pc 1, t1 0 , t2 0 , t3 0 , x 0 , y 0]
m[pc 2, t1 137]
m[pc 3, t1 137, y 140]
m[pc 7, t1 137, y 140]
m[pc 8, t1 137, t3 140, y 140]
m[pc 9, t1 137, t3 140, , x 140, y 140]
m[pc 10, t1 137, t3 140, , x 140, y 140]
49
Proof methodology
50
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
51
Defining and Proving
equivalence for
expressions
52
Exercise 1
• Are the following equivalent in your opinion?
While
x := 137
IL
1: t1 := 137
2: x := t1
53
Exercise 2
• Are the following equivalent in your opinion?
While
x := 137
IL
1: y := 137
2: x := y
54
Exercise 3
• Are the following equivalent in your opinion?
While
x := 137
IL
2: t2 := 138
3: t1 := 137
4: x := t1
55
Exercise 4
• Are the following equivalent in your opinion?
While
x := 137
IL
2:
3:
4:
5:
t2 := 138
t1 := 137
x := t1
t1 := 138
56
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
57
Defining equivalence for expressions
While
a
Definition:
a
IL
cgen
(P, t)
(P, t)
if m: m(pc)=label(P)
a
m|Var
=
(P
m) t
58
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 or
b m|Var = ff and (P m) x = 0
59
Equivalence of atomic expressions
cgen(n) = (l: t:=n, t)
where l and t are fresh
Claim: n (l: t:=n, t)
Proof: choose w.l.o.g m such that m(pc)=l
n m|Var = n
(l: t:=n m) t = m[pc l+1, t n] t = n
Q.E.D
cgen(x) = (l: t:=x, t)
where l and t are fresh
Claim: n (l: t:=x, t)
Proof: choose w.l.o.g m such that m(pc)=l
x m|Var = m(x) since xVar
(l: t:=x m) t = m[pc l+1, t m(x)] t = m(x)
Q.E.D
60
Lemmas for expressions
cgen(e1) = (P1, t1)
cgen(e2) = (P2, t2)
where l and t are fresh
cgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)
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) = and
Temps(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[t e])|Var.
61
Lemmas for expressions
cgen(e1) = (P1, t1)
cgen(e2) = (P2, t2)
where l and t are fresh
cgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)
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) =
Lemma 4: Let P1 and P2 be two IL programs such that
cgen(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
62
Equivalence of compound expressions
cgen(e1) = (P1, t1)
cgen(e2) = (P2, t2)
where l and t are fresh
cgen(e1 op e2) = (P1· P2· l: t:=t1 op t2, t)
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 m
By 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’[pc l+1, t m’(t1) op m’(t2) m)
t = e1 + e2 = e1 + e2
Q.E.D
63
Conclusion
• Missing: proof for Boolean expression
(exercise for home)
• Theorem 1: for each While expression e we
have that cgen(e) e
64
Defining and Proving
equivalence for
statements
65
State equivalence
• Define m iff =m|Var
– That is, for each xVar (x)=m(x)
66
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
67
Defining equivalence for statements
While
S
IL
cgen
Definition:
S
S
P
P
if m, : m(pc)=label(P) and m
P m
68
Equivalence of skip
cgen(skip) = l: skip
where l is fresh
Claim: skip l: skip
Proof: choose w.l.o.g m such that m(pc)=l
and =m|Var
skip m|Var = m|Var
(l: skip m)|Var = m[pc l+1]|Var = m|Var
Q.E.D
69
Equivalence of assignments
cgen(a) = (P, t)
cgen(x := a) = P · l: x := t
where l is fresh
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
70
Equivalence of assignments
cgen(a) = (P, t)
cgen(x := a) = P · l: x := t
where l is fresh
Claim: x := a P · l: x := t
Proof: choose w.l.o.g m such that m(pc)=l
and =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
71
Natural semantics for sequencing
[compns]
S1, ’, S2, ’ ’’
S1; S2, ’’
Lemma 6: S1; S2 = S2 (S1 )
72
Helper lemmas for sequencing
cgen(S1) = P1, cgen(S2) = P2
cgen(S1; S2) = P1 · P2
Lemma 7: Let P1 and P1 be two IL programs such that
such that cgen(S1; S2) = P1 · P2
Then labels(P1) labels(P2) = and
Temps(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’
73
Equivalence of sequencing
cgen(S1) = P1, cgen(S2) = P2
cgen(S1; S2) = P1 · P2
Claim: assume S1 P1 and S2 P2
then S1; S2 P1 · P2
Proof: choose w.l.o.g m such that m(pc)=l
and =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 that
P2 m’ S2 ’
By the definition of the natural semantics (lemma 6), we have that
S1; S2 P1 · P2 m
Q.E.D
74
Equivalence of 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)
where lb, lfinish, lafter
P1
are fresh
lfinish: Goto Lafter
P2
lafter: skip
Lemma 8: for all states we have that
If b = tt then, if b then S1 else S2 = S1
If b = ff then, if b then S1 else S2 = S2
75
Helper lemmas
cgen(b) = (Pb, t), cgen(S1) = P1, cgen(S2) = P2
cgen(if b then S1 else S2) =
Pb
lb: IfZ t Goto label(P2)
where lb, lfinish, lafter
P1
are fresh
lfinish: Goto Lafter
P2
lafter: skip
76
Equivalence of conditions: ff
Claim: Let P=cgen(if b then S1 else S2)
if b then S1 else S2 P
Proof: choose w.l.o.g m such that m(pc)=l
and =m|Var
From Theorem 1: b (P,t) therefore Pb m t = 0
Case 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
77
Equivalence of conditions: tt
Claim: Let P=cgen(if b then S1 else S2)
if b then S1 else S2 P
Proof: choose w.l.o.g m such that m(pc)=l
and =m|Var
From Theorem 1: b (P,t) therefore Pb m t = 0
Case 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
78
Equivalence for loops
cgen(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
79
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)
80
Case 1: while b S
terminates
81
Case 2: while b S loops
82
Next lecture:
Dataflow-based Optimization
© Copyright 2026 Paperzz