Docsity

Compiling & Programming Systems Mid-term Exam Solutions | CS 50200, Exams of Computer Science

Toggle sidebar
bg1
CS 502 โ€“ Compiling and Programming Systems
Mid-term Examination, 10/30/07
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
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Compiling & Programming Systems Mid-term Exam Solutions | CS 50200 and more Exams Computer Science in PDF only on Docsity!

CS 502 โ€“ Compiling and Programming Systems

Mid-term Examination, 10/30/

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

  1. (MiniJava front-end; 20%) Consider the changes you would need to make to each phase of the MiniJava compiler front-end in order to add support for Java inner classes, which are introduced to the MiniJava grammar as follows:

::= class [ extends ] { โˆ—^ } ::= | | < Class >

where the only change is to permit to appear as a member. The rules for , , and are as before. Every instance of an inner class is scoped inside the instance of the outer class in which it is instantiated. Consider the following Java program:

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

  1. (Run-time representations; 10%) Bonus for those awake in class! Javaโ€™s inner classes are an example of lexical scoping. In a functional programming language, one might write the following analogous program:

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.

  1. (Context free grammars, LL parsing; 20%) Consider the following simple context free gram- mar: S โ†’ U;W U โ†’ Ua U โ†’ ฮต W โ†’ bW W โ†’ b

(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 โ€ฒ^ โ†’ ฮต

  1. (Control flow graphs, liveness analysis, register allocation; 30%) Consider the following MiniJava program to compute factorial:

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:

  • rs: a callee-save register
  • ra: a caller-save argument/result register

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

fact : M [sloc] โ† rs rs ra

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

L 0 : rs โ† M [sloc] rs rs, ra

return rs, ra

rs ra i i ร— vii. Simplify i

viii. Color i โ‰ก rs

ix. Final program:

fact : M [sloc] โ† rs

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

L 0 : rs โ† M [sloc]

return