Microchip Technology SW006022-1N Data Sheet

Page of 338
MPLAB
®
 XC16 C Compiler User’s Guide
DS52071B-page 274
 2012 Microchip Technology Inc.
E.3
VARIABLES IN SPECIFIED REGISTERS
The compiler allows you to put a few global variables into specified hardware registers.
You can also specify the register in which an ordinary register variable should be 
allocated. 
• Global register variables reserve registers throughout the program. This may be 
useful in programs such as programming language interpreters which have a 
couple of global variables that are accessed very often.
• Local register variables in specific registers do not reserve the registers. The 
compiler’s data flow analysis is capable of determining where the specified 
registers contain live values, and where they are available for other uses. Stores 
into local register variables may be deleted when they appear to be unused. 
References to local register variables may be deleted, moved or simplified. 
These local variables are sometimes convenient for use with the extended inline 
assembly (see Chapter 13. “Mixing C and Assembly Code”), if you want to write one 
output of the assembler instruction directly into a particular register. (This will work pro-
vided the register you specify fits the constraints specified for that operand in the inline 
assembly statement). 
E.3.1
Defining Global Register Variables
You can define a global register variable like this: 
register int *foo asm ("w8");
Here w8 is the name of the register which should be used. Choose a register that is 
normally saved and restored by function calls (W8-W13), so that library routines will not 
clobber it.
Defining a global register variable in a certain register reserves that register entirely for 
this use, at least within the current compilation. The register will not be allocated for any 
other purpose in the functions in the current compilation. The register will not be saved 
and restored by these functions. Stores into this register are never deleted even if they 
would appear to be dead, but references may be deleted, moved or simplified. 
It is not safe to access the global register variables from signal handlers, or from more 
than one thread of control, because the system library routines may temporarily use the 
register for other things (unless you recompile them especially for the task at hand). 
It is not safe for one function that uses a global register variable to call another such 
function foo by way of a third function lose that was compiled without knowledge of 
this variable (i.e., in a source file in which the variable wasn’t declared). This is because 
lose
 might save the register and put some other value there. For example, you can’t 
expect a global register variable to be available in the comparison-function that you 
pass to qsort, since qsort might have put something else in that register. This 
problem can be avoided by recompiling qsort with the same global register variable 
definition.
If you want to recompile qsort or other source files that do not actually use your global 
register variable, so that they will not use that register for any other purpose, then it 
suffices to specify the compiler command-line option -ffixed-reg. You need not 
actually add a global register declaration to their source code. 
Note:
Using too many registers, in particular register W0, may impair the ability of 
the 16-bit compiler to compile. It is not recommended that registers be 
placed into fixed registers.