Docsity

CS 502 Compilers Midterm Examination: CPS Transformation and A-Normal Form, Exams of Computer Science

Toggle sidebar
bg1
CS 502. Compilers
Midterm Examination
October 23, 2006
Name:
Answer each of the questions in the space provided. If you require additional space, use the blank pages
attached at the end of the exam. Unless otherwise noted, the language under consideration is defined
by the following grammar:
e∈Exp ::= c|x|C|(fn x=>e)|e(e)|C(e)|p(e...e)|if etest then etrue else ef alse)
c∈Const ::= 0 |1|. . .
C∈Constructor ::= Cons |true |false |. . .
p∈P rimop ::= + |−|∗|. . .
In the program fragments you write, you are free to use any syntactic abstractions you wish including
let and letrec.
Score:
Maximum Score: 145
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS 502 Compilers Midterm Examination: CPS Transformation and A-Normal Form and more Exams Computer Science in PDF only on Docsity!

CS 502. Compilers

Midterm Examination

October 23, 2006

Name:

Answer each of the questions in the space provided. If you require additional space, use the blank pages attached at the end of the exam. Unless otherwise noted, the language under consideration is defined by the following grammar:

e ∈ Exp ::= c | x | C | (fn x=>e) | e(e) | C(e) | p(e...e) | if etest then etrue else ef alse) c ∈ Const ::= 0 | 1 |... C ∈ Constructor ::= Cons | true | false |... p ∈ P rimop ::= + | − | ∗ |...

In the program fragments you write, you are free to use any syntactic abstractions you wish including let and letrec.

Score: Maximum Score: 145

Question 1 (50 points):

  1. (10 points) There is a one-to-one correspondence between the language of CPS and direct (non- CPS) expressions. Assuming that k ranges over continuation variables, write an expression that when CPS’ed would produce the following CPS expression; you may assume (fn y=>y) defines the initial continuation. (The expression λx.e is shorthand for “fn x => e.”, and λcx.e denotes a continuation expression.)

λx k.f (x, (λcv 1. g(x, (λcv 2. v 1 (v 2 , (λcv 3. k v 3 ))))))

λ x.((f x) (g x))

  1. (20 points) The ML language provides exceptions as way to express non-local transfers of control. The expression:

handle e 1 e 2

evaluates e 1 in the context of an exception handler defined by e 2. If e 1 produces a value v, then the value of the entire expression is v. If e 1 raises an exception with value v, then the value of the expression is the result of applying e 2 to v. A subexpression of e 1 can raise an exception by evaluating:

raise e

The raise operator supplies the value of e to the closest dynamically-enclosing handler. One way to compile exceptions to a CPS intermediate representation is to have every CPS expres- sion take two continuations, one which represents the normal continuation, and the other which represents the exception continuation, the continuation to jump to if an exception is raised. Show the CPS translation for a core language consisting of variables, conditional expressions, (single-argument) functions, and (single-argument) function application augmented with handle and raise expressions, by defining a function Cexn that given an expression, a normal continuation, and an exception continuation returns a CPS’ed expression.

C[handle e 1 e 2 ]kn ke = C[e 1 ]knCPS [e 2 ]kn ke C[raise e]kn ke = C[e]ke ke

CPS [x ]kn ke = kn(x) CPS [λx .e]kn ke = kn(λx k′ n k′ e.CPS [e]k (^) n′ k (^) e′ ) CPS [e 1 (e2 )]kn ke = CPS [e 1 ](λc f .CPS [e 2 ](λc x .f (x , kn , ke )) ke ) ke CPS [if e 1 then e 2 elsee 3 ]kn ke = CPS [e 1 ](λc b.if b = 0 then CPS [e 2 ]kn ke else CPS [e 3 ]kn ke ) ke

  1. (10 points) The CPS transform makes explicit both order of evaluation, and the naming of inter- mediate computation(s). A-normal form is an alternative intermediate representation that avoids the creation of explicit continuations, encapsulating their role using let. For example, the following expression:

Question 2. (30 points)

Consider a program P of the form begin D; S end where D consists of a collection of procedure declarations, and S is a sequence of statements drawn from the simple while language augmented with procedures and procedure calls. Procedures take a single call-by-value parameter. The label on is marks the program point upon entry to the procedure, and the label on end marks the program point on exit. The two labels associated with a call mark the program point immediately prior and immediately after the corresponding call.

e ∈ AExp ::= x | n | a 1 op a 2 b ∈ BExp ::= true | false | not b | b 1 op b 2 D ∈ Decl ::= proc p(val x) isn^ S endx^ | D D S ∈ Stmt ::= [x:=a]^ | [skip]^ | S 1 ; S 2 | call p(a){y^ ,z^ } | if [b]then S 1 else S 2 | while [b]^ do S

For each procedure of the form:

proc p(val x) isn^ S endx

the set IAV(p) contains the global variables that might be assigned directly or indirectly when p is called.

  1. (10 points) Precisely define the set AV(S) that for each statement S gives the set of variables that could be assigned to S, but ignores the effects of procedure calls; and the set CP(S) that gives for each statement the set of procedure names that could be directly called in S but ignores the effects of procedure calls.

AV ([skip]^ = φ AV ([x := a]) = {x} AV (S 1 ; S 2 ) = AV (S 1 ) ∪ AV (S 2 ) AV (if[b]^ then S 1 else S 2 ) = AV (S 1 ) ∪ AV (S 2 ) AV (while [b]^ do S) = AV (S) AV ([call p(a)]{lc, lr }) = φ

CP ([skip]^ = φ CP ([x := a]) = φ CP (S 1 ; S 2 ) = CP (S 1 ) ∪ CP (S 2 ) CP (if[b]^ then S 1 else S 2 ) = CP (S 1 ) ∪ CP (S 2 ) CP (while [b]^ do S) = CP (S) CP ([call p(a)]{lc, lr }) = {p}

  1. (15 points) Formulate a system of dataflow equations that specifies how to obtain the set IAV(p) in terms of AV and CP.

IAV (p) = (AV (S)/{x} ∪

{IAV (p′)|p′^ ∈ CP (S)}

where proc p(val x) isn^ S endx^ ∈ D

  1. (5 points) Show the IAV equations and solutions for procedures fib and add in the following program. Explain whether you must compute the least or greatest solution.

begin proc fib(val z) is if z<3 then call add(1) else (call fib(z-1); call fib(z-2)) end; proc add(val u) is (y := y + u; u:= 0) end; y := 0; call fib(x) end

IAV (fib) = (φ/{z}) ∪ IAV (fib) ∪ IAV (add) IAV (add) = {y, u}/{u}

The least solution is IAV (fib) = IAV (add) = {y}.

Question 4 (20 points)

Consider the inclusion of an inlining optimization into your mini-ML compiler. Inlining is an optimization that replaces a procedure definition at the call-site where it is invoked. (Assume you have some set of metrics that determine which procedures are good candidates for inlining at their call-sites.)

  1. (10 points) Is it preferable to perform inlining on the CPS IL or the core-ML IL? Justify your answer. It’s probably better to perform the optimization on core-ML. If we did it in the CPS-IL, then the continuation list of the function in which the inlining occurred would need to be modified. More importantly, Return operations within the inlined function must be replaced with Gotos to the return continuation, as would Tail and Non-Tail calls as well.
  2. (10 points) Provide details of an inlining implementation for Mini-ML based on your answer to (1) and be sure to highlight those issues that must be handled to preserve safety of the translation. (Hint: why is a naive translation, i.e., one in which the procedure definition is simply substituted for the procedure expression at a call-site incorrect?) Lexical offsets for variables that were computed on the original program are no longer valid. They must be changed to take into account the context in which the function is being inlined.

Question 5 (25 points)

Consider the following program fragment:

let val x = 1 val y = 2 val z = 3 val f = fn w => x = y + w in f 100 end

One possible closure-converted representation for this program fragment would be:

let val x = 1 val y = 2 val z = 3 val code = (fn env => (fn w => (#x env) + (#y env) + w)) val env = { x = x, y = y } val f = (code,env) in (#1 f) (#2 f) 100 end

  1. (5 points) What is the type of code? τe → τ 1 → τ 2 where τe is the type of the environment. The closure as a whole would have type:

(τe → τ 1 → τ 2 ) × τe

  1. (5 points) Based on this answer, justify the conclusion that closure-conversion does not in general preserve types under a Hindley-Milner type system. Observe that the type of a closure exposes the type of the environment over which it is closed. This obligates all environments in which the closure is instantiated to have the same types. In the absence of a richer type-system, this is an overly restrictive burden. For types to be preserved, the environment must be hidden.
  2. (15 points) Construct an example that shows a well-typed program which fails to type-check after the closure conversion translation shown above. Consider the program:

let val y = 1 in if true then fn x => x + y else fn z => z

Using a flat-closure representation we get:

let val y = 1 in if true then (fn env => (fn x => x + (#y env)), {y=y}) else (fn env => (fn z => z), { }) end

This program will not type-check. Note the problem remains even under a linked-closure strategy.