name binding and scope
play

Name, Binding and Scope A name is exactly what you think it is - PDF document

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,


  1. 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 2 Maria Hybinette, UGA Maria Hybinette, UGA Binding Time Bind Time : Language System View � � language design time � � When the “binding” is created or, more generally, the point at which any » � bind operator symbols (e.g., * + …) to operations (multiplication, addition, …) implementation decision is made » � 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 3 4 Maria Hybinette, UGA Maria Hybinette, UGA 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 – � module entry time » � plan for data layout (bind a variable to a data type in 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) 5 6 Maria Hybinette, UGA Maria Hybinette, UGA

  2. Static and Dynamic Binding Time Summary � � Early binding times are associated with � � Typically means before or at run time? greater efficiency » � A binding is static if it first occurs before run time and remains unchanged throughout program � � Later binding times are associated with execution. greater flexibility » � A binding is dynamic if it first occurs during � � Compiled languages tend to have early execution or can change during execution of the program. binding times � � Interpreted languages tend to have later binding times (run time) 7 8 Maria Hybinette, UGA Maria Hybinette, UGA Lifetime and Storage Management Storage Binding and Lifetime Key Events: Need to distinguish between names � � Binding Lifetime: time between creation and and the object to which they refer 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 9 10 Maria Hybinette, UGA Maria Hybinette, UGA 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 ) 11 12 Maria Hybinette, UGA Maria Hybinette, UGA

  3. 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) 13 Maria Hybinette, UGA 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 15 16 Maria Hybinette, UGA Maria Hybinette, UGA 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 17 18 Maria Hybinette, UGA Maria Hybinette, UGA

  4. Creating Static Scopes � � Static scope rules: � � Nested subroutine scope rules (later in chapter 8) » � Most closest nested rule used in blocks � � Access to nonlocal objects C and C++: for (...) { int index; ... } » � To resolve a reference, we examine the local scope and statically enclosing scopes until a binding is found 19 20 Maria Hybinette, UGA Maria Hybinette, UGA 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 21 22 Maria Hybinette, UGA Maria Hybinette, UGA - First + Second Dynamic Scope Scope Pragmatics (Review) First a: integer // global � � Static scoping: procedure first() � � Interpreted languages { variables always a = 1 // global or local? refers to its nearest » � early LISP dialects assumed dynamic } 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 first() at run time and the write_integer(a) type determination isn't always order subroutines Static: prints 1 : a is global scope are called, refers to possible when dynamic scope rules are of a is closest enclosed a, so the closest active for “first”’s a refers to global a binding, in effect Dynamic: prints 1 or 2: if we go to second first, first’s a refers to second’s local a (closest binding). 23 24 Maria Hybinette, UGA Maria Hybinette, UGA

Recommend


More recommend