Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Run-Time Storage Organization
Run time vs. Compile time
Chapter 9: Subprogram Control
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.
Chapter 8 :: Subroutines and Control Abstraction
CS 2104 Prog. Lang. Concepts Subprograms
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
Chapter 8 - Control II: Procedures and Environments
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
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.
Basic Semantics Associating meaning with language entities.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
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
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
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
Functions.
Names and Attributes Names are a key programming language feature
Run-Time Storage Organization
Run-Time Storage Organization
Names.
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Names, Scopes, and Bindings: Scopes
Implementing Subprograms
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Run Time Environments 薛智文
CSE 3302 Programming Languages
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
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
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Presentation transcript:

Names and Scope

Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name is used, to which of these entities does it refer?

Types of names Variables Constants Statement labels Functions/procedures Types Macros Exceptions Modules (packages, files, etc.)

Postponing Names of modules and references between modules (to lecture on modules) Variables used in functions passed as parameters and returned as values (to lecture on functional programming) Exceptions (to lecture on exceptions).

Easy cases Types and macros are (as far as I know) global to a module. Statements labels are scoped within the function, and are visible in that functions and in lexically contained functions.

Function names In non-hierarchical languages (e.g. C), function names are global to the module. Some languages require that a function be declared lexically before it is used; others do not. If you impose this rule for mutually recursive functions, then there must be declarations that are separate from definition.

Function names: Hierarchical languages Closest nested scope: Suppose that function C is lexically inside B which is lexically inside A, and C calls function F. In order look for a declaration of F immediately inside first C, then B, then A, then global. Otherwise, the reference is invalid.

Example (Pascal) function A() function F() begin … end // Body of F 1 function B() function F() begin … end // Body of F 2 function C() begin … F() … end // Body of C. Call to F 2 begin … F() … end // Body of B. Call to F 2 function D() begin … F() … end // Body of D. Call to F 1 begin … F() … end // Body of A. Call to F 1

Scope of Function Name Nothing particularly interesting has to be done in terms of the compiled code. The compiler uses the scope rule to assign the reference, but the calling and returning protocols are unaffected.

Scope of Variables Static scope (also called “lexical scope”). As with functions, use the nearest containing scope that declares the variable. Note: The reference can be determined at compile time. Dynamic scope. Use the most recent declaration in the calling sequence

Static and dynamic scoping: Example int N; void main() {N=1; A(); printf(“main: %1d”,N);} void A() int N; { N = 2; B(); printf(“A: %1d. ”, N); } void B() { N = 3; } Static scoping: Prints “A: 2. main: 3” (I in B is global) Dynamic scoping: Prints “A: 3. main: 1” (I in B is I in A)

Implementing static scoping: no function imbedding Trivial: Variables are either local (in current activation record) or global (fixed address).

Implementing static scoping: function imbedding Each activation record on the stack has a static link to the activation record of the most recent call to the routine that lexically contains it. To resolve a reference to a variable in a routine K levels out from the current routine, follow the chain of static links K steps to the proper activation record, and then go the appropriate offset. Number of links and offset are known at compile time.

function A() { int I; function B() { int J; function C() { int K; K=I+J; B(); } C(); } // body of B. B(); } // body of A. A calls B calls C calls B calls C.

Setting static links in calling protocol If F calls G and G is immediately lexically contained in H, then (by the scoping rules for functions) either –F=H, in which case the static link from G points to the static link in F’s activation record –or F is lexically contained in H (not necessarily immediately), in which case the link to H is a known number of steps down the static chain from F. In either case, the compiler can determine how F should set the static link for G.

Implementing dynamic scoping: symbol table Symbol table: For each variable name X, keep a pointer to the most recent version of X. If function F declares X, then F must store a pointer to the previous version of X. On entering routine F, push a pointer to previous location of X onto stack, On exiting F, restore previous value of X to symbol table.

Example function A() { int I,J; B(); } function B() { int J,K; C(); } function C() { int I,K B(); } A calls B calls C calls B.

Declarations and definitions Is declaration separate from definition? If a name is declared, is the scope the entire context, or only after it is declared? Can a name be used before it is declared? Particularly important for mutually recursive types and function definitions. In weird cases, it can be important for constant definitions.

Mutually recursive functions: Pascal forward procedure B(I: integer) // declaration procedure A(I: integer) // declaration and begin B(I-1) end // definition procedure B(I: integer) // definition begin A(I-1) end

Mutually recursive functions: C void B(int I); // declaration void A(int I) { B(I-1); } // declaration and // definition void B(int I) { A(I-1); } // definition Ada is similar.

Recursive types: Pascal In defining a pointer type P, you can refer to another type T before T has been declared (because nothing about P, such as the size, actually depends on T). type PRED = ^RED; PBLACK = ^BLACK; RED = record V,D: Integer; L,R: PBLACK end; BLACK = record V,D: Integer; L,R: PRED end;

Recursive types: C Separate declaration from definition, like functions. struct RED; struct BLACK { int V, P; struct RED *L, *R; } struct RED { int V,P; struct BLACK *L, *R; }

Constant anomaly in Pascal (and some other languages) Scope of a declaration is the entire block. Declarations must precede use. const N=10; procedure f; const M=N; N=20; In “M=N”, N refers to the declaration in f; hence it is used before it is declared.