You can define a global register variable in GNU C like this:
register int *foo asm ("a5");
a5 is the name of the register that should be used. Choose a
register that is normally saved and restored by function calls on your
machine, so that library routines will not clobber it.
Naturally the register name is cpu-dependent, so you need to
conditionalize your program according to cpu type. The register
a5 is a good choice on a 68000 for a variable of pointer
type. On machines with register windows, be sure to choose a “global”
register that is not affected magically by the function call mechanism.
In addition, different operating systems on the same CPU may differ in how they
name the registers; then you need additional conditionals. For
example, some 68000 operating systems call this register
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.
Defining a global register variable in a certain register reserves that register entirely for this use, at least within the current compilation. The register is not allocated for any other purpose in the functions in the current compilation, and is not saved and restored by these functions. Stores into this register are never deleted even if they appear to be dead, but references may be deleted or moved or simplified.
It is not safe to access the global register variables from signal handlers, or from more than one thread of control, because the system library routines may temporarily use the register for other things (unless you recompile them specially for the task at hand).
It is not safe for one function that uses a global register variable to
call another such function
foo by way of a third function
lose that is compiled without knowledge of this variable (i.e. in a
different source file in which the variable isn't declared). This is
lose 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
might have put something else in that register. (If you are prepared to
qsort with the same global register variable, you can
solve this problem.)
If you want to recompile
qsort or other source files that do not
actually use your global register variable, so that they do not use that
register for any other purpose, then it suffices to specify the compiler
option -ffixed-reg. You need not actually add a global
register declaration to their source code.
A function that 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 that is the entry point into the part of the program that uses the global register variable must explicitly save and restore the value that belongs to its caller.
On most machines,
longjmp restores to each global register
variable the value it had at the time of the
setjmp. On some
longjmp does not change the value of global
register variables. To be portable, the function that called
should make other arrangements to save the values of the global register
variables, and to restore them in a
longjmp. This way, the same
thing happens regardless of what
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.
Global register variables may not have initial values, because an executable file has no means to supply initial contents for a register.
On the SPARC, there are reports that g3 ... g7 are suitable
registers, but certain library functions, such as
getwd, as well
as the subroutines for division and remainder, modify g3 and g4. g1 and
g2 are local temporaries.
On the 68000, a2 ... a5 should be suitable, as should d2 ... d7. Of course, it does not do to use more than a few of those.