<<

Topics

Binding Chapter 5 Lifetime Variables Constants

Chapter 5: Variables 2

Variables: attributes Binding

A variable can be thought of as being completely The assignment statement is really an specified by its 6 basic attributes: instance of a more general phenomenon of 1. Name: identifier attaching various kinds of values to names. 2. Address: memory location(s) The association of a name to an attribute is 3. Value: particular value at a moment called binding 4. Type: range of possible values n Assignment statement binds a value to a 5. Lifetime: when the variable is accessible location. 6. Scope: where in the program it can be accessed n Identifiers are bond to locations, types, and other attributes at various points in the translations of a program. Chapter 5: Variables 3 Chapter 5: Variables 4

Binding Binding

Binding time. Bindings happen at different 3. and invisible points. n Example: bind a variable to a type in or Java Possible binding times 4. Link time n Example: bind a call to a function to the 1. Language design time function code. n Bind operator symbols to operations 5. Load time n Example: bind * to multiplication n Example: bind a C variable to a memory 2. Language implementation time cell. n Example: bind floating point type to a representation (IEEE floating-point format) 6. Runtime n Example: bind a nonstaticlocal variable to a n Example: the data type int in Java is bound to a range of values. memory cell

Chapter 5: Variables 5 Chapter 5: Variables 6

1 The Concept of Binding Static and Dynamic Binding

Consider the following: A binding is static

int C; n it occurs before run time and

C := C + 5; n It remains unchanged throughout program n Some of the bindings and their binding times A binding is dynamic are: n It occurs during execution or The type of C is bound at compiletime. The set of possible values of C is bound at compiler design n It can change during execution of the program time. As binding time gets earlier: The meaning of the operator + is bound at compiletime (when the types of its operands have been determined) n Efficiency goes up The internal representation of the literal 5 is bound at n Safety goes up compiler design time. The value of C is bound at run time. n Flexibility goes down

Chapter 5: Variables 7 Chapter 5: Variables 8

Type Bindings Variable Declarations

A variable must be bound to a data type before An explicit declaration is a program statement it can be referenced. used for declaring the types of variables. Two key issues in binding a type to an n Example: identifier: int x; n 1. How is the type specified? Advantage: safer, cheaper 2. When does the binding take place? n Disadvantage: less flexible How? – two kinds of declarations: An implicit declaration is a default mechanism 1. Explicit declarations for specifying types of variables (the first 2. Implicit declarations appearance of the variable in the program) When? - three kinds of type bindings: n Example: in FORTRAN, variables beginning with I-N 1. Static type binding are assumed to be of type integer. 2. Dynamic type binding 3. Type inference Chapter 5: Variables 9 Chapter 5: Variables 10

Variable Declarations Variable Declarations

Advantages: convenience Implicit declarations leave more room for Disadvantage: reliability (some typographical and error programmer errors cannot be detected. n Example: In FORTRAN variables left Intermediate position: Names for specific types undeclared will be implicitly declared as an must begin with a given character. integer. n Example: in Perl, variables of type scalar, array and hash structures begin with a $, @, or %, respectively.

n Advantages: Different namespaces for different type variables @apple vs. %apple vs. @apple The type of a variable is know through its name.

Chapter 5: Variables 11 Chapter 5: Variables 12

2 Dynamic Type Binding Dynamic Type Binding

The variable is bound to a type when it is n Disadvantages: Compiler’s type error detection is minimized. assigned a value in an assignment If RHS is not compatible with LHS, the type of LHS is changed as statement. opposed to generating an error.

n This issue also appears in static type binding languages like C and n JavaScript and PHP C++ n Example: in JavaScript Must be implemented by a pure interpreter rather than a compiler list = { 2, 4, 6, 8 }; n It is not possible to create machine code instructions whose operand types are not known at compile time. list = 17.3; High cost: n Dynamic binding of objects. n Type checking must be done at runtime

n Every variable must know its current type n Advantage: flexibility (generic program units) n A variable might have varying sizes because different type values require different amounts of storage.

n Must be interpreted. Chapter 5: Variables 13 Chapter 5: Variables 14

Type Inference Type Inference

Rather than by assignment statement, types n Illegal: are determined from the context of the fun square(x) = x * x reference. // can’t deduce anything ( a default value could be Type inferencing is used in some assigned) programming languages including ML, n Fixed Miranda, and Haskell. fun square(x : real) = x * x; Example: // use explicit declaration fun square(x) = (x : real) * x; n Legal: fun square(x) : real = x * (x : real); fun circumf(r) = 3.14159 * r * r; // infer r is real fun time10(x) = 10 * x; // infer s is integer

Chapter 5: Variables 15 Chapter 5: Variables 16

Storage Bindings & Lifetime Variables: lifetime

Allocation is the process of getting a cell from some pool of available cells. Categories of scalar variables by lifetimes: Deallocation is the process of putting a cell n Static back into the pool. n Stack-dynamic

The lifetime of a variable is the time during n Explicit heap-dynamic which it is bound to a particular memory cell. n Implicit hep-dynamic

n Begin: when the variable is bound to a specific cell

n Ends: when the variable is unbound from that cell.

Chapter 5: Variables 17 Chapter 5: Variables 18

3 Static Variables Static Variables

Bound to memory cells before execution and remains bound to the same memory cell Disadvantages: throughout execution n If a language only has static variables then

n Example: all FORTRAN 77 variables Recursion cannot be supported (lack of flexibility). n Example: C static variables Storage cannot be shared among variables Advantages: (more storage required) n Efficiency (direct addressing)

n No allocation/deallocation needed (which is run time overhead)

n History-sensitive subprogram support (retain values between separate executions of the subprogram) Chapter 5: Variables 19 Chapter 5: Variables 20

Stack-dynamic Variables Stack-dynamic Variables

Storage bindings are created for variables in Advantages:

the run time stack when their declaration n Allows recursion

statement are elaborated (or execution n Conserves storage reaches the code to which declaration is Disadvantages:

attached), but types are statically bound. n Run time overhead for allocation and n If scalar, all attributes except address are deallocation. statically bound n Subprogram cannot be history sensitive

Example: local variables in C subprograms and Java n Inefficient references (indirect addressing) methods n Limited by stack size.

Chapter 5: Variables 21 Chapter 5: Variables 22

Explicit Heap-dynamic Variables Explicit Heap-dynamic Variables

Allocated and deallocated by explicit Disadvantages: directives, specified by the programmer, which take effect during execution. n Unreliable (forgetting to delete) n Difficult of using pointer and reference variables n Referenced only through pointers or correctly references n Inefficient. Example: dynamic objects in C++ (via new/delete, malloc/free) Example: Example: all objects in Java (except primitives) int *intnode; // create a pointer Advantages: … intnode = new int // create the heap-dynamic variable n Provides for dynamic storage management … delete intnode; // deallocate the heap-dynamic variable

Chapter 5: Variables 23 Chapter 5: Variables 24

4 Implicit Heap-dynamic Variables Summary Table

Allocation and deallocation caused by Variable Storage binding time Dynamic storage Type assignment statements and types not Category from binding determined until assignment. Static Before execution Static

n Example: All arrays and strings in Perl and JavaScript

n Example: all variables in APL Stack-dynamic When declaration is Run-time stack Static Advantage: highest degree of flexibility elaborated (run time) Disadvantages: Explicit heap- By explicit instruction Heap Static dynamic (run time) n Inefficient because all attributes are dynamic (a lot of overhead) Implicit heap- By assignment (run Heap Dynamic dynamic time) n Loss of error detection

Chapter 5: Variables 25 Chapter 5: Variables 26

Type Checking Type Checking

Generalizes the concept of operands and A compatible type is one that is either: operators to include subprograms and n Legal for the operator, or assignments: n Allowed under language rules to be implicitly n Subprogram is operator, parameters are converted to a legal type by compiler-generated operands. code. n Assignment is operator, LHS and RHS are n This automatic conversion is called coercion operands. Example: adding an int to a float in Java is allowed, Type checking is the activity of ensuring then int is coerced. that the operands of an operator are of compatible types. A type error is the application of an operator to an operand of an inappropriate type. Chapter 5: Variables 27 Chapter 5: Variables 28

Type Checking Strong Typing

If all type bindings are A is strongly typed if

n Static: nearly all type checking can be static n Type errors are always detected.

n Dynamic: type checking must be dynamic n There is strict enforcement of type rules with no Static type checking is less costly (it is better to exceptions. catch errors at compile time) but it is also less n All types are known at compile time, i.e. are statically bound. flexible (fewer shortcuts and tricks). n With variables that can store values of more than one Static type checking is difficult when the type, incorrect type usage can be detected at run time. language allows a cell to store a value of Advantages: different types at different time, such as C n Strong typing catches more errors at compile time than unions, Fortran Equivalences or Ada variant weak typing, resulting in fewer run time exceptions. records. n Detects misuses of variables that result in type errors.

Chapter 5: Variables 29 Chapter 5: Variables 30

5 Which languages have strong Strong Typing vs. No Type typing? FORTRAN 77 is not because it does not check Coercion rules strongly affect strong typing parameters and because of variable equivalence n They can weaken it considerably statements. n Although Java has just half the assignments Ada is almost strongly typed but UNCHECKED coercions of C++, its strong typing is still weak (less CONVERSIONS is a loophole. effective than Ada). Haskell is strongly typed. n Languages such as Fortran, C and C++ have a great Pascal is (almost) strongly typed, but variant records deal of coercion and are less reliable than those with screw it up. little coercion, such as Ada, Java, and C#. C and C++ are sometimes described as strongly typed, In practice, languages fall on between strongly but are perhaps better described as weakly typed typed and untyped. because parameter type checking can be avoided and unions are not type checked.

Chapter 5: Variables 31 Chapter 5: Variables 32

Type Compatibility Name Type Compatibility

There are 2 different types of compatibility Easy to implement but highly restrictive: methods for structure (nonscalar) variables: n Subranges of integer types are not compatible n Name type compatibility with integer types. n Structure type compatibility Example: count cannot be assigned to index Name type compatibility (“name type IndexType is 1..100; equivalence”) means that two variables count: Integer; have compatible types if index: Indextype;

n They are defined in the same declaration or n Only two type names will be compared to

n They are defined in declarations that uses the determine compatibility. same type name.

Chapter 5: Variables 33 Chapter 5: Variables 34

Structure Type Compatibility Type Compatibility

Type compatibility by structure (“structural Consider the problem of two structured types: equivalence) means that two variables have n Are two record types compatible if they are structurally compatible types if their types have identical the same but use different field names? structures. n Are two array types compatible if they are the same More flexible, but harder to implement. except that the subscripts are different (e.g. [1..10] and n The entire structures of two types must be compared. [0..9])?

n May create types that are, but should not be n Are two enumeration types compatible if their compatible components are spelled differently? Example: Celsius vs. Fahrenheit n With structural type compatibility, you cannot type celsius= float; differentiate between types of the same structure (e.g. Fahrenheit = float; different units of speed, both float).

Chapter 5: Variables 35 Chapter 5: Variables 36

6 Scope Static Scope

The scope of a variable is the range of statements in a program over which it is visible. Also known as “lexical scope” n A variable is visible if it ca be referenced in a statement. In static scoping, the scope of a variable can be Typical cases: determined at compile time, based on the text of n Explicitly declared Þ local variables a program. n Explicitly passed to a subprogram Þ parameters To connect a name reference to a variable, the n The nonlocal variables of a program unit are those that are visible but not declared compiler must find the declaration

n Global variables Þ visible everywhere n Search process: search declarations, first locally, then The scope rules of a language determine how references to in increasingly larger enclosing scopes, until one is names are associated with variables. found for the given name. The two major schemes are static scoping and dynamic n Enclosing static scopes to a specific scope are called scoping. its static ancestors; the nearest static ancestor is called a static parent.

Chapter 5: Variables 37 Chapter 5: Variables 38

Blocks Blocks

A block is a section of code in which local Variables can be hidden from a unit by variables are allocated/deallocated at the having a “closer” variable with the same start/end of the block. name. Provides a method of creating static C++ allows access to “hidden” variables scopes inside program units. with the use of :: scope operator.

Introduced by ALGOL 60 and found in n Example: if x is a hidden in a most PLs. subprogram by a named x, the global could be reference as class_name::x

n Ada: unit.x

Chapter 5: Variables 39 Chapter 5: Variables 40

Example of Blocks Scope

C and C++ Common Lisp Consider the example: for (…) { (let ((a 1) int index; (b foo) Assume MAIN calls A and B … (c)) } A calls C and D (setqa (* a a )) (bar a b ) ) B calls A and E Ada Declare LCL: FLOAT; begin … end

Chapter 5: Variables 41 Chapter 5: Variables 42

7 Static Scope Example Static Scope Example

The desired call graph The potential call graph

MAIN MAIN A

C A B MAIN MAIN D C D E A B A B B

E C D E C D E

Chapter 5: Variables 43 Chapter 5: Variables 44

Static Scope Evaluation Dynamic Scope

Suppose now that E() needs to get access to a Based on calling sequences of program units, variable in D() not their textual layout. One solution is to move E() inside the scope of D() Reference to variables are connected to n But then E can no longer access the scope of B declarations by searching back through the Another solution is to move the variables defined in chain of subprogram calls that forced execution D to main at this point. n Suppose x was moved from D to main, and another x Used in APL, Snobol and LISP was declared in A, the latter will hide the former. n Note that these languages were all (initially) n Also having variable declared very far from where they implemented as interpreters rather than compilers. are used is not good for readability. Consensus is that PLs with dynamic scoping Overall: static scope often encourages many global lead to programs which are difficult to read and variables. maintain. Chapter 5: Variables 45 Chapter 5: Variables 46

Scope Example Static vs. Dynamic Scoping

MAIN Advantages of Static Scoping: - declaration of x SUB1 MAIN calls SUB1 n Readability - declaration of x - SUB1 calls SUB2 n Locality of reasoning ... SUB2 uses x call SUB2 n Less run time overhead ... Disadvantages:

n Some loss of flexibility SUB2 • Static scoping Advantages of Dynamic Scoping ... • reference to x is to MAIN’s x - reference to x - • Dynamic scoping n Some extra convenience ... • reference to x is to SUB1’s x Disadvantages

... n Loss of readability call SUB1 n Unpredictable behavior (minimal parameter passing) … Chapter 5: Variables 47 n More run-time overhead.Chapter 5: Variables 48

8 Scope vs. Lifetime Referencing Environment

While these two issues seem related, they can The referencing environment of a differ. statement is the collection of all names In Pascal, the scope of a local variable and the that are visible in the statement. lifetime of the local variable seem the same. In a static-scoped language, it is the local In C/C++, a local variable in a function might variables plus all of the variables in all the be declared static but its lifetime extends over enclosing scopes. the entire execution of the program and In a dynamic-scoped language, the therefore, even through it is inaccessible, it is referencing environment is the local still memory. variable plus all visible variables in all active subprograms.

Chapter 5: Variables 49 Chapter 5: Variables 50

Named Constants Named Constants

A named constant is a variable that is Advantages: bound to a value only when it is bound to n Readability storage. n Maintenance The value of a named constant can not change while the program is running. The binding of values to named constants can be either static of dynamic

n const int length = 5 * x; Type Name Lifetime Value n final flow rate = 1.5*values; Scope

Chapter 5: Variables 51 Chapter 5: Variables 52

Named Constants Variable Initialization

Languages The binding of a variable to a value at the

n Pascal: literals only time it is bound to storage is called

n Modula-2 and FORTRAN 90: constant- initialization. value expressions Initialization is often done on the n Ada, C++, and Java: expressions of any declaration statement kind n Example: In Java Advantages int sum = 0;

n Increases readability without loss of effective.

Chapter 5: Variables 53 Chapter 5: Variables 54

9 Summary

Case sensitivity and the relationship of names to special words represent design issues of names Variables are characterized by the sextuples: name, address, value, type, lifetime, scope Binding is the association of attributes with program entities Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic Strong typing means detecting all type errors

Chapter 5: Variables 55

10