Calling convention

from Wikipedia, the free encyclopedia

In calling convention ( English calling convention ) is the method in the computer programs a subroutine data is passed. As a rule, it is up to the compiler which convention is used, so that the programmer does not have to deal with it. However, developing software in multiple languages ​​requires that all modules use compatible calling conventions.

X86 architecture calling conventions

The x86 architecture has many different calling conventions. Because of the limited number of registers in many x86 calling conventions, the arguments are mainly transferred via the stack, while the return value (or a pointer to it) is returned via a register. Some conventions use registers for the first arguments, which benefits the performance for simple, frequently called functions (e.g. functions that are not called by others and are therefore not returned to).

cdecl

The so-called cdecl calling convention is used by many C and C ++ compilers that run on the x86 architecture . The parameters are placed one after the other on the stack from right to left . Return values ​​are usually saved by the called function in the EAX register of the CPU . Floating point numbers that are stored in ST0 are an exception . The registers EAX, ECX and EDX are available for use within the function. If, in addition, other registers are to be used, the content of these registers must be saved by the function (usually by placing them on the stack) and restored before the return jump.

Example in C code:

int function(int, int, int); /* Prototyp der Funktion */
int a, b, c, x; /* Variablendeklaration */

x = function(a, b, c); /* Funktionsaufruf */

The function call in the last line generates the following x86 assembler code (in MASM syntax):

; Argumente in umgekehrter Reihenfolge auf den Stack legen
push c
push b
push a

; Funktion aufrufen
call function

; Stack-Pointer zurücksetzen
add esp, 12

; Rückgabewert der Funktion sichern
mov x, eax

After returning, the caller clears the stack again by setting the stack pointer (stored in the ESP register) so that it points again to the position in the memory that it pointed to before the push operations. In the example above, three integers , i.e. 12 bytes , are placed on the stack. Since the stack in x86 systems grows from top to bottom, ESP is decremented by 12. To get back to the position from before, 12 must be added again to the value in the ESP register after the call. This means that functions with a variable number and length of arguments can also be implemented.

The cdecl calling convention is usually the standard calling convention of an x86 C compiler. However, many compilers have the option to use a different convention.

A function can be declared manually as a cdecl function with the following syntax:

int _cdecl function(int,int,int);

stdcall

The stdcall calling convention is de facto the standard calling convention for the Microsoft Win32 API . Function parameters are passed from right to left. The registers EAX, ECX, and EDX are reserved for use within the function, so they may be changed. Return values ​​are returned in the EAX register. Unlike cdecl, the called function cleans up the stack, not the caller. Because of this fact, stdcall functions do not support variable argument lists.

Example (declaration of a stdcall function in C):

int _stdcall function(int,int,int);

Functions that use the stdcall method are easy to recognize in assembly code because they always clear the stack themselves before returning to the calling code. The x86 command ret allows an optional parameter that specifies the size of the stack to be reduced.

Example: Remove 12 bytes from the stack when returning.

ret 12

Pascal

With the Pascal calling convention, in contrast to the cdecl convention, the parameters are placed on the stack in the order from left to right, and the called function must reset the stack pointer itself before returning to the calling code.

Register (FastCall)

The Register or FastCall calling convention is compiler-specific. In general, it says that the first two or three function arguments with a size of 32 bits or less in the registers EAX, EDX, and possibly also ECX are passed instead of through the stack. The remaining arguments are placed on the stack from right to left, similar to cdecl. The Borland and Delphi compilers, on the other hand, place the remaining arguments on the stack from left to right, as in the Pascal calling convention. The return values ​​are returned in registers AL, AX, or EAX. In x64 systems, up to four arguments with 64 bits or less are passed in special registers, the rest on the stack.

This convention is used, among other things, in the Linux kernel to pass arguments to system calls . The system call number, which uniquely determines each possible call, is stored in the EAX register, while all arguments to the kernel function are stored in the EBX, ECX, EDX, ESI and EDI registers. If more arguments have to be passed, a data structure with the required elements is simply stored in memory and a pointer to this is passed on to the function as an argument.

thiscall

This calling convention is used for calling non-static C ++ member functions. There are two major versions of thiscall that are used depending on the compiler and whether the function supports variable argument lists or not.

In the GCC , thiscall is almost identical to cdecl , the caller cleans up the stack and the parameters are placed on the stack from right to left. The difference lies in the this pointer , which is put on the stack as the last argument, as if it were the first parameter of the function to be passed.

With the Microsoft Visual C ++ compiler, the this pointer is passed in the ECX register and the called function cleans up the stack, so it is handled as with the stdcall calling convention. On the other hand, if variable argument lists are used, the caller cleans up the stack (as with cdecl ).

The thiscall calling convention can only be used explicitly with Microsoft Visual C ++ 2005 and later versions and allows member functions to be called from native code when classes use the clrcall calling convention by default (managed code).

Summary table of calling conventions

architecture Calling convention Operating system, compiler Parameters in registers Order of parameters on the stack Stack is cleaned up by ... Return parameter, comment
16bit cdecl C. caller
Pascal Pascal function
fastcall Microsoft (non-member) ax, dx, bx Pascal function return pointer in bx
fastcall Microsoft (member function) ax, dx Pascal function "this" on the low stack address. return pointer in ax
fastcall Borland ax, dx, bx Pascal function "this" on the low stack address. return ptr on the upper stack address
Watcom ax, dx, bx, cx C. function return pointer in si
32bit cdecl C. caller
GNU compiler C. hybrid Stack possibly aligned on 16.
fastcall Microsoft ecx, edx C. function return pointer on the stack if not member function
fastcall gnu ecx, edx C. function
fastcall Borland eax, edx, ecx Pascal function
thiscall Microsoft ecx C. function default for member functions
Watcom eax, edx, ebx, ecx C. function return pointer in esi
64bit Microsoft x64 calling convention Windows ( Microsoft Compiler , Intel Compiler ) rcx / xmm0, rdx / xmm1, r8 / xmm2, r9 / xmm3 C. caller Stack aligned on 16. 32 bytes of shadow space on the stack. The specified 8 registers can only be used for parameters 1, 2, 3 and 4 (e.g. either rcx or xmm0, but not both, therefore only 4 in total).
AMD64 ABI convention Linux, BSD, Mac (Gnu compiler, Intel compiler) rdi, rsi, rdx, rcx, r8, r9, xmm0-7 C. caller Stack aligned on 16. Red zone under the stack.

Individual evidence

  1. ^ Raymond Chen: The history of calling conventions, part 1 . The Old New Thing. January 2, 2004. Retrieved September 26, 2010.
  2. a b c d Agner Fog: Calling conventions for different C ++ compilers and operating systems (PDF; 416 kB) February 16, 2010. Accessed August 30, 2010.
  3. IBM: Developing COBOL and PL / I applications for Windows, CDECL (as of December 7, 2008)
  4. ___stdcall . msdn.microsoft.com. February 16, 2010. Retrieved September 24, 2010.
  5. Delphi Calling Conventions
  6. x64 Software Conventions: Calling Conventions . msdn.microsoft.com. 2010. Retrieved September 27, 2010.
  7. Michael Matz, Jan Hubicka, Andreas Jaeger, and Mark Mitchell: System V Application Binary Interface AMD64 Architecture Processor Supplement (PDF), Advanced Micro Devices, September 3, 2010. Archived from the original on July 16, 2011 (accessed on September 26 2010).