Test 2      

Abstract Syntax Tree
 
program = main_class class_decl*;
main_class = [classname]:id [arg]:id statement;
class_decl =
     id var_decl* method_decl* |
             {extends} [classname]:id
                             [extend]:id var_decl* method_decl*;
var_decl = type id;
method_decl = type id formal_list var_decl* statement* exp;
statement =
             {statementlist} statement* |
             {if} exp [true]:statement [false]:statement |
             {while} exp statement |
             {println} exp |
             {assign} id exp |
             {array_assign} id [index]:exp [r]:exp;
formal_list = type id formal_rest* | {empty};
formal_rest = type id;
 
type=   {int_array} |
            {boolean}   |
            {int}        |
            {id} id;
exp =    {and}   [l]:exp [r]:exp |
             {lt}    [l]:exp [r]:exp |
             {plus}  [l]:exp [r]:exp |
             {minus} [l]:exp [r]:exp |
             {times} [l]:exp [r]:exp |
             {length} exp                   |
             {arrayindex} [array]:exp [index]:exp |
             {methodcall} exp id exp_list |
             {integerliteral} integer_literal |
             {trueliteral} true |
             {falseliteral} false |
             {id} id |
             {this} this |
             {new} id |
             {newarray} exp |
             {not}     exp ;
exp_list = exp*;
  1. For the MiniJava abstract syntax above, what must be stored in the symbol table for each:
    1. variable   
    2. statement   
    3. method_decl
  2. For the MiniJava abstract syntax above, what methods need to be implemented for type checking the following?
    1. 4 * a   
    2. while(a<b) a = a + 1;
  3. Give the method for type-checking
    1. falseliteral
       
       
    2. lt
       
  1. public class Q1 {
  2.     public static void main(String [] args) {
  3.        Q2 qa = new Q2();
  4.        Q2 qb = new Q2();
  5.        qa.f1( qb, 5);
  6.     }
  7. }
  1. class Q2 {
  2.    int a;
  3.    public void f1( Q2 q, int n ) {  
  4.        int i;
  5.        int j;
  6.        i = n;
  7.        j = i + 1;
  8.        n = q.f2();
  9.        this.a = n;
  10.    }
  11. }
  1.    public int f2( ) {  
  2.        int i;
  3.        i = 3;
  4.        return i;
  5.    }
  6. }

 

  1. (2) Diagram the stack (show TOS) for the method call:  qa.f1( qb, 5 );
 
  1. (6) Diagram the JVM frame contents (show both formal and actual parameters) for the two method calls starting with: qa.f1( qb, 5 );
       
  2. (2) Diagram the stack (show TOS) for the method call:  q.f2(  );

     

  3. Give the IR for the following:
    1. (2) 3 - 5 - 4
       
       
    2. (3) a = 3-5, assume a is in frame at offset 7 and TEMP(fp) is the frame pointer value.
       
    3. (3) a = b-3, assume a and b are in frame at offset 7 and 9 respectively and TEMP(fp) is the frame pointer value.
       
       
    4. (4) 4 < a, assume a is in frame at offset 5 and TEMP(fp) is the frame pointer value.
       
       
    5. (6) while ( 4<a )
           a = b-3
  1. (9) Give the linearization of each of the following:
    1. SEQ(MOVE(TEMP(t0),CONST(4)),SEQ(MOVE(TEMP(t0), CONST(3)),SEQ(MOVE(TEMP(t0), CONST(2)),MOVE(TEMP(t0),CONST(1))))) Þ
       
    2. SEQ(SEQ(SEQ(MOVE(TEMP(t0),CONST(4)),MOVE(TEMP(t0),CONST(3))),MOVE(TEMP(t0), CONST(2))),MOVE(TEMP(t0),CONST(1))) Þ
       
  2.  Give the linearization, basic blocks and trace of:
    1. MOVE(TEMP t0, ESEQ(MOVE(TEMP t0,CONST 4),CONST 1)) Þ
       
    2. SEQ(SEQ(JUMP(b2),MOVE( TEMP(t0),BINOP(BINOP.PLUS, CONST(1), ESEQ( MOVE(TEMP(t2), CONST(2)), TEMP(t1))))),LABEL(b1)) Þ