9.7 __global_reg

The __global_reg storage class specifier allocates the declared variable to a global variable register.

Syntax

__global_reg(n) type varName
Where:
n
Is an integer between one and eight.
type
Is one of the following types:
  • Any integer type, except long long.
  • Any char type
    .
  • Any pointer type.
varName
Is the name of a variable.

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 register allocated to r5:
__global_reg(5) int x; x is allocated to r5
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++
Related reference
7.70 --global_reg=reg_name[,reg_name,...]
Non-ConfidentialPDF file icon PDF versionARM DUI0375E
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.