Chapter 10 Implementing Subprograms 1-1 Chapter 10 Topics The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic Local Variables 1-2 The General Semantics of Calls and Returns The subprogram call and return operations of a language are together called its subprogram linkage A subprogram call has numerous actions associated with it Implementation of parameter passing method For non-static local variables, must cause storage allocation and binding of the storage to the variables. Save the execution status of the calling program (register values, CPU status bits, and the environment pointer (EP) Arrange to transfer control to the called subprogram Ensure that control can return to the caller (proper place) Subprogram nesting 1-3 The General Semantics of Calls and Returns (cont.) The required actions of a subprogram returns: Return the out mode or the inout mode formal parameter values to the actual parameters De-allocate the storage used for local variables Restore the execution status of the calling program unit Return the control to the calling program 1-4 Implementing “Simple” Subprograms: Call Semantics By “simple” we mean that subprograms cannot be nested and all local variables are static (i.e. early version of Fortran) Save the execution status of the caller Carry out the parameter-passing process Pass the return address to the callee Transfer control to the callee 1-5 Implementing “Simple” Subprograms: Return Semantics If pass-by-value-result parameters are used, move the current values of those parameters to their corresponding actual parameters If it is a function, move the functional value to a place the caller can get it Restore the execution status of the caller Transfer control back to the caller Required storage for the call and return actions: Status information of caller, parameters, return address, return value for functions, the local variables, and the subprogram code. 1-6 Implementing “Simple” Subprograms: Parts Two separate parts: the actual code and the noncode part (local variables and data that can change) The linkage actions of callee can occure at the beginning of its execution ( prologue) or at the end (epilogue). For simple subprograms, all of the linkage actions of the callee occur at the end of its execution (epilogue) The format, or layout, of the noncode part of an executing subprogram is called an activation record An activation record instance is a concrete example of an activation record (the collection of data for a particular subprogram activation) 1-7 An Activation Record for “Simple” Subprograms The saved execution status of the caller is omitted here 1-8 - Code and Activation Records of a Program with “Simple” Subprograms - The construction of this is done by the help of the linker, which is part of the operating system. 1-9 Linkers (AKA loaders, linker/loaders, or link editors) When the linker is called for a main program, its first task is to find the files that contain the translated subprograms referenced in that program and load them into memory. Then the linker must set the target addresses of all calls in the main program to the entry addresses of those subprograms. The same must be done for all calls inside of the subprograms and all calls to the library subprograms. 1-10 Implementing Subprograms with StackDynamic Local Variables More complex activation record The compiler must generate code to cause implicit allocation and de-allocation of local variables Recursion must be supported (adds the possibility of multiple simultaneous activations of a subprogram) The format of an activation record for a given subpro is known at compile time. In many cases the size is also known at compile time (when all the local data are of fixed size) 1-11 Typical Activation Record for a Language with Stack-Dynamic Local Variables 1-12 Implementing Subprograms with Stack-Dynamic Local Variables: Activation Record The activation record format is static, but its size may be dynamic The dynamic link points to the base of the instance of the activation record of the caller An activation record instance is dynamically created when a subprogram is called Activation record instances reside on the run-time stack The Environment Pointer (EP) must be maintained by the run-time system. It always points at the base of the activation record instance of the currently executing program unit 1-13 An Example: C Function void sub(float total, int part) { int list[4]; float sum; … } 1-14 [4] [3] [2] [1] [0] Environmental Pointer EP (environmental pointer) initially points at the base, or first address of 1-15 the activation record instance of the main program. Subsequently, the run-time system must ensure that it always points at the base of the activation record of the currently executed program unit. When a sub program is called the current EP is saved in the new activation record instance along with the other execution status information. The EP is then set to point at the base of the new activation record instance. Upon return from the subprogram, the EP is restored from the activation record instance. Implementing Subprograms with Stack-Dynamic The caller actions: Create an activation record instance Save the execution status of the current program unit Compute and pass the parameters Pass the return address to the callee Transfer control to the callee The prologue actions of the callee: Save the old EP in the stack as the dynamic link and create the new value Allocate local variables 1-16 Implementing Subprograms with Stack-Dynamic (cont.) The epilogue actions of the callee: If pass-by-value-result or out-more, the current values of those parameters 1-17 are moved to the corresponding actual parameters. If the subprogram is a function, the function value is returned Restore the stack pointer by setting it to the value of the current EP minus one and set the EP to the dynamic link Restore the execution status of the caller Transfer control back to the caller An Example Without Recursion void A(int x) { int y; ... C(y); ... } void B(float r) { int s, t; ... A(s); ... } void C(int q) { ... } void main() { float p; ... B(p); ... } 1-18 main calls B B calls A A calls C An Example Without Recursion 1-19 Dynamic Chain and Local Offset The collection of dynamic links in the stack at a given time is called the dynamic chain, or call chain Local variables can be accessed by their offset from the beginning of the activation record. This offset is called the local_offset The local_offset of a local variable can be determined by the compiler at compile time 1-20 An Example With Recursion The activation record used in the previous example supports recursion, e.g. int factorial (int n) { <-----------------------------1 if (n <= 1) return 1; else return (n * factorial(n - 1)); <-----------------------------2 } void main() { int value; value = factorial(3); <-----------------------------3 } 1-21 Activation Record for factorial 1-22 Copyright © 2009 AddisonWesley. All rights reserved.