1 / 34

Chapter 7 Runtime Environments Section 0 Overview

Chapter 7 Runtime Environments Section 0 Overview. 1.Program vs program execution A program consists of several procedures (functions) An execution of a program is called as a process An execution of a program would cause the activation of the related procedures

monita
Télécharger la présentation

Chapter 7 Runtime Environments Section 0 Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 Runtime Environments Section 0 Overview 1.Program vs program execution • A program consists of several procedures (functions) • An execution of a program is called as a process • An execution of a program would cause the activation of the related procedures • A name (e.g, a variable name)in a procedure would be related to different data objects Notes: An activation may manipulate data objects allocated for its use.

  2. Chapter 7 Runtime Environments Section 0 Overview 2. Allocation and de-allocation of data objects • Managed by the run-time support package • Allocation of a data object is just to allocate storage space to the data object relating to the name in the procedure Notes: The representation of a data object at run time is determined by its type.

  3. Chapter 7 Runtime Environments Section 0 Overview 3.Activation Trees A tree to depict the control flow enters and leaves activation of a procedure f(4) f(3) f(2) f(2) f(1) f(1) f(0) f(1) f(0) Enter f(4) Enter f(3) Enter f(2) Enter f(1) Leave f(1) Enter f(0) Leave f(0) Leave f(2) Enter f(1) Leave f(1) Leave f(3) Enter f(2) Enter f(1) Leave f(1) Enter f(0) Leave f(0) Leave f(2) Leave f(4)

  4. Chapter 7 Runtime Environments Section 0 Overview 4.Control stacks A stack to keep track of live procedure activations

  5. Chapter 7 Runtime Environments Section 0 Overview 5.Bindings of Names When an environment associates storage location s with a name x, we say that x is bound to s; the association itself is referred to as a binding of x. Notes: 1)Even each name is declared once in a program, the same name may denote different object at run time 2)The “ data object” corresponds to a storage location that can hold values

  6. Chapter 7 Runtime Environments Section 0 Overview 5.Bindings of Names Notes: 3)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 state name storage value

  7. Chapter 7 Runtime Environments Section 0 Overview 5.Bindings of Names Notes: 4)Environments and states are different; an assignment changes the state, but not the environment 5)If x is not of a basic type, the storage s for x may be a collection of memory words 6)A binding is the dynamic counterpart of a declaration

  8. Chapter 7 Runtime Environments Section 0 Overview 5.Factors that determines the run time environment of a program • Recursive definition • Local name storage space allocation strategy • global name storage space allocation strategy • Parameter passing mechanism • A function is whether allowed to be a parameter of or return value of a function

  9. code Static data stack heap Chapter 7 Runtime Environments Section 1 Storage organization 1.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

  10. Chapter 7 Runtime Environments Section 1 Storage organization 2.Activation Records/Frames 1) Definition A contiguous block of storage that stores information needed by a single execution of a procedure

  11. Returned value Actual parameters Optional control link Optional access link Saved machine status Local data temporaries Chapter 7 Runtime Environments Section 1 Storage organization 2.Activation Records 2) A general activation record

  12. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 1.Storage 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.

  13. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 2.Static allocation • The size of a data object and constraints on its position in memory must be known at compile time. • Recursive procedures are restricted. • Data structures cannot be created dynamically. • Fortran permits static storage allocation

  14. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 3.Heap allocation • A separate area of run-time memory, called a heap. • Heap allocation parcels out pieces of contiguous storage, as needed for activation records or other objects. Pieces may be de-allocated in any order.

  15. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 4. Stack allocation 1)Main idea • Based on the idea of a control stack; storage is organized as a stack, and activation records are pushed and popped as activations begin and end, respectively. • 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

  16. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 4. Stack allocation 2)Calling sequences • A call sequence allocates an activation record and enters information into its fields • A return sequence restores the state of the machine so the calling procedure can continue execution.

  17. e.g. the calling sequence is like the following: main () {…… q( ); } p( ) {……} q( ) { …… p( ); …… }

  18. TOP Top-SP Activation record for P Activation record for Q Activation record for main

  19. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 4. Stack allocation 2)Calling sequences Steps: (1)The caller evaluates actual parameters. (2)The caller stores a return address and the old value of top-sp into the callee’s activation record. (3)The callee saves register values and other status information (4)the callee initializes its local data and begins execution.

  20. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 4. Stack allocation 2)calling sequences A possible return sequence : • (1)The callee places a return value next to the activation record of the caller • (2)Using the information in the status field, the callee restores top-sp and other registers and branches to a return address in the caller’s code. • (3)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.

  21. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 4. Stack allocation 3)Stack allocation for non-nested procedure • Stack allocation for C • When entering a procedure, the activation record of the procedure is set up on the top of the stack • Initially, put the global(static) variables and the activation record of the Main function

  22. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 4. Stack allocation 3)Stack allocation for non-nested procedure TOP P AR SP Q AR Main AR global Var

  23. Caller’s SP Calling Return address Amount of parameters Formal parameters Local data Array data Temporaries Returned value Chapter 7 Runtime Environments Section 2 Storage allocation strategies 4. Stack allocation 3)Stack allocation for non-nested procedure

  24. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 4. Stack allocation 3)Stack allocation for non-nested procedure #include <stdio.h> int x,y; int main() { x=5; y=f(x); }

  25. Chapter 7 Runtime Environments Section 2 Storage allocation strategies 4. Stack allocation 3)Stack allocation for non-nested procedure int f(int n) { if (n<=1) return 1; else { int t1,t2,t; t1=f(n-1); t2=f(n-2); t=t1+t2; return t} }

  26. Storage Organization of C Language ….. Activation Record of the function called by Main function Activation Record of Main function Global Variable Data Area

  27. The Form of Activation Record of any a function in C Unit for Returned Value of function Internal Temporary Work Units Local Variable Data Storage Units Formal parameter Storage Units Number of Formal Parameters Returned Address Caller’s SP (Start Address of caller’s activation record) TOP SP

  28. Chapter 7 Runtime Environments Section 3 Parameter passing 1.Parameter-passing methods • Call-by-value • Call-by-reference • Copy-restore • Call-by-name

  29. Chapter 7 Runtime Environments Section 3 Parameter passing 2.Call-by-value 1) in C and Pascal 2)implementation • 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.

  30. Chapter 7 Runtime Environments Section 3 Parameter passing 3.Call-by-Reference 1) also known as call-by-address or call-by-location. 2)implementation • If an actual parameter is a name or an expression having an l-value, then that l-value itself is passed. • If the actual parameter is an expression that has no l-value, then the expression is evaluated in a new location, and the address of that location is passed.

  31. Chapter 7 Runtime Environments Section 4 Symbol Tables 1.Functions of symbol tables • Keep track of scope and binding information about names • Allow us to add new entries and find existing entries efficiently Notes: 1)Changes to the table occur if a new name or new information about an existing name is discovered 2)The table may be a linear list or a hash table 3)It is useful for a compiler to be able to grow the symbol table dynamically, if necessary, at compile time

  32. Chapter 7 Runtime Environments Section 4 Symbol Tables 2.Symbol-table entries • Each entry in the symbol table is for the declaration of a name • Each entry can be implemented as a record consisting of a sequence of consecutive words of memory Notes: 1)Information may be entered into the symbol table at various times 2)Attributes of a name are entered in response to declarations

More Related