Unformatted text preview:

1Names, scopes, and bindingsAmer DiwanBindings• An association between two things• Typically a (name, thing) pair• E.g., – (x, 5)– (x, address of x)– (procedure name, procedure code)– (dll name, dll code)• Bindings come in all shapes and sizes!2“Shapes and sizes”• Binding time– When does the binding happen?• Binding scope– When is the binding “active”?• Object lifetime– What is the lifetime of a bound value?• We will examine the above in detail along with some implementation possibilitiesBinding timeTime ExampleLanguage designLanguage implementationProgram writingCompileLinkLoadRunControl flow constructsPrecisionAlgorithmLayout of dataLayout of modulesInstruction addressesVariable values3Why is early binding time important?• Earlier binding ? better performance (usually)• const x = 10 (can evaluate x+10 at compile time)versus int x; (x+10 must be evaluated at run time)• o.m(10) (requires run-time method dispatch)versus foo(10) (a direct call!)Why is late binding time important?• Late binding ? Greater flexibility (usually)• Dynamically-linked (can pick a different implementation for every run)versus Statically (fixed implementation for all runs)• o.m(10) (called procedure changes with type of “o”) versusfoo(o, 10) (same procedure called every time)4Binding scope• The textual region of the program in which the binding is active– Static scope: determined on inspection of code– Dynamic scope: determined at run timeStatic scopeint xint xint yint yA-CBCDABCD5When is a new scope created?Java, Modula-3, C+++ ClassesModula-2, C+ File/ModulePascal+ Block statements (BEGIN/END, {})Some FORTRAN+ ProcedureSome BASICGlobalExample languagesScopesAdvantages and disadvantages of having more scopes• Expressiveness– Fine grained control of the referencing environment• Simplicity– Limits the code a programmer must inspect when investigating a bug• Ease of implementation– Fewer scopes are easier to implement6What can go in a particular scope?• Variables• Types• Procedures (sometimes)What can go in a particular scope?void foo(int cond) {if (cond) {...}}int i;void bar() {...}7What does it mean to declare a function inside another?• void foo(int i) {int j;void bar(int k) {int l;l = j + k;j = l;}bar(i);j = j - l;}• bar can see its own variables and all variables in outer scopes: l, i, and jfoo can see: bar, j, and lPros and cons of allowing nested procedures• Expressiveness– Better control over information hiding• Simplicity– Uniformity in the language definition• Ease of implementation– Hard to implement (more on this when we study Chapter 8)8Dynamic scope• The run-time flow of the program and not static nesting determines the scope of a binding• a: integerprocedure firsta := 1procedure seconda: integerfirst()a := 2if cond then second() else first() endWhy have dynamic scoping?• Makes more sense in macros rather than in calls• Can use them to emulate parametersprint(num) {...num/base...foo() {VAR base: int := 10;print(10);bar();print(10);}bar() {VAR base: int := 16print(20)}9Static versus dynamic scoping(all else being equal...)• Expressiveness– incomparable• Simplicity– Easier to understand code with static scoping• Ease of implementation– Similar• Performance– Static scoping has overhead at compile time, dynamic has overhead at run timeObject lifetime• When do objects die?– Object lifetime determines how to allocate objects• Local variables– When enclosing procedure returns (FIFO)• Global variables– When program ends• Dynamically allocated variables– At any time10Local variables and memory• Allocated with enclosing procedure is called• Deallocated when enclosing procedure returns• How about allocating a single copy of every local variable?– Each time a procedure is calls, it uses the copy of its variables– No work necessary when a procedure returnsLocal variables and memorymain() {int i = ...; int j = ...;foo(i)}foo(int f) {int k;if (f>10) {foo(f-1)}ijfkfkfkfk11g’s local varsmain calls gOrganization of memorymain’s local varsglob1glob3glob2Local variables Global variables Dyn. alloc. variablesg returnsmain calls h, ...h’s local varsg allocates heap object, assigns it to globalGlobal variables and memory• Live for the entire lifetime of the program– Allocated in a separate area (static)– May not be visible everywhere, however!12Global variables, lifetime, and visibilityMODULE M1;IMPORT I;BEGINI.gl := 10END M1.MODULE M2;BEGINI.gl := 10(*ERROR*)END M2.INTERFACE I;VAR gl: INTEGER;END I.int gl;void f() {int gl;...}Heap variables and memory• Heap variables may live for any portion of the program execution– main() { v = new T; ...; free T; }main() { v = new T; foo(); free T; bar(); }• They are allocated in a separate “unstructured” area of memory13More on memory organization• We will discuss organization of local variables with Chapter 8 (subroutines)• We will discuss organization of dynamically allocated memory with Chapter 7 (types)Implementation of bindings: Symbol tables• Issue: How to enforce binding scope?• In class we will look only at mechanisms for static binding in a compiler: see text for dynamic binding14Symbol tablesint xint zint xint yint ychar xABCDMaps names to the information that the compiler knows about themSimplest implementation: attach a symbol table to each scopey: int, offset 4x: char, offset 8x: int, offset 0z: int, offset 4More operationally• Each scope node in a parse table has a symbol table node• All declarations in the scope go in this symbol table• When compiler encounters a name, it searches for its information by searching scopes starting with the innermost scope15Exampleint xint zint xint yint ychar x...x...BCDy: int, offset 4x: char, offset 8x: int, offset 0z: int, offset 4Forward declarations and symbol tables• { int i;{int j = i + 1;int i;}}• Which “i” is used in the addition?• If forward declarations are allowed, the compiler needs two passes:– pass 1: build all the symbol tables– pass 2: resolve all symbols16Putting it together:parsing and name analysis{ int i;{int j = i + 1;int i;}}stmts symtabi intblockstmts symtabj inti intblocklhs rhsop1 op2“j”“i” 1assignaddAfter name resolution{ int i;{int j = i + 1;int i;}}stmts symtabi intblockstmts symtabj inti intblocklhs rhsop1 op21assignadd17Relationship to readings• Text covers symbol table management in greater


View Full Document

CU-Boulder CSCI 3155 - Names, Scopes, and Bindings

Download Names, Scopes, and Bindings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Names, Scopes, and Bindings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Names, Scopes, and Bindings 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?