具有可寻址GPR文件,寄存器变量地址以及存储器和寄存器之间的别名的CPU [英] CPUs with addressable GPR files, address of register variables, and aliasing between memory and registers

查看:176
本文介绍了具有可寻址GPR文件,寄存器变量地址以及存储器和寄存器之间的别名的CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些CPU,例如

Some CPUs, such as the Atmel AVR, have a general purpose register file that is also addressable as part of main memory -- see Figure 7-2 in section 7.4 and the paragraph after the figure.

鉴于此,为什么C委员会选择做出

Given this, why did the C committee choose to make

register int ri;
int* pi = &ri;

根据 N1124 第6.7.1节?考虑到上面的代码在至少一个处理器上是有意义的,并且C向后弯曲以适应比AVR更陌生(而且更稀缺!)的目标,因此未定义或实现定义的行为是否更有意义?

universally ill-formed, as per footnote 101 to N1124 section 6.7.1? Wouldn't undefined or implementation-defined behavior make more sense, considering that the code above is meaningful on at least one processor, and C bends over backwards to accommodate far stranger (and scarcer!) targets than the AVR?

101)实现可以将任何register声明都简单地视为auto 宣言.但是,无论是否实际使用可寻址存储,该地址 用存储类说明符register声明的对象的任何部分的 显式计算(通过使用6.5.3.2中讨论的一元&运算符) 或隐式(通过将数组名称转换为指针,如6.3.2.1中所述).因此, 唯一可以应用于用存储类说明符声明的数组的运算符 registersizeof.

101) The implementation may treat any register declaration simply as an auto declaration. However, whether or not addressable storage is actually used, the address of any part of an object declared with storage-class specifier register cannot be computed, either explicitly (by use of the unary & operator as discussed in 6.5.3.2) or implicitly (by converting an array name to a pointer as discussed in 6.3.2.1). Thus, the only operator that can be applied to an array declared with storage-class specifier register is sizeof.

我刚刚通过指针更改了CPU寄存器. ?!

此外,使用GCC显式寄存器变量扩展,可以指示编译器将变量放入特定的寄存器中.在这种情况下,您可以获得一个以注册变量作为别名的指针,如下所示:

I just changed a CPU register through a pointer. Wat?!

Furthermore, using the GCC explicit register variables extension, it is possible to direct the compiler to place a variable into a specific register. In this case, you can get a pointer that aliases with a register variable, as below:

register int ri asm("r15") = 0;
int* pi = (int*)0x15;
/* pi now aliases ri */
*pi = 42;
/* ri is 42 now */
assert(ri == 42);

海湾合作委员会如何处理这种情况?真让我感到奇怪的是,没有考虑过这样的事情……还是已经发生了?

How does GCC deal with such a case? It strikes me as truly bizarre that something like this has not been considered...or has it?

推荐答案

C是一种抽象语言,在不了解最终实现该语言的机器的情况下定义. C的定义不假定基础计算机甚至会以常规形式(或堆栈,连续内存或其他与实际计算机上存在的与此问题无关的其他事物)拥有寄存器.

C is an abstract language defined without knowledge of the machine that will eventually implement it. The definition of C does not assume that the underlying machine will even have registers in the conventional form (or a stack, or contiguous memory, or many other things irrelevant to this question that are present on real machines).

重点是register不会 意味着应该为变量分配一个机器寄存器.关键字的含义是该变量不能使用其地址;这样,编译器理论上就可以在其上执行更好的优化,因为它减少了可潜在修改变量的路径数量.不管在哪个处理器上运行,在C中使用register变量的地址都没有意义,因为register

The point being that register does not mean that the variable should be assigned a machine register. The meaning of the keyword is that the variable cannot have its address taken; the compiler is then theoretically able to perform better optimisations on it because it reduces the number of paths through which the variable can potentially be modified. Taking the address of a register variable isn't meaningful in C, regardless of what processor it runs on, because register is an incredibly badly-named keyword (named for the most obvious optimisation it enables) that specifically means the address should not be taken. That is all it means.

一个用于AVR的智能编译器无论如何都不需要您暗示就可以进行优化(实际上,该关键字毫无用处,因为任何半个体面的编译器都可以检测到何时可以应用,因为基本上没有定义auto对象而无需显式获取其地址的明确方法).

An intelligent compiler for the AVR should be able to make that optimisation without needing you to hint at it, anyway (in practice the keyword is useless precisely because any halfdecent compiler can detect when it would be applicable anyway, since there's basically no well-defined way to reference an auto object without taking its address explicitly).

这篇关于具有可寻址GPR文件,寄存器变量地址以及存储器和寄存器之间的别名的CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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