In computer science, a calling convention is an implementation level (low level) scheme for how subroutines receive parameters from their caller and how they return a result. This is somewhat related to a particular programming language's evaluation strategy but most often not considered part of it (or vice versa) as the latter is considered part of the language rather than the compiler and largely defined on a higher abstraction level.
Calling conventions can differ in:
- where parameters and return values are placed (in registers, on the call stack, a mix of both, or in other memory structures)
- the order in which actual arguments for formal parameters are passed (or the parts of a large or complex argument)
- how a (possibly long or complex) return value is delivered from the callee back to the caller (on the stack, in a register, or within the heap)
- how the task of setting up for and cleaning up after a function call is divided between the caller and the callee
In some cases, differences also include the following:
- conventions on which registers may be directly used by the callee, without being preserved (otherwise regarded as an ABI detail)
- which registers are considered to be volatile and, if volatile, need not be restored by the callee (often regarded as an ABI detail)
Although some languages actually specify (parts of) this in the programming language specification (or in some pivotal implementation), different implementations of such languages (i.e. different compilers) may typically still use various calling conventions, often selectable. One reason for this is performance, another is a frequent adaption to other popular languages conventions (with or without technical reason), and a third is that "platforms" (CPU architecture + operating system combinations) also may impose restrictions or conventions.
This must be considered when combining modules written in multiple languages, or when calling operating system or library APIs from a language other than the one in which they are written; in these cases, special care must be taken to coordinate the calling conventions used by caller and callee. Even a program using a single programming language may use multiple calling conventions, either chosen by the compiler, for code optimization, or specified by the programmer.
CPU architectures always have more than one possible calling convention. With many general-purpose registers and other features, the potential number of calling conventions is large, although some architectures are formally specified to use only one calling convention, supplied by the architect.
Calling conventions on different platforms
The x86 architecture is used with many different calling conventions. Due to the small number of architectural registers, the x86 calling conventions mostly pass arguments on the stack, while the return value (or a pointer to it) is passed in a register. Some conventions use registers for the first few parameters, which may improve performance for short and simple leaf-routines very frequently invoked (i.e. routines that do not call other routines and do not have to be reentrant).
push eAX ; pass some register result push byte[eBP+20] ; pass some memory variable (FASM/TASM syntax) push 3 ; pass some constant call calc ; the returned result is now in eAX
Typical callee structure: (some or all (except ret) of the instructions below may be optimized away in simple procedures)
calc: push eBP ; save old frame pointer mov eBP,eSP ; get new frame pointer sub eSP,localsize ; reserve place for locals . . ; perform calculations, leave result in eAX . mov eSP,eBP ; free space for locals pop eBP ; restore old frame pointer ret paramsize ; free parameter space and return
The PowerPC architecture has a large number of registers so most functions can pass all arguments in registers for single level calls. Additional arguments are passed on the stack, and space for register-based arguments is also always allocated on the stack as a convenience to the called function in case multi-level calls are used (recursive or otherwise) and the registers must be saved. This is also of use in variadic functions, such as
printf(), where the function's arguments need to be accessed as an array. A single calling convention is used for all procedural languages.