A calling convention specifies where arguments to a function are placed, and where the function places its return value. You shouldn't worry about it, unless you are an assembly programmer trying to call assembly functions from C code or vice-versa.
regparm
)Unless otherwise specified, GTC uses the most efficient calling convention, regparm
.
Basically, it consists of placing as many arguments as possible into a small set of registers for maximum efficiency, and when they are all used, place the remaining arguments on the stack, which is less efficient.
More precisely, here is the specific algorithm for placing arguments:
a
, from left to right
d0
,d1
,d2
,a0
,a1
} are already used by other arguments
a
on the stack
d0
,d1
,d2
} unused and one of {a0
,a1
} unused
a
's type is a pointer
a
in the next free address register
a
in the next free data register
a0
,a1
} is unused
a
in the next free address register
a
in the next free data register
Here is how the placement of an argument is done:
a
in the next free data register:
a
in d0
if it is unused
a
in d1
if it is unused
a
in d2
a
in the next free address register:
a
in a0
if it is unused
a
in a1
a
on the stack:
stkparm
Here is the algorithm for placing the return value:
a0
d0
struct
or a union
d0
(not the address!)
Note that regparm
requires the function to have a fixed number of arguments: otherwise, for variable-argument functions, no arguments are placed in registers, and the stkparm
convention is used instead.
Note that TIGCC uses the same algorithm, as long as you specify the function attribute __attribute__((regparm(2,1)))
.
You can also use a different set of variables than d0
-d2
/a0
-a1
thanks to the regparm
function attribute, but it is not recommended.
stkparm
)The stkparm
convention is less efficient, but is useful because it is the one used internally by AMS. It is also much simpler to describe.
The arguments are pushed one after the other onto the stack, starting from the rightmost argument. char
s and unsigned char
s should be widened to an int
before being pushed.