/
Fall 2015-2016  Compiler Principles Fall 2015-2016  Compiler Principles

Fall 2015-2016 Compiler Principles - PowerPoint Presentation

webraph
webraph . @webraph
Follow
342 views
Uploaded On 2020-06-22

Fall 2015-2016 Compiler Principles - PPT Presentation

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

amp cgen goto boolean cgen amp boolean goto extending formal short ifz skip result variables expression semantics states set

Share:

Link:

Embed:

Download Presentation from below link

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.


Presentation Transcript

Slide1

Fall 2015-2016 Compiler PrinciplesExercise Set: Lowering and Formal Semantics

Roman Manevich

Ben-Gurion University of the Negev

Slide2

Q1: 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

Slide3

Q2: 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

Slide4

Q2: 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

Slide5

Extending IL with memory5

Slide6

Q3: 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

Slide7

Q3: 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

Slide8

solution8

Slide9

A1: 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

)

Slide10

A2: 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

Slide11

A3: 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

Slide12

A3: 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[pcl+1], h[s(x)s(y)])