(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

Implementing Subprograms
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ISBN Chapter 10 Implementing Subprograms.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Run time vs. Compile time
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
ISBN Chapter 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Chapter 9 Subprograms Fundamentals of Subprograms
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
April 23, ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Subprograms - implementation
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Chapter Ten: Implementing Subprograms Lesson 10. Implementing?  Previous lesson: parameter passing  In, out, inout  By value  By reference  Passing.
ISBN Chapter 10 Implementing Subprograms.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
ISBN Chapter 10 Implementing Subprograms.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
COMPILERS Activation Records
Implementing Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms

(2) Objectives To understand the guts of subprogram calls basic procedure of subprogram linkage activation records static and dynamic chaining deep and shallow access

(3) General issues When calling: Save state of calling procedure Determine parameter-passing mechanism for each parameter and arrange access appropriately Allocate storage for local variables Initialize local variable values Arrange transfer of control to procedure Arrange return of control to calling procedure Arrange access to visible non-local variables

(4) General issues (cont.) When Returning: Move final parameter values back to calling arguments (when pass by result) Deallocate storage used for local variables Undo access to visible non-local variables Return control to calling procedure

(5) Activation Records Programs consist of Code, Data, and book- keeping Noncode part is organized in an Activation Record Activation record instances can include Parameter definitions Local variables Functional value Return address Other data to be discussed

(6) Fortran 77 A simple situation: Actual code is fixed at compile time Local variables/data are also of fixed size Subprograms cannot be recursive A simple solution: Local variables statically allocated (static activation records) All referencing of non-local variables is through COMMON: a globally accessible static memory space

(7) Fortran 77 Procedure call: Save the execution status of current procedure Carry out parameter-passing Pass return address to callee Transfer control to callee

(8) Fortran 77 (cont.) Procedure return: If using pass-by-value-result, final values of parameters are moved to arguments If subprogram is a function, final value is moved back Execution status of the caller is restored Control is transferred back to caller

(9) Fortran77 Activation Records No more than one can exist for a procedure AR Instances can be statically allocated Either separate or interleave code and data Code Return address Local variables Code Main Procedure A Parameters Local variables Common

(10) Algol-like Languages New complexities over Fortran 77 Parameters can be passed in different ways (value or reference) Subprogram variables may be dynamically allocated Recursion legal: multiple activation records may exist simultaneously Scope rules (usually static) determine visibility of non-local variables

(11) Algol-like Languages Activation records Format and size is known at compile time since all local variables and their size is statically fixed Instances of activation records are dynamically generated at run-time

(12) Contents of Activation Records Return address Pointer to code segment of caller and offset address of instruction Static link: A pointer to the activation record of static parent Used to support non-local variable access Dynamic link: A pointer to activation record of caller Used in static scoped languages to support procedure return (indicates how much of stack to remove) Used in dynamic scoped languages for non-local variable access

(13) Activation Records (continued) Parameters: Either values or addresses provided by caller Local variables: Scalars stored directly within activation record Non-scalars (structures) stored elsewhere References to locals can be via offset from beginning of record: known at compile time Functional Value (if a function) Place to put the returned value of the function (In some languages the function name can be treated like a local variable)

(14) procedure sub (var tot: real); var sum : real; begin : end; A new activation record is created each time sub is called Run-time Stack holds activation records Stack discipline is appropriate for conventional procedures But not for co-routines Activation Record Example Return Address Local Parameter Dynamic Link Static Link su m tot Start of record Offset of sum }

(15) Factorial Example Return (main) Functional: ? Parameter n=3 Dynamic Link Static Link Local value = ? Return (main) Functional: ? Parameter n=3 Dynamic Link Static Link Local value = ? Return (fact) Functional: ? Parameter n=2 Dynamic Link Static Link Return (fact) Functional: ? Parameter n=1 Dynamic Link Static Link Return (main) Functional: ? Parameter n=3 Dynamic Link Static Link Local value = ? Return (fact) Functional: ? Parameter n=2 Dynamic Link Static Link

(16) Factorial Example Return (main) Functional: 6 Parameter n=3 Dynamic Link Static Link Local value = ? Return (main) Functional: ? Parameter n=3 Dynamic Link Static Link Local value = ? Return (fact) Functional: 2 Parameter n=2 Dynamic Link Static Link Return (fact) Functional: 1 Parameter n=1 Dynamic Link Static Link Return (main) Functional: ? Parameter n=3 Dynamic Link Static Link Local value = ? Return (fact) Functional: ? Parameter n=2 Dynamic Link Static Link

(17) Non-local references Reference to a non-local variable requires a two step process: Find the activation record instance where the variable was allocated Use local offset to access the variable within the record Finding the ARI (under static scoping) is not straightforward

(18) Non-local references program Prog; int i=0; procedure A; int i=1; B(); end; procedure B; int i=2; C(); end; procedure C; i=3; end; A(); end; Show the set of activation records in existence when the statement ‘i=3’ is encountered. Which declaration of ‘i’ will be updated? Following the dynamic chain will find the wrong ‘i’!

(19) Static Chains A link from each procedure’s activation record to its parent’s activation record with respect to static scope The static chain is different from the dynamic chain: The static parent of C is Prog. The dynamic parent of C is B.

(20) Static Chains (continued) No need to search for variable: number of static links is known at compile time (why?) Static Depth: nesting level of scope Chain Offset (Nesting Depth): difference between static depth of reference environment and static depth of declaration environment References represented as (chain_offset, local_offset) Locals can be handled with chain_offset=0

(21) Maintaining Static Chains At subprogram call: Correct parent known at compile time Must find most recent activation record of parent Avoid search of dynamic chain by using existing static chain to find parent (treat procedure reference like variable At return: just remove record from stack

(22) Cost of Static Chains Static chains allow references to non-local variables to be resolved, but at a cost: References to non-local variables “above” parent are slow Time to resolve a reference is difficult to predict, and sensitive to source code changes

(23) Displays: an alternative Static links are in a separate array One element per static depth Exactly two steps per reference: (display_offset, local_offset) When a subprogram at a given static depth is called, save and replace the link at that depth Efficiency compared to Static Chains: Deeply nested references more efficient Maintenance of subprogram calls less efficient Yet most programs are not deeply nested (three in practice)

(24) Display Example Static Depth 0 Display Static Depth 3 Static Depth 2 Static Depth 1Sub1 Sub2 MAINStatic Depth 0 Display Static Depth 3 Static Depth 2 Static Depth 1Sub1 Sub5 Sub4 Sub3 Sub2 MAIN

(25) Dynamic Scoping Deep Access (NOT same as deep binding): References to non-local variables are be resolved by following dynamic links Easy to implement Search down call chain may be slow Activation records must store variable names Shallow Access: Maintain for each variable a stack of its bindings, and reference the top-most one

(26) Implementing Subprogram Parameters Passing Subprograms with Static Chaining Defining parent must be on stack Pass link to static parent and use this to initialize activation record With Displays May need to access multiple static ancestors Some implementations copy and pass the entire display for use by subprogram Correct referencing environment may not be apparent: see example