Do the C language standards specify support for global register variables

一曲冷凌霜 提交于 2019-12-23 15:43:40

问题


I read that gcc provides support to define global variables as register stored variables. What I want to know is that do the standards have any specifications for this support.


回答1:


There is a common misconsception that C's keyword register talks about hardware registers. That might be the origin of that concept, but in modern C this is not the purpose. The only real effect that register has, is that the & is not allowed on such a beast.

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.

And to answer your question more directly, no register in file scope is not part of the C language. 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.

Mapping hardware register to specific variables is an extension that compilers provide, e.g gcc. Gcc's feature, as an extension, also works in file scope. But that is quite prohibitive, since usually CPU's don't have many hardware registers to spare.




回答2:


register may not be used for global variables. This is covered by C11 6.9/2:

Constraints

The storage-class specifiers auto and register shall not appear in the declaration specifiers in an external declaration.

Here, external declaration means any declaration that is not within a function. (Not to be confused with extern, or external linkage).




回答3:


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.

That said,

  • A global variable cannot be defined with register storage class, as per C11, chapter §6.9

    The storage-class specifiers auto and register shall not appear in the declaration specifiers in an external declaration.

  • Use of register does not guarantee the allocation in register, Chapter §6.7.1 (emphasis mine)

    A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined.




回答4:


Global register variables are not supported or permitted by C99 or C11 standards (see Sourav Ghosh's or Matt McNabb's answers).

As an extension, GCC accepts

  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.

But it it is GCC specific, Clang/LLVM does not support that extension, even if it is supporting several other GCC extensions (e.g. computed goto-s, ...).




回答5:


No. The standards do not specify any feature like this. The storage class register behaves like auto and cannot be used for global variables.

As a rule of thumb, everything that is intrinsic to a certain machine is not part of standard C.



来源:https://stackoverflow.com/questions/31607507/do-the-c-language-standards-specify-support-for-global-register-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!