CS461

Final Exam


Instructions

  1. Write your name clearly at the top of each of your answer sheets.
  2. There are 9 questions on the exam totalling 110 points. Feel free to answer all 9 questions to try to get 110 points, or to leave a question blank if it seems too tough and/or time-consuming (although some questions are worth more than 10 points, so leaving them blank will be detrimental to your grade).
  3. You have until the end of the class period to finish the exam. When the proctor calls time you must immediately drop your pen/pencil and hand in your exam.
  4. Ensure that you write clearly and legibly. Failure to do so may result in the deduction of points.
  5. The exam is closed note, closed book, but you are permitted to have one page, front and back, of notes.
  6. No electronic devices are allowed and all electronic devices that you have with you must be turned off.
  7. Good luck!

  1. (18 points) For each of the following questions, choose the most appropriate answer from the following list:

    structural equivalencecontent equivalence semantic equivalencename equivalence
    hot spot compilationdynamic compilation just in time compilationvirtual machine
    linkerloadercompilerunrolling
    inliningtail trimmingbasic blockdata block
    control blockdope vectorsymbol tablerelocation table
    dimensions tablejump tablehotspot tablere-ordering evaluation
    short circuit evaluationlifting evaluationhot read evaluationruntime environment

    1. ________________________ The name of a node in a control flow graph that contains a maximal-length set of operations that should execute sequentially at run time, with no branches in or out.
    2. ________________________ The name of the data structure that is maintained by the run-time environment to keep track of the lower and upper dimensions of a dynamic array, and the size of each dimension of the dynamic array.
    3. ________________________ The name given to an interpreter of byte codes.
    4. ________________________ Under this form of type equivalence, two types are considered equivalent if they consist of the same components, put together the same way.
    5. ________________________ The name for the code generated by the compiler for a switch statement that allows control to transfer in O(1) time to the appropriate branch of the switch statement.
    6. ________________________ The name of the optimization that occurs when the compiler replaces a function call with the function body.
    7. ________________________ The name given to a compiler that converts byte codes to machine language immediately before the program is about to execute.
    8. ________________________ The name of the optimization that occurs when the compiler replaces a loop that executes exactly 5 times with 5 consecutive copies of the loop body.
    9. ________________________ The name given to evaluation of a conditional when control transfers to the then or else branch as soon as the condition is known to be true or false
  2. (10 points) Explain two ways that Java dynamic compilation (e.g., Sun's Hotspot compiler) is able to achieve performance comparable to C code. Use less than three sentences for each of the two ways.

  3. (10 points) In class we talked about how the compiler could improve instruction scheduling if it is allowed to re-order the operations of an arithmetic expression. In C, is it safe for the compiler to also try to re-order the order in which the operands of a boolean expression are evaluated in order to improve instruction scheduling. For example, if you have an expression of the form
    if (exp1 && exp2)
    
    is it safe to evaluate exp2 before exp1? Why or why not? If you say it is not safe, please give me a concrete C example that shows why it is unsafe.

  4. (8 points) Reference counting has a "flaw" that can prevent big chunks of memory from being garbage collected, even if that memory is no longer accessable. In three sentences or less, describe how this flaw can prevent the garbage collection of memory.

  5. (8 points) In three sentences or less describe how a compiler can optimize a tail recursive function.

  6. (8 points) In three sentences or less describe why it is important to know whether a multi-dimensional array is organized in memory in row major or column major order when you are writing a series of nested loops that will end up touching each element in the array, as in:
      for (i = 0; i < 10; i++)
        for (j = 0; j < 20; j++)
          for (k = 0; k < 30; k++)
             ... a[i][j][k]
    

  7. (10 points) Suppose you are given the follow declarations:
    typedef struct {
      double centimeters;
      double meters;
    } measure;
    
    typedef measure metric_measure;
    
    struct unitA {
      double centimeters;
      double meters;
    };
    
    struct unitB {
      double meters;
      double centimeters;
    };
    
    Answer the following questions:

    1. Under structural type equivalence, which, if any, of the above types are considered equivalent?

    2. Under strict name type equivalence, which, if any, of the above types are considered equivalent?
  8. (18 points) Consider the following two pseudo-C code files:

    A.cB.c
    Imports:Imports:
    Exports:Exports:
    Relocatable
    Names:
    Relocatable
    Names:
    Code:
    int x;
    int y;
    
    int main() {
      z = add(x,y);
      print x, y, z;
    }
    
    Code:
    int z;
    int add(int a, int b) {
      return a + b;
    }
    
    Data:Data:

    Fill in the remainder of the table as follows:

    1. Imports: Give a comma-separated list of all names imported into this file
    2. Exports: Give a comma-separated list of all names exported from this file. Assume that all names in the global namespace of the file get exported.
    3. Relocatable names: Give a comma-separated list all the names in this file that might have to be assigned new addresses by the linker. Include in this list both names that appear in the data section and those that appear in the code section, but list a name at most once.
    4. Data: Give a comma-separated list all names in the file that would be allocated storage in this section.

    If there are no names for a particular section, just leave that section blank.

  9. (20 points) Using the attached figure from the Scott text, show the assembly code that would be generated for the following code fragment. To assist you, I have also shown the abstract syntax tree for this code :
                         
    while (i < 10)
      sum = sum + i * i;
      i = i + 1;
    
        while-----------------------------
       /        \                         \
      <         := ------------          null
     /   \     /  \            \
    i    10  sum   +           := ------
                 /   \        /   \     \
               sum    *      i     +   null
                    /   \         / \
                   i     i       i   1