Calling convention
Encyclopedia
In computer science
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...

, a calling convention is a scheme for how subroutine
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

s receive parameters from their caller and how they return a result; calling conventions can differ in:
  • where parameters and return values are placed (in registers
    Processor register
    In computer architecture, a processor register is a small amount of storage available as part of a CPU or other digital processor. Such registers are addressed by mechanisms other than main memory and can be accessed more quickly...

    ; on the call stack
    Call stack
    In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack"...

    ; a mix of both)
  • the order in which parameters are passed (or parts of a single parameter)
  • how the task of setting up for and cleaning up after a function call is divided between the caller and the callee
  • which registers that may be directly used by the callee may sometimes also be included (otherwise regarded as an ABI
    Application binary interface
    In computer software, an application binary interface describes the low-level interface between an application program and the operating system or another application.- Description :...

    -detail)

Different programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s use different calling conventions, and so can different platforms (CPU architecture + operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

). This can sometimes cause problems 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.

Architectures almost 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 specified to use only one calling convention, supplied by the architect.

x86

The x86 architecture
X86 architecture
The term x86 refers to a family of instruction set architectures based on the Intel 8086 CPU. The 8086 was launched in 1978 as a fully 16-bit extension of Intel's 8-bit based 8080 microprocessor and also introduced segmentation to overcome the 16-bit addressing barrier of such designs...

 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 short and simple leaf-routines very frequently invoked (i.e routines that do not call other routines and do not have to be reentrant).

Example call:
push eAX ; pass some register result
push byte[eBP+20] ; pass some memory variable (FASM
FASM
FASM in computing is an assembler. It supports programming in Intel-style assembly language on the IA-32 and x86-64 computer architectures. It claims high speed, size optimizations, operating system portability, and macro abilities. It is a low-level assembler and intentionally uses very few...

/TASM
Tasm
TASM can refer to:*Turbo Assembler, Borland's x86 assembler* Turbo Assembler, Omikron's Commodore 64-based 6502 assembler*Table Assembler, a table driven cross-assembler for small microprocessors.*Tomahawk Anti-Ship Missile...

 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 AX
.
mov eSP,eBP ; free space for locals
pop eBP ; restore old frame pointer
ret paramsize ; free parameter space and return

PowerPC

The PowerPC
PowerPC
PowerPC is a RISC architecture created by the 1991 Apple–IBM–Motorola alliance, known as AIM...

 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
Variadic function
In computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages....

 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.

MIPS

The O32 ABI
Application binary interface
In computer software, an application binary interface describes the low-level interface between an application program and the operating system or another application.- Description :...

 defined by the MIPS
MIPS architecture
MIPS is a reduced instruction set computer instruction set architecture developed by MIPS Technologies . The early MIPS architectures were 32-bit, and later versions were 64-bit...

 passes the first four arguments to a function 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; a second return value may be stored in $v1. The 64 bit ABI allows for more arguments in registers for more efficient function calls when there are more than four parameters. There is also the N32 ABI which also allows for more arguments in registers.

SPARC

The SPARC
SPARC
SPARC is a RISC instruction set architecture developed by Sun Microsystems and introduced in mid-1987....

 architecture, unlike most RISC architectures, is built on register window
Register window
In computer engineering, the use of register windows is a technique to improve the performance of a particularly common operation, the procedure call...

s. 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 need to be pushed onto the stack
Call stack
In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack"...

. However, space is always allocated by the called function to handle a potential register window overflow, local variables, and returning a struct by value. To call a function, one places the arguments 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 accesses the arguments 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.

ARM

The standard ARM
ARM architecture
ARM is a 32-bit reduced instruction set computer instruction set architecture developed by ARM Holdings. It was named the Advanced RISC Machine, and before that, the Acorn RISC Machine. The ARM architecture is the most widely used 32-bit ISA in numbers produced...

 calling convention allocates the 16 ARM registers as:
  • r15 is the program counter.
  • r14 is the link register. (The BL instruction, used in a subroutine call, stores the return address in this register).
  • r13 is the stack pointer. (The Push/Pop instructions in "Thumb" operating mode use this register only).
  • r12 is the Intra-Procedure-call scratch register.
  • r4 to r11: used to hold local variables.
  • r0 to r3: used to hold argument values passed to a subroutine, and also hold results returned from a subroutine.


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.

Subroutines must preserve the contents of r4 to r11 and the stack pointer. (Perhaps by saving them to the stack in the function prolog, then using them as scratch space, then restoring them from the stack in the function epilog). In particular, subroutines that call other subroutines *must* save the return value in the link register r14 to the stack before calling those other subroutines. However, such subroutines do not need to return that value to r14 -- they merely need to load that value into r15, the program counter, to return.

The ARM stack is full-descending.

This calling convention causes a "typical" ARM subroutine to
  • In the prolog, push r4 to r11 to the stack, and push the return address in r14, to the stack. (This can be done with a single STM instruction).
  • copy any passed arguments (in r0 to r3) to the local scratch registers (r4 to r11).
  • allocate other local variables to the remaining local scratch registers (r4 to r11).
  • do calculations and call other subroutines as necessary using BL, assuming r0 to r3, r12 and r14 will not be preserved.
  • put the result in r0
  • In the epilog, pull r4 to r11 from the stack, and pulls the return address to the program counter r15. (This can be done with a single LDM instruction).

Threaded code

Threaded code places all the responsibility for setting up for and cleaning up after a function call on the called code. The calling code does nothing but list the subroutines to be called. This puts all the function setup and cleanup code in one place -- the prolog and epilog of the function -- rather than in the many places that function is called. This makes threaded code the most compact calling convention.

Threaded code passes all arguments on the stack. All return values are returned on the stack. This makes naive implementations slower than calling conventions that keep more values in registers. However, threaded code implementations that cache several of the top stack values in registers -- in particular, the return address -- are usually faster than subroutine calling conventions that always push and pop the return address to the stack.

See also

  • Language binding
    Language binding
    In computing, a binding from a programming language to a library or OS service is an API providing that service in the language.Many software libraries are written in systems programming languages such as C or C++...

  • Foreign function interface
    Foreign function interface
    A foreign function interface is a mechanism by which a program written in one programming language can call routines or make use of services written in another. The term comes from the specification for Common Lisp, which explicitly refers to the language features for inter-language calls as...

  • Name mangling
    Name mangling
    In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages....

  • Application programming interface
    Application programming interface
    An application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...

     - API
  • Application Binary Interface
    Application binary interface
    In computer software, an application binary interface describes the low-level interface between an application program and the operating system or another application.- Description :...

     - ABI
  • Comparison of application virtual machines
    Comparison of Application Virtual Machines
    This article lists some software virtual machines that are typically used for allowing application bytecode to be portably run on many different computer architectures and operating systems. The application is usually run on the computer using an interpreter or just-in-time compilation...

  • SWIG
    SWIG
    SWIG is an open source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other languages like C#, Java, Modula-3, Objective Caml, Octave, and Scheme...

     - opensource interfaces bindings generator from many languages to many languages

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK