Specifying Registers for Function Parameters

Previous Variables in Specified Registers

In TIGCC, it is possible to specify explictly where the parameters to a function a stored, in a syntax similar to global and local register variables.

TIGCC can pass function parameters either on the stack or through registers. You can use the regparm attribute or the '-mregparm' compiler switch to let the compiler automatically choose registers for the parameters, but you can also tell TIGCC to put a parameter into a specific register. This can be very handy when interfacing with assembly code. For example, the following assembly function expects its 2 parameters in the d1 and d2 registers:

asm(".globl add
add:
     move.l %d2,%d0
     add.l %d1,%d0
     rts");
Therefore, its prototype would be:
unsigned long add (unsigned long a asm("d1"), unsigned long b asm("d2"));
Explicit register specifications for function parameters are also supported in function headers (not only in prototypes). Therefore, the assembly function above could be replaced by the following C equivalent:
unsigned long add (unsigned long a asm("d1"), unsigned long b asm("d2"))
{
  return a + b;
}
to pass a register parameter. The registers do not necessarily need to be call-clobbered. You can also use a2-a5/d3-d7 to pass a register parameter. But note that the called function still has to save and restore those registers, even if they are used as arguments! In C code, TIGCC takes care of that automatically for you, but in assembly code, it is something you need to remember.


See also: regparm