6.4 Function Structure and Calling Conventions
The C/C++ compiler imposes a strict set of rules on function calls. Except for special run-time support functions, any function that calls or is called by a C/C++ function must follow these rules. Failure to adhere to these rules can disrupt the C/C++ environment and cause a program to fail.
The following sections use this terminology to describe the function-calling conventions of the C/C++ compiler:
- Argument block. The part of the local frame used to pass arguments to other functions. Arguments are passed to a function by moving them into the argument block rather than pushing them on the stack. The local frame and argument block are allocated at the same time.
- Register save area. The part of the local frame that is used to save the registers when the program calls the function and restore them when the program exits the function.
- Save-on-call registers. Registers R0-R1 and R14-R29. The called function does not preserve the values in these registers; therefore, the calling function must save them if their values need to be preserved.
- Save-on-entry registers. Registers R3.w2 through R13. It is the called function's responsibility to preserve the values in these registers. If the called function modifies these registers, it saves them when it gains control and preserves them when it returns control to the calling function.
Figure 6-1 illustrates a typical function call. In this example, arguments are passed to the function, and the function uses local variables and calls another function. Registers F14-R29 are used to pass arguments 1 through 16. If there are additional arguments, the stack is used. This example also shows allocation of a local frame and argument block for the called function. Functions that have no local variables and do not require an argument block do not allocate a local frame.