I will use this nomenclature, which is more familiar to me:
Activation entry : stack frame
Dynamic link : [saved] frame pointer
So, I interpret your question as: why do we need frame pointers? [1]
A frame pointer is not required.
Some compilers (for example, Green Hills C ++, GCC with -O2) usually do not generate one or may ask not to generate one (MSVC, GCC).
However, this, of course, has its advantages:
A simple pass through the call stack : creating a stack trace is as simple as moving a linked list where the frame pointer forms the head. Improves stack trace and debugger traces.
Simple code generation : stack variables can be referenced by indexing the frame pointer instead of the stack pointer that changes over time. The stack pointer changes with each press / pop, the frame pointer remains constant inside the function (between prolog / epilogue )
If everything goes wrong, stack promotion can be done using the frame pointer. This is how Borland structured exception handling (SEH) works.
Stack management optimization . In particular, the implementation of setjmp(3) , alloca(3) and C99-VLA may (and usually) depend on it.
Disadvantages:
- Use registration : x86 received only 8 general-purpose registers. one of them should be fully allocated to hold the frame pointer.
- Overhead : A prologue / epilogue is generated for each function.
But, as you noticed, the compiler can generate completely accurate code without the need to maintain a frame pointer.
[1] If this does not mean, please specify.
source share