1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall 20051.

Slides:



Advertisements
Similar presentations
Chapter 3:: Names, Scopes, and Bindings
Advertisements

Names, Scope, Memory, and Binding. Name, Scope, and Binding A name is exactly what you think it is – Usually think of identifiers but can be more general.
CSC 4181 Compiler Construction Scope and Symbol Table.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Programming Languages and Paradigms
Names and Bindings.
CS 415: Programming Languages Algol Aaron Bloomfield Fall 2005.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 5 Basic Semantics
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
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.
Run time vs. Compile time
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
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 TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Binding Time and Storage Allocation (Section ) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
1 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main goals:Two main goals: –Machine independence –Ease of programming.
1 Subroutines and Control Abstraction Aaron Bloomfield CS 415 Fall 2005.
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Chapter 3 :: Names, Scopes, and Bindings Michael L. Scott School of Computer & Information Engineering, Sangji University Kwangman.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Symbol Table and Subroutine Closures (Sections 3.3 & 3.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos.
Basic Semantics Associating meaning with language entities.
Runtime Organization.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
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
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
Names, Scope, and Bindings Programming Languages and Paradigms.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
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?
Advanced Programming in C
Data Types In Text: Chapter 6.
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
Implementing Subprograms
Run-Time Storage Organization
Run-Time Storage Organization
Names, Scopes, and Bindings: Scopes
Names, Binding, and Scope
Scope, Visibility, and Lifetime
CSC 533: Programming Languages Spring 2015
Binding Times Binding is an association between two things Examples:
Programming Languages
Names and Binding In Text: Chapter 5.
Run Time Environments 薛智文
Abstract data types Programming Language Design and Implementation
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
CSC 533: Programming Languages Spring 2019
Types and Related Issues
Presentation transcript:

1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall 20051

2 Binding A binding is an association between two things, such as a name and the thing it represents Example: int x –When this statement is compiled, x is bound to a memory space

3 Binding Time The time at which the association between two items is created There are many different binding times that can be implemented: –Language design time –Language implementation time –Program writing time –Compile time –Link time –Load time –Run time

4 Binding Times Language design time –These are control flow constructs, primitives, and other semantics –Think of reserved words: i.e. if, for, else, while, etc. Language implementation time –These include I/O couplings –System dependent things such as max heap and stack sizes –Think of constants: MAX_INT, etc. –In Algol, this included the I/O procedure names

5 Binding Times Program writing time –When you the programmer choose data structures and names Compile time –The compiler maps high level constructs to machine code –Think of the assembly for a compiled procedure Link time –Separate compilation means not every part of a program has to be compiled at the same time –Libraries are linked to your program and bound

6 Binding Times Load time –For primitive operating systems –Binds physical addresses –Most OSes do this during link time with virtual addresses Run time –Very broad term covering the span of execution –Values to variable bindings occur here

7 Binding Times Very important to design and implementation of programming languages –Early bindings = greater efficiency –Later bindings = greater flexibility

8 Scope rules The textual region of the program in which a binding is active is its scope Static scoping is more common in modern programming langauges –Found in C, C++, Pascal

9 Static scope All scoping can be determined at compile time Most often this is done with a textual top-to- bottom scan Most basic static scope rule: everything has global scope

10 Fortran Implementation Global scopes Local scopes limited to their subroutines Local variable scope extends through the execution of the subroutine unless the variable is saved

11 Nested Subroutines Introduced in Algol 60 and is found in some modern languages –Pascal, Ada Subroutines can be declared inside other subroutines –This confuses the scope rules even more

12 Closest Nested Scope Rule A variable declared has its scope in the current subroutine, and any internally nested subroutines, unless there is a definition with the same name more locally

13 Closest Nested Scope Rule Example procedure level1() variable x; procedure level2() variable x; begin (* level 2 code *) end begin (* level 1 code *) end procedure outside() begin (*outside code*) end

14 Dynamic Scope Rules Generally less complicated to implement The “current” binding for a name is the one most recently encountered during execution, and not yet destroyed by returning from its scope APL, Snobol, early dialects of Lisp, and Perl have dynamic scoping

15 Implications Semantic rules are dynamic rather than static –Type checking is generally deferred until run time To accommodate these rules, dynamically scoped languages are most often interpreted Very easy for an interpreter to look up the meaning of a name –All that is needed is a stack of declarations However, it makes it harder to understand programs –The modern consensus seems to be that dynamic scoping is usually a bad idea

16 Dynamic scope example int print_base = 10; print_integer (int n) { switch (print_base) { case 10:... case 8:... case 16:... case 2:... } foo { print_integer (10); int print_base = 16; print_integer (10); print_base = 2; print_integer (4); }

17 Alternatives to achieve the same result Overloading and optional parameters –print_integer takes an optional parameter (one with a default value) that specifies the base –print_integer is overloaded: one form prints in decimal, the other form takes an additional parameter that specifies the base Better or worse than dynamic scoping?

18 More implications With dynamic scoping, no program fragment that makes use of non-local names is guaranteed a predictable referencing environment

19

20 Referencing Environments A referencing environment is the complete set of bindings in effect at a given point in a program Static scope rules: –Referencing environment dependent on lexical nesting of program blocks Dynamic scope rules: –Referencing environment dependent on the order in which declarations are reached

21 What about references to functions? When are dynamic scope rules applied? –When the function is called? Shallow binding –When the reference is created? Deep binding

22 int max_score; float scale_score (int raw_score) { return (float) raw_score / (float) max_score; } float highest_score (int[] scores, function_ptr scaling_function) { float max_score = 0; foreach score in scores { float percent = scaling_function (score); if ( percent > max_score ) max_score = percent; } return max_score; } main() { max_score = 50; int[] scores =... print highest_score (scores, scale_score); } function is called reference is created

23 Deep Binding Generally the default in lexically scoped languages Dynamically scoped languages tend to use shallow binding

24 Implementing Deep Binding Subroutine Closure

25 The Symbol Table During compilation, keeps track of all the identifiers Only matters in a statically scoped language! –Why? Basic functions: lookup() and insert() Must keep track of scope as well –Often by enter_scope() and leave_scope() functions

26 Activation records When a subroutine starts, a number of things have to be stored: –A copy of the parameters (pass by value) –The return address when the subroutine exits –Local variables in the subroutine –Etc. This data is called the activation record Fortran (up until 90) only had space for one activation record –Thus, you couldn’t call more than one subroutine at a time

27 Recursive vs. Iterative Ocaml will recognize tail recursion and convert it to a while loop: let foo x = if x = 0 then 0 else 1 + foo (x-1);; A call to foo would require one million activation records if called recursively –That’s many, many megs of memory! –In addition to the time spent creating them and branching to the next recursive call of the subroutine An iterative solution avoids all that –Although there is still the branch for the loop

28 Lifetimes Lifetime = time from creation to deletion Bindings Objects

29 Storage Management Static - absolute address, retained throughout program Stack - LIFO, subroutine calls Heap – allocation/deallocation at arbitrary times

30 Heap Management Free list

31 Who Manages the Heap? User System

32 Garbage Collection Mark and Sweep Reference Counting

33 Aliases