3.2.5 Bound Procedure Values

There are two distinct classes of procedures:

A simple procedure is a procedure that does not need direct access to the stack of its execution environment. A bound procedure is a procedure that does need direct access to the stack of its execution environment, typically to reference an up-level variable or to perform a non-local goto. Both a simple procedure and a bound procedure will have an associated procedure descriptor as described in previous sections. Bound procedure values are designed for multilanguage use and allow callers of procedures to use common code to call both bound and simple procedures.

When a bound procedure is called, the caller must pass some kind of pointer to the called code that allows it to reference its up-level environment; typically such a pointer is the frame pointer for that environment, but many variations are possible. When the caller is itself executing within that outer environment then it can usually make such a call directly to the code for the nested procedure without recourse to any additional mechanism. However, when a procedure value for the nested procedure must be passed outside of that environment to a call site that has no knowledge of the target procedure, a special bound procedure is created so that the nested procedure can be called just like a simple procedure.

The procedure value of a bound procedure is defined as the address of the first instruction of a sequence of instructions that establish the proper environment for the bound procedure and then transfer control to that procedure.

One direct scheme for constructing a jacket to a bound procedure that can be called like a simple procedure is to allocate a sequence of instructions on the stack and use the address of those instructions as the procedure value. Assume that a bound procedure named PROC expects its static link to be passed in R1. Then, a suitable sequence of instructions might look like the following:

    LDAH    R1,frame-hi(R31)     ;Create up-level pointer in R1  
    LDA     R1,frame-lo(R1)      ;  
    LDAH    R28,PROC-hi(R31)     ;Materialize address of bound procedure  
    LDA     R28,PROC-lo(R28)     ;  
    JMP     (R28)                ;Transfer to the bound procedure  
  

Note that this sequence can be created only by code that is executing within the context of the containing procedure so that the appropriate frame pointer value is known and can be easily incorporated into the sequence illustrated. The lifetime of this sequence is, of course, limited to the lifetime of the stack frame in which it is allocated.

After creating the above jacket instructions, it is necessary to execute an IMB instruction prior to execution of them to ensure instruction cache coherence, as described in the Alpha System Reference Manual.