__global_reg storage class specifier
allocates the declared variable to a global variable register.
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, it is recommended 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.
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_regstorage class specifier are not treated as volatile.
Example 5 declares
a global variable register allocated to
Example 6 produces an error because global registers must be specified in all declarations of the same variable.
__global_reg variables cannot be
initialized at definition. Example 7 produces an error in C, but not in C++.