Name, Binding and

!! 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 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 with variables »! Most compiled languages, C and Pascal included, employ 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, fast calls) !! Advantages: »! Simple implementation for interpreted languages »! hunt down from top of stack to find a variable »! Lack of static structure (e.g., Unix environment –! This is equivalent to searching the activation records on variables) the dynamic chain 2.! keep a central table with one slot for every !! Disadvantages: variable name (fast lookup, slow calls) »! Confusing, better to use static variables, default parameters »! If names cannot be created at run time, the table layout (and the location of every slot) can be fixed at compile time –! Otherwise, you'll need a hash function or something to do lookup »! Every subroutine changes the table entries for its locals at entry and exit. Maria Hybinette, UGA 27 Maria Hybinette, UGA 28

Some Binding Rules Binding within a Scope

!! Recall that a referencing environment of a statement at !! Aliasing run time is the !! Overloading »! set of active bindings. »! A referencing environment corresponds to a collection of »! operator overloading scopes that are examined (in order) to find a binding. »! function overloading !! Scope rules: determine that collection and its order »! polymorphism !! Binding rules: determine which instance of a scope »! generic functions should be used to resolve references when calling a procedure that was passed a parameter !! Modules »! they govern the binding of referencing environments to »! between compilations formal procedures

Maria Hybinette, UGA 29 Maria Hybinette, UGA 30 Aliasing Overloading

!! What are aliases good for? (consider uses of !! some overloading happens in almost all FORTRAN equivalence) languages –! space saving - modern data allocation methods are better »! integer + v. real + –! multiple representations - unions are better »! read and write in Pascal –! linked data structures - legit »! function return in Pascal !! Also, aliases arise in parameter passing as an !! some languages get into overloading in a big unfortunate side effect way –! Euclid scope rules are designed to prevent this »! Ada (see Figure 3.18 for examples) !! In general aliases tend to make programs »! C++ (see Figure 3.19 for examples) more confusing and more difficult for compiler to perform code improvements

Maria Hybinette, UGA 31 Maria Hybinette, UGA 32

Polymorphism (having many Overloaded functions forms)

!! ad-hoc polymorphism overloading, coercion (finite types) !! subtype polymorphism – classes related by a super type. !! overloaded functions - two different things »! e.g., in OO languages allow parameters to have different types in the same type hierarchy by calling virtual functions appropriate to the with the same name; in C++ concrete type of the actual parameter. !! parametric polymorphism : »!overload norm »! Explicit (generic) int norm (int a){return a>0 ? a : -a;) –! Syntactic template that can be instantiated in more than one way at compile time complex norm (complex c ) { // ... !! specify parameters when you declare or use generic !! Templates in C++ !! ad hoc polymorphism !! Macro expansion –! Generic Programming (arguably): Only one definition (e.g., templates, macro, note instantiation is laziness, “not evaluated until needed” characteristics »! Implicit (true) –! Don’t have to specify types for which code works, language implementation figures it out and won’t let you perform operations on object that do not support them Maria Hybinette, UGA 33 Maria Hybinette, UGA 34 –! Lisp (run time), ML (compiler)

Polymorphism (review) Separate Compilation

!! Universal polymorphism : Allows writing code that works with different types !! Separately-compiled files in C provide !! Ad-hoc polymorphism : Selecting the right implementation a sort of poor person's modules: code to be executed »!Rules for how variables work with separate Parametric (e.g., C compilation are messy ++ templates) Universal »!Language has been jerry-rigged to match the behavior of the linker Inclusion() »!Static on a function or variable outside a Polymorphism function means it is usable only in the current source file Overloading/Overriding (e.g., C ++, Java) –! This static is a different notion from the static Ad hoc variables inside a function (retains, persistant value) Coercion (automatic -multi-method, Maria Hybinette, UGA single dispatch) 35 Maria Hybinette, UGA 36 Separate Compilation (cont) Separate Compilation (cont)

»!Extern on a variable or function means »! Variables or functions (with bodies) that don't say that it is declared in another source file static or extern are either global or common (a Fortran term) »!Functions headers without bodies are –! Functions and variables that are given initial values extern by default are global » Extern declarations are interpreted as –! Variables that are not given initial values are ! common forward declarations if a later declaration »! Matching common declarations in different files overrides them refer to the same variable –! They also refer to the same variable as a matching global declaration

Maria Hybinette, UGA 37 Maria Hybinette, UGA 38

Summary Conclusion

!! The morals of the story: !! A language that is easy to compile often leads to »! a language that is easy to understand »!language features can be surprisingly subtle »! more good compilers on more machines (compare »!designing languages to make life easier for Pascal and Ada!) the compiler writer can be a GOOD THING »! better (faster) code »!most of the languages that are easy to »! fewer compiler bugs understand are easy to compile, and vice »! smaller, cheaper, faster compilers versa »! better diagnostics

Maria Hybinette, UGA 39 Maria Hybinette, UGA 40

Next Week…

!! Project 3 (ML prime numbers, should not take long) !! Exam 1 discussed in detail. !! Contest period – one week after turning back exams… (after that Spring Break…).

Maria Hybinette, UGA 41