Exercise Set Lowering and Formal Semantics Roman Manevich BenGurion University of the Negev Q1 Extending Boolean expressions We would like to extend the set of Boolean expressions with an expression of the form ID: 783523
Download The PPT/PDF document "Fall 2015-2016 Compiler Principles" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Fall 2015-2016 Compiler PrinciplesExercise Set: Lowering and Formal Semantics
Roman Manevich
Ben-Gurion University of the Negev
Slide2Q1: Extending Boolean expressionsWe would like to extend the set of Boolean expressions with an expression of the formB B ? A : AThe idea is that in ‘
b
?
a1 : a2’ is that if b is true then the result is that of a1 and otherwise the result is that of a2Define the formal semantics for evaluating these expressionsDefine a corresponding lowering rule (cgen)
2
Slide3Q2: Boolean short-cutsWe would like to extend the set of Boolean expressions with an expression of the formB B && BThe idea is that in ‘b1
&&
b2’ if b1 is true then the result is that of b2, and otherwise it is falseDefine the formal semantics for evaluating these expressions3
Slide4Q2: Boolean short-cutsConsider the two following translations.Are they equivalent? Explain what is the formal definition of equivalence that you consider.4
cgen
(
b1 && b2) = cgen(b1 b
2
)
where
l
1,
l
2,
l
end and t are fresh
cgen
(b1) = (P1, t1) cgen(b2) = (P2, t2)cgen(b1 && b2) = (P1· l1: t:=t1 · IfZ t Goto lend · P2· l2: t:=t2 · lend: skip, t)
1
2
Slide5Extending IL with memory5
Slide6Q3: Extending IL with memoryWe would like to support two additional types of commands, as shown in the next pageExplain how the states of the revised language should be definedWrite the rules for the new commands6
Slide7Q3: Extended syntaxV n | xR V
Op
VOp - | + | * | / | = | | << | >> | …C
l
:
skip
|
l
:
x
:= R | l: x
:=
[y] | l: [x] := y | l: Goto l’| l: IfZ x Goto l’| l: IfNZ x Goto l’IR C+7
n
Num
Numerals
l
Num
Labels
x
Temp
Var
Temporaries and variables
Slide8solution8
Slide9A1: Extending Boolean expressions b ? a1 :
a
2
= if b
=
tt
then
a
1
else
a
2
9cgen(b) = (Pb, tb) cgen(a1) = (P1, t1) cgen(a2) = (P2, t2)cgen(b ? a1 :
a2) = (Pb
IfZ t Goto label(
P2)P1
t := t1lfinish:
Goto
L
after
P
2
t
:=
t
2
l
after
:
skip,
t
)
Slide10A2: Boolean short-cutsThe two translations are indeed equivalent according to the truth table of conjunctionWe consider equivalence by projecting states on the variables (and temporaries) used in the sub-expression b1 and b2
and the output temporary
t
10
Slide11A3: Extending program statesZ Integers {0, 1, -1, 2, -2, …}IState (Var Temp
{pc})
Z Z Z
Intermediate states are pairs:
The first component, the stack, gives values to variables
The second component, the heap, maps addresses to values
11
Slide12A3: adding memory access rules12s(pc) = l P(l
) =
x:=[y](s,h)
(
s
[pc
l
+1,
x
h(s(y))],
h
)s(pc) = l P(l) = [x]:=y(s,h) (s[pcl+1], h[s(x)s(y)])