⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 global-reg-vars.html

📁 自己收集的linux入门到学懂高级编程书集 包括linux程序设计第三版
💻 HTML
字号:
<html lang="en"><head><title>Using the GNU Compiler Collection (GCC)</title><meta http-equiv="Content-Type" content="text/html"><meta name="description" content="Using the GNU Compiler Collection (GCC)"><meta name="generator" content="makeinfo 4.6"><!--Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.   <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below).  A copy of the license isincluded in the section entitled "GNU Free Documentation License".   <p>(a) The FSF's Front-Cover Text is:   <p>A GNU Manual   <p>(b) The FSF's Back-Cover Text is:   <p>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.--><meta http-equiv="Content-Style-Type" content="text/css"><style type="text/css"><!--  pre.display { font-family:inherit }  pre.format  { font-family:inherit }  pre.smalldisplay { font-family:inherit; font-size:smaller }  pre.smallformat  { font-family:inherit; font-size:smaller }  pre.smallexample { font-size:smaller }  pre.smalllisp    { font-size:smaller }--></style></head><body><div class="node"><p>Node:&nbsp;<a name="Global%20Reg%20Vars">Global Reg Vars</a>,Next:&nbsp;<a rel="next" accesskey="n" href="Local-Reg-Vars.html#Local%20Reg%20Vars">Local Reg Vars</a>,Up:&nbsp;<a rel="up" accesskey="u" href="Explicit-Reg-Vars.html#Explicit%20Reg%20Vars">Explicit Reg Vars</a><hr><br></div><h3 class="subsection">Defining Global Register Variables</h4><p>You can define a global register variable in GNU C like this:<pre class="smallexample">     register int *foo asm ("a5");     </pre><p>Here <code>a5</code> is the name of the register which should be used.  Choose aregister which is normally saved and restored by function calls on yourmachine, so that library routines will not clobber it.   <p>Naturally the register name is cpu-dependent, so you would need toconditionalize your program according to cpu type.  The register<code>a5</code> would be a good choice on a 68000 for a variable of pointertype.  On machines with register windows, be sure to choose a "global"register that is not affected magically by the function call mechanism.   <p>In addition, operating systems on one type of cpu may differ in how theyname the registers; then you would need additional conditionals.  Forexample, some 68000 operating systems call this register <code>%a5</code>.   <p>Eventually there may be a way of asking the compiler to choose a registerautomatically, but first we need to figure out how it should choose andhow to enable you to guide the choice.  No solution is evident.   <p>Defining a global register variable in a certain register reserves thatregister entirely for this use, at least within the current compilation. The register will not be allocated for any other purpose in the functionsin the current compilation.  The register will not be saved and restored bythese functions.  Stores into this register are never deleted even if theywould appear to be dead, but references may be deleted or moved orsimplified.   <p>It is not safe to access the global register variables from signalhandlers, or from more than one thread of control, because the systemlibrary routines may temporarily use the register for other things (unlessyou recompile them specially for the task at hand).   <p>It is not safe for one function that uses a global register variable tocall another such function <code>foo</code> by way of a third function<code>lose</code> that was compiled without knowledge of this variable (i.e. in adifferent source file in which the variable wasn't declared).  This isbecause <code>lose</code> might save the register and put some other value there. For example, you can't expect a global register variable to be available inthe 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 torecompile <code>qsort</code> with the same global register variable, you cansolve this problem.)   <p>If you want to recompile <code>qsort</code> or other source files which do notactually use your global register variable, so that they will not use thatregister for any other purpose, then it suffices to specify the compileroption <code>-ffixed-</code><var>reg</var><code></code>.  You need not actually add a globalregister declaration to their source code.   <p>A function which can alter the value of a global register variable cannotsafely be called from a function compiled without this variable, because itcould clobber the value the caller expects to find there on return. Therefore, the function which is the entry point into the part of theprogram that uses the global register variable must explicitly save andrestore the value which belongs to its caller.   <p>On most machines, <code>longjmp</code> will restore to each global registervariable the value it had at the time of the <code>setjmp</code>.  On somemachines, however, <code>longjmp</code> will not change the value of globalregister variables.  To be portable, the function that called <code>setjmp</code>should make other arrangements to save the values of the global registervariables, and to restore them in a <code>longjmp</code>.  This way, the samething will happen regardless of what <code>longjmp</code> does.   <p>All global register variable declarations must precede all functiondefinitions.  If such a declaration could appear after functiondefinitions, the declaration would be too late to prevent the register frombeing used for other purposes in the preceding functions.   <p>Global register variables may not have initial values, because anexecutable file has no means to supply initial contents for a register.   <p>On the SPARC, there are reports that g3 <small class="dots">...</small> g7 are suitableregisters, but certain library functions, such as <code>getwd</code>, as wellas the subroutines for division and remainder, modify g3 and g4.  g1 andg2 are local temporaries.   <p>On the 68000, a2 <small class="dots">...</small> a5 should be suitable, as should d2 <small class="dots">...</small> d7. Of course, it will not do to use more than a few of those.   </body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -