Prologs and epilogs

Prologs and epilogs are used for functions, including setting the registers on function entry and restoring the registers on function exit.

Prologs and epilogs may be used for functions, including setting the registers on function entry and restoring the registers on function exit.

No predetermined code sequences representing function prologs and epilogs are dictated. However, certain operations must be performed under certain conditions. The following diagram shows the stack frame layout.

Figure 1. Stack Frame Layout
Stack Frame Layout

A typical function's execution stack is:

  • Prolog action
  • Body of function
  • Epilog action

The Prolog Actions and Epilog Actions tables show the conditions and actions required for prologs and epilogs.

Table 1. Prolog Actions
If: Then:
Any nonvolatile FPRs (FPR14:FPR31) are used Save them in the FPR save area (area 4 in the previous figure).
Any nonvolatile GPRs (GPR13:GPR31) are used Save them in the GPR save area (area 5 in the previous figure).
LR is used for a nonleaf procedure Save the LR at offset eight from the caller function SP.
Any of the nonvolatile condition register (CR) fields are used. Save the CR at offset four from the caller function SP.
A new stack frame is required Get a stack frame and decrement the SP by the size of the frame padded (if necessary) to a multiple of 16 to acquire a new SP and save caller's SP at offset 0 from the new SP.
Note: A leaf function that does not require stack space for local variables and temporaries can save its caller registers at a negative offset from the caller SP without actually acquiring a stack frame.
Table 2. Epilog Actions
If: Then:
Any nonvolatile FPRs were saved Restore the FPRs that were used.
Any nonvolatile GPRs were saved Restore the GPRs that were saved.
The LR was altered because a nonleaf procedure was invoked Restore LR.
The CR was altered Restore CR.
A new stack was acquired Restore the old SP to the value it had on entry (the caller's SP). Return to caller.

While the PowerPC® architecture provides both load and store multiple instructions for GPRs, it discourages their use because their implementation on some machines may not be optimal. In fact, use of the load and store multiple instructions on some future implementations may be significantly slower than the equivalent series of single word loads or stores. However, saving many FPRs or GPRs with single load or store instructions in a function prolog or epilog leads to increased code size. For this reason, the system environment must provide routines that can be called from a function prolog and epilog that will do the saving and restoring of the FPRs and GPRs. The interface to these routines, their source code, and some prolog and epilog code sequences are provided.

As shown in the stack frame layout, the GPR save area is not at a fixed position from either the caller SP or the callee SP. The FPR save area starts at a fixed position, directly above the SP (lower address) on entry to that callee, but the position of the GPR save area depends on the number of FPRs saved. Thus, it is difficult to write a general-purpose GPR-saving function that uses fixed displacements from SP.

If the routine needs to save both GPRs and FPRs, use GPR12 as the pointer for saving and restoring GPRs. (GPR12 is a volatile register, but does not contain input parameters.) This results in the definition of multiple-register save and restore routines, each of which saves or restores m FPRs and n GPRs. This is achieved by executing a bla (Branch and Link Absolute) instruction to specially provided routines containing multiple entry points (one for each register number), starting from the lowest nonvolatile register.

Note:
  1. There are no entry points for saving and restoring GPR and FPR numbers greater than 29. It is more efficient to save a small number of registers in the prolog than it is to call the save and restore functions.
  2. If the LR is not saved or restored in the following code segments, the language processor must perform the saving and restoring as appropriate.

Language processors must use a proprietary method to conserve the values of nonvolatile registers across a function call.

Three sets of save and restore routines must be made available by the system environment. These routines are:

  • A pair of routines to save and restore GPRs when FPRs are not being saved and restored.
  • A pair of routines to save and restore GPRs when FPRs are being saved and restored.
  • A pair of routines to save and restore FPRs.