The subroutine linkage convention specifies the machine state at subroutine entry and exit, allowing routines that are compiled separately in the same or different languages to be linked. The information on subroutine linkage and system calls in the System V Application Binary Interface: PowerPC Processor Supplement and 64-bit PowerPC ELF Application Binary Interface Supplement are the base references on this topic. You should consult these for full details. This section summarizes the information needed to write mixed-language Fortran and assembler programs or to debug at the assembler level, where you need to be concerned with these kinds of low-level details.
The system linkage convention passes arguments in registers, taking full advantage of the large number of floating-point registers (FPRs) and general-purpose registers (GPRs) and minimizing the saving and restoring of registers on subroutine entry and exit. The linkage convention allows for argument passing and return values to be in FPRs, GPRs, or both.
The following table lists floating-point registers and their
functions. The floating-point registers are double precision (64
bits).
Table 22. Floating-Point Register Usage across Calls
Register | Preserved Across Calls | Use |
---|---|---|
0 | no | |
1 | no | FP parameter 1, function return 1. |
2 | no | FP parameter 2, function return 2. |
3 | no | FP parameter 3, function return complex *32. |
4 | no | FP parameter 4, function return complex *32. |
· · · | · · · | · · · |
8 | no | FP parameter 8 |
9-13 | no |
|
14-31 | yes | local variables |
The following table lists general-purpose registers and their
functions.
Table 23. General-Purpose Register Usage across Calls
Register | Preserved Across Calls | Use |
---|---|---|
0 | no | |
1 | yes | Stack pointer. |
2 | yes | System-reserved. |
3 | no | 1st word of arg list; return value 1. |
4 | no | 2nd word of arg list; return value 2. |
5 | no | 3rd word of arg list |
· · · | · · · | · · · |
10 | no | 8th word of arg list. |
11-12 | no |
|
13 | yes | SDA pointer. |
14-30 | no | Local variables. |
31 | yes | Local variables or "environment pointers". |
If a register is not designated as preserved, its contents may be changed during the call, and the caller is responsible for saving any registers whose values are needed later. Conversely, if a register is supposed to be preserved, the callee is responsible for preserving its contents across the call, and the caller does not need any special action. |
The following table lists special-purpose register conventions.
Table 24. Special-Purpose Register Usage across Calls
Register | Preserved Across Calls |
---|---|
Condition register Bits 0-7 (CR0,CR1) Bits 8-22 (CR2,CR3,CR4) Bits 23-31 (CR5,CR6,CR7) |
no yes no |
Link register | no |
Count register | no |
XER register | no |
FPSCR register | no |
The stack is a portion of storage that is used to hold local storage, register save areas, parameter lists, and call-chain data. The stack grows from higher addresses to lower addresses. A stack pointer register (register 1) is used to mark the current "top" of the stack.
A stack frame is the portion of the stack that is used by a single procedure. The input parameters are considered part of the current stack frame. In a sense, each output argument belongs to both the caller's and the callee's stack frames. In either case, the stack frame size is best defined as the difference between the caller's stack pointer and the callee's.
The following diagrams show the storage maps of typical stack frames for 32-bit and 64-bit environments.
In these diagrams, the current routine has acquired a stack frame that allows it to call other functions. If the routine does not make any calls and there are no local variables or temporaries, and it does not need to save any non-volatile registers, the function need not allocate a stack frame. It can still use the register save area at the top of the caller's stack frame, if needed.
The stack frame is double-word aligned.
Run-time Stack for 32-bit Environment HIGH | | ADDRESSES | | |--------------------| Caller's stack --> | Back chain | pointer | | | | |--------------------| | | -8*nfprs --> | Save area for | Ffirst = F14 for a | caller's FPRs | full save | max 18 dblwds | F31 | | |--------------------| | | -8*nfprs-4*ngprs --> | Save area for | Rfirst = R14 for full save | caller's GPRs | save | max 18 words | R31 | | |--------------------| | | | Save area for | | CR | | | |--------------------| | | | Locals | | | |--------------------| | | Space for parameters | Pn | OUTPUT ARGUMENT AREA that do not fit in | ... | <---(Used by callee registers | P9 | to construct | | argument list) |--------------------| | | 4 | Saved LR | <-----+ | | | |--------------------| Minimum stack frame | | "link area" Callee's stack --> 0 | Back chain | <-----+ pointer | | |--------------------| | | LOW | | Stack grows at ADDRESSES | | this end. |
Run-time Stack for 64-bit Environment Low | | Stack grows at Addresses | | this end. |--------------------| Callee's stack --> 0 | Back chain | pointer 8 | Saved CR | 16 | Saved LR | 24-32 | Reserved | <--- LINK AREA 40 | Saved TOC | (callee) |--------------------| Space for P1-P8 | P1 | OUTPUT ARGUMENT AREA is always reserved | ... | <---(Used by callee | Pn | to construct |--------------------| argument list) | Callee's | | stack | <--- LOCAL STACK AREA | area | |--------------------| | | (Possible word wasted |--------------------| for alignment.) -8*nfprs-8*ngprs --> | Save area for | Rfirst = R13 for full save | caller's GPR | save | max 19 doublewords | R31 |--------------------| -8*nfprs --> | Save area for | Ffirst = F14 for a | caller's FPR | full save | max 18 dblwds | F31 |--------------------| Caller's stack --> 0 | Back chain | pointer 8 | Saved CR | 16 | Saved LR | 24-32 | Reserved | <--- LINK AREA 40 | Saved TOC | (caller) |--------------------| Space for P1-P8 48 | P1 | INPUT PARAMETER AREA is always reserved | ... | <---(Callee's input | Pn | parameters found |--------------------| here. Is also | Caller's | caller's arg area.) | stack | High | area | Addresses | | |
In a 32-bit environment, the link area consists of two words at offset zero from the callee's stack pointer on entry to a procedure. The first word contains the caller's back chain (pointer to the previous stack frame). The second word is the location where the caller saves the Link Register (LR), if it is needed.
In a 64-bit environment, this area consists of six doublewords at offset zero from the caller's stack pointer on entry to a procedure. The first doubleword contains the caller's back chain (stack pointer). The second doubleword is the location where the callee saves the Condition Register (CR) if it is needed. The third doubleword is the location where the callee's prolog code saves the Link Register if it is needed. The fourth doubleword is reserved for C SETJMP and LONGJMP processing, and the fifth doubleword is reserved for future use. The last doubleword (doubleword 6) is reserved for use by the global linkage routines that are used when calling routines in other object modules (for example, in shared libraries).
In a 32-bit environment, the input parameters that do not fit in registers go into the ouput argument area (P9... Pn).
In a 64-bit environment, the input parameter area is a contiguous piece of storage reserved by the calling program to represent the register image of the input parameters of the callee. The input parameter area is double-word aligned and is located on the stack directly following the caller's link area. This area is at least 8 doublewords in size. If more than 8 doublewords of parameters are expected, they are stored as register images that start at positive offset 112 from the incoming stack pointer.
The first 8 doublewords only appear in registers at the call point, never in the stack. Remaining words are always in the stack, and they can also be in registers.
In a 32-bit environment, the register save area provides the space that is needed to save all nonvolatile FPRs and GPRs used by the callee program. The FPRs are saved next to caller's minimum stack frame. The GPRs are saved below the FPRs (in lower addresses).
In a 64-bit environment, the register save area is double-word aligned. It provides the space that is needed to save all nonvolatile FPRs and GPRs used by the callee program. The FPRs are saved next to the link area. The GPRs are saved below the FPRs (in lower addresses). The called function may save the registers here even if it does not need to allocate a new stack frame. The system-defined stack floor includes the maximum possible save area:
32-bit platforms: 18*8 for FPRs + 18*4 for GPRs 64-bit platforms: 18*8 for FPRs + 19*8 for GPRs
A callee needs only to save the nonvolatile registers that it actually uses.
The local stack area is the space that is allocated by the callee procedure for local variables and temporaries.
In a 32-bit environment, the input parameters that do not fit in registers go into the ouput argument area (P9... Pn).
If more than 8 words are being passed, an extension list is constructed beginning at offset 8 from the current stack pointer.
The first 8 words only appear in registers at the call point, never in the stack. Remaining words are always in the stack, and they can also be in registers.
In a 64-bit environment, the output parameter area (P1...Pn) must be large enough to hold the largest parameter list of all procedures that the procedure that owns this stack frame calls. This area is at least 8 doublewords long, regardless of the length or existence of any argument list. If more than 8 doublewords are being passed, an extension list is constructed, which begins at offset 112 from the current stack pointer.
The first 8 doublewords only appear in registers at the call point,
never in the stack. Remaining doublewords are always in the stack, and
they can also be in registers.