Syntax
__global_reg(n
) type
varName
Where:
n
Is an integer between one and eight.
type
Is one of the following types:
varName
Is the name of a global variable.
Usage
__global_reg
assigns a global variable to a specific
register. It prevents the compiler from generating code that otherwise uses the register, in
the same way as the --global_reg
command-line option. The register number
specified must be in the range 1-8. This corresponds to a register in the range r4 to r11.
Restrictions
If you use this storage class, you cannot use any additional storage class such as
extern
, static
, or
typedef
.
In C, global register variables cannot be qualified or initialized at declaration. In C++,
any initialization is treated as a dynamic initialization.
The number of available registers varies depending on the variant of the AAPCS being used,
there are between five and seven registers available for use as global variable
registers.
In practice, ARM recommends that you do not use more than:
Three global register variables in ARM or Thumb on a processor with Thumb-2
technology.
One global register variable in Thumb on a processor without Thumb-2
technology.
Half the number of available floating-point registers as global floating-point
register variables.
If you declare too many global variables, code size increases significantly. In some cases,
your program might not compile.
Caution
You must take care when using global register variables because:
There is no check at link time to ensure that direct calls between different
compilation units are sensible. If possible, define global register variables used in
a program in each compilation unit of the program. In general, it is best to place the
definition in a global header file. You must set up the value in the global register
early in your code, before the register is used.
A global register variable maps to a callee-saved register, so its value is saved
and restored across a call to a function in a compilation unit that does not use it as
a global register variable, such as a library function.
Calls back into a compilation unit that uses a global register variable are
dangerous. For example, if a function using a global register is called from a
compilation unit that does not declare the global register variable, the function
reads the wrong values from its supposed global register variables.
This storage class can only be used at file scope.
Volatile variables with the __global_reg
storage class specifier
are not treated as volatile.
Examples
This example declares a global variable x
and reserves
r5
for it:
__global_reg(2) int x; // r5 is reserved for x
This example produces an error because global registers must be specified
in all declarations of the same variable:
int x;
__global_reg(1) int x; // error
In C, __global_reg
variables cannot be
initialized at definition. This example produces an error in C, but not in C++:
__global_reg(1) int x=1; // error in C, OK in C++