CS 132 Fall 2004, Final Solution ================================ Question 1 ---------- Symboltable: Name | Information ---------------------- B | superclass: | fields: Name Type | f B | methods: Name Information | m args: Name Type | a int | result type: void | locals: C | superclass: B | fields: Name Type | g C | methods: Name Information | m args: Name Type | a int | result type: void | locals: Name Type | j int | p args: | result type: int | locals: Question 2 ---------- Heap layout for a C-object: /-----\ --->| mt ------->/---\ | f | | m | | g | | p | \-----/ \___/ Piglet code for the body of the method m in class C: // TEMP 0 : this // TEMP 1 : a // TEMP 2 : j // TEMP 100 : the result of the translation of new B() // TEMP 101 : the method table to for B-object // TEMP 102 : the method table for the object to call // TEMP 103 : the procedure name to call // TEMP 104 : dummy variable to allow the grammar C_m[2] BEGIN MOVE TEMP 2 1 CJUMP LT 0 TEMP 1 ELSE MOVE TEMP 2 PLUS TEMP 1 1 HSTORE TEMP 0 4 BEGIN MOVE TEMP 101 HALLOCATE 4 HSTORE TEMP 101 0 B_m MOVE TEMP 100 HALLOCATE 8 HSTORE TEMP 100 0 TEMP 101 HSTORE TEMP 100 4 0 HSTORE TEMP 100 8 0 RETURN TEMP 100 END HLOAD TEMP 102 TEMP 0 0 HLOAD TEMP 103 TEMP 102 0 MOVE TEMP 104 CALL TEMP 103 ( TEMP 0 MINUS TIMES TEMP 2 TEMP 1 1 ) JUMP ENDD ELSE HSTORE TEMP 0 8 TEMP 0 ENDD NOOP RETURN 0 END Question 3 ---------- A MIPS stack layout for the method m in class C: /-----------\ | this | | a | | ret addr | | j | | TEMP 3 | | TEMP 4 | | TEMP 5 | | TEMP 6 | | callee | | save | | regs | \-----------/ Hopefully this and a can be passed in the a-registers, and hopefully register allocation will take care of allocating TEMP 3-6 in registers. Similarly, we may be able to pass the ret addr in the ra-register. Question 4 ---------- y | > i = 0 i y | > a = 1 a i y | > if y < 0 then goto L1 a y | | > | a i i = y | a i | | > > L1: a = a + i < a i | \ > | a i i = i + 1 | a i | | > | if i < 100 then goto L1 a | > return a Question 5 ---------- Colored interference graph: ___________ / \ | | | d(1) a(1) | | \ / \ | b(2) \ | / | \ c(3) | f(4) | \ | / | | e(1) | | | \_________/ The program after register allocation: r1 = 1 r2 = 10 r3 = 9 + r1 r1 = r1 + r3 r1 = r3 + r1 r4 = r2 + 8 r3 = r4 + r1 r4 = r1 + r3 r2 = r3 + 5 return r2+r4 Question 6 ---------- Three registers: a:1 b:2 c:3 a expires d:1 d expires e:1 f spilled r1 = 1 r2 = 10 r3 = 9 + r1 r1 = r1 + r3 r1 = r3 + r1 f = r2 + 8 r3 = f + r1 f = r1 + r3 r2 = r3 + 5 return r2+f Two registers: a:1 b:2 b spilled // because b's live range ends last c:2 a expires d:1 d expires e:1 f:spilled s1 = 1 b = 10 s2 = 9 + s1 s1 = s1 + s2 s1 = s2 + s1 f = b + 8 s2 = f + s1 f = s1 + s2 b = s2 + 5 return b+f