DOC PREVIEW
UT Dallas CS 4337 - #Sebesta ch10 subprogram imp

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 10Chapter 10 TopicsThe General Semantics of Calls and ReturnsSlide 4Implementing “Simple” SubprogramsImplementing “Simple” Subprograms (continued)Slide 7An Activation Record for “Simple” SubprogramsCode and Activation Records of a Program with “Simple” SubprogramsImplementing Subprograms with Stack-Dynamic Local VariablesTypical Activation Record for a Language with Stack-Dynamic Local VariablesImplementing Subprograms with Stack-Dynamic Local Variables: Activation RecordAn Example: C FunctionRevised Semantic Call/Return ActionsRevised Semantic Call/Return Actions (continued)An Example Without RecursionSlide 17Dynamic Chain and Local OffsetAn Example With RecursionActivation Record for factorial1. Stacks for calls to factorial2. Stacks for returns from factorialNested SubprogramsLocating a Non-local ReferenceStatic ScopingStatic Scoping (continued)Example Ada ProgramExample Ada Program (continued)Stack Contents at Position 1Static Chain MaintenanceBlocksImplementing BlocksImplementing Dynamic ScopingStack contents for a dynamic-scoped programUsing Shallow Access to Implement Dynamic ScopingSummaryChapter 10Implementing SubprogramsCopyright © 2012 Addison-Wesley. All rights reserved. 1-2Chapter 10 Topics•The General Semantics of Calls and Returns•Implementing “Simple” Subprograms•Implementing Subprograms with Stack-Dynamic Local Variables•Nested Subprograms•Blocks•Implementing Dynamic ScopingCopyright © 2012 Addison-Wesley. All rights reserved. 1-3The General Semantics of Calls and Returns•The subprogram call and return operations of a language are together called its subprogram linkage•General semantics of calls to a subprogram –Parameter passing methods–Stack-dynamic allocation of local variables–Save the execution status of calling program–Transfer of control and arrange for the return–If subprogram nesting is supported, access to nonlocal variables must be arrangedCopyright © 2012 Addison-Wesley. All rights reserved. 1-4The General Semantics of Calls and Returns•General semantics of subprogram returns:–In mode and inout mode parameters must have their values returned–Deallocation of stack-dynamic locals–Restore the execution status–Return control to the callerCopyright © 2012 Addison-Wesley. All rights reserved. 1-5Implementing “Simple” Subprograms•Call Semantics:- Save the execution status of the caller- Pass the parameters- Pass the return address to the called- Transfer control to the calledCopyright © 2012 Addison-Wesley. All rights reserved. 1-6Implementing “Simple” Subprograms (continued)•Return Semantics:–If pass-by-value-result or out mode 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: –Status information, parameters, return address, return value for functions, temporariesCopyright © 2012 Addison-Wesley. All rights reserved. 1-7Implementing “Simple” Subprograms (continued)•Two separate parts: the actual code and the non-code part (local variables and data that can change)•The format, or layout, of the non-code 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)Copyright © 2012 Addison-Wesley. All rights reserved. 1-8An Activation Record for “Simple” SubprogramsCopyright © 2012 Addison-Wesley. All rights reserved. 1-9Code and Activation Records of a Program with “Simple” SubprogramsCopyright © 2012 Addison-Wesley. All rights reserved. 1-10Implementing Subprograms with Stack-Dynamic Local Variables•More complex activation record–The compiler must generate code to cause implicit allocation and deallocation of local variables–Recursion must be supported (adds the possibility of multiple simultaneous activations of a subprogram)Copyright © 2012 Addison-Wesley. All rights reserved. 1-11Typical Activation Record for a Language with Stack-Dynamic Local VariablesCopyright © 2012 Addison-Wesley. All rights reserved. 1-12Implementing 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 top of an 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 unitCopyright © 2012 Addison-Wesley. All rights reserved. 1-13An Example: C Functionvoid sub(float total, int part){int list[5]; float sum;…}Revised Semantic Call/Return Actions•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 called–Transfer control to the called•Prologue actions of the called:–Save the old EP in the stack as the dynamic link and create the new value–Allocate local variablesCopyright © 2012 Addison-Wesley. All rights reserved. 1-14Revised Semantic Call/Return Actions (continued)•Epilogue actions of the called:–If there are pass-by-value-result or out-mode parameters, the current values of those parameters are moved to the corresponding actual parameters–If the subprogram is a function, its value is moved to a place accessible to the caller–Restore the stack pointer by setting it to the value of the current EP-1 and set the EP to the old dynamic link –Restore the execution status of the caller–Transfer control back to the callerCopyright © 2012 Addison-Wesley. All rights reserved. 1-15Copyright © 2012 Addison-Wesley. All rights reserved. 1-16An Example Without Recursionvoid fun1(float r) {int s, t;... fun2(s);...}void fun2(int x) {int y;... fun3(y);...}void fun3(int q) {... }void main() {float p;...fun1(p);...}main calls fun1fun1 calls fun2fun2 calls fun3<= r, s, t<= x, y <= q<= pCopyright © 2012 Addison-Wesley. All rights reserved. 1-17An Example Without RecursionCopyright © 2012 Addison-Wesley. All rights reserved. 1-18Dynamic Chain and Local


View Full Document

UT Dallas CS 4337 - #Sebesta ch10 subprogram imp

Download #Sebesta ch10 subprogram imp
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view #Sebesta ch10 subprogram imp and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view #Sebesta ch10 subprogram imp 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?