isbn 0-321-33025-0 chapter 10 implementing subprograms

Download ISBN 0-321-33025-0 Chapter 10 Implementing Subprograms

If you can't read please download the document

Upload: clifford-scott

Post on 18-Jan-2018

262 views

Category:

Documents


6 download

DESCRIPTION

Copyright © 2006 Addison-Wesley. All rights reserved.1-3 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 –Parameter passing methods –Static local variables –Execution status of calling program –Transfer of control –Subprogram nesting

TRANSCRIPT

ISBN Chapter 10 Implementing Subprograms Copyright 2006 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing Dynamic Scoping Copyright 2006 Addison-Wesley. All rights reserved.1-3 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 Parameter passing methods Static local variables Execution status of calling program Transfer of control Subprogram nesting Copyright 2006 Addison-Wesley. All rights reserved.1-4 Implementing Simple Subprograms: Call Semantics Save the execution status of the caller (current program unit). Carry out the parameter-passing process. Pass the return address to the callee. Transfer control to the callee. Copyright 2006 Addison-Wesley. All rights reserved.1-5 Implementing Simple Subprograms: Return Semantics If there are out-mode parameters are used, the current values of those parameters are moved to their corresponding actual parameters. If the subprogram is a function, the functional value is moved to a place accessible to the caller. Restore the execution status of the caller Transfer control back to the caller Copyright 2006 Addison-Wesley. All rights reserved.1-6 Implementing Simple Subprograms: Parts A simple subprogram consists of Two separate parts: the actual code the noncode part (local variables and data that can change) 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). Because languages with simple subprograms do not support recursion, there can be only one active version of a given subprogram at a time. Copyright 2006 Addison-Wesley. All rights reserved.1-7 An Activation Record for Simple Subprograms One possible layout for activation record instance is shown here. Copyright 2006 Addison-Wesley. All rights reserved.1-8 A program consisting of a main program and three subprograms: A, B, C Copyright 2006 Addison-Wesley. All rights reserved.1-9 Implementing Subprograms with Stack-Dynamic Local Variables More complex activation record than simple subprogram. The compiler must generate code to cause implicit allocation and de-allocation of local variables Recursion must be supported There can be more than one instance (incomplete execution) of a subprogram at a given time. Copyright 2006 Addison-Wesley. All rights reserved.1-10 Typical Activation Record for a Language with Stack- Dynamic Local Variables The return address, dynamic link, and parameters are placed in the activation record by the caller, these entries must appear first. Copyright 2006 Addison-Wesley. All rights reserved.1-11 Implementing Subprograms with Stack-Dynamic Local Variables: Activation Record The return address is a pointer to the code segment of the caller. The dynamic link is a pointer to the top of the activation record of the caller. An activation record instance is dynamically created when a subprogram is called Local variables are bound to storage within an activation record. Local variables that are structures are sometimes allocated elsewhere, and a pointer to that storage is apart of the activation record. Copyright 2006 Addison-Wesley. All rights reserved.1-12 An Example: C Function void sub(float total, int part) { int list[5]; float sum; } Copyright 2006 Addison-Wesley. All rights reserved.1-13 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);...} main calls B B calls A A calls C Copyright 2006 Addison-Wesley. All rights reserved.1-14 An Example Without Recursion Copyright 2006 Addison-Wesley. All rights reserved.1-15 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 Copyright 2006 Addison-Wesley. All rights reserved.1-16 An Example With Recursion Consider the following C, which uses recursion to compute factorial. int factorial (int n) { < if (n