Calling convention

In computer science, a calling convention is an implementation-level (low-level) scheme for how subroutines or functions receive parameters from their caller and how they return a result.

Calling conventions are usually considered part of the application binary interface (ABI).

The names of members within passed structures and objects would also be considered part of the API, and not ABI.

Calling conventions do not typically include information on handling lifespan of dynamically-allocated structures and objects.

Other supplementary documentation may state where the responsibility for freeing up allocated memory lies.

For some languages, the calling convention includes details of error or exception handling, (e.g. Go, Java) and for others, it does not (e.g. C++).

For RISCs including SPARC, MIPS, and RISC-V, registers names based on this calling convention are often used.

For example, MIPS registers $4 through $7 have "ABI names" $a0 through $a3, reflecting their use for parameter passing in the standard calling convention.

(RISC CPUs have many equivalent general-purpose registers so there's typically no hardware reason for giving them names other than numbers.)

The function declarations will include additional platform-specific keywords that indicate the calling convention to be used.

When handled correctly, the compiler will generate code to call functions in the appropriate manner.

Example call: Typical callee structure: (some or all (except ret) of the instructions below may be optimized away in simple procedures).

The standard 32-bit ARM calling convention allocates the 16 general-purpose registers as: If the type of value returned is too large to fit in r0 to r3, or whose size cannot be determined statically at compile time, then the caller must allocate space for that value at run time, and pass a pointer to that space in r0.

Similarly, the 32 floating-point registers are allocated as:[5] RISC-V has a defined calling convention with two flavors, with or without floating point.

This perceived slowness, along with an antique floating-point model with 16 registers only, has encouraged the proliferation of many other calling conventions.

[10] EABI inspired MIPS Technologies to propose a more radical "NUBI" ABI that additionally reuses argument registers for the return value.

However, space is always allocated by the called function to handle a potential register window overflow, local variables, and (on 32-bit SPARC) returning a struct by value.

Calling program: Called program: Standard entry sequence: Standard return sequence: Notes: In the System/390 ABI[13] and the z/Architecture ABI,[14] used in Linux: Additional arguments are passed on the stack.

The most common calling convention for the Motorola 68000 series is:[15][16][17][18] The IBM 1130 was a small 16-bit word-addressable machine.

This makes naive implementations slower than calling conventions that keep more values in registers.