1<html lang="en"> 2<head> 3<title>Global Reg Vars - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Explicit-Reg-Vars.html#Explicit-Reg-Vars" title="Explicit Reg Vars"> 9<link rel="next" href="Local-Reg-Vars.html#Local-Reg-Vars" title="Local Reg Vars"> 10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 11<!-- 12Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 131998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 142010 Free Software Foundation, Inc. 15 16Permission is granted to copy, distribute and/or modify this document 17under the terms of the GNU Free Documentation License, Version 1.3 or 18any later version published by the Free Software Foundation; with the 19Invariant Sections being ``Funding Free Software'', the Front-Cover 20Texts being (a) (see below), and with the Back-Cover Texts being (b) 21(see below). A copy of the license is included in the section entitled 22``GNU Free Documentation License''. 23 24(a) The FSF's Front-Cover Text is: 25 26 A GNU Manual 27 28(b) The FSF's Back-Cover Text is: 29 30 You have freedom to copy and modify this GNU Manual, like GNU 31 software. Copies published by the Free Software Foundation raise 32 funds for GNU development.--> 33<meta http-equiv="Content-Style-Type" content="text/css"> 34<style type="text/css"><!-- 35 pre.display { font-family:inherit } 36 pre.format { font-family:inherit } 37 pre.smalldisplay { font-family:inherit; font-size:smaller } 38 pre.smallformat { font-family:inherit; font-size:smaller } 39 pre.smallexample { font-size:smaller } 40 pre.smalllisp { font-size:smaller } 41 span.sc { font-variant:small-caps } 42 span.roman { font-family:serif; font-weight:normal; } 43 span.sansserif { font-family:sans-serif; font-weight:normal; } 44--></style> 45<link rel="stylesheet" type="text/css" href="../cs.css"> 46</head> 47<body> 48<div class="node"> 49<a name="Global-Reg-Vars"></a> 50<p> 51Next: <a rel="next" accesskey="n" href="Local-Reg-Vars.html#Local-Reg-Vars">Local Reg Vars</a>, 52Up: <a rel="up" accesskey="u" href="Explicit-Reg-Vars.html#Explicit-Reg-Vars">Explicit Reg Vars</a> 53<hr> 54</div> 55 56<h4 class="subsection">6.44.1 Defining Global Register Variables</h4> 57 58<p><a name="index-global-register-variables-2684"></a><a name="index-registers_002c-global-variables-in-2685"></a> 59You can define a global register variable in GNU C like this: 60 61<pre class="smallexample"> register int *foo asm ("a5"); 62</pre> 63 <p class="noindent">Here <code>a5</code> is the name of the register which should be used. Choose a 64register which is normally saved and restored by function calls on your 65machine, so that library routines will not clobber it. 66 67 <p>Naturally the register name is cpu-dependent, so you would need to 68conditionalize your program according to cpu type. The register 69<code>a5</code> would be a good choice on a 68000 for a variable of pointer 70type. On machines with register windows, be sure to choose a “global” 71register that is not affected magically by the function call mechanism. 72 73 <p>In addition, operating systems on one type of cpu may differ in how they 74name the registers; then you would need additional conditionals. For 75example, some 68000 operating systems call this register <code>%a5</code>. 76 77 <p>Eventually there may be a way of asking the compiler to choose a register 78automatically, but first we need to figure out how it should choose and 79how to enable you to guide the choice. No solution is evident. 80 81 <p>Defining a global register variable in a certain register reserves that 82register entirely for this use, at least within the current compilation. 83The register will not be allocated for any other purpose in the functions 84in the current compilation. The register will not be saved and restored by 85these functions. Stores into this register are never deleted even if they 86would appear to be dead, but references may be deleted or moved or 87simplified. 88 89 <p>It is not safe to access the global register variables from signal 90handlers, or from more than one thread of control, because the system 91library routines may temporarily use the register for other things (unless 92you recompile them specially for the task at hand). 93 94 <p><a name="index-g_t_0040code_007bqsort_007d_002c-and-global-register-variables-2686"></a>It is not safe for one function that uses a global register variable to 95call another such function <code>foo</code> by way of a third function 96<code>lose</code> that was compiled without knowledge of this variable (i.e. in a 97different source file in which the variable wasn't declared). This is 98because <code>lose</code> might save the register and put some other value there. 99For example, you can't expect a global register variable to be available in 100the comparison-function that you pass to <code>qsort</code>, since <code>qsort</code> 101might have put something else in that register. (If you are prepared to 102recompile <code>qsort</code> with the same global register variable, you can 103solve this problem.) 104 105 <p>If you want to recompile <code>qsort</code> or other source files which do not 106actually use your global register variable, so that they will not use that 107register for any other purpose, then it suffices to specify the compiler 108option <samp><span class="option">-ffixed-</span><var>reg</var></samp>. You need not actually add a global 109register declaration to their source code. 110 111 <p>A function which can alter the value of a global register variable cannot 112safely be called from a function compiled without this variable, because it 113could clobber the value the caller expects to find there on return. 114Therefore, the function which is the entry point into the part of the 115program that uses the global register variable must explicitly save and 116restore the value which belongs to its caller. 117 118 <p><a name="index-register-variable-after-_0040code_007blongjmp_007d-2687"></a><a name="index-global-register-after-_0040code_007blongjmp_007d-2688"></a><a name="index-value-after-_0040code_007blongjmp_007d-2689"></a><a name="index-longjmp-2690"></a><a name="index-setjmp-2691"></a>On most machines, <code>longjmp</code> will restore to each global register 119variable the value it had at the time of the <code>setjmp</code>. On some 120machines, however, <code>longjmp</code> will not change the value of global 121register variables. To be portable, the function that called <code>setjmp</code> 122should make other arrangements to save the values of the global register 123variables, and to restore them in a <code>longjmp</code>. This way, the same 124thing will happen regardless of what <code>longjmp</code> does. 125 126 <p>All global register variable declarations must precede all function 127definitions. If such a declaration could appear after function 128definitions, the declaration would be too late to prevent the register from 129being used for other purposes in the preceding functions. 130 131 <p>Global register variables may not have initial values, because an 132executable file has no means to supply initial contents for a register. 133 134 <p>On the SPARC, there are reports that g3 <small class="dots">...</small> g7 are suitable 135registers, but certain library functions, such as <code>getwd</code>, as well 136as the subroutines for division and remainder, modify g3 and g4. g1 and 137g2 are local temporaries. 138 139 <p>On the 68000, a2 <small class="dots">...</small> a5 should be suitable, as should d2 <small class="dots">...</small> d7. 140Of course, it will not do to use more than a few of those. 141 142 </body></html> 143 144