zoukankan      html  css  js  c++  java
  • Call stack Structure

     
     
     
     
     
     
     
     
     

    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]

    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.

  • 相关阅读:
    几种常用的认证机制
    几种任务调度的 Java 实现方法与比较
    vue 安装教程
    JDK版本导致Unsupported major.minor version 52.0 error
    在SpringMVC框架下实现文件的 上传和 下载
    SpringMVC框架下实现JSON(类方法中回传数据到jsp页面,使用jQuery方法回传)
    SpringMVC框架下数据的增删改查,数据类型转换,数据格式化,数据校验,错误输入的消息回显
    SpringMVC框架的基础知识;
    hibernate的二级缓存
    hibernate检索方式(HQL 检索方式,QBC 检索方式,本地 SQL 检索方式)
  • 原文地址:https://www.cnblogs.com/feng9exe/p/7885317.html
Copyright © 2011-2022 走看看