Function calls & Memory
Flow of Control--
1.The instruction just before the call is executed,
2.The function is called and its body is executed, and
3.The instruction following the function call is executed,
and the execution of the program then continues in its customary top-down fashion. During a function call, control is passed to the callee (the function being called) and returned back to the caller (the function doing the calling) upon termination of the callee. This flow of control requires that the "execution environment" of the caller is somehow preserved and that the "execution environment" of the callee is somehow installed and used for the execution of the callee's body. On the termination of the callee, the execution environment of the callee is somehow disposed of and the previously preserved execution environment of the caller is reinstated.
Activation Frame:-
The data structure to hold the information of this environment is called an activation frame (or activation record), since each call to (or invocation of) a function represents an activation of the function.
Returned value
|
Actual Arguments
|
Optional access Link
|
Optional control link
|
Saved Machine status
|
Local data
|
Temporaries
The returned value field is reserved for the value the function is returning, and this is where the value is stored prior to termination of the function.
The actual arguments field holds the values of, or references to, "objects" being passed to the function.
The optional control link field, if used, points to the activation frame of the caller
the optional access link field, if needed, is used to refer to nonlocal data stored in other activation frames.
The temporaries field holds temporary values (such as those arising from the evaluation of expressions);
the saved machine status field holds the values of system registers (e.g., program counter) at the moment of the call;
The local data field is where all local "objects" are stored.
Allocation strategies for activation frames:-
Generally, there are three ways to allocate (create) the activation frames.
Static allocation -
lays out storage for the activation frame at compile time.The compiler creates "empty" activation frames at compile time and, during the actual function call, only the relevant data are stored there, so the overhead of the function call remains low and thus enables very fast execution. The downside of static allocation is that multiple activations of the same function are not possible; in particular, recursion is not permitted.
Dynamic allocation on the system heap -
each activation frame is dynamically allocated on the system heap at run time during the actual function call.
Dynamic allocation on the system heap may cause the activation frame to "outlive" activation of the function itself or even the caller's activation.So this is not used
Dynamic allocation on the system stack -
Same as above but with the system stack rather than the heap.
Allocation on the system stack benefits from the system stack functioning as a control stack of the function calls: the activation frame is pushed onto the stack when the function is invoked and popped from the stack when the function terminates. Thus, the top of the stack correctly reflects the flow of control and this approach neatly facilitates not only multiple activations of functions but also recursion in its most general form. The disadvantage of this approach lies in the significant overhead associated with (a) the dynamic creation of a new activation frame, (b) pushing it onto the system stack at the beginning of a function call, and (c) popping it from the stack upon termination of the function.
NOTE:C and C++ are languages that follow the third strategy of dynamic allocation of the activation frames on the system stack. As such, C/C++ allows multiple invocation of functions and, in particular, recursion
Calling sequence & Return Sequence:--
calling sequence---
1.The activation frame for the callee is created by the caller.
2.The arguments are evaluated by the caller. For an argument, if passing by value is required, then the value of the argument is stored in the "actual arguments" field; if if passing by reference is required, the pointer to the argument is stored there.
3.The callee saves the machine's status in its activation frame - in particular, the value of the program counter that indicates which instruction should be executed next (i.e., the one right after the call).
4.The callee initializes its local data and starts executing.
return sequence---
1.The callee stores the return value in the "returned value" field in its activation frame.
2.The callee uses the information from the "saved machine status" field and restores the registers for the caller; this includes popping the system stack.
3.The callee branches to the return address of the caller.
4.The caller copies the returned value from the activation frame of the callee. Even though the system stack was "popped", the data is still there because we do not deallocate the memory but only manipulate the register that points to the top of the stack.
5.If a value is returned, then the caller uses it for evaluation of an expression and continues with its normal execution.
Flow of Control--
1.The instruction just before the call is executed,
2.The function is called and its body is executed, and
3.The instruction following the function call is executed,
and the execution of the program then continues in its customary top-down fashion. During a function call, control is passed to the callee (the function being called) and returned back to the caller (the function doing the calling) upon termination of the callee. This flow of control requires that the "execution environment" of the caller is somehow preserved and that the "execution environment" of the callee is somehow installed and used for the execution of the callee's body. On the termination of the callee, the execution environment of the callee is somehow disposed of and the previously preserved execution environment of the caller is reinstated.
Activation Frame:-
The data structure to hold the information of this environment is called an activation frame (or activation record), since each call to (or invocation of) a function represents an activation of the function.
Returned value
|
Actual Arguments
|
Optional access Link
|
Optional control link
|
Saved Machine status
|
Local data
|
Temporaries
The returned value field is reserved for the value the function is returning, and this is where the value is stored prior to termination of the function.
The actual arguments field holds the values of, or references to, "objects" being passed to the function.
The optional control link field, if used, points to the activation frame of the caller
the optional access link field, if needed, is used to refer to nonlocal data stored in other activation frames.
The temporaries field holds temporary values (such as those arising from the evaluation of expressions);
the saved machine status field holds the values of system registers (e.g., program counter) at the moment of the call;
The local data field is where all local "objects" are stored.
Allocation strategies for activation frames:-
Generally, there are three ways to allocate (create) the activation frames.
Static allocation -
lays out storage for the activation frame at compile time.The compiler creates "empty" activation frames at compile time and, during the actual function call, only the relevant data are stored there, so the overhead of the function call remains low and thus enables very fast execution. The downside of static allocation is that multiple activations of the same function are not possible; in particular, recursion is not permitted.
Dynamic allocation on the system heap -
each activation frame is dynamically allocated on the system heap at run time during the actual function call.
Dynamic allocation on the system heap may cause the activation frame to "outlive" activation of the function itself or even the caller's activation.So this is not used
Dynamic allocation on the system stack -
Same as above but with the system stack rather than the heap.
Allocation on the system stack benefits from the system stack functioning as a control stack of the function calls: the activation frame is pushed onto the stack when the function is invoked and popped from the stack when the function terminates. Thus, the top of the stack correctly reflects the flow of control and this approach neatly facilitates not only multiple activations of functions but also recursion in its most general form. The disadvantage of this approach lies in the significant overhead associated with (a) the dynamic creation of a new activation frame, (b) pushing it onto the system stack at the beginning of a function call, and (c) popping it from the stack upon termination of the function.
NOTE:C and C++ are languages that follow the third strategy of dynamic allocation of the activation frames on the system stack. As such, C/C++ allows multiple invocation of functions and, in particular, recursion
Calling sequence & Return Sequence:--
calling sequence---
1.The activation frame for the callee is created by the caller.
2.The arguments are evaluated by the caller. For an argument, if passing by value is required, then the value of the argument is stored in the "actual arguments" field; if if passing by reference is required, the pointer to the argument is stored there.
3.The callee saves the machine's status in its activation frame - in particular, the value of the program counter that indicates which instruction should be executed next (i.e., the one right after the call).
4.The callee initializes its local data and starts executing.
return sequence---
1.The callee stores the return value in the "returned value" field in its activation frame.
2.The callee uses the information from the "saved machine status" field and restores the registers for the caller; this includes popping the system stack.
3.The callee branches to the return address of the caller.
4.The caller copies the returned value from the activation frame of the callee. Even though the system stack was "popped", the data is still there because we do not deallocate the memory but only manipulate the register that points to the top of the stack.
5.If a value is returned, then the caller uses it for evaluation of an expression and continues with its normal execution.
No comments:
Post a Comment