chapter 7 runtime environments section 0 overview

34
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.

Upload: monita

Post on 13-Jan-2016

47 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 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• 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.

Page 2: Chapter 7 Runtime Environments  Section 0 Overview

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.

Page 3: Chapter 7 Runtime Environments  Section 0 Overview

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)

Page 4: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 0 Overview

4.Control stacks

A stack to keep track of live procedure activations

Page 5: Chapter 7 Runtime Environments  Section 0 Overview

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

Page 6: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 0 Overview

5.Bindings of NamesNotes: 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

Page 7: Chapter 7 Runtime Environments  Section 0 Overview

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

Page 8: Chapter 7 Runtime Environments  Section 0 Overview

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

Page 9: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 1 Storage organization

1.Subdivision of Runtime Memory

Typical subdivision of runtime memory into code and data areas

codeStatic data

stack

heap

The generated target code;

Data objects;

A counterpart of the control stack to keep track of procedure activations

Page 10: Chapter 7 Runtime Environments  Section 0 Overview

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

Page 11: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 1 Storage organization

2.Activation Records

2) A general activation record

Returned value

Actual parameters

Optional control link

Optional access linkSaved machine status

Local data

temporaries

Page 12: Chapter 7 Runtime Environments  Section 0 Overview
Page 13: Chapter 7 Runtime Environments  Section 0 Overview
Page 14: Chapter 7 Runtime Environments  Section 0 Overview

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.

Page 15: Chapter 7 Runtime Environments  Section 0 Overview

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

Page 16: Chapter 7 Runtime Environments  Section 0 Overview

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.

Page 17: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 2 Storage allocation strategies

4. Stack allocation1)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

Page 18: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 2 Storage allocation strategies

4. Stack allocation2)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.

Page 19: Chapter 7 Runtime Environments  Section 0 Overview

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

Page 20: Chapter 7 Runtime Environments  Section 0 Overview

Activation record for main

Activation record for Q

Activation record for P

TOP

Top-SP

Page 21: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 2 Storage allocation strategies

4. Stack allocation2)Calling sequencesSteps: (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.

Page 22: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 2 Storage allocation strategies

4. Stack allocation2)calling sequencesA 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.

Page 23: Chapter 7 Runtime Environments  Section 0 Overview

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

Page 24: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 2 Storage allocation strategies

4. Stack allocation

3)Stack allocation for non-nested procedure

Main AR

Q AR

P ARTOP

SP

global Var

Page 25: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 2 Storage allocation strategies

4. Stack allocation

3)Stack allocation for non-nested procedureCaller’s SP

Calling Return address

Amount of parameters

Formal parametersLocal data

Array data

Temporaries

Returned value

Page 26: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 2 Storage allocation strategies

4. Stack allocation3)Stack allocation for non-nested procedure #include <stdio.h> int x,y; int main(){ x=5; y=f(x);} 

Page 27: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 2 Storage allocation strategies

4. Stack allocation3)Stack allocation for non-nested procedureint 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} }

Page 28: Chapter 7 Runtime Environments  Section 0 Overview

…..Activation Record of the function called by Main functionActivation Record of Main functionGlobal Variable Data Area

Storage Organization of C Language

 

Page 29: Chapter 7 Runtime Environments  Section 0 Overview

The Form of Activation Record of any a function in C

Unit for Returned Value of functionInternal Temporary Work UnitsLocal Variable Data Storage UnitsFormal parameter Storage UnitsNumber of Formal ParametersReturned Address Caller’s SP (Start Address of caller’s activation record)

TOP

SP

Page 30: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 3 Parameter passing

1.Parameter-passing methods

• Call-by-value

• Call-by-reference

• Copy-restore

• Call-by-name

Page 31: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 3 Parameter passing

2.Call-by-value1) in C and Pascal2)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.

Page 32: Chapter 7 Runtime Environments  Section 0 Overview

Chapter 7 Runtime Environments Section 3 Parameter passing

3.Call-by-Reference1) 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.

Page 33: Chapter 7 Runtime Environments  Section 0 Overview

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

Page 34: Chapter 7 Runtime Environments  Section 0 Overview

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