Lab 8: Code generation for Factorial.java

  1. SimpleMethodCall.java
    class SimpleMethodCall {
        public static void main(String[] a){
            System.out.println(new A().echo(123));
        }
    }
    
    class A {
        public int echo(int i) {
            return (i);
        }
    }
    
  2. simple_method_call.cc
    // beginning of preamble  ================================
    #include<string>            // Write out the preamble     
    #include<iostream>
    #define lv(T,P,D) *((T*)(P+D))                            
    #define COUT(x) cout << (x) <<endl;                       
    #define ALLOC(C)  new word[C];                            
    using namespace std;                                      
    typedef char word;                                        
    word* TP;                   // the This Pointer           
    word* BP = ALLOC(8000);     // the Base Pointer of stack  
    word* SP = BP+8000;         // the Stack Pointer (top)    
    word* junkreg_wordptr;      // a useful thing to have     
    void* junkreg_voidptr;      // a useful thing to have     
    int   junkreg_int;          // a useful thing to have     
    int*  junkreg_intptr;       // a useful thing to have     
    // end of preamble and beginning of main declarations ====
    
    word* T1_SimpleMethodCall;		     // From MainClass::install
                                                 // for starting addr of class
                                                 // SimpleMethodCall
                                                 // Not really being used!
    word* T2_A;		                     // From ClassDecl::install
                                                 // for starting addr of class A
    word* T3_SimpleMethodCall;		     // From NewIdExp::analyze
                                                 // for class A which is word*
    int T4_SimpleMethodCall;		     // From MethodExp::analyze
                                                 // for return value
    int T5_SimpleMethodCall;		     // From ConstExp::analyze
                                                 // for arg 123
    word* T6_SimpleMethodCall;		     // From MethodExp::analyze
                                                 // for saveTP
    void* T7_echo_A;		             // From MethodDecl::analyze
                                                 // for return addr
    int i_echo_A;		                     // From Formal::analyze
                                                 // for arg i
    int echo_A_arg_1;		             // From MethodDecl::gen
                                                 // for passing arg i
    int echo_A_result_;                          // From MethodDecl::gen
                                                 // for return result
    // end of declarations and beginning of midamble  ========
    
    int main() {
        // Begin init
        T2_A = ALLOC(4);		             // From ClassDecl::gen
        junkreg_voidptr =&& echo_A;              // T2_A[0] = A::echo
        lv(void*,T2_A,0) = junkreg_voidptr;      // 
        // End init
        
        // Begin code
        // Begin MainClass::gen
        TP = ALLOC(20);                          // From MainClass::gen
                                                 // allocate space to store all
                                                 // registers before using them
        T5_SimpleMethodCall = 123;		     // int const
        echo_A_arg_1 = T5_SimpleMethodCall;      // From MethodExp::gen
                                                 // to pass param to A::echo
        T3_SimpleMethodCall = ALLOC(4);          // From NewIdExp::gen
        lv(word*,T3_SimpleMethodCall,0) = T2_A;  // for storing starting addr of
                                                 // class A
        lv(word*,TP,0) = T1_SimpleMethodCall;    //store
        lv(word*,TP,4) = T3_SimpleMethodCall;    //store
        lv(int,TP,8) = T4_SimpleMethodCall;	     //store
        lv(int,TP,12) = T5_SimpleMethodCall;     //store
        lv(word*,TP,16) = T6_SimpleMethodCall;   //store
        T6_SimpleMethodCall = TP;		     //saveTP - save old TP
        TP = T3_SimpleMethodCall;		     //set TP - set to class A, T2_A
        T7_echo_A =&& L0;                        // Setup return address, L0
        junkreg_wordptr = lv(word*,TP,0);        // T2_A[0], the addr of A::echo
        junkreg_voidptr = lv(void*,junkreg_wordptr,0);//calling echo.
        goto* junkreg_voidptr;
     L0:;
        TP = T6_SimpleMethodCall;		     //restoreTP
        T1_SimpleMethodCall = lv(word*,TP,0);    //fetch
        T3_SimpleMethodCall = lv(word*,TP,4);    //fetch
        T4_SimpleMethodCall = lv(int,TP,8);	     //fetch
        T5_SimpleMethodCall = lv(int,TP,12);     //fetch
        T6_SimpleMethodCall = lv(word*,TP,16);   //fetch
        T4_SimpleMethodCall = echo_A_result_;
        COUT(T4_SimpleMethodCall);
        goto EXIT;                               // From MainClass::gen
        // End MainClass::gen
    
        // Begin ClassDecl::gen
                                                 // From VarDecl::gen
                                                 // nothing because no variable
     echo_A:;                                    // From MethodDecl::gen
        SP = SP - 8;		             // allocate act. rec. for echo_A
        lv(int,SP,4) = i_echo_A;	             // Push arg i
        lv(void*,SP,0) = T7_echo_A;	             // Push return addr of A::echo
        T2_A = lv(word*,TP,0);	             //fetch
        i_echo_A = echo_A_arg_1;		     // Copy arg-reg to parm.
        echo_A_result_ = i_echo_A;               // return (i);
        i_echo_A = lv(int,SP,4);	             //fetch
        T7_echo_A = lv(void*,SP,0);	             //fetch
        lv(word*,TP,0) = T2_A;	             //store
        SP =SP + 8;		                     // deallocate echo_A.
        goto* T7_echo_A;	                     // returning from echo_A;
        // End ClassDecl::gen
        // End code
    
    // end of main code and beginning of postamble ===========
     EXIT:;                      // to stop                    
    } // end of main            // end of postamble