The stack frame at the top of the stack is for the currently executing routine. The stack frame usually includes at least the following items (in push order):

  • the arguments (parameter values) passed to the routine (if any);
  • the return address back to the routine's caller (e.g. in the DrawLine stack frame, an address into DrawSquare's code); and
  • space for the local variables of the routine (if any).

 

Call stack layout

Stack and frame pointers[edit]

When stack frame sizes can differ, such as between different functions or between invocations of a particular function, popping a frame off the stack does not constitute a fixed decrement of the stack pointer. At function return, the stack pointer is instead restored to the frame pointer, the value of the stack pointer just before the function was called. Each stack frame contains a stack pointer to the top of the frame immediately below. The stack pointer is a mutable register shared between all invocations. A frame pointer of a given invocation of a function is a copy of the stack pointer as it was before the function was invoked.[2]

The locations of all other fields in the frame can be defined relative either to the top of the frame, as negative offsets of the stack pointer, or relative to the top of the frame below, as positive offsets of the frame pointer. The location of the frame pointer itself must inherently be defined as a negative offset of the stack pointer.

Storing the address to the caller's frame[edit]

In most systems a stack frame has a field to contain the previous value of the frame pointer register, the value it had while the caller was executing. For example, the stack frame of DrawLine would have a memory location holding the frame pointer value that DrawSquare uses (not shown in the diagram above). The value is saved upon entry to the subroutine and restored upon return. Having such a field in a known location in the stack frame enables code to access each frame successively underneath the currently executing routine's frame, and also allows the routine to easily restore the frame pointer to the caller's frame, just before it returns.

Lexically nested routines[edit]

Further information: Nested function and Non-local variable

Programming languages that support nested subroutines also have a field in the call frame that points to the stack frame of the latest activation of the procedure that most closely encapsulates the callee, i.e. the immediate scope of the callee. This is called an access link or static link (as it keeps track of static nesting during dynamic and recursive calls) and provides the routine (as well as any other routines it may invoke) access to the local data of its encapsulating routines at every nesting level. Some architectures, compilers, or optimization cases store one link for each enclosing level (not just the immediately enclosing), so that deeply nested routines that access shallow data do not have to traverse several links; this strategy is often called a "display".[3]

Access links can be optimized away when an inner function does not access any (non-constant) local data in the encapsulation, as is the case with pure functions communicating only via arguments and return values, for example. Some historical computers, such as the Burroughs large systems, had special "display registers" to support nested functions, while compilers for most modern machines (such as the ubiquitous x86) simply reserve a few words on the stack for the pointers, as needed.

Overlap[edit]

For some purposes, the stack frame of a subroutine and that of its caller can be considered to overlap, the overlap consisting of the area where the parameters are passed from the caller to the callee. In some environments, the caller pushes each argument onto the stack, thus extending its stack frame, then invokes the callee. In other environments, the caller has a preallocated area at the top of its stack frame to hold the arguments it supplies to other subroutines it calls. This area is sometimes termed the outgoing arguments area or callout area. Under this approach, the size of the area is calculated by the compiler to be the largest needed by any called subroutine.

Call stack Structure的更多相关文章

  1. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  2. 【LeetCode OJ】Evaluate Reverse Polish Notation

    Problem link: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ According to the wik ...

  3. MakeObjectInstance的前世今生(关键是ECX的何时入栈以及Self指针何时存储的)

    高手们的文章有很大启发,但是总有些小错,也有没交代清楚的,以下是我的理解: 编译器编译MainWndProc的时候,它是一个正常Delphi普通函数,MakeObjectInstance对它做变换是运 ...

  4. java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历

    import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; //structure of binary ...

  5. [转] iOS ABI Function Call Guide

    source: apple ARMv6 Function Calling Conventions When functions (routines) call other functions (sub ...

  6. iOS - Block底层解析

    Block是iOS开发中一种比较特殊的数据结构,它可以保存一段代码,在合适的地方再调用,具有语法简介.回调方便.编程思路清晰.执行效率高等优点,受到众多猿猿的喜爱.但是Block在使用过程中,如果对B ...

  7. allego 输出报告说明

    List of Available Reports Assigned Function Report Lists all assigned functions, sorted by function ...

  8. SECD machine

    SECD machine 对程序语言理论的理解 程序语言理论主要研究语法.语义及语言的实现.编程语言有语法,各种数学逻辑.结构化数据都有语法.乔姆斯基的语言体系及巴科斯范式是语法分析的基础,语法分析将 ...

  9. 【deep learning精华部分】稀疏自编码提取高阶特征、多层微调完全解释及代码逐行详解

    我们前面已经讲了如何训练稀疏自编码神经网络,当我们训练好这个神经网络后,当有新的样本输入到这个训练好的稀疏自编码器中后,那么隐藏层各单元的激活值组成的向量就可以代表(因为根据稀疏自编码,我们可以用来恢 ...

随机推荐

  1. CSDN-markdown编辑器之从本机导入Markdown文件(二)

      CSDN-markdown编辑器支持从本机导入Markdown文件的功能,假设你有从其他站点上下载的博客文章或说明文档,或是用软件编写的博客文章或说明文档.想公布到CSDN博客中,就能够使用本功能 ...

  2. 开放-封闭"原则(OCP)

    Open-Closed Principle原则讲的是:一个软件实体应当对扩展开放,对修改关闭. 优点: 通过扩展已有软件系统,可以提供新的行为,以满足对软件的新的需求,使变化中的软件有一定的适应性和灵 ...

  3. php 获取今天,本周,本月,三个月内,半年内,今年的开始和结束时间

    $now = time();         //今天        $today_audit_num = 0;        $today_use_num = 0;        $beginTim ...

  4. Java集合类汇总记录--JDK篇

    接口类图 Java Collection由两套并行的接口组成,一套是Collection接口,一套是Map接口.例如以下图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...

  5. 使用MyBatis Generator自动生成MyBatis的代码

    这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生 ...

  6. C# 函数的传值与传址(转)

    http://www.cnblogs.com/mdnx/archive/2012/09/04/2671060.html using System; using System.Collections.G ...

  7. 解决 2003 Can’t connect to MySQL server on ‘localhost’ (10048)

    2003 Can’t connect to MySQL server on ‘localhost’ (10048)一般见于使用mysql的windows 2003服务器.错误的出现的原因: 第一种原因 ...

  8. Linux 上安装 Node.js

    Linux 上安装 Node.js 直接使用已编译好的包(在个人阿里云服务器47.100.6.106上安装) Node 官网已经把 linux 下载版本更改为已编译好的版本了,我们可以直接下载解压后使 ...

  9. 0621补-MVC的基础整理

    包括:Model-模型.view-视图.Controller-控制器. 特点: 将功能强制分成两个部分,显示html文件,和逻辑PHP文件: 要求浏览器请求负责功能的PHP逻辑文件,该PHP逻辑文件, ...

  10. bzoj 1731: [Usaco2005 dec]Layout 排队布局【差分约束】

    差分约束裸题,用了比较蠢的方法,先dfs_spfa判负环,再bfs_spfa跑最短路 注意到"奶牛排在队伍中的顺序和它们的编号是相同的",所以\( d_i-d_{i-1}>= ...