A called function (child function) must perform the following tasks:
- The called function (child) already has 16 bytes of reserved stack frame space available, but if the space required to store its local variables, temporary storage areas, and arguments to functions is larger than 16 bytes, then the called function allocates additional space on the stack.
- If the called function calls any other functions, it must make sure that 16 bytes of unused space is also allocated on the stack and reserved for its called functions. The called function must also save its return address on the stack. Otherwise, it is left in the return register (RP) and is overwritten by the next function call.
- If the called function modifies any save-on-entry registers (A8-A15, VB14-VB15), it must save them, either in other registers or on the stack. The called function can modify any other registers without saving them because they will have been saved by a caller function prior to the call if they were used.
- If the called function expects a structure argument, effort is made to pass the structure by value if its value can fit into a 64-bit or 512-bit argument register. Otherwise, the called function 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.
- The return value is handled as follows:
- If the called function returns any integer, pointer, float, double, long double, long type, or vector data type less than or equal to 64 bits in size, the return value is placed in register A4.
- If the called function returns a vector data type greater than 64 bits in size, the return value is placed in register VB0.
- If the called function returns a vector predicate type, the return value is placed in register P0.
- If the called function returns a structure, it is returned by value if it is small enough to fit into register A4 or register VB0. Otherwise, the caller allocates space for the structure and passes the address of the return space to the called function in register A1. To return a structure, the called function copies the structure to the memory block pointer 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 save-on-entry register (A8-A15, B14/VB14, B15/VB15) that was saved in Step 3 is restored.
- The value of the return register (RP) is also restored if it was saved.
- Any space that was allocated during this call sequence is reclaimed.
- The function returns by invoking the RET instruction, which returns to the location contained in the return register (RP).