简体   繁体   English

C语言标准是否指定了对全局寄存器变量的支持

[英]Do the C language standards specify support for global register variables

I read that gcc provides support to define global variables as register stored variables. 我读到gcc提供了将全局变量定义为寄存器存储变量的支持。 What I want to know is that do the standards have any specifications for this support. 我想知道的是标准是否有这种支持的任何规范。

There is a common misconsception that C's keyword register talks about hardware registers. C的关键字register涉及硬件寄存器,这是一种常见的误解。 That might be the origin of that concept, but in modern C this is not the purpose. 这可能是这个概念的起源,但在现代C中,这不是目的。 The only real effect that register has, is that the & is not allowed on such a beast. ,真正的效果register了,就是&是不允许这样的野兽。

They may be realized in any way the compiler wants, hardware registers, instruction immediates, or on the stack, you wouldn't know. 它们可能以编译器想要的任何方式实现,硬件寄存器,指令即时,或者在堆栈中,您不会知道。 What you do know is that a register variable can't alias with other variables. 你知道的是register变量不能与其他变量别名。

And to answer your question more directly, no register in file scope is not part of the C language. 为了更直接地回答您的问题,文件范围中的register不是C语言的一部分。 If it would, that would allow us to declare register const variables of almost any base, that could serve as some sort of global constants . 如果它会,那将允许我们声明几乎任何基数的register const变量,它可以作为某种全局常量

Mapping hardware register to specific variables is an extension that compilers provide, eg gcc. 将硬件寄存器映射到特定变量是编译器提供的扩展,例如gcc。 Gcc's feature, as an extension, also works in file scope. 作为扩展,Gcc的功能也适用于文件范围。 But that is quite prohibitive, since usually CPU's don't have many hardware registers to spare. 但这是非常令人望而却步的,因为通常CPU没有多余的硬件寄存器。

register may not be used for global variables. register不能用于全局变量。 This is covered by C11 6.9/2: 这由C11 6.9 / 2涵盖:

Constraints 约束

The storage-class specifiers auto and register shall not appear in the declaration specifiers in an external declaration. 存储类说明符autoregister不应出现在外部声明的声明说明符中。

Here, external declaration means any declaration that is not within a function. 这里, 外部声明是指任何不在函数内的声明。 (Not to be confused with extern , or external linkage). (不要与extern或外部联系混淆)。

First and foremost. 首先是最重要的。 allocation of "register stored variables" , if any, are the job of compiler. “寄存器存储变量”的分配(如果有的话)是编译器的工作。 C standard does not specify anything mandatory about the same. C标准没有规定任何强制性相同的内容。

That said, 那说,

  • A global variable cannot be defined with register storage class, as per C11 , chapter §6.9 根据C11 ,第6.9章,无法使用register存储类定义全局变量

    The storage-class specifiers auto and register shall not appear in the declaration specifiers in an external declaration. 存储类说明符autoregister不应出现在外部声明的声明说明符中。

  • Use of register does not guarantee the allocation in register, Chapter §6.7.1 ( emphasis mine ) register使用不保证在寄存器中的分配,章节§6.7.1( 强调我的

    A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. 具有存储类说明符register的对象的标识符声明表明对对象的访问尽可能快。 The extent to which such suggestions are effective is implementation-defined. 这些建议有效的程度是实施定义的。

Global register variables are not supported or permitted by C99 or C11 standards (see Sourav Ghosh's or Matt McNabb's answers). C99或C11标准不支持或不允许全局寄存器变量 (参见Sourav GhoshMatt McNabb的答案)。

As an extension , GCC accepts 作为延伸GCC接受

  register int *foo asm ("a5");

to declare a global int variable sitting in register a5 ... This is very rarely useful, and you need to understand how GCC generates code and allocates registesr, what are the calling conventions of your ABI, ... to be able to use that without fears... BTW, it probably weaken GCC optimization abilities. 声明一个坐在寄存器a5全局 int变量...这很少有用,你需要了解GCC如何生成代码并分配registesr,你的ABI的调用约定是什么,......能够使用它没有恐惧......顺便说一下,它可能会削弱GCC优化能力。

But it it is GCC specific , Clang/LLVM does not support that extension, even if it is supporting several other GCC extensions (eg computed goto -s, ...). 但是,它是GCC的特殊 ,锵/ LLVM并没有支持扩展,即使是支持其他几个GCC扩展(例如计算goto -s,...)。

No. The standards do not specify any feature like this. 不。标准没有指定任何这样的功能。 The storage class register behaves like auto and cannot be used for global variables. 存储类register行为类似于auto ,不能用于全局变量。

As a rule of thumb, everything that is intrinsic to a certain machine is not part of standard C. 根据经验,某台机器固有的所有东西都不是标准C的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM