4.1.6. __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, it is recommended that you do not use more than:

  • three global register variables in ARM or Thumb-2

  • one global register variable in Thumb-1

  • 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.

Example

Example 4.1 declares a global variable register allocated to r5.

Example 4.1. Declaring a global integer register variable

__global_reg(2) int x      ; v2 is the synonym for r5

Example 4.2 produces an error because global register must be specified in all declarations of the same variable.

Example 4.2.  Global register - declaration error

int x;
__global_reg(1) int x; // error

In C, __global_reg variables cannot be initialized at definition. Example 4.3 produces an error in C, but not in C++.

Example 4.3. Global register - initialization error

__global_reg(1) int x=1; // error in C, OK in C++

Copyright © 2007, 2010 ARM Limited. All rights reserved.ARM DUI 0348A
Non-Confidential