Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the static link used for?

Tags:

assembly

mips

(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)?

like image 637
Evert Heylen Avatar asked Jan 03 '15 17:01

Evert Heylen


2 Answers

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

like image 67
drRobertz Avatar answered Oct 11 '22 14:10

drRobertz


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.

like image 40
Tommy Avatar answered Oct 11 '22 12:10

Tommy