Docsity

Compiler Construction Exam 4 - Spring 2007 - Prof. Robert A. Van Engelen, Exams of Computer Science

Toggle sidebar
bg1
COP5621 Compiler Construction Exam 4 - Spring 2007
Name: (Please print)
Put the answers on these sheets. Use additional sheets when necessary. You can collect 100 points
in total for this exam.
1. Which of the following optimizations is considered a peephole optimization? (mark one)(4
points)
(a) Common-subexpression elimination
(b) Code motion
(c) Branch chaining
(d) Register assignment
2. Subroutine frames (activation records) manage a procedure’s local data. For a typical pro-
gramming language implementation, who deallocates the frame? (mark one)(4 p oints)
(a) Caller
(b) Callee
(c) Caller and callee
(d) None of the above
3. List three local optimizations of your choice and describe what they optimize. (9 points)
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Compiler Construction Exam 4 - Spring 2007 - Prof. Robert A. Van Engelen and more Exams Computer Science in PDF only on Docsity!

COP5621 Compiler Construction Exam 4 - Spring 2007

Name: (Please print)

Put the answers on these sheets. Use additional sheets when necessary. You can collect 100 points

in total for this exam.

1. Which of the following optimizations is considered a peephole optimization? (mark one)(

points)

(a) Common-subexpression elimination

(b) Code motion

(c) Branch chaining

(d) Register assignment

2. Subroutine frames (activation records) manage a procedure’s local data. For a typical pro-

gramming language implementation, who deallocates the frame? (mark one)(4 points)

(a) Caller

(b) Callee

(c) Caller and callee

(d) None of the above

3. List three local optimizations of your choice and describe what they optimize. (9 points)

4. Consider the following program:

program P() var p : integer; procedure Q(k : integer) begin R(k, p) end procedure R(i : integer, j : integer); var n : integer procedure T(i : integer) begin ... (* body of T *) end; procedure S() var m : integer begin T(n) end; begin S() end; begin Q(p) end

(a) Program P calls Q, Q in turn calls R, R in turn calls S, and S in turn calls T. Draw the

resulting stack layout with activation records. Show the arguments and local variables

in each record and draw the access links. (10 points)

6. Apply global constant propagation to the following CFG.(8 points)

z := 0 y := 5

x := z

x := 1 z := 3

y := y + x y := y - z

y := x

z := y + z

7. Apply register allocation and assignment using graph coloring to the following CFG:

x := read()

t := read()

x := x + 1

y := 0

y := t

t := 0

p := t + y

p := p + x

t > 0?

x, y, t are not live

Hint: show the live ranges of the variables in the CFG, assuming that x, y, and t are no

longer live at the exit from the CFG. Draw the register-interference graph (conflict graph) for

the variables and determine the minimum number of colors to color the graph. (12 points)

9. Give the data-flow equations for reaching definitions (gen, kill, in and out sets) as described in

the book and illustrated in class for the four programming constructs (assignment, statement

composition, if-then-else, and do-while). (10 points)

10. Consider the following program:

if n>0 then d 1 : i := 0; d 2 : f := 1; do d 3 : i := i + 1; d 4 : f := f * i while i < n; else d 5 : f := 0; end if

Annotate the syntax tree of the program with in, gen, kill, and out bit-vectors:

if

n > 0

d 5

in= 00000 gen= kill= out=

in= gen= kill= out=

d 1 ;

d 2 do

; i < n

d 3 d 4

in= gen= kill= out=

in= 00000 gen= 10000 kill= 00100 out=

in= gen= kill= out=

in= gen= kill= out=

in= gen= kill= out=

in= gen= kill= out=

in= gen= kill= out=

in= gen= kill= out=

Hint: compute the gen and kill vectors bottom-up first, i.e. start at the leaves. Then compute

the in and out vectors in a top-down, left-to-right traversal. (in is inherited, while out is

synthesized.) (10 points)