Home Exam in Compilation

Nurit Dor and Mooly Sagiv

Due 10/3/00 24:00  - Put in Nurit's box on first floor

Part I : Tiger Extensions (40%)

The following exceptions to Tiger were motivated by MAWL and BIGWIG internet programming languages.
Please explain in details the changes that are needed to the various compiler phases.
Focus your explanations on phases mentioned.
 
1. Adding a built-in functions isset and unset.
  • unset(lvalue) marks the object as not set as well as resetting its value  to the default

  • NOTE: For the purpose of this question assume that a declaration statement may not have an initialization.
    For example: "var i: int;"  is now a legal declaration.

    Example of a Tiger program using the isset and unset functions.
     

    let
       type name = { first: string,  last: string}
       var customer_name: name

       function setname() =
                   /* sets the global variable customer_name via internet service */

       function printname() =
                /* prints the global variable customer_name taking into 
                   account only one name */
                if  isset(customer_name) then
                    if  isset(customer_name.first) & isset(customer_name.last)=0 then
                            print( concat(customer_name.first, "  first is last name"))
                    else if isset(customer_name.first) & isset(customer_name.last) then
                            print( concat(customer_name.first, customer_name.last))
                    else if isset(customer_name.first) = 0 & isset(customer_name.last) then
                            print( concat("last is first name ",customer_name.last))

       in
            ( setname();
              printname())
    end

    Describe the changes needed to the various compiler phases and in particular code-generation, livenness analysis and register allocation. Demonstrate your answer on the example.
     

     

    2. Adding type exceptions and statements try-catch-finally and throw.

    Example of a Tiger program with exceptions:
     
    let
      exception UnknownUser(string)
      exception WrongPin()
      type ID = { name: string, pin: int }
      type IDarray = array  of ID
      var user: string = ""

      function getLogin(): ID =
                /* gets form the user login name and pin */

      function login(): string = 
        let  var ids := IDarray[2] of nil 
              var id: ID := getLogin()
              var i:  int := 0
              var res:string := ""
        in 
           ( ids[1]  := ID {name = "dal",   pin=1234};
            ids[2]  :=  ID {name = "tball", pin= 5678};

           while  i < 2 do
            ( if ids[i].name = id.name then
                if ids[i].pin = id.pin  then res := id.name
                else throw WrongPin;
              i := i+1 );

        if i = 2 then
              throw UnknownUser(id.name)
        else
               res )
      end
     

    in
         while(1) do
            (try 
                 user := login()
            catch(UnknownUser u) 
                 (print( concat( u, " is an invalid user name" ));
                  break );
            catch(WrongPin) 
                (print(  "invalid PIN" );
                 break );
            print( concat(user ," is logged in" ) );
            break )
    end


     
     

    a. (15%) Describe the changes needed to the various compiler phases and in particular code-generation.
    Show the intermediate assembler representation of each relevant statement. Demonstrate your answer on the example.

    b.(5%) Do exceptions give the programmer any new power  (can everything done with a program using exceptions be done by a
    program without exceptions)? Give advantages/disadvantages of exceptions.
     
     

    Part II : Tiger Simplification (40%)

    3. Removing user defined types. This means there is no declarations of type thus only string and int primitive data types are supported.
       a (17%) Which phases of the Tiger compiler will be affected and how?
       b. (3%)  How does this affect the usability of the Tiger language?

    4. Not allowing nested functions. This means all functions have the same level (like C).
       a (17%) Which phases of the Tiger compiler will be affected and how?
       b. (3%)  How does this affect the usability of the Tiger language?
     
     

     
    Part III : General (20%)
    5. Identify for every activity when does it occur, i.e., at compile-time (at which phase), run-time, or different time (specify). Justify your answer.
    1. Nil dereference in Tiger. For example referring to a field of a Nil record variable.
    2. Spill of register ri into the current activation record.
    3. a reference in C to an element which have been freed.
    4. the regular expression r  requires a  lookahead buffer which is too big.
    5. "stack overflow" for a Tiger program. That is no more space in the activation record.

    Good Luck!!