实现可以将提示视为实际语句吗? [英] Can an implementation consider hints as actual statements?

查看:23
本文介绍了实现可以将提示视为实际语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 中,register 存储限定符是一个提示,表明应尽可能快地访问此类标识符(例如,存储在 CPU 寄存器中).

In C, the register storage qualifier is an hint to the implementation that such identifier should be accessed as fast as possible (e.g. stored in a CPU register).

§6.7.1具有存储类说明符寄存器的对象标识符声明表明对对象的访问尽可能快.此类建议的有效程度由实施定义.

§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. The extent to which such suggestions are effective is implementation-defined.

§6.7.3 限制限定符的预期用途(如寄存器存储class) 是为了促进优化 [...]

§6.7.3 The intended use of the restrict qualifier (like the register storage class) is to promote optimization [...]

但是,我听说过实现(特别是在嵌入式系统中发现),其中 register 具有更强的含义:它是一个 command 并且编译器应放置合格的寄存器中的标识符.

However, I've heard about implementations (specifically found in embedded systems) where register has a stronger meaning: it is a command and the compiler shall place the qualified identifier in a register.

那么,实现是否允许遵循该行为并因此被视为符合标准?什么允许这样做?

So, is an implementation allowed to follow that behavior and thus considered as standard-compliant? What would permit that?

我提出这个问题是因为我发现,按照标准的规定,有义务将该对象放入寄存器中不再是建议;换句话说,它们是冲突的.

I'm raising this question because I find that being obligated to place that object in a register is no longer suggestion, as mandated by the Standard; they conflict, in other words.

推荐答案

正如你所说,标准说这些建议的有效程度是实现定义的."

As you say, the standard says "The extent to which such suggestions are effective is implementation-defined."

这使实施可以自由地做任何事情,从忽略建议到移动天地来实施它.选择接受 register 说明符作为需要使用寄存器的实现当然不与标准相矛盾,而且无论说明符如何,只自行决定寄存器放置的实现也不是.

That gives the implementation free range to do anything from ignoring the suggestion to moving heaven and earth to implement it. An implementation which chooses to accept the register specifier as requiring the use of a register is certainly not contradicting the standard, and nor is an implementation which just makes its own decisions about register placements regardless of specifiers.

实现不应该做的一件事是拒绝编译程序,因为它需要溢出 register —— 至少要达到 §5.2.4.1 中指定的限制 翻译限制——但没有什么能阻止编译器发出警告.(没有什么能阻止编译器对任何事情发出警告;编译器通常会对被认为是危险的完全合法的构造发出警告.)

The one thing the implementation should not do is refuse to compile a program because it would need to spill a register -- at least, up to the limits specified in §5.2.4.1 Translation limits -- but nothing stops the compiler from issuing a warning. (Nothing stops the compiler from issuing warnings about anything; it's common for compilers to warn about perfectly legal constructs which are considered dangerous.)

重读 5.2.4.1,在我看来,实现实际上可能拒绝编译它认为有太多 register 说明符的程序,因为限制子句仅将实现绑定为能够翻译和执行一个程序",其中包括(例如)在一个块中声明的具有块范围的 511 个标识符",而不是任何这样做的程序.据我所知,编译器可以坚持认为达到该限制的至少一个程序"没有任何 register 规范.

Rereading 5.2.4.1, it seems to me that an implementation could actually refuse to compile a program which it considers to have too many register specifiers, since the limits clause only binds the implementation to be able to translate and execute "one program" which includes (for example) "511 identifiers with block scope declared in one block", and not any program which does so. So as far as I can see, the compiler could insist that the "at least one program" which hits that limit not have any register specifications.

注意:并非所有 CPU 都具有常识意义上的寄存器,但该标准实际上并没有说明任何关于硬件的内容.它只是说 register 说明符传达了程序员希望尽可能快地访问对象"的愿望.此外,编译器试图满足这种愿望实际上并不需要优化对对象的访问;这并不违反优化尝试失败的标准.

Note: Not all CPUs have registers in the common sense of the word, but the standard does not actually say anything about hardware. It simply says that the register specifier communicates the programmer's desire to make "access to the object be as fast as possible". Moreover, the compiler's attempt to satisfy that desire does not actually have to optimize access to the object; it's not a violation of the standard for optimization attempts to fail to optimize.

这篇关于实现可以将提示视为实际语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆