blob: 93c7e71f32c3a3a22fdc9f5e36a54d882b466932 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2015 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below). A copy of the license is included in the section entitled
"GNU Free Documentation License".
(a) The FSF's Front-Cover Text is:
A GNU Manual
(b) The FSF's Back-Cover Text is:
You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development. -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Using the GNU Compiler Collection (GCC): Global Reg Vars</title>
<meta name="description" content="Using the GNU Compiler Collection (GCC): Global Reg Vars">
<meta name="keywords" content="Using the GNU Compiler Collection (GCC): Global Reg Vars">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Explicit-Reg-Vars.html#Explicit-Reg-Vars" rel="up" title="Explicit Reg Vars">
<link href="Local-Reg-Vars.html#Local-Reg-Vars" rel="next" title="Local Reg Vars">
<link href="Explicit-Reg-Vars.html#Explicit-Reg-Vars" rel="prev" title="Explicit Reg Vars">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Global-Reg-Vars"></a>
<div class="header">
<p>
Next: <a href="Local-Reg-Vars.html#Local-Reg-Vars" accesskey="n" rel="next">Local Reg Vars</a>, Up: <a href="Explicit-Reg-Vars.html#Explicit-Reg-Vars" accesskey="u" rel="up">Explicit Reg Vars</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Defining-Global-Register-Variables"></a>
<h4 class="subsection">6.44.1 Defining Global Register Variables</h4>
<a name="index-global-register-variables"></a>
<a name="index-registers_002c-global-variables-in"></a>
<p>You can define a global register variable in GNU C like this:
</p>
<div class="smallexample">
<pre class="smallexample">register int *foo asm (&quot;a5&quot;);
</pre></div>
<p>Here <code>a5</code> is the name of the register that should be used. Choose a
register that is normally saved and restored by function calls on your
machine, so that library routines will not clobber it.
</p>
<p>Naturally the register name is cpu-dependent, so you need to
conditionalize your program according to cpu type. The register
<code>a5</code> is a good choice on a 68000 for a variable of pointer
type. On machines with register windows, be sure to choose a &ldquo;global&rdquo;
register that is not affected magically by the function call mechanism.
</p>
<p>In addition, different operating systems on the same CPU may differ in how they
name the registers; then you need additional conditionals. For
example, some 68000 operating systems call this register <code>%a5</code>.
</p>
<p>Eventually there may be a way of asking the compiler to choose a register
automatically, but first we need to figure out how it should choose and
how to enable you to guide the choice. No solution is evident.
</p>
<p>Defining a global register variable in a certain register reserves that
register entirely for this use, at least within the current compilation.
The register is not allocated for any other purpose in the functions
in the current compilation, and is not saved and restored by
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.
</p>
<p>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 specially for the task at hand).
</p>
<a name="index-qsort_002c-and-global-register-variables"></a>
<p>It is not safe for one function that uses a global register variable to
call another such function <code>foo</code> by way of a third function
<code>lose</code> that is compiled without knowledge of this variable (i.e. in a
different source file in which the variable isn&rsquo;t declared). This is
because <code>lose</code> might save the register and put some other value there.
For example, you can&rsquo;t expect a global register variable to be available in
the comparison-function that you pass to <code>qsort</code>, since <code>qsort</code>
might have put something else in that register. (If you are prepared to
recompile <code>qsort</code> with the same global register variable, you can
solve this problem.)
</p>
<p>If you want to recompile <code>qsort</code> or other source files that do not
actually use your global register variable, so that they do not use that
register for any other purpose, then it suffices to specify the compiler
option <samp>-ffixed-<var>reg</var></samp>. You need not actually add a global
register declaration to their source code.
</p>
<p>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.
</p>
<a name="index-register-variable-after-longjmp"></a>
<a name="index-global-register-after-longjmp"></a>
<a name="index-value-after-longjmp"></a>
<a name="index-longjmp"></a>
<a name="index-setjmp"></a>
<p>On most machines, <code>longjmp</code> restores to each global register
variable the value it had at the time of the <code>setjmp</code>. On some
machines, however, <code>longjmp</code> does not change the value of global
register variables. To be portable, the function that called <code>setjmp</code>
should make other arrangements to save the values of the global register
variables, and to restore them in a <code>longjmp</code>. This way, the same
thing happens regardless of what <code>longjmp</code> does.
</p>
<p>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.
</p>
<p>Global register variables may not have initial values, because an
executable file has no means to supply initial contents for a register.
</p>
<p>On the SPARC, there are reports that g3 &hellip; g7 are suitable
registers, but certain library functions, such as <code>getwd</code>, as well
as the subroutines for division and remainder, modify g3 and g4. g1 and
g2 are local temporaries.
</p>
<p>On the 68000, a2 &hellip; a5 should be suitable, as should d2 &hellip; d7.
Of course, it does not do to use more than a few of those.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Local-Reg-Vars.html#Local-Reg-Vars" accesskey="n" rel="next">Local Reg Vars</a>, Up: <a href="Explicit-Reg-Vars.html#Explicit-Reg-Vars" accesskey="u" rel="up">Explicit Reg Vars</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>