Why is a dynamic link required in a function activation record? (in static language)

I read that a dynamic link points to a previous activation record, so it makes sense in a dynamic programming language. But in a static programming language, why is an access link (which indicates activation of a function record at one lower nesting level) insufficient? And specifically in C - why is the access link not needed? And why do we need a dynamic link?

+5
source share
2 answers

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.

+4
source

Your question may be related to the -fomit-frame-pointer GCC parameter, see.

By the way, many people call call frames (on the call stack ) what you call an activation record. The concept of continuation , as well as the style of continuing the passage and the A-normal form, are closely related.

A dynamic link is really only useful for nested functions (and possibly ), and the C standard does not. Some people talk about screen links. Standard C does not have any nested functions, so no related trick is needed (link to image, trampoline, ...).

The GCC compiler provides nested functions as an extension of the C language and implement them with dynamic links to activation records, very close to what you are thinking about. Read also the wikipages on the test of a man or boy and on trampoline .

+2
source

Source: https://habr.com/ru/post/1243563/


All Articles