






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Instructions: Read carefully through the whole exam first and plan your time. Note the relative weight of each question and part (as a percentage of the score for the whole exam). The total points is 100 (ie, your grade will be the percentage of your answers that are correct).
This exam is open book, open notes. You are free to refer to any book or other study materials you bring to the exam room.
You have two hours to complete all five (5) questions. Write your answers on this paper (use both sides if necessary).
Name:
Student Number:
Signature
where the only change is to permit
class A { int value; class B { B() { System.out.println(A.this.value); } } A(int i) { this.value = i; new B(); } public static void main (String[] args) { new A(1); new A(2); } }
Here, two instances of A are constructed, each of which has a single inner B instance. Picture this as follows:
A instance value:
B instance
A.this
A instance value:
B instance
A.this
The syntax A.this allows each B instance to refer to its enclosing A instance. Thus, running this Java program results in the output:
1 2
function A(int value) { function B() { println(value); } B(); } A(1); A(2);
which produces the same output as the Java program, except that in this program the call frames for each activation of functions A (which includes storage for value) and B can be allocated on a stack, whereas in the Java program the instances of classes A and B are usually allocated in the (garbage collected) dynamic allocation heap.
(a) (5%) Describe a mechanism by which function B can refer to the appropriate value of function A. Answer: Static chains: Calls to nested functions must pass their outer scopeโs static link (FP to most recent activation of outer-scope function) as one of the arguments to the call. (b) (5%) In the Java program above, it is possible to avoid allocating each B instance sep- arately in the heap (so as to reduce work for the garbage collector), instead physically inlining each B instance inside its containing A instance, like this:
A instance value:1 (^) B instance
A instance value:2 (^) B instance
Why can Java not generally allocate storage for inner instances inside their containing instance? Answer: Inner objects can escape โ they can be returned to dynamic scopes outside the outer object. This allows them to outlive the outer object.
(a) (10%) Modify this grammar so that it can be parsed by an LL(1) top-down parser. Answer:
S โ U;W U โ Uโฒ Uโฒ^ โ aUโฒ Uโฒ^ โ ฮต W โ bW โฒ W โฒ^ โ W W โฒ^ โ ฮต
(b) (10%) Give the LL(1) parse table for the modified grammar. Answer:
; a b $ S S โ U;W S โ U;W U U โ Uโฒ^ U โ Uโฒ Uโฒ^ Uโฒ^ โ ฮต Uโฒ^ โ aUโฒ W W โ bW โฒ W โฒ^ W โฒ^ โ W W โฒ^ โ ฮต
class Fact { static int fact (int i) { if (i > 1) return Fact.fact(i-1) * i; return 1; } }
This program has been compiled for a machine with 2 general-purpose registers:
as follows: fact : s โ rs i โ ra if i > 1 goto L 2 L 1 : t โ 1 goto L 0 L 2 : ra โ i โ 1 ra โ fact(ra) t โ ra โ i goto L 0 L 0 : ra โ t rs โ s return (uses rs, ra)
(a) (5%) Draw the control flow graph for this program, with nodes that are the programโs basic blocks (ie, not individual instructions) and with edges representing the flow of control between the basic blocks. Answer:
L 1 : t โ 1 goto L 0
L 2 : ra โ i โ 1 ra โ fact(ra) t โ ra โ i goto L 0
fact : s โ rs i โ ra if i > 1 goto L 2
L 0 : ra โ t rs โ s return (uses rs, ra)
(b) (10%) Annotate each instruction with the variables/registers live-out at that instruction.
Def Use LiveOut fact : s โ rs i โ ra if i > 1 goto L 2 L 1 : t โ 1 goto L 0 L 2 : ra โ i โ 1 ra โ fact(ra) t โ ra โ i goto L 0 L 0 : ra โ t rs โ s return rs, ra Answer: Def Use LiveOut fact : s โ rs s rs s, ra i โ ra i ra s, i if i > 1 goto L 2 i s, i L 1 : t โ 1 t s,t goto L 0 s,t L 2 : ra โ i โ 1 ra i s, i, ra ra โ fact(ra) ra ra s, i, ra t โ ra โ i t ra, i s,t goto L 0 s,t L 0 : ra โ t ra t s, ra rs โ s rs s rs, ra return rs, ra
vi. Retain coalesces from before first spill, rewrite program, recompute live- ness:
Def Use LiveOut
i โ ra i ra i if i > 1 goto L 2 i i L 1 : ra โ 1 ra ra goto L 0 ra L 2 : ra โ i โ 1 ra i i, ra ra โ fact(ra) ra ra i, ra ra โ ra โ i ra ra, i ra goto L 0 ra
return rs, ra
rs ra i i ร vii. Simplify i
viii. Color i โก rs
ix. Final program:
rs โ ra if rs > 1 goto L 2 L 1 : ra โ 1 goto L 0 L 2 : ra โ rs โ 1 ra โ fact(ra) ra โ ra โ rs goto L 0
return