Calling Convention

From Cppwiki

Jump to: navigation, search

Calling convention is the method by which a function is called, its arguments are provided, and its return value is retrieved. Typically a calling convention will specify which arguments are passed on the stack, which (if any) are placed in registers, etc. Calling convention is OS- and compiler-specific; often an OS will define a standard calling convention which programs must use to be interoperable, but compilers may provide additional calling conventions for special purposes. The calling convention is usually part of a larger ABI for the platform.

Contents

Microsoft Windows calling conventions

cdecl (standard c calling convention)

The cdecl convention is the most common for c/c++ functions. The main benefit it gives is the ability to call functions with a variable number of parameters. Because only the calling function knows how many parameters were actually passed, the calling function is always responsible for stack cleanup.

Details:

Arguments are passed right to left with the left-most argument being the closest to the stack pointer on function entry.

The calling code is responsible for stack cleanup

stdcall (sometimes pascal)

The stdcall convention is widely used in languages other than c/c++, such as fortran and pascal. It is also the main convention used by the Microsoft Windows API. The main benefit of the stdcall convention is that the number of parameters a function receives is fixed and the called function can handle the stack cleanup, which may in some cases offer a very slight performance boost.

Details:

Arguments are passed left to right, with the right most argument being the closest to the stack pointer on function entry.

The called function is responsible for stack cleanup

fastcall

The fastcall convention is limited mainly to Intel x86 systems and other machines with very few general purpose registers. The main benefit of fastcall is that it can make some function calls much faster by passing function arguments in registers instead of on the stack.

Details:

The first (variable number, but often limited to two) arguments are passed in general purpose registers instead of being pushed onto the stack. Remaining arguments are passed right to left.

The called function is responsible for stack cleanup.

thiscall

The thiscall convention is used only for non-static member functions, for both virtual and non-virtual members. The thiscall is generally a modified form of cdecl that includes a hidden 'this' parameter, either in a specified register or as the last parameter pushed onto the stack before a function call. The thiscall convention is implicit for non-static member functions and the programmer has no control over it. The most common time it is seen is in error messages.

Unix calling conventions

SVID ABI for Intel 386

The AT&T System V Interface Definition ABI (Intel386™ Architecture Processor Supplement) defines a standard calling convention for C functions. This ABI passes all arguments on the stack, at word (32-bit) alignment, including appropriate padding for struct and union arguments. If a function returns an integral or pointer value, its value is placed in %eax. Otherwise, the caller allocates space for the return value and passes this address as the first stack word (a hidden argument). The calling function is responsible for removing arguments from the stack, and for saving registers other than %ebp, %ebx, %edi, %esi, and %esp (these registers must be saved by the called function).

Personal tools