19. Call – by – Name1.Call – by – Value: Call – by – value is the simplest method of passing parameters. The actual parameters are evaluated and their r – values are passed to the called procedure. This method is used in pascal and C. l- value: It refers to the storage represented by an expression. r-value: It refers to the value contained in the storage. Call – by – value can be implemented as follows: (i)A formal parameter to treated just like a local name, So the storage for the formals is in the activation record of the called procedure. (ii)The caller evaluates the actual parameters and places their r-values in the storage for the formals. 2.Call – by – reference: This method is otherwise called as call-by-address or call-by-location. The caller passes a pointer to each location of actual parameters. It an actual parameter is a name or an expression having an l-value then that l-value itself is passed. However, if the actual parameter is an expression like a +b or 5, that has no l-value, than the expression is evaluated in a new location and the address of that location is passed. 3.Copy – Restore: This method is a hybrid between Call – by – value and Call – by – reference. This is also known as copy – in – copy – out or value reset. This Calling procedure calculates the value of the actual parameter and it is then copied to activation record for the called procedure. The l – values of these actual parameters having l-values are determined before the call. When control returns, the current r- values of the formal parameters are copied back into the l-values of the actual, using the i-values computed before the call. 4. Call – by – Name: This procedure is treated like a macro, that is, its body is substituted for the call in the caller, with the actual parameters literally substituted for the formals. Such a literal substitution is called macro- expansion or in- line expansion. The local names of the called procedure are kept distinct from the names of the calling procedure. The actual parameters are surrounded by parenthesis if necessary to presence their integrity. 6.Storage allocation strategies: A different storage – allocation strategy is used in run – time memory organization. They are, 1.Static allocation: Lays out storage at compile time for all data objects. 2.Stack allocation: Manages the run time storage. 3.Heap allocation: Allocates and de-allocates storage as needed at run time from heap. These allocation strategies are applied to allocate memory for activation records. Different languages use different strategies for this purpose. For eg: FORTRANStatic allocation Algol Stack allocation LISP Heap allocation 1.Static allocation: The fundamental characteristics of static allocation are as follows: (i)Name binding occurs during compilation there is no need for a run-time support package. (ii)Bindings do not change at run time. (iii)On every invocation of procedure, its names are bound to the same storage locations. When control returns to a procedure, the values of the locals are the same as they were when control left the last time.(ie., this property allows the values of local names to be retained across activations of a procedure). Eg: Function F() { int a; Print (a); a=10; } After calling F() once, if it was called a second time, the value of ‘a’ would initially be 10, and this will be printed. (iv)The type of a name determines its storage requirement. The address for this storage is an offset from the procedures activation record and the compiler must decide where the activation records go, relative to the target code and to one another. After this position has been decided, the address of the activation records and hence of the storage for each name in the records are fixed. Thus at compile time, the addresses at which the target code can find the data it operates upon can be filled in. The addresses at which information is to be saved when a procedure call takes places are also known at compile time. Static allocation have some limitations are: (i)Size of data objects, as well as any constraints on their positions in memory, must be available at compile time. (ii)No recursion, because all activations of a given procedure use the same bindings for local names. (iii)No dynamic data structures, since no mechanism is provided for run-time storage allocation. 2.Stack allocation: It is based on the idea of a control stack. Storage is organized as 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. Thus locals are bounds to fresh storage in each activation, because a new activation record is pushed on to the stack when a call is made. The values of locals are deleted when the activation ends. ie., the values are lost because the storage for locals disappears when the activation record is popped. Eg: Activation tree, DIAGRAM Calling Sequences: A call sequence allocates an activation records and enters information into its fields. A return sequences and activation records differ, even for the same language. The code in the calling sequence is often divided between the calling procedure and the procedure it calls. There is no exact division of run time tasks between the caller and the callee. DIAGRAM The register stack top points to the end of the machine status field in the activation records. This position is known to the Caller, So it can be made responsible for setting up stack top before control flows to the called procedure. This code for the callee can access its temporaries and the local data using offsets from stack top. The call sequence is: Caller: Evaluates actual Stores return address & old values of top-SP Increments top-SP Callee: Saves register values & other status information. Initializes local data & begins execution. The return sequence is Callee: Places return value next to callers Activation Record. Restores top-SP & other registers. Branches to return address. Caller: Copies returned value into its own Activation Record. Limitations of Stack allocation: Values of locals cannot be retained when activation ends. A called activation cannot outlive the caller. 3. Heap Allocation: Limitations of stack allocation are mentioned already, in those cases de-allocation of Activation records cannot occur in last in first out fashion. Heap gives out pieces of contiguous storage for activation records. 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 tree blocks of that size. Fill a request of size S with block of size S where S is the smallest size greater than or equal to S. DIAGRAM For large blocks of storage use heap manager. For large amount of storage computation may take some time to use up the memory So that time taken by the manager be negligible compared to the computation. Heap manage will dynamically allocate memory. This will come a run time overhead. As heap manager will have to take care of defragmentation and garbage collection.