0% found this document useful (0 votes)
36 views

Runtime System

The document discusses runtime environments and procedures. It describes how runtime support manages the relationship between names and data objects, and allocates and deallocates memory. It also discusses procedure activations, recursive procedures, and the lifetimes of procedure activations. The activation tree is used to depict the flow of control between procedures.

Uploaded by

Nick Damor
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Runtime System

The document discusses runtime environments and procedures. It describes how runtime support manages the relationship between names and data objects, and allocates and deallocates memory. It also discusses procedure activations, recursive procedures, and the lifetimes of procedure activations. The activation tree is used to depict the flow of control between procedures.

Uploaded by

Nick Damor
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Runtime Environment

• Relationship between names and data objects (of


target machine)

• Allocation & de-allocation is managed by run time


support package

• Each execution of a procedure is an activation of


the procedure. If procedure is recursive, several
activations may be alive at the same time.

• If a and b are activations of two procedures then their


lifetime is either non overlapping or nested

• A procedure is recursive if an activation can begin


before an earlier activation of the same procedure has
ended 1
Procedure
• A procedure definition is a declaration that
associates an identifier with a statement
(procedure body)

• When a procedure name appears in an


executable statement, it is called at that
point

• Formal parameters are the one that appear


in declaration. Actual Parameters are the
one that appear in when a procedure is
called
2
Activation tree
• Control flows sequentially

• Execution of a procedure starts at the beginning of body

• It returns control to place where procedure was called from

• A tree can be used, called an activation tree, to depict the


way control enters and leaves activations

• The root represents the activation of main program

• Each node represents an activation of procedure

• The node a is parent of b if control flows from a to b

• The node a is to the left of node b if lifetime of a occurs


before b
3
Example

4
Activation Tree
Sort

r q(1,9)

p(1,9) q(1,3) q(5,9)


returns 4

p(1,3) q(1,0) q(2,3) p(5,9) q(5,5) q(7,9)


returns 1 returns 6

p(2,3) q(2,1) q(3,3) p(7,9) q(7,7) q(9,9)


returns 2 returns 8

5
Control stack
• Flow of control in program corresponds to
depth first traversal of activation tree

• Use a stack called control stack to keep


track of live procedure activations

• Push the node when activation begins and


pop the node when activation ends

• When the node n is at the top of the stack


the stack contains the nodes along the
path from n to the root
6
Scope of declaration
• A declaration is a syntactic construct associating
information with a name

– Explicit declaration :Pascal (Algol class of languages)


var i : integer

– Implicit declaration: Fortran


i is assumed to be integer

• There may be independent declarations of same name in a


program.

• Scope rules determine which declaration applies to a name

• Name binding
environment state
name storage value
7
Storage organization
• The runtime storage
might be subdivided into code

– Target code static data


stack
– Data objects

– Stack to keep track of


procedure activation
heap
– Heap to keep all other
information

8
Activation Record
• temporaries: used in expression
evaluation
Temporaries
• local data: field for local data local data

• saved machine status: holds info about machine status


machine status before procedure call
Access links

• access link : to access non local data Control links

• control link :points to activation Parameters


record of caller
Return value
• actual parameters: field to hold actual
parameters

• returned value: field for holding value 9


to be returned
Storage Allocation Strategies
• Static allocation: lays out storage at
compile time for all data objects

• Stack allocation: manages the runtime


storage as a stack

• Heap allocation :allocates and de-


allocates storage as needed at
runtime from heap
10
Static allocation
• Names are bound to storage as the program
is compiled

• No runtime support is required

• Bindings do not change at run time

• On every invocation of procedure names are


bound to the same storage

• Values of local names are retained across


activations of a procedure
11
• Type of a name determines the amount of storage to
be set aside

• Address of a storage consists of an offset from the


end of an activation record

• Compiler decides location of each activation

• All the addresses can be filled at compile time

• Constraints

– Size of all data objects must be known at compile time

– Recursive procedures are not allowed

– Data structures cannot be created dynamically 12


Stack Allocation
Sort Sort

Sort Sort
readarray readarray

Sort Sort
qsort(1,9)
readarray qsort(1,9)

Sort Sort
readarray qsort(1,9) qsort(1,9)

partition(1,9) qsort(1,3) qsort(1,3) 13


Calling Sequence
• A call sequence
allocates an Parameter and
Return value

activation record and Control link Caller’s


Activation
enters information Links and saved values record
into its field Space for temporaries
And local data
Caller’s
responsibility Parameter and
Return value

• A return sequence Control link Callee’s


Activation
restores the state of Callee’s
Links and saved values record

the machine so that responsibility


Space for temporaries
And local data

calling procedure can


continue execution
14
Call Sequence
• Caller evaluates the actual parameters

• Caller stores return address and other


values (control link) into callee’s
activation record

• Callee saves register values and other


status information

• Callee initializes its local data and begins


execution
15
Return Sequence
• Callee places a return value next to
activation record of caller

• Restores registers using information in


status field

• Branch to return address

• Caller copies return value into its own


activation record
16
Long Length Data
ptr to A
ptr to activation of
B
ptr to C P

array
A
array Long length
B data
array
C

activation of
Q

arrays of Q

17
Dangling references
Referring to locations which have been deallocated
main()
{int *p;
p = dangle(); /* dangling reference */
}

int *dangle();
{
int i=23;
return &i;
}
18
Heap Allocation
• Stack allocation cannot be used if:
– The values of the local variables must be retained when
an activation ends
– A called activation outlives the caller

• In such a case de-allocation of activation


record cannot occur in last-in first-out
fashion

• Heap allocation gives out pieces of


contiguous storage for activation records
19
Heap Allocation …
• Pieces may be de-allocated in any order

• Over time the heap will consist of alternate areas


that are free and in use

• Heap manager is supposed to make use of the free


space

• For efficiency reasons it may be helpful to handle


small activations as a special case

• For each size of interest keep a linked list of free


blocks of that size
20
Heap Allocation …
• Fill a request of size s with block of size s′
where s′ is the smallest size greater than
or equal to s

• For large blocks of storage use heap


manager

• For large amount of storage computation


may take some time to use up memory so
that time taken by the manager may be
negligible compared to the computation
time
21
Parameter Passing
• Call by value

– actual parameters are evaluated and their rvalues


are passed to the called procedure

– used in Pascal and C

– formal is treated just like a local name

– caller evaluates the actual parameters and places


rvalue in the storage for formals

– call has no effect on the activation record of


caller 25
Parameter Passing …
• Call by reference (call by address)

– the caller passes a pointer to each


location of actual parameters

– if actual parameter is a name then lvalue


is passed

– if actual parameter is an expression then


it is evaluated in a new location and the
address of that location is passed
26
Parameter Passing …
• Copy restore (copy-in copy-out, call
by value result)

– actual parameters are evaluated, rvalues


are passed by call by value, lvalues are
determined before the call

– when control returns, the current


rvalues of the formals are copied into
lvalues of the locals
27
Parameter Passing …
• Call by name (used in Algol)

– names are copied

– local names are different from names of


calling procedure

swap(i,a[i])
temp = i
i = a[i]
a[i] = temp 28

You might also like