东南大学 编译原理 课件 Cp7.ppt_第1页
东南大学 编译原理 课件 Cp7.ppt_第2页
东南大学 编译原理 课件 Cp7.ppt_第3页
东南大学 编译原理 课件 Cp7.ppt_第4页
东南大学 编译原理 课件 Cp7.ppt_第5页
已阅读5页,还剩49页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Chapter 7 Runtime Environments,Zhai Yuqing Zhou Xiaoyu,Program vs Program Execution,An execution of a program is called as a process. A program consists of several procedures (functions). An execution of a program would cause the activation of the related procedures.,Allocation and De-allocation of

2、Data Objects,A name (e.g, a variable name) in a procedure would be related to different data objects. An activation may manipulate data objects allocated for its use.,Bindings of Names,When an environment associates storage location s with a name x, we say that x is bound to s; the association itsel

3、f is referred to as a binding of x. A binding is the dynamic counterpart of a declaration.,Data Object,Even each name is declared once in a program, the same name may denote different objects at run time. The “ data object” corresponds to a storage location that can hold values.,Environment and Stat

4、e,In programming language semantics, the term “environment” refers to a function that maps a name to a storage location, and term “state” refers to a function that maps a storage location to the value held there.,Environment and State,environment state name storage value,Environment and State,An ass

5、ignment changes the state, but not the environment.,Allocation and De-allocation of Data Objects,Managed by the run-time support package. The representation of a data object at run time is determined by its type. If x is not of a basic type, the storage s for x may be a collection of memory words.,F

6、actors that Determines the Run Time Environment of a Program,Recursive call of procedures. Local name storage space allocation strategy. global name storage space allocation strategy. Parameter passing mechanism.,Factors that Determines the Run Time Environment of a Program,whether a function is all

7、owed to be a parameter of or return value of a function.,Subdivision of Runtime Memory,Typical subdivision of runtime memory into code and data areas,The generated target code; Data objects; A counterpart of the control stack to keep track of procedure activations;,Storage Allocation Strategies,Stor

8、age Allocation Strategies,Static allocation. Lays out storage for all data objects at compile time. Stack allocation. Manages the run-time storage as a stack. Heap allocation. Allocates and de-allocates storage as needed at run time from a heap.,Restrictions Going along with Using Static Allocation

9、Alone,The size of a data object and constraints on its position in memory must be known at compile time. Data structures cannot be created dynamically. Recursive procedures are restricted. Fortran permits static storage allocation.,Heap Allocation,Heap allocation parcels out pieces of contiguous sto

10、rage when needed. Pieces may be de-allocated in any order.,Stack Allocation,Activation Trees,A tree to depict the control flow enters and leaves activation of a procedure,f4() f3; f2; f3() f2; f1,f2() f1(); f0();,f4,f3,f2,f2,f1,f0,f1,f0,f1,Control Stacks,A stack to keep track of live procedure activ

11、ations. Lifetime of the procedures corresponding to the nodes.,Activation Records/Frames,A contiguous block of storage that stores information needed by a single execution of a procedure.,Activation Records,A general activation record,Stack Allocation,Based on the idea of a control stack; storage is

12、 organized as a stack, and activation records are pushed and popped as activations begin and end, respectively.,Stack Allocation,Storage for the locals in each call of a procedure is contained in the activation record for that call. Locals are bound to fresh storage in each activation.,Calling Seque

13、nces:An Example,the calling sequence is like the following,Calling Sequences:An Example,Calling Sequences,The caller evaluates actual parameters. The caller stores a return address and the old value of top-sp into the callees activation record. The callee saves register values and other status infor

14、mation. The callee initializes its local data and begins execution.,A Possible Return Sequence,The callee places a return value next to the activation record of the caller. Using the information in the status field, the callee restores top-sp and other registers and branches to a return address in t

15、he callers code.,A Possible Return Sequence,Although top-sp has been decremented, the caller can copy the returned value into its own activation record and use it to evaluate an expression.,Stack Allocation for Non-nested Procedure,Storage Organization of C Language,Main AR,Q AR,P AR,TOP,SP,global V

16、ar,Activation Record of a Function in C,TOP,SP,An Example,#include int i,j; int main() int k,l; cinl; /assume l=3 k=p(l); coutk; ,int p(int m) int t; if (m=0 | m=1) return(1); else t=m*p(m-1); return(t); ,Stack Allocation for Nested Procedures,Procedure Definitions in A Pascal Program,P0,P1,P2,Call

17、P2,Call P1,P0,P1,P2,Call P2,Call P1,Call P0,Nesting Depth,i1,P0,Pi-1,Pi,i,0,Access Links,P0,P1,P2,Call P2,Call P1,Top-SP,P2,P1,P0,Access link,Access link,Access link,Access Links,TOP,Top-SP,P2,P1,P0,Access link,Access link,Access link,P0,P1,P2,Call P2,Call P1,Displays,Faster access to global than wi

18、th access links can be obtained using an array of pointers to activation records, called a display.,Displays,Suppose control is in an activation of procedure pj at nesting depth j, then the display of activation of pj is:,j1,SP for p0,SP for pj-1,SP for pj,j,0,Displays,When a new activation record f

19、or a procedure at nesting depth i is setup, we Copy d0di-1 from activation record of the caller; Set di to point to the new activation record.,Question,Which subprogram can call subprogram p ?,Parameter Passing,Parameter-passing Methods,Call-by-value Call-by-reference Copy-restore Call-by-name,Call-

20、by-value (in C and Pascal),A formal parameter is treated just like a local name, so the storage for the formals is in the activation record of the called procedure. The caller evaluates the actual parameters and places their r-values in the storage for the formals.,Call-by-Reference,also known as call-by-address or call-by-location. If an actual parameter is a name or an expression having an l-value, then that l-value itself is passed.,Call-by-Reference,If the actual parameter is an expression th

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论