| Defining Global Register Variables

| Variables in Specified Registers | Next

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.

You can define a global register variable in GNU C like this:

    register int *foo asm ("a3");

Here `a3` 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.
Naturally the register name is cpu-dependent, so you would need to conditionalize your program according to cpu type. The register `a3` would be 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, 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 `%a3`.
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 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.
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).
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 was compiled without knowledge of this variable (i.e. in a different source file in which the variable wasn't declared). This is because `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 qsort, 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.)
If you want to recompile `qsort` 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 '-ffixed-reg'. You need not actually add a global register declaration to their source code.
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.
Be careful if you use longjmp with global register variables, because it will restore to each global register variable the value it had at the time of the setjmp.
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 Motorola 68000, `a2` ... `a5` should be suitable for global register variables, as should `d3` ... `d7`. Of course, it will not do to use more than a few of those. Note that `a5` is used by `OPTIMIZE_ROM_CALLS`.