Calling convention

From Wikipedia, the free encyclopedia

In computer science, a calling convention is a standardized method for a program to pass parameters to a function and receive a result value back from it. Calling conventions can differ in where they place parameters and return values (in registers; on the call stack; a mix of both), in the order in which parameters are passed, in name mangling, and in how responsibility for setting up and cleaning up a function call is distributed between the calling and the called code.

Different programming languages use different calling conventions, and so can different platforms (CPU architecture + Operating system). In fact, multiple calling conventions can be used within the same program written using a single programming language (example: __stdcall, __cdecl and __fastcall calling conventions on Microsoft Windows). This can cause problems when writing software that combines 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.

Contents

[edit] Calling conventions on different platforms

[edit] x86

The x86 architecture features 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 very short and simple procedures.

[edit] PowerPC

Since the PowerPC architecture has a large number of registers, most functions can pass all arguments in registers; further arguments are passed on the stack, and space for register-based arguments is always allocated on the stack as a convenience to the called function, in case it needs to free up more registers. A single calling convention is used for all procedural languages.

[edit] MIPS

The first four arguments to a function are passed in the registers $a0-$a3; subsequent arguments are passed on the stack. The return value (or a pointer to it) is stored in register $v0.

[edit] SPARC

The SPARC architecture, unlike most RISC architectures, is built on register windows. There are 24 accessible registers in each register window, 8 of them are the "in" registers, 8 are registers for local variables, and 8 are out registers. The in registers are used to pass arguments to the function being called, so any additional arguments needed to be pushed onto the stack. However, space is always allocated by the called function to handle a potential register window overflow, local variable, and returning a struct by value. To call a function, one places the argument for the function to be called in the out registers, when the function is called the out registers become the in registers and the called function access the argument in its in registers. When the called function returns, it places the return value in the first in register, which becomes the first out register when the called function returns.

The System V ABI, which most modern Unix-like systems follow, passes the first six arguments in "in" registers %i0 through %i5, reserving %i6 for the frame pointer and %i7 for the return address.

[edit] See also

[edit] External links

Wikibooks
Wikibooks' Embedded Systems has more about this subject:
Wikibooks
Wikibooks' Reverse Engineering has more about this subject: