6

(before any misconceptions may arise, I'm not talking about the linkage of libraries.)

In my textbook (about MIPS assembly), the following is stated:

Procedure/Function frame (aka activation record)

  • Used by some compilers to manage stack storage
  • In addition to Stack Pointer, use Frame Pointer register $fp to keep track of all pertinent information on the stack pertaining to a procedure/function invocation.

Caller side:

  • Caller pushes arguments on the stack (or passes them via $a0 - $a3 if not more than 4 arguments)
  • Caller reserves space on the stack for return values (or they are returned via $v0 - $v1)
  • Caller passes static link (address on the stack of the nearest occurrence of the next lexically enclosing procedure/function) via $v0

(goes on about callee side etc...)

It was hard for me to understand the dynamic links in MIPS (frame pointers etc) because I couldn't find out why one would need that. Eventually I found out that they are not needed at all, it just comes in handy when debugging.

I feel similar about these static links, can someone explain to me what they are used for (preferably with an example)?

Community
  • 1
  • 1
Evert Heylen
  • 960
  • 9
  • 28

2 Answers2

6

The static link, lexically enclosing scope, or static parent, is needed in languages where you can declare functions inside functions. E.g., in the following pseudo-code

int foo(int s, int t) {
    int x;
    ... 
    int bar(int a) {
        return a + x;
    }
}

in bar, the variable x is accessed relative to the static link.

With the stack frame layout

------------------------
arg 1: s
arg 2: t
return address
local variable: x
... 
-------------------------

assuming 32 bit values for all, the address for accessing x in bar would be static_link + 12

drRobertz
  • 3,490
  • 1
  • 12
  • 23
  • I think I understand, but what in this case: http://hastebin.com/redotivalu Would the function bar have to create a copy of x and put it in it's own frame? – Evert Heylen Jan 03 '15 at 17:37
  • See edited answer. No, x is in the stack frame of foo, reached through the static link of bar, which is reached through the static link of quux. – drRobertz Jan 03 '15 at 18:02
  • By the way, global variables can be seen as a special case of static parent. – drRobertz Jan 03 '15 at 18:28
  • Ok! thanks for the answer, reaching x while going through 2 static links was the other option in my head :) – Evert Heylen Jan 03 '15 at 18:36
  • 1
    Both answers did answer the question, but I will select this answer because it's simpler and has a more direct example (pseudocode instead of a bunch of text) – Evert Heylen Jan 03 '15 at 18:38
  • An alternative to avoid having to traverse the links is to keep all (or the n closest) static links in a "display" – drRobertz Jan 03 '15 at 18:39
1

It's about scope. Some languages allow functions to be nested within functions; in those the static link is a link up to the stack frame of the function that includes that language. It's called the static link because to what it will link is known at completive (though not exactly to where, since that'll be wherever on the stack the parent function established its frame).

Compare and contrast with the dynamic link, which always just points to the stack frame above. That's dynamic because (usually) any of a number of functions may have called in so an instance of which function owns the frame pointed to isn't known at compile time.

Imagine a function F, with sub functions Fa, Fb, Fc. F calls Fa, Fa calls Fb, Fb calls Fc. Fa, Fb and Fc can all access the local storage of F according to the semantics of the language.

Then Fa's dynamic and static links will point to a stack frame associated with F.

Fb's dynamic link will point to Fa's frame; its static link will point to F's.

Fc's dynamic link will point to Fb's frame; its static link will point to F's.

Tommy
  • 99,986
  • 12
  • 185
  • 204