4500/6500 Programming Languages Name, Binding and Scope Binding
Total Page:16
File Type:pdf, Size:1020Kb
Name, Binding and Scope !! A name is exactly what you think it is CSCI: 4500/6500 Programming »! Most names are identifiers Languages »! symbols (like '+') can also be names !! A binding is an association between two things, such as a name and the thing it names Names, Scopes (review) and Binding »! Example: the association of Reflecting on the Concepts –! values with identifiers !! The scope of a binding is the part of the program (textually) in which the binding is active 1 Maria Hybinette, UGA Maria Hybinette, UGA 2 Binding Time Bind Time: Language System View !! When the “binding” is created or, more !! language design time generally, the point at which any »! bind operator symbols (e.g., * + …) to operations implementation decision is made (multiplication, addition, …) »! Set of primitive types »! language design time »! language implementation time !! language implementation time »! bind data type, such as int in C to the range of »! program writing time possible values (determined by number of bits and »! compile time affect the precision) »! link time –! Considerations: arithmetic overflow, precision of fundamental type, coupling of I/O to the OS’ notion of »! load time files »! run time Maria Hybinette, UGA 3 Maria Hybinette, UGA 4 Bind Time: User View Bind Time: User View !! program writing time !! run time (dynamically) »! Programmers choose algorithms, data structures »! value/variable bindings, sizes of strings and names. »! subsumes !! compile time –! program start-up time »! plan for data layout (bind a variable to a data type in –! module entry time Java or C) –! elaboration time (point a which a declaration is first "seen") !! link time –! procedure entry time »! layout of whole program in memory (names of –! block entry time separate modules (libraries) are finalized. –! statement execution time !! load time »! choice of physical addresses (e.g. static variables in C are bound to memory cells at load time) Maria Hybinette, UGA 5 Maria Hybinette, UGA 6 Static and Dynamic Binding Time Summary !! Typically means before or at run time? !! Early binding times are associated with »! A binding is static if it first occurs before run time greater efficiency and remains unchanged throughout program !! Later binding times are associated with execution. greater flexibility »! A binding is dynamic if it first occurs during execution or can change during execution of the !! Compiled languages tend to have early program. binding times !! Interpreted languages tend to have later binding times (run time) Maria Hybinette, UGA 7 Maria Hybinette, UGA 8 Lifetime and Storage Management Storage Binding and Lifetime Key Events: Need to distinguish between names and the object to which they refer !! Binding Lifetime: time between creation and destruction of a name-to-object binding !! creation of objects !! Object Lifetime: time between creation and !! destruction of objects destruction of an object !! creation of bindings Implications: ! ! references to variables (which use bindings) !! If object outlives binding it's garbage ! ! (temporary) deactivation of bindings !! If binding outlives object it's a dangling !! reactivation of bindings reference !! destruction of bindings Maria Hybinette, UGA 9 Maria Hybinette, UGA 10 Storage Allocation Mechanisms Static Allocation !! Static: absolute address that is retained !! Example: Code, globals, static variables, throughout program execution explicit constants, scalars !! Stack: storage bindings are created when !! Advantages: efficiency (direct addressing), declaration statements are elaborated (e.g., history-sensitive subprogram support (static subroutine calls and returns are allocated in variables retain values between calls of last in first-out order). subroutines). !! Heap: created and destructed by explicit !! Disadvantage: lack of flexibility (does not directives (e.g. new and delete in Java creates support recursion) objects ) Maria Hybinette, UGA 11 Maria Hybinette, UGA 12 Stack Allocation !! Storage bindings are created for variables when their declaration statements are elaborated !! Central stack for parameters, local variables and temporaries »!Easy to allocate space for locals on stack: fixed offset from the stack pointer or frame pointer at compile time !! Advantage: allows recursion; conserves storage !! Disadvantages: »! Overhead of allocation and deallocation »! Subprograms cannot be history sensitive »! Inefficient references (indirect addressing) Maria Hybinette, UGA 13 Heap Allocation Heap Management !! Speed and space tradeoff: !! heap-dynamic - Allocated and deallocated by »! Space fragmentations explicit directives (malloc, new), specified by – Internal - space left in internal blocks the programmer, which take effect during ! –! External - unused space is scattered through heap but not execution one single piece is large enough to satisfy a single request !! Referenced only through pointers or references e.g., dynamic objects in C++ (via new and delete) all objects in Java !! Advantage: provides for dynamic storage management !! Disadvantage: inefficient (instead of static) and unreliable »! Speed - first fit, best fit, buddy systems Maria Hybinette, UGA 15 Maria Hybinette, UGA 16 Scope Rules Static or Lexical Scope !! A scope is a program section of maximal size in which !! Here, scope is defined in terms of the physical no bindings change, or at least in which no re- (lexical) structure of the program declarations are permitted (see below) »! The determination of scopes can be made by the compiler !! In most languages with subroutines, we OPEN a new (bindings are resolved by examining the program text ) scope on subroutine entry: »! Enclosing static scopes (to a specific scope) are called its »! create bindings for new local variables, static ancestors; the nearest static ancestor is called a static »! deactivate bindings for global variables that are re- parent declared (these variable are said to have a "hole" in their scope) – ‘shadowed’ »! Variables can be hidden from a unit by having a “closer” »! make references to variables variable with the same name !! The scope rules (static, dynamic) of a language –! Ada and C++ allow access to these (e.g. class_name:: name) determine how references to names are associated »! Most compiled languages, C and Pascal included, employ with variables static scope rules Maria Hybinette, UGA 17 Maria Hybinette, UGA 18 Creating Static Scopes !! Static scope rules: !! Nested subroutine scope rules (later in »! Most closest nested rule used in blocks chapter 8) C and C++: for (...) !! Access to nonlocal objects { int index; ... } »! To resolve a reference, we examine the local scope and statically enclosing scopes until a binding is found Maria Hybinette, UGA 19 Maria Hybinette, UGA 20 Static Scope Dynamic and Static Scope Rules !! Key idea: in static scope rules »! bindings are defined by the physical (lexical) structure of the program. !! With dynamic scope rules, bindings depend on the current state of program execution »!They cannot always be resolved by examining the program because they are dependent on the calling sequences »!To resolve a reference, we use the most recent, active binding made at run time Maria Hybinette, UGA 21 Maria Hybinette, UGA 22 - First + Second Dynamic Scope Scope Pragmatics (Review) First a: integer // global !! Static scoping: procedure first() !! Interpreted languages { variables always a = 1 // global or local? »!early LISP dialects assumed dynamic refers to its nearest } enclosed binding procedure second scope rules (Perl you can chose static or (between name and { a: integer // local dynamic) object). Compile first() time } »!Later use static – e.g., ML. a = 2 !! Dynamic scoping: if read_integer() > 0 !! Such languages do not normally have binding depends on second() the flow of control else type checking at compile time because at run time and the first() write_integer(a) type determination isn't always order subroutines are called, refers to Static: prints 1 : a is global scope of a is closest enclosed a, so possible when dynamic scope rules are the closest active for “first”’s a refers to global a binding, Dynamic: prints 1 or 2: if we go to second in effect first, first’s a refers to second’s local a (closest binding). Maria Hybinette, UGA 23 Maria Hybinette, UGA 24 Example: Static versus Dynamic Scoping Referencing Environment !! Static scope rules require that the reference !! The referencing environment of a statement is resolve to the most recent, compile-time the collection of all names that are visible in binding, namely the global variable a the statement (e.g., env: remember scheme) »! In a static-scoped language, it is the local variables !! Dynamic scope rules, on the other hand, plus all of the visible variables in all of the require that we choose the most recent, active enclosing scopes binding at run time !! A subprogram is active if its execution has »! Example use: implicit parameters to subroutines begun but has not yet terminated »! This is generally considered bad programming »! In a dynamic-scoped language, the referencing practice nowadays environment is the local variables plus all visible –! Alternative mechanisms exist variables in all active subprograms !! static variables that can be modified by auxiliary routines !! default and optional parameters Maria Hybinette, UGA 25 Maria Hybinette, UGA 26 Dynamic Scope: Accessing Advantages and Disadvantages Variables – Trade-Off Dynamic Scope 1.! keep a stack (association list) of all active variables (slow access,