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. When some code calls a function, design choices have been taken for where and how parameters are passed to that function, and where and how results are returned from that function, with these transfers typically done via certain registers or within a stack frame on the call stack. There are design choices for how the tasks of preparing for a function call and restoring the environment after the function has completed are divided between the caller and the callee. Some calling conventions specify the way... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
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. When some code calls a function, design choices have been taken for where and how parameters are passed to that function, and where and how results are returned from that function, with these transfers typically done via certain registers or within a stack frame on the call stack. There are design choices for how the tasks of preparing for a function call and restoring the environment after the function has completed are divided between the caller and the callee. Some calling conventions specify the way every function should get called. The correct calling convention should be used for every function call, to allow the correct and reliable execution of the whole program using these functions. ===Related concepts=== The names or meanings of the parameters and return values are defined in the application programming interface (API, as opposed to ABI), which is a separate though related concept to ABI and calling convention. The names of members within passed structures and objects would also be considered part of the API, and not ABI. Sometimes APIs do include keywords to specify the calling convention for functions. 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. Calling conventions are unlikely to specify the layout of items within structures and objects, such as byte ordering or structure packing. 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 remote procedure calls, there is an analogous concept called Marshalling. Calling conventions may be related to a particular programming language's evaluation strategy, but most often are not considered part of it (or vice versa), as the evaluation strategy is usually defined on a higher abstraction level and seen as a part of the language rather than as a low-level implementation detail of a particular language's compiler. ===Calling conventions within one platform=== Sometimes multiple calling conventions appear on a single platform; a given platform and language implementation may offer a choice of calling conventions. Reasons for this include performance, adaptation of conventions of other popular languages, and restrictions or conventions imposed by various computing platforms. Many architectures only have one widely used calling convention, often suggested by the architect. For RISCs including SPARC, MIPS, and RISC-V, registers names based on this calling convention are often used. For example, MIPS registers through have "ABI names" through , 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 calling convention of a given program's language may differ from the calling convention of the underlying platform, OS, or of some library being linked to. For example, on 32-bit Windows, operating system calls have the stdcall calling convention, whereas many C programs that run there use the cdecl calling convention. To accommodate these differences in calling convention, compilers often permit keywords that specify the calling convention for a given function. 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. Some languages allow the calling convention for a function to be explicitly specified with that function; other languages will have some calling convention but it will be hidden from the users of that language, and therefore will not typically be a consideration for the programmer. push EAX ; pass some register result push dword [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). Some conventions leave the parameter space allocated, using plain instead of . In that case, the caller could in this example, or otherwise deal with the change to ESP. calc: push EBP ; save old frame pointer mov EBP,ESP ; get new frame pointer sub ESP,localsize ; reserve stack space 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. LA 1,ARGS Load argument list address L 15,=V(SUB) Load subroutine address 1 BALR 14,15 Branch to called routine 2 ... ARGS DC A(FIRST) Address of 1st argument DC A(SECOND) ... DC A(THIRD)+X'80000000' Last argument 3 Called program: SUB EQU * This is the entry point of the subprogram 4 Standard entry sequence: USING *,15 5 STM 14,12,12(13) Save registers 6 ST 13,SAVE+4 Save caller's savearea addr LA 12,SAVE Chain saveareas ST 12,8(13) LR 13,12 ... Standard return sequence: L 13,SAVE+4 7 LM 14,12,12(13) L 15,RETVAL 8 BR 14 Return to caller SAVE DS 18F Savearea 9 Notes: Use or for internal subroutine; use for external subroutines. The instruction stores the address of the next instruction (return address) in the register specified by the first argument—register 14—and branches to the second argument address in register 15. BASR is preferred on models that support it. BASSM may be used if switching between 24 and 31 bit modes is required. The caller passes the address of a list of argument addresses in register 1. The last address has the high-order bit set to indicate the end of the list. This limits programs using this convention to 31-bit addressing. For external subroutines, either include after or replace it with . The address of the called routine is in register 15. Normally this is loaded into another register with and informing the assembler that register 15 is no longer to be used as a base register. The instruction saves registers 14, 15, and 0 through 12 in a 72-byte area provided by the caller called a save area pointed to by register 13. The called routine provides its own save area for use by subroutines it calls; the address of this area is normally kept in register 13 throughout the routine. The instructions following update forward and backward chains linking this save area to the caller's save area. The return sequence restores the caller's registers. Register 15 is usually used to pass a return code. The code shown is incorrect but illustrates the principle. Declaring a savearea statically in the called routine makes it non-reentrant and non-recursive; a reentrant program uses a dynamic savearea , acquired either from the operating system and freed upon returning, or in storage passed by the calling program. In the System/390 ABI and the z/Architecture ABI, used in Linux: Registers 0 and 1 are volatile Registers 2 and 3 are used for parameter passing and return values Registers 4 and 5 are also used for parameter passing Register 6 is used for parameter passing, and must be saved and restored by the callee Registers 7 through 13 are for use by the callee, and must be saved and restored by them Register 14 is used for the return address Register 15 is used as the stack pointer Floating-point registers 0 and 2 are used for parameter passing and return values Floating-point registers 4 and 6 are for use by the callee, and must be saved and restored by them In z/Architecture, floating-point registers 1, 3, 5, and 7 through 15 are for use by the callee Access register 0 is reserved for system use Access registers 1 through 15 are for use by the callee Additional arguments are passed on the stack. ==Calling conventions outside machine architectures== ===PL/I=== The default calling convention for programs written in the PL/I language passes all arguments by reference, although other conventions may optionally be specified. The arguments are handled differently for different compilers and platforms, but typically the argument addresses are passed via an argument list in memory. A final, hidden, address may be passed pointing to an area to contain the return value. Because of the wide variety of data types supported by PL/I a data descriptor may also be passed to define, for example, the lengths of character or bit strings, the dimension and bounds of arrays (dope vectors), or the layout and contents of a data structure. Dummy arguments are created for arguments which are constants or which do not agree with the type of argument the called procedure expects.
Cite this
"Calling convention". 1788758470. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1373678707 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy