Solutions -- programming language principles, part 2, CS 288, April 26, 2006

 

  1. The desired values are summarized in the table below (cf. Scott, pp 04-6).

    node program * + - / a b c d e
    next_free_reg - 0 0 1 2 0 1 1 2 3
    reg - r1 r1 r2 r3 r1 r2 r2 r3 r4
    code r1 := a
    r2 := b
    r1 := r1 + r2
    r2 := c
    r3 := d
    r4 := e
    r3 := r3 / r4
    r2 := r2 - r3
    r1 := r1 * r2
    r1 := a
    r2 := b
    r1 := r1 + r2
    r2 := c
    r3 := d
    r4 := e
    r3 := r3 / r4
    r2 := r2 - r3
    r1 := r1 * r2
    r1 := a
    r2 := b
    r1 := r1 + r2
    r2 := c
    r3 := d
    r4 := e
    r3 := r3 / r4
    r2 := r2 - r3
    r3 := d
    r4 := e
    r3 := r3 / r4
    r1 := a r2 := b r2 := c r3 := d r4 := e

     

    1. To save on the stack the current contents of registers that the current computation needs to write to.

    2. Registers in the subset to be saved by the caller rarely need to be saved at all. This is because the caller can and does use these registers for short-lived values that rarely need to be saved across calls.

      As a general example, suppose there are 8 registers (4 that the caller saves and 4 that the callee saves), the callee uses 5 registers, and the caller needs to preserve 5 of the registers across the call. The callee can use 4 of the caller-saves registers; it will have to save the 1 other register that it uses. The caller can arrange to put 4 of the 5 values that it needs to saved into the callee-saves registers; it will need to save the 1 remaining register. Thus only 2 registers need to be saved.

    3. Initializing the static pointer for a new frame needs to be done by the caller, since it depends on the lexical nesting depth of the caller, which is not known by the callee.

    4. These objects are stored at locations that are a fixed, known offset from the value of the frame pointer.

    5. Pointers to these objects are stored at locations that are a fixed, known offset from the value of the frame pointer. Other components of known size (e.g., dope vectors) may be stored with the pointer. The objects themselves are stored elsewhere in the frame, in an area for variable-sized data.

     

    1. In C or C*: (*p).first or p->first. In Ada and Java, p.first.
    2. In Ada and Java.
    3. No.
    4. Passing parameters by reference can be simulated by the use of pointer variables. The differences from the true pass-by-reference of C++ are that (1) the caller would pass a pointer to the parameter value, (2) the function prototype would declare the corresponding parameter to be a pointer, and (3) the function body would have to dereference this pointer explicitly every time the corresponding parameter value is to be used.