Function Calling Sequence. STMicroelectronics PGI
Reklama
Reklama
Linux86-64 Programming Model
Function Calling Sequence
This section describes the standard function calling sequence, including the stack frame, register usage, and parameter passing.
Type
General
XMM x87
Register Usage Conventions
The following table defines the standard for register allocation. The x64 Architecture provides a variety of registers. All the general purpose registers, XMM registers, and x87 registers are global to all procedures in a running program.
Name
Table 21.7. Register Allocation
Purpose
%rbp
%rsi
%rdi
%r8
%r9
%r10
%rax
%rbx
%rcx
%rdx
%rsp
1st return register callee-saved; optional base pointer pass 4th argument to functions pass 3rd argument to functions; 2nd return register stack pointer callee-saved; optional stack frame pointer pass 2nd argument to functions pass 1st argument to functions pass 5th argument to functions pass 6th argument to functions temporary register; pass a function’s static chain pointer
%r11
%r12-r15 temporary register callee-saved registers
%xmm0-%xmm1 pass and return floating point arguments
%xmm2-%xmm7 pass floating point arguments
%xmm8-%xmm15 temporary registers
%st(0)
%st(1) temporary register; return long double arguments temporary register; return long double arguments
%st(2) - %st(7) temporary registers
In addition to the registers, each function has a frame on the run-time stack. This stack grows downward from
shows the stack frame organization.
Position
8n+16 (%rbp)
Table 21.8. Standard Stack Frame
Contents Frame
argument eightbyte n previous
362
Chapter 21. Run-time Environment
Position
16 (%rbp)
8 (%rbp)
0 (%rbp)
-8 (%rbp)
0 (%rsp)
-128 (%rsp)
Contents
. . .
argument eightbyte 0
Frame
return address caller's %rbp unspecified
. . .
variable size red zone current current
Key points concerning the stack frame:
• The end of the input argument area is aligned on a 16-byte boundary.
• The 128-byte area beyond the location of %rsp is called the red zone and can be used for temporary local data storage. This area is not modified by signal or interrupt handlers.
• A call instruction pushes the address of the next instruction (the return address) onto the stack. The return instruction pops the address off the stack and effectively continues execution at the next instruction after the call instruction. A function must preserve non-volatile registers, a register whose contents must be preserved across subroutine calls. Additionally, the called function must remove the return address from the stack, leaving the stack pointer (%rsp) with the value it had before the call instruction was executed.
All registers on an x64 system are global and thus visible to both a calling and a called function. Registers
%rbx, %rsp, %rbp, %r12, %r13, %r14, and %r15 are non-volatile across function calls. Therefore, a function must preserve these registers’ values for its caller. Remaining registers are volatile (scratch) registers, that is a register whose contents need not be preserved across subroutine calls. If a calling function wants to preserve such a register value across a function call, it must save its value explicitly.
Registers are used extensively in the standard calling sequence. The first six integer and pointer arguments are passed in these registers (listed in order): %rdi, %rsi, %rdx, %rcx, %r8, %r9. The first eight floating point arguments are passed in the first eight XMM registers: %xmm0, %xmm1, …, %xmm7. The registers %rax and %rdx are used to return integer and pointer values. The registers %xmm0 and %xmm1 are used to return floating point values.
Additional registers with assigned roles in the standard calling sequence:
%rsp
The stack pointer holds the limit of the current stack frame, which is the address of the stack’s bottommost, valid word. The stack must be 16-byte aligned.
%rbp
The frame pointer holds a base address for the current stack frame. Consequently, a function has registers pointing to both ends of its frame. Incoming arguments reside in the previous frame, referenced as positive offsets from %rbp, while local variables reside in the current frame, referenced as negative offsets from %rbp. A function must preserve this register value for its caller.
363
Stiahnuť
Reklama