gcc: Global Register Variables
1
1 6.45.5.1 Defining Global Register Variables
1 ...........................................
1
1 You can define a global register variable and associate it with a
1 specified register like this:
1
1 register int *foo asm ("r12");
1
1 Here 'r12' is the name of the register that should be used. Note that
1 this is the same syntax used for defining local register variables, but
1 for a global variable the declaration appears outside a function. The
1 'register' keyword is required, and cannot be combined with 'static'.
1 The register name must be a valid register name for the target platform.
1
1 Registers are a scarce resource on most systems and allowing the
1 compiler to manage their usage usually results in the best code.
1 However, under special circumstances it can make sense to reserve some
1 globally. For example this may be useful in programs such as
1 programming language interpreters that have a couple of global variables
1 that are accessed very often.
1
1 After defining a global register variable, for the current compilation
1 unit:
1
1 * The register is reserved entirely for this use, and will not be
1 allocated for any other purpose.
1 * The register is not saved and restored by any functions.
1 * Stores into this register are never deleted even if they appear to
1 be dead, but references may be deleted, moved or simplified.
1
1 Note that these points _only_ apply to code that is compiled with the
1 definition. The behavior of code that is merely linked in (for example
1 code from libraries) is not affected.
1
1 If you want to recompile source files that do not actually use your
1 global register variable so they do not use the specified register for
1 any other purpose, you need not actually add the global register
1 declaration to their source code. It suffices to specify the compiler
1 option '-ffixed-REG' (⇒Code Gen Options) to reserve the register.
1
1 Declaring the variable
1 ......................
1
1 Global register variables can not have initial values, because an
1 executable file has no means to supply initial contents for a register.
1
1 When selecting a register, choose one that is normally saved and
1 restored by function calls on your machine. This ensures that code
1 which is unaware of this reservation (such as library routines) will
1 restore it before returning.
1
1 On machines with register windows, be sure to choose a global register
1 that is not affected magically by the function call mechanism.
1
1 Using the variable
1 ..................
1
1 When calling routines that are not aware of the reservation, be cautious
1 if those routines call back into code which uses them. As an example,
1 if you call the system library version of 'qsort', it may clobber your
1 registers during execution, but (if you have selected appropriate
1 registers) it will restore them before returning. However it will _not_
1 restore them before calling 'qsort''s comparison function. As a result,
1 global values will not reliably be available to the comparison function
1 unless the 'qsort' function itself is rebuilt.
1
1 Similarly, it is not safe to access the global register variables from
1 signal handlers or from more than one thread of control. Unless you
1 recompile them specially for the task at hand, the system library
1 routines may temporarily use the register for other things.
1
1 On most machines, 'longjmp' restores to each global register variable
1 the value it had at the time of the 'setjmp'. On some machines,
1 however, 'longjmp' does not change the value of global register
1 variables. To be portable, the function that called 'setjmp' should
1 make other arrangements to save the values of the global register
1 variables, and to restore them in a 'longjmp'. This way, the same thing
1 happens regardless of what 'longjmp' does.
1
1 Eventually there may be a way of asking the compiler to choose a
1 register automatically, but first we need to figure out how it should
1 choose and how to enable you to guide the choice. No solution is
1 evident.
1