框架指针不会使堆栈指针变得多余吗? [英] Doesn't the frame pointer make the stack pointer redundant?

查看:87
本文介绍了框架指针不会使堆栈指针变得多余吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,堆栈指针指向堆栈上的空闲"内存,堆栈上的推"数据写入堆栈指针所指向的位置并对其进行递增/递减.

As far as I understand it, the stack pointer points to the "free" memory on the stack, and "pushing" data on the stack writes to the location pointed by the stack pointer and increments/decrements it.

但是不可能使用帧指针的偏移量来实现相同的目的,从而节省寄存器.向帧指针添加偏移量的开销与递增和递减堆栈指针的开销几乎相同.我看到的唯一好处是,只要不是推或弹出操作,例如从顶部"(或底部)访问数据,就会更快.仅读取或写入该地址而无需递增/递减.但是话又说回来,这样的操作将使用帧指针花费一个额外的周期,并且将有一个额外的寄存器供通用使用.

But isn't it possible to use offsets from the frame pointer to achieve the same thing, thus saving a register. The overhead from adding offsets to the frame pointer is pretty much the same as the overhead of incrementing and decrementing the stack pointer. The only advantage I see is accessing data from the "top" (or bottom) will be faster, as long as it is not a push or pop operation, e.g. just reading or writing to that address without incrementing/decrementing. But then again, such operations would take a single extra cycle using the frame pointer, and there will be one additional register for general purpose use.

似乎只需要帧指针.它甚至比修改当前堆栈帧中的数据有更多的用途,例如用于调试和堆栈展开.我想念什么吗?

It seems like only the frame pointer is really needed. And it even serves a lot more purpose than just modifying data in the current stack frame, such as to be used in debugging and for stack unwinding. Am I missing something?

推荐答案

嗯,是的,实际上对于64位代码生成器来说很常见.但是,有些复杂情况并不能普遍实现.硬性要求是在编译时知道堆栈指针的值,以便代码生成器可以可靠地生成偏移量.在以下情况下不起作用:

Well, yes, and in fact common for 64-bit code generators. There are complications however that do not make it universally possible. A hard requirement is that the value of the stack pointer is known at compile time so the code generator can generate the offset reliably. This does not work when:

  • 语言运行时提供了平凡的对齐保证.当堆栈帧包含8个字节的变量(例如 double )时,尤其是32位代码中的问题.访问未对齐的变量非常昂贵(如果未对齐4,则为x2,如果跨越L1缓存行则为x3),并且可能使内存模型保证无效.代码生成器通常不能假定函数是使用对齐的堆栈输入的,因此需要在函数序言中生成代码,这可能导致堆栈指针额外减少4个字节.

  • the language runtime provides non-trivial alignment guarantees. Particularly a problem in 32-bit code when the stack frame contains 8-byte variables, like double. Accessing a mis-aligned variable is very expensive (x2 if misaligned by 4, x3 if it straddles an L1 cache-line) and might invalidate a memory model guarantee. The code generator cannot normally assume that the function is entered with an aligned stack so needs to generate code in the function prologue, this can cause the stack pointer to decrement by an extra 4 bytes.

语言运行时为程序动态分配堆栈空间提供了一种方法.非常普遍和理想,它是非常便宜和快速的内存.示例包括CRT中的alloca(),C99 +中的可变长度数组,C#语言中的 stackalloc 关键字.

the language runtime provides a way for a program to dynamically allocate stack space. Very common and desirable, it is very cheap and fast memory. Examples are alloca() in the CRT, variable length arrays in C99+, the stackalloc keyword in the C# language.

语言运行时需要提供一种可靠的方式来遍历堆栈.在异常处理中很常见,沙盒的实现需要能够发现调用者的权限,垃圾收集的语言需要能够发现指向对象的指针.当然,有许多可能的方法可以做到这一点,但是使用基本指针并将调用者的基本指针存储在堆栈帧中的已知位置将使其变得简单.

the language runtime needs to provide a reliable way to walk the stack. Common in exception handling, implementation of a sandbox that need to be able to discover the caller's rights, garbage collected languages that need to be able to discover pointers to objects. Many possible ways to do this of course, but using the base pointer and storing the caller's base pointer in a known location in the stack frame makes it simple.

这篇关于框架指针不会使堆栈指针变得多余吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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