Commit cbb5c732 by David Wohlferd Committed by Jeff Law

Proposed doc update for Explicit Reg Vars 2/3

	* doc/extend.exp (Local Register Variables): Rewrite.

From-SVN: r229124
parent 5d3498b4
2015-10-21 David Wohlferd <dw@LimeGreenSocks.com>
* doc/extend.exp (Local Register Variables): Rewrite.
2015-10-21 Richard Sandiford <richard.sandiford@arm.com> 2015-10-21 Richard Sandiford <richard.sandiford@arm.com>
* match.pd: Add rules to simplify ccos, ccosh, hypot, copysign * match.pd: Add rules to simplify ccos, ccosh, hypot, copysign
...@@ -8492,68 +8492,79 @@ the two, as explained in the sections below. ...@@ -8492,68 +8492,79 @@ the two, as explained in the sections below.
@cindex registers, global variables in @cindex registers, global variables in
@cindex registers, global allocation @cindex registers, global allocation
You can define a global register variable in GNU C like this: You can define a global register variable and associate it with a specified
register like this:
@smallexample @smallexample
register int *foo asm ("a5"); register int *foo asm ("r12");
@end smallexample @end smallexample
@noindent @noindent
Here @code{a5} is the name of the register that should be used. Choose a Here @code{r12} is the name of the register that should be used. Note that
register that is normally saved and restored by function calls on your this is the same syntax used for defining local register variables, but for
machine, so that library routines will not clobber it. a global variable the declaration appears outside a function. The
@code{register} keyword is required, and cannot be combined with
Naturally the register name is CPU-dependent, so you need to @code{static}. The register name must be a valid register name for the
conditionalize your program according to CPU type. The register target platform.
@code{a5} is a good choice on a 68000 for a variable of pointer
type. On machines with register windows, be sure to choose a ``global'' Registers are a scarce resource on most systems and allowing the
register that is not affected magically by the function call mechanism. compiler to manage their usage usually results in the best code. However,
under special circumstances it can make sense to reserve some globally.
For example this may be useful in programs such as programming language
interpreters that have a couple of global variables that are accessed
very often.
After defining a global register variable, for the current compilation
unit:
In addition, different operating systems on the same CPU may differ in how they @itemize @bullet
name the registers; then you need additional conditionals. For @item The register is reserved entirely for this use, and will not be
example, some 68000 operating systems call this register @code{%a5}. allocated for any other purpose.
@item The register is not saved and restored by any functions.
@item Stores into this register are never deleted even if they appear to be
dead, but references may be deleted, moved or simplified.
@end itemize
Note that these points @emph{only} apply to code that is compiled with the
definition. The behavior of code that is merely linked in (for example
code from libraries) is not affected.
Eventually there may be a way of asking the compiler to choose a register If you want to recompile source files that do not actually use your global
automatically, but first we need to figure out how it should choose and register variable so they do not use the specified register for any other
how to enable you to guide the choice. No solution is evident. purpose, you need not actually add the global register declaration to
their source code. It suffices to specify the compiler option
@option{-ffixed-@var{reg}} (@pxref{Code Gen Options}) to reserve the
register.
Defining a global register variable in a certain register reserves that @subsubheading Declaring the variable
register entirely for this use, at least within the current compilation.
The register is not allocated for any other purpose in the functions Global register variables can not have initial values, because an
in the current compilation, and is not saved and restored by executable file has no means to supply initial contents for a register.
these functions. Stores into this register are never deleted even if they
appear to be dead, but references may be deleted or moved or
simplified.
It is not safe to access the global register variables from signal When selecting a register, choose one that is normally saved and
handlers, or from more than one thread of control, because the system restored by function calls on your machine. This ensures that code
library routines may temporarily use the register for other things (unless which is unaware of this reservation (such as library routines) will
you recompile them specially for the task at hand). restore it before returning.
On machines with register windows, be sure to choose a global
register that is not affected magically by the function call mechanism.
@subsubheading Using the variable
@cindex @code{qsort}, and global register variables @cindex @code{qsort}, and global register variables
It is not safe for one function that uses a global register variable to When calling routines that are not aware of the reservation, be
call another such function @code{foo} by way of a third function cautious if those routines call back into code which uses them. As an
@code{lose} that is compiled without knowledge of this variable (i.e.@: in a example, if you call the system library version of @code{qsort}, it may
different source file in which the variable isn't declared). This is clobber your registers during execution, but (if you have selected
because @code{lose} might save the register and put some other value there. appropriate registers) it will restore them before returning. However
For example, you can't expect a global register variable to be available in it will @emph{not} restore them before calling @code{qsort}'s comparison
the comparison-function that you pass to @code{qsort}, since @code{qsort} function. As a result, global values will not reliably be available to
might have put something else in that register. (If you are prepared to the comparison function unless the @code{qsort} function itself is rebuilt.
recompile @code{qsort} with the same global register variable, you can
solve this problem.) Similarly, it is not safe to access the global register variables from signal
handlers or from more than one thread of control. Unless you recompile
If you want to recompile @code{qsort} or other source files that do not them specially for the task at hand, the system library routines may
actually use your global register variable, so that they do not use that temporarily use the register for other things.
register for any other purpose, then it suffices to specify the compiler
option @option{-ffixed-@var{reg}}. You need not actually add a global
register declaration to their source code.
A function that can alter the value of a global register variable cannot
safely be called from a function compiled without this variable, because it
could clobber the value the caller expects to find there on return.
Therefore, the function that is the entry point into the part of the
program that uses the global register variable must explicitly save and
restore the value that belongs to its caller.
@cindex register variable after @code{longjmp} @cindex register variable after @code{longjmp}
@cindex global register after @code{longjmp} @cindex global register after @code{longjmp}
...@@ -8568,22 +8579,6 @@ should make other arrangements to save the values of the global register ...@@ -8568,22 +8579,6 @@ should make other arrangements to save the values of the global register
variables, and to restore them in a @code{longjmp}. This way, the same variables, and to restore them in a @code{longjmp}. This way, the same
thing happens regardless of what @code{longjmp} does. thing happens regardless of what @code{longjmp} does.
All global register variable declarations must precede all function
definitions. If such a declaration could appear after function
definitions, the declaration would be too late to prevent the register from
being used for other purposes in the preceding functions.
Global register variables may not have initial values, because an
executable file has no means to supply initial contents for a register.
On the SPARC, there are reports that g3 @dots{} g7 are suitable
registers, but certain library functions, such as @code{getwd}, as well
as the subroutines for division and remainder, modify g3 and g4. g1 and
g2 are local temporaries.
On the 68000, a2 @dots{} a5 should be suitable, as should d2 @dots{} d7.
Of course, it does not do to use more than a few of those.
@node Local Register Variables @node Local Register Variables
@subsubsection Specifying Registers for Local Variables @subsubsection Specifying Registers for Local Variables
@anchor{Local Reg Vars} @anchor{Local Reg Vars}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment