6.Activation Records
Storage Organization
High-Order function
Stack Frame
栈帧(frame)
frame-resident variables
A variable escapes if:(需要被放在内存上)
- it is passed by reference,
- its address is taken (using C’s & operator)
- or it is accessed from a nested function Escaping variables must be written to memory.
Static Link
如何处理嵌套函数,一般有三种方法
- Whenever a function f is called, it can be passed a pointer to the frame of the function statically enclosing f; thispointer is the static link.
- A global array can be maintained, containing-in position i-a pointer to the frame of the most recently entered procedure whose static nesting depth is i. This array is called a display.
- When g calls f, each variable of g that is actually accessedby f (or by any function nested inside f) is passed to f as an extra argument. This is called lambda lifting.
重点讲static link. 习题6.7 讲的是display
Warning
Whenever a function f is called, it is passed a pointer to the stackframe of the “current"(most recently entered) activation of the function g that immediately encloses f in the text of the program
- g immediately enclose f 的定义: 函数f是在函数g里 声明 的, 只差一层, 所以叫g直接包含f
- current activation: 因为g可能被调用多次(如递归, g在栈里有多个栈帧. 所以指的是最近一次g的调用
Note
static link是一个指针。每个函数的栈帧里面都有一个static link

注意indent调用write, 但是write不是在indent里面声明的,先找到indent的static link show, 再找到pp. 最终因为 write是在pp里声明的 所以write的static link=pp的fp
- 在函数调用时,沿着static link往上找。
- 如果不是递归调用,将被调用函数的static link赋值为 声明 它的函数 最近使用的栈帧的 fp
- 如果是递归调用,直接把传递自己的static link
- 在访问变量时,同样沿着static link往上