Binding = fixing a value or some other property of an object from set of possibilities
MAKING A DECISION
Example: Bind variable to location and value
Time of making a decision is called binding time
Possibilities: Execution, translation, language implementation, language definition
Dynamic
Execution:
Translation:
Language Def: Structure of language, possible types, rep of values in program text.
Example: When is meaning of "+" bound to its meaning in "x + 10"?
"Integer" may be redefined in Pascal, but not FORTRAN or Ada.
Why care about binding time?
Early vs. late binding - many language design decisions relate to binding time
Early binding supports compilation, late binding -> interpretation
Small changes may delay binding time -
Ex: recursion forces delay in binding time for local variables to locations
As work down layers in examining or translating language, may find able to make more binding, e.g., by constant propagation - support optimizers.
Bindings are maintained in structures both at compile and at run-time.
During compilation, declarations stored in Symbol table.
Other attributes are needed at execution-time:
Run-time environment keeps track of meanings of names:
Memory: Locations -> Values
With interpreter, just keep it all 3 sets of values together in one Environment.
(Notice that your homework interpreter has no run-time environment since there are no identifiers being interpreted yet!)
Variable has 6 components
Scope and Lifetime same in some languages - clearly different in some (FORTRAN)
N := N + 1<First N refers to location (l-value), second to value (r-value).
Obtaining value of variable called dereferencing. (going from reference or location to value)
E.g., name can change (via call by reference parameter)
Aliasing:
Similarly assignment of variables (e.g., x := y) can be by copying or by sharing.
In copy semantics, target variable retains its location, but copies new value from source variable.
In sharing semantics, target variable gets location of source variable, so both share the same location (like objects in Java).
Constants have values, but no location.
One way of classifying a language is according to sorts of entities that can be bound to an identifier. Called denotable values.
In Pascal, objects which can be bound to id's and corresponding declarations are:
Static vs Dynamic
Scope is associated with the static text of program.
Can determine scope by looking at structure of program rather than execution path telling how got there.
May have holes in scope of variable
program ... var M : integer; .... procedure A ... var M : array [1..10] of real; begin ... end; begin ... end.Variable M declared in main program is not visible in procedure A, since new declaration of M "shades" old declaration.
Symbol table keeps track of which declarations are currently visible.
Think of symbol table as stack. When enter a new scope, new declarations are pushed on and can shade old ones. When exit scope, declarations arising from the scope are popped off.
In particular an occurrence of an identifier in a procedure may be associated with different variables at different times in the execution of the program.
Example:
program ... var A : integer; procedure Y(...); begin ...; B := A + B; ... end; {Y} procedure Z(...); var A: integer; begin ...; Y(...); ... end; {Z} begin {main} ...; Z(...);... end.Question: Which variable with name A is used when Y is called from Z?
Push and pop entries when enter and exit scopes at run-time.
For obvious reasons, dynamic scoping usually implies dynamic typing!
LISP and APL use dynamic scoping (though SCHEME has default of static)
In Pascal, when enter procedure any local variables are allocated and are then deallocated when exit. (Called dynamic allocation of variables).
In block-structured language (e.g., Pascal, C, Modula-2, etc.):
Use run-time stack to allocate space for local variables and parameters when enter a new unit (procedure, functions, etc.). Space is called an activation record.
Pop it off run-time stack when exit unit.
Note that a procedure may have several activation records on stack if called recursively.
Even without recursion may have several distinct variables on stack with same name!
When pointers are used, utilize another kind of memory, called "heap".
When do "new(p)" operation where p is of type pointer to T, sufficient memory is allocated from the heap to hold a value of type p and p is assigned the location of that memory. The value is accessed by writing p^ (in Pascal).
This memory does not follow the stack discipline. The lifetime of the heap-allocated memory is determined manually by "new" and "dispose" commands. Entering or exiting a scope has no impact on the allocation or deallocation of this memory.
Therefore in Pascal (for example) there are three kinds of memory: static (occupied by global variables), stack-based or automatic (occupied by parameters and local variables of procedures and functions), and heap-based or manually allocated.
In ML, everything comes off of heap. But automatically allocated when needed and deallocated when no way of accessing it (by garbage collector). Java is similar.
More implementation details later.
If freeze at compilation, then program constant
const size = 100; doubleSize = 2 * size {called manifest constant}In other languages allow:
procedure ... (n : integer) is var x: constant integer := 3 * n - 2; {value bound & frozen on procedure entry} A: array[1..n] of real;Some language allow variables to be initialized at declaration.
var x : integer := 5;But when is binding done - only first time procedure is entered or every time ? FORTRAN only first time, Java every time.
Postpone discussion of binding time for types until later.
This can occur especially easily when using var parameters.
Ex: If p(x,y) is a procedure where x and y are both var parameters, then the call p(z,z) makes x and y aliases in the body of p.
If the body of p(x,y) first increases x by one and then y by one, what is the result of the call to p(z,z)? Get z increased by 2! Aliasing often producing surprising (undesirable) behavior in functions.
Also arises when global variable, x, is used inside procedure where x was used as an actual parameter for a var parameter.
Also easy to get aliasing with pointers:
var x, y: ^ int; ... x := y;Then x^ and y^ are aliases - any change to one, changes the other!
In languages with assignment by sharing (e.g., Java), get aliasing automatically with all assignments.
"Pointers have been lumped with the goto statement as a marvelous way to create impossible to understand programs."
Kernighan & Ritchie, The C Programming Language
(In fairness, they then go on to defend the use of pointers.)