[Main]
Title=Specifying Registers for Function Parameters
See Also=SEC85_regparm: regparm

[Description]
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.

[Top]
TIGCC can pass function parameters either on the stack or through registers.
You can use the <CODE><A HREF="$$LINK(SEC85_regparm)">regparm</A></CODE> attribute or the <B>'-mregparm'</B>
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 <CODE>d1</CODE> and <CODE>d2</CODE> registers:
<PRE>asm(".globl add
add:
     move.l %d2,%d0
     add.l %d1,%d0
     rts");
</PRE>
Therefore, its prototype would be:
<PRE>unsigned long add (unsigned long a asm("d1"), unsigned long b asm("d2"));
</PRE>
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:
<PRE>unsigned long add (unsigned long a asm("d1"), unsigned long b asm("d2"))
{
  return a + b;
}
</PRE>
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.
