A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED 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

Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
1 Chapter 8 – Subroutines and Control Abstractions.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: 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.
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.
ISBN Chapter 10 Implementing Subprograms.
Run time vs. Compile time
Semantics of Calls and Returns
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
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.
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.
Scope.
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.
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.
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)
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
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.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
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.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
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
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
PZ09A - Activation records
Implementing Subprograms
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
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:

A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS

Topic in this Lecture Nested Subprograms (Sec. 10.4) Blocks (Sec. 10.5) Implementing Dynamic Scoping (Sec. 10.6) 2

Nested Subprograms Some languages (e.g., Fortran 95, Ada, Python, JavaScript, Ruby) use stack-dynamic local variables and allow subprograms to be nested procedure A is procedure B is procedure C is end; -- of C end; -- of B end; -- of A 3

Nested Subprograms How to access variables that are non-local but are defined in outer subprograms? These variables must reside in some AR instances deep in the stack The process of locating a non-local reference: Find the correct activation record instance down in the stack: hard Determine the correct offset within that activation record instance: easy 4

Finding Correct AR Instance Static scope semantics: Only variables that are declared in static ancestor scope are visible and can be accessed All non-local variables that can be referenced have been allocated in some AR instance on the stack when the reference is made Idea: chain AR instances of static ancestors 5

Static Chain Static link in an AR instance points to bottom of AR instance of the static parent Static chain connects all static ancestors of an executing subprogram, static parent first Can find correct AR instance following the chain But, can be even easier, because nesting of scopes is known at compile time and thus the length of static chain to follow 6

Following Static Chain Static_depth: depth of nesting of a static scope Chain_offset or nesting_depth of a nonlocal reference is the difference between static_depth of the reference and that of the declare scope A reference to a variable can be represented by: (chain_offset, local_offset), where local_offset is the offset in the activation record of the variable being referenced 7

For example, consider the following skeletal Python program: # Global scope... def f1(): def f2(): def f3():... # end of f3... # end of f2... # end of f1

`-`- The static_depths of the global scope, f1, f2, and f3 are 0, 1, 2, and 3, respectively. If procedure f3 references a variable declared in f1, the chain_offset of that reference would be 2 (static_depth of f3 minus the static_depth of f1). If procedure f3 references a variable declared in f2, the chain_offset of that reference would be 1. References to locals can be handled using the same mechanism, with a chain_offset of 0, but instead of using the static pointer to the activation record instance of the subprogram where the variable was declared as the base address, the EP is used.

Example Ada Program procedure Main_2 is X : Integer; procedure Bigsub is A, B, C : Integer; procedure Sub1 is A, D : Integer; begin -- of Sub1 A := B + C; < end; -- of Sub1 procedure Sub2(X : Integer) is B, E : Integer; procedure Sub3 is C, E : Integer; 10 begin -- of Sub3 Sub1; E := B + A: < end; -- of Sub3 begin -- of Sub2 Sub3; A := D + E; < end; -- of Sub2 } begin -- of Bigsub Sub2(7); end; -- of Bigsub begin Bigsub; end; of Main_2 }

Stack Contents at Position 1 11 Main_2 calls Bigsub Bigsub calls Sub2 Sub2 calls Sub3 Sub3 calls Sub1 Reference to variable A: Position 1: (0,3) Position 2: (2,3) Position 3: (1,3)

Static Chain Maintenance At the call, AR instance must be built The dynamic link is just the old stack top pointer The static link must point to the most recent AR instance of the static parent Two methods: 1. Search the dynamic chain to find the parent scope 2. When compiler encounter a subprogram call, it finds its static parent and records the nesting_depth from that parent to itself. When that subprogram is called, its static link can be found starting from the caller’s static link and the number of nesting_depth 12

Evaluation of Static Chains Problems: A nonlocal reference is slow if the nesting depth is large Time-critical code is difficult: Costs of nonlocal references are difficult to determine Code changes can change the nesting depth, and therefore the cost 13

Displays An alternative to static chains that solves the problems with that approach Static links are stored in a single array called a display The contents of the display at any given time is a list of addresses of the accessible activation record instances 14

Outline Semantics of Calls and Returns (Sec. 10.1) Implementing “Simple” Subprograms (Sec. 10.2) Implementing Subprograms with Stack-Dynamic Local Variables (Sec. 10.3) Nested Subprograms (Sec. 10.4) Blocks (Sec. 10.5) Implementing Dynamic Scoping (Sec. 10.6) 15

Blocks User-specified local scopes for variables {int temp; temp = list [upper]; list [upper] = list [lower]; list [lower] = temp } The lifetime of temp in the above example begins when control enters the block An advantage of using a local variable like temp is that it cannot interfere with any other variable with the same name 16

Two Methods Implementing Blocks Treat blocks as parameter-less subprograms that are always called from the same location Every block has an activation record; an instance is created every time the block is executed Put locals of a block in the same AR of the containing subprogram Since the maximum storage required for a block can be statically determined, this amount of space can be allocated after the local variables in the activation record 17

Implementing Dynamic Scoping 1-18 Deep Access: non-local references are found by searching the activation record instances on the dynamic chain - Length of the chain cannot be statically determined - Every activation record instance must have variable names Shallow Access: put locals in a central place One stack for each variable name Central table with an entry for each variable name

Copyright © 2009 Addison-Wesley. All rights reserved void sub3() { int x, z; x = u + v;... } void sub2() { int w, x;... } void sub1() { int v, w;... } void main() { int v, u;... } Suppose the following sequence of function calls occurs: main calls sub1 sub1 calls sub1 sub1 calls sub2 sub2 calls sub3

Copyright © 2009 Addison-Wesley. All rights reserved. 1-20

Using Shallow Access to Implement Dynamic Scoping Copyright © 2009 Addison-Wesley. All rights reserved.1-21 void sub3() { int x, z; x = u + v; … } void sub2() { int w, x; … } void sub1() { int v, w; … } void main() { int v, u; … }

Summary Subprogram linkage semantics requires many action by the implementation Stack-dynamic languages are more complex and often have two components Actual code Activation record: AR instances contain formal parameters and local variables among other things Static chains are main method of implementing accesses to non-local variables in static-scoped languages with nested subprograms 22