Compiler Construction
Spring 2000
Assignment IV


Due : March 29.

1.  From text: problem 7.6 (p. 456).

2.  Consider the following two programs whose purpose is two swap the values
of two variables:

   void swap (int x, int y) {
     int temp = x;
     x = y;
     y = temp;
   }

   void swap (int x, int y) {
      int foo () {
         int temp = x;
         x = y;
         return temp;
      }
      y = foo();
   }

Disregarding the C-syntax, assume that calls to these procedures pass
parameters a) strictly by value, b) by reference, c) by name.
For each of the two procedures, , discuss whether the procedures correctly swap
the values. You may want to consider different forms that the variables can
take, for example simple variables, array references, etc.

3.  Consider the following in a language allowing nested procedures

procedure main is
      procedure a is
          x : integer;

          procedure b is
              y : integer;

              procedure c is
                z : integer;
              begin
                 print (x + y + z);
              ..
             end c;

          begin   --  code for b
              c; 
          end b;
      ..
      begin   --  code for a
        b;
      end a; 

begin  --  main program
   a;
end;

a) Suppose that access to non-local variables is achieved by means of a
global display. Show the state of the stack and the  display when procedure c
computes the expression (x + y + z).

b) Same question assuming that the run-time uses a static chain.

c) Informally, estimate the number of instructions executed in each case to
compute the expression, assuming that individual arithmetic operations, array
indexing, and simple dereference count as one instruction.