Important: The information in this document is obsolete and should not be used for new development.
Prologs and Epilogs
The called routine is responsible for allocating its own stack frame, making sure to preserve 16-byte alignment on the stack. This action is accomplished by the prolog before entering the actual routine. The compiler-generated prolog code does the following:
- Decrements the stack pointer to account for the new stack frame.
- Writes the previous value of the stack pointer to its own linkage area. This procedure ensures that the stack can be restored to its original state after returning from the call.
- Saves all nonvolatile general-purpose and floating-point registers into the saved-registers area. Note that if the called routine does not change a particular nonvolatile register, it does not save it.
- Saves the Link Register and Condition Register values in the caller's linkage area, if needed.
Listing 4-1 shows some sample prolog code. Note that the order of these actions differs from the order previously described.
- Note
- The order in which the prolog executes these actions is determined by convention, not by any requirements of the PowerPC runtime architecture.
Listing 4-1 Sample prolog code
linkageArea: set 24 ; size in PowerPC environment params: set 32 ; callee parameter area localVars: set 0 ; callee local variables numGPRs: set 0 ; volatile GPRs used by callee numFPRs: set 0 ; volatile FPRs used by callee) spaceToSave: set linkageArea + params + localVars spaceToSave: set spaceToSave + 4*numGPRs + 8*numFPRs .moo: ; PROLOG mflr r0, ; extract return address stw r0,8(SP) ; save the return address stwu SP, -spaceToSave(SP); skip over caller save areaAfter the called routine exits, the epilog code executes, which does the following:
Listing 4-2 shows some sample epilog code.
- Restores the nonvolatile general-purpose and floating-point registers that were saved in the stack frame.
- Restores the Condition Register and Link Register values that were stored in the linkage area.
- Restores the stack pointer to its previous value.
- Returns to the calling routine using the address stored in the Link Register.
Listing 4-2 Sample epilog code
; EPILOG lwz r0,spaceToSave(SP)+8; get the return address mtlr R0 ; reset Link Register addic SP,SP,spaceToSave ; restore stack pointer blr ; returnThe calling routine is responsible for restoring its GPR2 value immediately after returning from the called routine.