Function Calling Sequence. STMicroelectronics PGI
Annons
Annons
Chapter 21. Run-time Environment
This chapter describes the programming model supported for compiler code generation, including register conventions and calling conventions for x86 and x64 processor-based systems. It addresses these conventions for processors running linux86 or Win32 operating systems, for processors running linux86-64 operating systems, and for processors running Win64 operating systems.
Note
In this chapter we sometimes refer to word, halfword, and double word. The equivalent byte information is word (4 byte), halfword (2 byte), and double word (8 byte).
Linux86 and Win32 Programming Model
This section defines compiler and assembly language conventions for the use of certain aspects of an x86 processor running a linux86 or Win32 operating system. These standards must be followed to guarantee that compilers, application programs, and operating systems written by different people and organizations will work together. The conventions supported by the PGCC ANSI C compiler implement the application binary interface (ABI) as defined in the System V Application Binary Interface: Intel Processor Supplement and the
System V Application Binary Interface, listed in the "Related Publications" section in the Preface.
Function Calling Sequence
This section describes the standard function calling sequence, including the stack frame, register usage, and parameter passing.
Register Usage Conventions
The following table defines the standard for register allocation. The 32-bit x86 Architecture provides a number of registers. All the integer registers and all the floating-point registers are global to all procedures in a running program.
355
Linux86 and Win32 Programming Model
356
Type Name
General %eax
%edx
%ecx
%ebx
%ebp
%esi
%edi
%esp
Floating-point %st(0)
%st(1)
%st(...)
%st(7)
Table 21.1. Register Allocation
Purpose
integer return value dividend register (for divide operations) count register (shift and string operations) local register variable optional stack frame pointer local register variable local register variable stack pointer floating-point stack top, return value floating-point next to stack top floating-point stack bottom
In addition to the registers, each function has a frame on the run-time stack. This stack grows downward from high addresses. The next table shows the stack frame organization.
Position
4n+8 (%ebp)
8 (%ebp)
4 (%ebp)
0 (%ebp)
-4 (%ebp)
-n (%ebp)
Table 21.2. Standard Stack Frame
Contents
argument word n argument words 1 to n-1 argument word 0 return address caller's %ebp n bytes of local variables and temps
Frame
previous current
Several key points concerning the stack frame:
• The stack is kept double word aligned.
• Argument words are pushed onto the stack in reverse order so the rightmost argument in C call syntax has the highest address. A dummy word may be pushed ahead of the rightmost argument in order to preserve doubleword alignment. All incoming arguments appear on the stack, residing in the stack frame of the caller.
• An argument’s size is increased, if necessary, to make it a multiple of words. This may require tail padding, depending on the size of the argument.
Chapter 21. Run-time Environment
All registers on an x86 system are global and thus visible to both a calling and a called function. Registers
%ebp, %ebx, %edi, %esi, and %esp are non-volatile across function calls. Therefore, a function must preserve these registers’ values for its caller. Remaining registers are volatile (scratch). If a calling function wants to preserve such a register value across a function call, it must save its value explicitly.
Some registers have assigned roles in the standard calling sequence:
%esp
The stack pointer holds the limit of the current stack frame, which is the address of the stack’s bottommost, valid word. At all times, the stack pointer should point to a word-aligned area.
%ebp
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 %ebp, while local variables reside in the current frame, referenced as negative offsets from %ebp. A function must preserve this register value for its caller.
%eax
Integral and pointer return values appear in %eax. A function that returns a structure or union value places the address of the result in %eax. Otherwise, this is a scratch register.
%esi, %edi
These local registers have no specified role in the standard calling sequence. Functions must preserve their values for the caller.
%ecx, %edx
Scratch registers have no specified role in the standard calling sequence. Functions do not have to preserve their values for the caller.
%st(0)
Floating-point return values appear on the top of the floating point register stack; there is no difference in the representation of single or double-precision values in floating point registers. If the function does not return a floating point value, then the stack must be empty.
%st(1) - %st(7)
Floating point scratch registers have no specified role in the standard calling sequence. These registers must be empty before entry and upon exit from a function.
EFLAGS
The flags register contains the system flags, such as the direction flag and the carry flag. The direction flag must be set to the "forward" (i.e., zero) direction before entry and upon exit from a function. Other user flags have no specified role in the standard calling sequence and are not reserved.
Floating Point Control Word
The control word contains the floating-point flags, such as the rounding mode and exception masking.
This register is initialized at process initialization time and its value must be preserved.
Signals can interrupt processes. Functions called during signal handling have no unusual restriction on their use of registers. Moreover, if a signal handling function returns, the process resumes its original execution path with registers restored to their original values. Thus, programs and compilers may freely use all registers without danger of signal handlers changing their values.
357
Ladda ner
Annons