Skip to content

Chapter 6: Activation Records

6.1 Stack Frames

  • 程序运行时过程调用在 stack 中产生 activation records(又叫做 frame)
  • 函数 f 的一次调用叫做 f 的 activation
  • 局部变量每次调用被生成,每次退出被销毁,用栈保存
  • 用 stack pointer 访问局部变量
  • 关键问题:排布栈使得调用者和被调用者可以正常通信alt text
    • 进入被调用者时,在 frame 存 fp,fp = sp,sp = sp - frame size
    • 离开被调用者时,sp = fp,从内存中取 fp,alt text

为了防止非必要 memory traffic,前几个参数用寄存器传递,多的参数用栈传递

A variable escapes if - 用栈传递 • it is passed by reference, • its address is taken (e.g., using C’s & operator), • or it is accessed from a nested function

Block Structure

嵌套函数访问外部函数的局部变量 - through the Frame Pointer

  • static link - 传递外层函数的栈帧和偏移量alt text
  • Display - 维护一个指向各个 frame 的全局数组,记录嵌套层数,position i 指向最近的嵌套深度为 i 的过程
  • Lambda lifting - 把内部函数需要用到的参数,作为函数参数传递下去