Nepedia
Back to Discover
Advanced Reader
💻

Calling convention

technology Maturity 11-13 Next article →

In computer science, a calling convention is a low-level scheme for managing communication between subroutines. A subroutine, or function, is a specific block of code designed to perform a task. When one part of a program, known as the caller, triggers a function, they must follow a strict set of rules. These rules dictate how parameters are passed to the function and how results are returned. This process ensures that different parts of a program can interact reliably. Without a shared convention, the caller and the function might look for data in different places, causing the program to fail.

These conventions act as a contract between the caller and the called function. This contract is typically considered part of the application binary interface (ABI). The ABI defines how software interacts with the underlying hardware and operating system. It is different from an application programming interface (API). An API defines the names and meanings of parameters and return values. In contrast, the ABI handles the actual physical transfer of data. This transfer usually happens through specific registers or within a stack frame on the call stack. The call stack is a specialized region of memory used to manage function calls.

Calling conventions involve many specific design choices regarding data movement. One major choice is where parameters are placed. They can be stored in registers, which are fast storage locations inside the CPU, or on the stack in main memory. Some systems use a mix of both. Another choice involves the order of parameters, such as passing them from left-to-right or right-to-left. Conventions also determine how return values are delivered. A function might return a result in a register or by placing it on the stack. Even the handling of complex values is decided here, such as splitting a large value across multiple registers.

Another critical aspect is the division of labor between the caller and the callee. The callee is the function being called. The convention decides who is responsible for preparing the environment before the call and cleaning it up afterward. This includes restoring the stack frame so the caller can continue its work. This involves managing the frame pointer, which helps track the current state of the stack. Some conventions also specify which registers must be preserved. These are called non-volatile registers, meaning their values must remain unchanged after the function finishes. This ensures the caller does not lose its own data during the process.

Different hardware architectures use very different calling conventions. The 32-bit x86 architecture has a limited number of registers. Because of this, many x86 conventions pass arguments on the stack. However, the 64-bit x86-64 architecture has more general-purpose registers. Consequently, both the Microsoft convention used in Windows and the AMD64 System V ABI used in Unix-like systems pass some arguments in registers. Using registers instead of the stack can significantly improve performance. This is especially true for "leaf routines," which are simple functions that do not call any other functions.

ARM architectures provide even more detailed examples of register allocation. In 32-bit ARM (A32), registers r0 through r3 are used for arguments and results. The system also uses a special link register, r14, to store the return address. This allows the function to know where to go once it is finished. In 64-bit ARM (AArch64), the system uses 31 general-purpose registers. For instance, x0 through x7 are dedicated to arguments and results. ARM also mandates a "full-descending stack" and requires the stack pointer to be aligned to specific byte boundaries. These strict rules allow for highly efficient execution on mobile and embedded devices.

Other architectures like RISC-V and POWER also follow specific patterns. RISC-V aims to pass arguments in registers whenever possible to maximize speed. The POWER architecture features a very large number of registers. This allows most functions to pass all arguments through registers for single-level calls. Even when registers are full, POWER architectures allocate space on the stack for these arguments as a convenience. This helps in complex scenarios, such as recursive calls where a function calls itself multiple times. These diverse methods show how calling conventions are tailored to the specific strengths of different computer designs.

712 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.