XL Fortran for AIX 8.1

User's Guide


Assembler-Level Subroutine Linkage Conventions

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 AIX Commands Reference is the base reference on this topic. You should consult it 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 27. 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.

·
·
·

·
·
·

·
·
·
13 no FP parameter 13, function return 13.
14-31 yes

The following table lists general-purpose registers and their functions.

Table 28. General-Purpose Register Usage across Calls

Register Preserved Across Calls Use
0 no  
1 yes Stack pointer.
2 yes TOC pointer.
3 no 1st word of arg list; return value 1.
4 no 2nd word of arg list; return value 2.

·
·
·

·
·
·

·
·
·
10 no 8th word of arg list; return value 8.
11 no DSA pointer to internal procedure (Env).
12 no  
13-31 yes  
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 29. 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
MQ register no
XER register no
FPSCR register no

The Stack

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, 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. The FPR save area and the parameter area (P1, P2, ..., Pn) are also double-word aligned. Other areas require word alignment only.



                      Run-time Stack for 32-bit Environment
 
  Low                 |                    |   Stack grows at
  Addresses           |                    |   this end.
                      |--------------------|
Callee's stack  --> 0 |     Back chain     |
pointer             4 |      Saved CR      |
                    8 |      Saved LR      |
                12-16 |      Reserved      | <--- LINK AREA
                   20 |      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-4*ngprs --> |   Save area for    | Rfirst = R13 for full
   save               |   caller's GPR     |          save
                      |    max 19 words    | 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            4 |      Saved CR      |
                    8 |      Saved LR      |
                12-16 |      Reserved      | <--- LINK AREA
                   20 |      Saved TOC     |      (caller)
                      |--------------------|
Space for P1-P8    24 |         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           |                    |



                      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           |                    |

The Link Area

In a 32-bit environment, the link area consists of six words at offset zero from the caller's stack pointer on entry to a procedure. The first word contains the caller's back chain (stack pointer). The second word is the location where the callee saves the Condition Register (CR) if it is needed. The third word is the location where the callee's prolog code saves the Link Register if it is needed. The fourth word is reserved for C SETJMP and LONGJMP processing, and the fifth word is reserved for future use. The last word (word 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 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).

The Input Parameter Area

In a 32-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 words in size. If more than 8 words of parameters are expected, they are stored as register images that start at positive offset 56 from the incoming 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 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.

The Register Save Area

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 above 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 + 19*4 for GPRs
   64-bit platforms:  18*8 for FPRs + 19*8 for GPRs

Locations at a numerically lower address than stack floor should not be accessed.

A callee needs only to save the nonvolatile registers that it actually uses. It always saves register 31 in the highest address of the particular save area:

The Local Stack Area

The local stack area is the space that is allocated by the callee procedure for local variables and temporaries.

The Output Parameter Area

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.

In a 32-bit environment, this area is at least 8 words long, regardless of the length or existence of any argument list. If more than 8 words are being passed, an extension list is constructed beginning at offset 56 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, 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.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]