Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Principles Fall 2015-2016 Compiler Principles Exercise Set: Lowering and Formal Semantics Roman Manevich Ben-Gurion University of the Negev.

Similar presentations


Presentation on theme: "Compiler Principles Fall 2015-2016 Compiler Principles Exercise Set: Lowering and Formal Semantics Roman Manevich Ben-Gurion University of the Negev."— Presentation transcript:

1 Compiler Principles Fall 2015-2016 Compiler Principles Exercise Set: Lowering and Formal Semantics Roman Manevich Ben-Gurion University of the Negev

2 Q1: Extending Boolean expressions We would like to extend the set of Boolean expressions with an expression of the form B  B ? A : A The idea is that in ‘b ? a 1 : a 2 ’ is that if b is true then the result is that of a 1 and otherwise the result is that of a 2 Define the formal semantics for evaluating these expressions Define a corresponding lowering rule (cgen) 2

3 Q2: Boolean short-cuts We would like to extend the set of Boolean expressions with an expression of the form B  B && B The idea is that in ‘b 1 && b 2 ’ if b 1 is true then the result is that of b 2, and otherwise it is false 1.Define the formal semantics for evaluating these expressions 3

4 Q2: Boolean short-cuts 2.Consider the two following translations. Are they equivalent? Explain what is the formal definition of equivalence that you consider. 4 cgen(b 1 && b 2 ) = cgen(b 1  b 2 ) where l1, l2, lend and t are fresh cgen(b 1 ) = (P 1, t 1 ) cgen(b 2 ) = (P 2, t 2 ) cgen(b 1 && b 2 ) = (P 1 · l1: t:=t 1 · IfZ t Goto lend · P 2 · l2: t:=t 2 · lend: skip, t) 1 2

5 Extending IL with memory 5

6 Q3: Extending IL with memory We would like to support two additional types of commands, as shown in the next page 1.Explain how the states of the revised language should be defined 2.Write the rules for the new commands 6

7 Q3: Extended syntax V  n | x R  V Op V Op  - | + | * | / | = |  | > | … 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  NumNumerals l  Num Labels x  Temp  VarTemporaries and variables

8 solution 8

9 A1: Extending Boolean expressions  b ? a 1 : a 2   = if  b   = tt then  a 1   else  a 2   9 cgen(b) = (P b, t b ) cgen(a 1 ) = (P 1, t 1 ) cgen(a 2 ) = (P 2, t 2 ) cgen(b ? a 1 : a 2 ) = ( Pb IfZ t Goto label(P 2 ) P 1 t := t 1 l finish : Goto L after P 2 t := t 2 l after : skip, t )

10 A2: Boolean short-cuts The two translations are indeed equivalent according to the truth table of conjunction We consider equivalence by projecting states on the variables (and temporaries) used in the sub-expression b 1 and b 2 and the output temporary t 10

11 A3: Extending program states Z 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

12 A3: adding memory access rules 12 s(pc) = lP(l) = x:=[y] (s,h)  (s[pc  l+1, x  h(s(y))], h) s(pc) = lP(l) = [x]:=y (s,h)  (s[pc  l+1], h[s(x)  s(y)])


Download ppt "Compiler Principles Fall 2015-2016 Compiler Principles Exercise Set: Lowering and Formal Semantics Roman Manevich Ben-Gurion University of the Negev."

Similar presentations


Ads by Google