Escolar Documentos
Profissional Documentos
Cultura Documentos
10/18/2012
10/18/2012
10/18/2012
10/18/2012
Storage organization
Subdivision of Runtime Memory
Typical subdivision of runtime memory into code and data areas
code
Static data stack The generated target code; Data objects; A counterpart of the control stack to keep track of procedure activations
heap
10/18/2012 Lovely Professional University,
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.
10/18/2012 Lovely Professional University,
Static allocation
Names are bound to storage as the program is compiled, so there is no need for a run-time support package. Since the bindings do not change at run time, Every time a procedure is activated, Its names are bound to the same storage locations.
10/18/2012
Some Limitations The size of a data object and constraints on its position in memory must be known at compile time.
Recursive procedures are restricted, because all activations of a procedure use the same bindings for local name.
Data structures cannot be created dynamically.
10/18/2012 Lovely Professional University,
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 deallocated in any order.
I -11 J -11 X 0 A -21
10/18/2012
Heap allocation is useful if either of the following is possible. 1. The value of local names must be retained when an activation ends. 2. A called activation outlives the caller.
10/18/2012
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
10/18/2012 Lovely Professional University,
10/18/2012
10/18/2012
10/18/2012
10/18/2012
10/18/2012
10/18/2012
10/18/2012
10/18/2012
10/18/2012
Binding of names
Same name may denote different data objects (i.e., locations) at runtime Two mapping are used
Environment
maps a name to a storage (or binding of a name to a storage)
State
maps storage to value
environment
state
name
10/18/2012
locatio n
Lovely Professional University,
value
10/18/2012
10/18/2012
Activation Records/Frames
A contiguous block of storage that stores information needed by a single execution of a procedure A general activation record
Returned value Actual parameters Optional control link Optional access link Saved machine status Local data temporaries The value that the callee return to caller Used by the caller to supply parameters to the callee Point to the activation record of the
caller Refer to non-local data held in other activation records/fixed place Information about the state of the machine just before the procedure Data that is local to the execution is called of a procedure Such as those values in the evaluation of expressions
10/18/2012
10/18/2012
Activation Trees
Activation trees
Represents the activations of procedures during program execution using a tree
10/18/2012
10/18/2012
Activation Trees A tree to depict the control flow enters and leaves activation of a procedure Enter f(4) Enter f(2) f(4) Enter f(3) Enter f(1) Enter f(2) Leave f(1) Enter f(1) Enter f(0) f(3) f(2) Leave f(1) Leave f(0) Enter f(0) Leave f(2) Leave f(0) Leave f(4) f(2) f(1) f(1) f(0) Leave f(2) Enter f(1) Leave f(1) f(1) f(0) Leave f(3)
10/18/2012
10/18/2012
10/18/2012
10/18/2012
Each node represents an activation of a procedure The root represents the activation of the main program The node for a is the parent of the node for b if and only if the control flows from activation of a to b, and The node for a is to the left of the node for b if and only if the lifetime of a occurs before the lifetime of b.
10/18/2012 Lovely Professional University,
Control Flow
The control flow in a program P corresponds to depth-first traversal of activation tree
Starts at the root Visits a node before its children Recursively visits children at each node from leftto-right The sequence of procedure calls corresponds to a preorder traversal of the activation tree The sequence of returns corresponds to postorder traversal of the activation tree
10/18/2012 Lovely Professional University,
Code in calling sequences divided between Caller and Callee In general, if procedure p calls Procedure q
The activation of q terminates normally The activation of q aborts and p ends at the same time The activation of q terminates due to some exception. The procedure p may or may not terminate
10/18/2012
Where the root of the activation tree is at the bottom of the stack
Having node nj on the top of the stack means There exists a path in Activation tree from node nj to the root E.g,
<root,,ni, nj>
10/18/2012
e.g., If control is currently in the activation write() of the tree, then activation record for write() is at the top of the control stack. Below it is the activation record for output(), and the bottom is the activation record for main()
10/18/2012 Lovely Professional University,
10/18/2012
10/18/2012
10/18/2012
10/18/2012
10/18/2012
10/18/2012
In this example, as is true in general, procedure activations are nested in time. If an activation of procedure p calls procedure q, then that activation of q must end before the activation of p can end. There are three common cases: 1. The activation of q terminates normally. Then in essentially any language, control resumes just after the point of p at which the call to q was made. 2. The activation of q, or some procedure q called, either directly or indirectly, aborts; i.e., it becomes impossible for execution to continue. In that case, p ends simultaneously with q. 3. The activation of q terminates because of an exception that q cannot handle. Procedure p may handle the exception, in which case the activation of q has terminated while the activation of p continues, although not necessarily from the point at which the call to q was made. If p cannot handle the exception, then this activation of p terminates at the same time as the activation of q, and presumably the exception will be handled by some other open activation of a procedure
10/18/2012
10/18/2012
10/18/2012
10/18/2012
Calling Sequence
The caller evaluates the actual parameters The caller stores a return address and the old value of top-sp into the callee's activation record. The callee saves the register values and other status information.
10/18/2012
Access to dynamically allocated arrays (NOTE: Here the top of the stack is downwards)
10/18/2012
10/18/2012
Better Alternative
Allocate dynamic data on heap Maintain only the address of the allocated memory (fixed ) [heap memory management will be discussed later while discussion on garbage collection]
10/18/2012
Static
Storage allocation decision is static if it can be made by the compiler only by looking at the source code text example: all variables of fixed size struct sample { int x, y; float u,v,w;}
Dynamic
Storage allocation decision is dynamic if it is based on execution of the program e.g. (1) allocation of memory (heap) through malloc() etc (2) allocation of memory (stack) during function calls
10/18/2012 Lovely Professional University,
Access Links:
Nested procedure definitions (Example language-only looks like C)
fun1() { integer a,b,c; a=2;b=3;c=4; integer fun2() { integer c,d,e; c=5;d=7; e=a+b+c+d; return e; } }
10/18/2012
fun2 needs to access values of a and b c is redefined in fun 2 fun2: Locals : c,d,e fun1: Locals : a,b,c Access link is required C does not require this feature as the scope of variables is within the function definition
Lovely Professional University,
4 xFFD 8 xFFD
C xFFE 0 xFFE
a=2
b=3 c=4 Access link-none Control link to self/driver Returned values-none Actual parameter - none Fixed variables
C
NOTE: 1. Sizes of the activation records for different functions are different 2. Some fields are common in Professional University, records 10/18/2012 Lovely all activation
} } }10/18/2012
Lovely Professional University,
np >=nq
10/18/2012
10/18/2012
10/18/2012
10/18/2012