Calling Convention¶
Issues with Function-Calling¶
- Storing the value of each register in each function call takes more space than what we have.
- Registers can be overwritten after calling a function. The function caller may not know which registers it uses!
Naive Solutions¶
- The caller pushes all registers that it (caller) uses on the stack before calling a function, and pops them after the function returns.
- The callee pushes all registers that it (callee) uses on the stack at the start of a function, and pops them before returning.
- If there is no overlap between registers used by the caller and the callee, using any of these will be pointless.
Register Spilling in Practice¶
It was found that having an equal amount of designated caller and callee saved registers gives the best performance. Caller and callee saving is a convention, which means it isn't enforced by the language. However, it is best practice to follow it.
Calling Convention¶
The calling convention tells us a lot of things about the ISA, including
- which registers are used to send function parameters
- which registers are used to return value from function
- which registers are caller-saved
- which registers are callee-saved
- which register is the stack pointer
Understand that a Calling Convention must be Universal to an ISA. This is because lots of different programmers will try using the same language. If you import a library written with a different calling conventions, it will break your code completely.