What is a static link used for?

(Before any misconceptions arise, I am not talking about linking libraries.)

My tutorial (about MIPS build) states the following:

Procedure / function frame (also activation record)

  • Used by some compilers to manage stack storage
  • In addition to the stack pointer, use the $ fp frame pointer register to keep track of all relevant stack information to the procedure / function call.

Caller's side:

  • The caller calls the arguments on the stack (or passes them through $ a0 - $ a3 if there are no more than 4 arguments)
  • Caller reserves stack space for return values โ€‹โ€‹(or they are returned via $ v0 - $ v1)
  • Caller passes a static reference (the address on the stack of the closest occurrence of the next lexically enclosing procedure / function) via $ v0

(talking about the side of similarity, etc.)

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

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

+3


source to share


2 answers


A static reference lexically covering a scope or static parent is required in languages โ€‹โ€‹where you can declare functions inside functions. For example, in the following pseudocode

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

      

in bar

, the variable x

is accessible relative to a static reference.



With a stack frame layout

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

      

assuming 32-bit values โ€‹โ€‹for all, the address to access x

in bar

would bestatic_link + 12

+3


source


It's about the sphere. Some languages โ€‹โ€‹allow you to embed functions within functions; in which a static link is a link to the stack frame of the function that includes that language. It is called a static link because what it will bind is known on the trailing one (although not exactly where, since it will be anywhere on the stack, the parent function has set its frame).

Compare and contrast with a dynamic link which always just points to the stack above. This dynamic is because (usually) any of several functions could have been called, so the instance of the function to which the associated frame belongs is not known at compile time.

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



The dynamic and static references of Fa then point to the stack frame associated with F.

The dynamic link Fb will point to the Fa frame; its static link will point to F.

The dynamic Fc link will point to the Fb frame; its static link will point to F.

0


source







All Articles