Drill 5 Solutions

  1. Does the current compiler ever put the address of a memory location on the heap onto the stack?
  1. Does the current compiler ever put the address of a memory location on the heap into a register?
  1. How many 8-byte heap "cells" will be used when each of the following programs are compiled and executed? (Remember that each pair takes up two cells).

    • (+ (left (pair 1 2)) (right (pair 3 4)))
      
      • 4
    • (let ((x false)) 
          (pair 1 (if x (pair 2 (pair 3 (pair 4 false))) 
                          (pair 2 (pair 3 false))
                  )
          )
      )
      
        1. Remember that only one branch of the conditional will actually be executed: in this case, the else branch! We can't count heap usage just by counting occurences of pair.
  2. For each expression below, will it result in an error or a valid result when run in the interpreter we have built in class?

    program error valid result
    (+ 1 3 4) x
    (+ 1 (let ((x 2)) x)) x
    (+ 1 (let ((x 2)) false)) x
    (left (pair 1 false)) x
    (left (left (pair 1 false))) x
    (** 2 3) x
    (if true (* 2 3) (** 2 3)) x
  3. For each expression below, will it result in a compile-time error, a run-time error, or a valid result when run in the compiler we have built in class? (Assume that we have added error handling for all supported operations).

    program compile error runtime error valid result
    (+ 1 3 4) x
    (+ 1 (let ((x 2)) x)) x
    (+ 1 (let ((x 2)) false)) x
    (left (pair 1 false)) x
    (left (left (pair 1 false))) x
    (** 2 3) x
    (if true (* 2 3) (** 2 3)) x