[Main]
Title=Defining Global Register Variables

[Description]
Global register variables reserve registers throughout the program.
This may be useful in programs such as programming language
interpreters which have a couple of global variables that are accessed
very often.

[Top]
You can define a global register variable in GNU C like this:
<PRE>register int *foo asm (&quot;a3&quot;);
</PRE>
Here <CODE>a3</CODE> is the name of the register which should be used.  Choose a
register which is normally saved and restored by function calls on your
machine, so that library routines will not clobber it.
<BR><BR>
Naturally the register name is cpu-dependent, so you would need to
conditionalize your program according to cpu type.  The register
<CODE>a3</CODE> would be a good choice on a 68000 for a variable of pointer
type.  On machines with register windows, be sure to choose a &quot;global&quot;
register that is not affected magically by the function call mechanism.
<BR><BR>
In addition, operating systems on one type of cpu may differ in how they
name the registers; then you would need additional conditionals.  For
example, some 68000 operating systems call this register <CODE>%a3</CODE>.
<BR><BR>
Eventually there may be a way of asking the compiler to choose a register
automatically, but first we need to figure out how it should choose and
how to enable you to guide the choice.  No solution is evident.
<BR><BR>
Defining a global register variable in a certain register reserves that
register entirely for this use, at least within the current compilation.
The register will not be allocated for any other purpose in the functions
in the current compilation.  The register will not be saved and restored by
these functions.  Stores into this register are never deleted even if they
would appear to be dead, but references may be deleted or moved or
simplified.
<BR><BR>
It is not safe to access the global register variables from interrupt
handlers, because the system
library routines may temporarily use the register for other things (unless
you recompile them specially for the task at hand).
<BR><BR>
It is not safe for one function that uses a global register variable to
call another such function <CODE>foo</CODE> by way of a third function
<CODE>lose</CODE> that was compiled without knowledge of this variable (i.e. in a
different source file in which the variable wasn't declared).  This is
because <CODE>lose</CODE> might save the register and put some other value there.
For example, you can't expect a global register variable to be available in
the comparison-function that you pass to <A HREF="$$LINK(stdlib.h/qsort)">qsort</A>, since it
might have put something else in that register.  (If you are prepared to
recompile it with the same global register variable, you can
solve this problem.)
<BR><BR>
If you want to recompile <CODE>qsort</CODE> or other source files which do not
actually use your global register variable, so that they will not use that
register for any other purpose, then it suffices to specify the compiler
option <B>'-ffixed-<I>reg</I>'</B>.  You need not actually add a global
register declaration to their source code.
<BR><BR>
A function which can alter the value of a global register variable cannot
safely be called from a function compiled without this variable, because it
could clobber the value the caller expects to find there on return.
Therefore, the function which is the entry point into the part of the
program that uses the global register variable must explicitly save and
restore the value which belongs to its caller.
<BR><BR>
Be careful if you use <A HREF="$$LINK(setjmp.h/longjmp)">longjmp</A> with global register
variables, because it will restore to each global register
variable the value it had at the time of the
<A HREF="$$LINK(setjmp.h/setjmp)">setjmp</A>.
<BR><BR>
All global register variable declarations must precede all function
definitions.  If such a declaration could appear after function
definitions, the declaration would be too late to prevent the register from
being used for other purposes in the preceding functions.
<BR><BR>
Global register variables may not have initial values, because an
executable file has no means to supply initial contents for a register.
<BR><BR>
On the Motorola 68000, <CODE>a2</CODE>&nbsp;...&nbsp;<CODE>a5</CODE> should be suitable for global register variables,
as should <CODE>d3</CODE>&nbsp;...&nbsp;<CODE>d7</CODE>.
Of course, it will not do to use more than a few of those.
Note that <CODE>a5</CODE> is used by
<CODE><A HREF="$$INFOLINK(httigcc/advanced_optrom)">OPTIMIZE_ROM_CALLS</A></CODE>.
