A called function (child function) must perform the following tasks:
- The called function (child) allocates enough space on the stack for any local variables, temporary storage areas, and arguments to functions that this function might call. This allocation occurs once at the beginning of the function and may include the allocation of the frame pointer (FP).
The frame pointer is used to read arguments from the stack and to handle register spilling instructions. If any arguments are placed on the stack or if the frame size exceeds 128K bytes, the frame pointer (A15) is allocated in the following manner:
- The old A15 is saved on the stack.
- The new frame pointer is set to the current SP (B15).
- The frame is allocated by decrementing SP by a constant.
- Neither A15 (FP) nor B15 (SP) is decremented anywhere else within this function.
If the above conditions are not met, the frame pointer (A15) is not allocated. In this situation, the frame is allocated by subtracting a constant from register B15 (SP). Register B15 (SP) is not decremented anywhere else within this function.
- If the called function calls any other functions, the return address must be saved on the stack. Otherwise, it is left in the return register (B3) and is overwritten by the next function call.
- If the called function modifies any registers numbered A10 to A15 or B10 to B15, it must save them, either in other registers or on the stack. The called function can modify any other registers without saving them.
- If the called function expects a structure
argument, it receives a pointer to the structure instead. If writes are made to
the structure from within the called function, space for a local copy of the
structure must be allocated on the stack and the local structure must be copied
from the passed pointer to the structure. If no writes are made to the
structure, it can be referenced in the called function indirectly through the
pointer argument.
You must be
careful to declare functions properly that accept structure arguments, both
at the point where they are called (so that the structure argument is passed
as an address) and at the point where they are declared (so the function
knows to copy the structure to a local copy).
- The called function executes the code for the function.
- If the called function returns any integer, pointer, or float type, the return value is placed in the A4 register. If the function returns a double, long double, long, or long long type, the value is placed in the A5:A4 register pair. For C6600 if the function returns a __x128_t, the value is placed in A7:A6:A5:A4.
If the function returns a structure, the caller allocates space for the structure and passes the address of the return space to the called function in A3. To return a structure, the called function copies the structure to the memory block pointed to by the extra argument.
In this way, the caller can be smart about telling the called function where to return the structure. For example, in the statement s = f(x), where s is a structure and f is a function that returns a structure, the caller can actually make the call as f(&s, x). The function f then copies the return structure directly into s, performing the assignment automatically.
If the caller does not use the return structure value, an address value of 0 can be passed as the first argument. This directs the called function not to copy the return structure.
You must be careful to declare functions properly that return structures, both at the point where they are called (so that the extra argument is passed) and at the point where they are declared (so the function knows to copy the result).
- Any register numbered A10 to A15 or B10 to B15 that was saved in Step 3 is restored.
- If A15 was used as a frame pointer (FP), the old value of A15 is restored from the stack. The space allocated for the function in Step 1 is reclaimed at the end of the function by adding a constant to register B15 (SP).
- The function returns by jumping to the value of the return register (B3) or the saved value of the return register.