CS 321 Programming Languages and...

59
CS 321 Programming Languages and Compilers Names, Scopes, and Bindings

Transcript of CS 321 Programming Languages and...

CS 321Programming Languages and Compilers

CS 321Programming Languages and Compilers

Names, Scopes, and Bindings

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings22

Binding TimeBinding Time

• The binding of a program element to a particularcharacteristic or property is the choice of theproperty from a set of possible properties.

• The time during program formulation orprocessing when this choice is made is thebinding time.

• There are many classes of bindings inprogramming languages as well as many differentbinding times.

• Also included within the concepts of binding andbinding times are the properties of programelements that are determined by the definition ofthe language or its implementation.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings33

Binding Time (Cont.)Binding Time (Cont.)

Binding times include:• Run time (execution time). Two subcategories:

– On entry to a subprogram or block.» Binding of formal to actual parameters» Binding of formal parameters to storage locations

– At arbitrary points during execution» binding of variables to values» binding of names to storage location in Scheme.

• e.g. (define size 2)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings44

Binding Time (Cont.)Binding Time (Cont.)

• Compile time (translation time)– Bindings chosen by the programmer

» Variable names» variable types» program statement structure

– Chosen by the translator» Relative location of data objects.

– Chosen by the linker» Relative location of different object modules.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings55

Binding Time (Cont.)Binding Time (Cont.)

• Language Implementation time (i.e. when thecompiler or interpreter is written)

» Representation of numbers. Usually determined by theunderlying computer, but not always (e.g. Java defines therepresentation to guarantee portability).

» Use of implementation-specific features precludeportability.

• e.g. in Fortran’s expression x*f(y), function f in the does not have tobe called when x is 0. If the function has side effects, differentimplementations produce different results.

• Language definition time.» Alternative statement forms» Data structure types» Array storage layout

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings66

Binding Time (Cont.)Binding Time (Cont.)

• Consider X = X + 10– Type of X

» At translation time in C» At run time in Scheme/MATLAB

– Set of possible values of X» At implementation time. If X is real it could be

• the IEEE floating point standard (almost always the choice),• the implementation of a specific machine, or• a software-based “infinite precision” representation.

– Value of X» Changed at run time.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings77

Binding Time (Cont.)Binding Time (Cont.)

– Properties of operator +» At compilation time (depending on the type of the

operands because of overloading.• If x is declared integer + means one thing,• if x is declared real means something else.• + can also be overloaded by the programmer. For example, in Fortran

95 it is possible to specify that + operate on intervals and on rationalnumbers:

INTERFACE OPERATOR(+) FUNCTION INTEGER_PLUS_INTERVAL(X, Y) … END .. MODULE PROCEDURE RATIONAL_ADD END INTERFACE

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings88

Binding Time (Cont.)Binding Time (Cont.)

• Many of the most important and subtledifferences between languages involvedifferences in binding time.

• The trade off is between efficient execution andflexibility.

– When efficiency is a consideration (Fortran, C) Languages aredesigned so that as many bindings as possible are performedduring translation.

– Where flexibility is the prime determiner,as in Scheme, mostbindings are delayed until execution time so that they may bemade data dependent.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings99

Objects lifetimeObjects lifetime

• Key events in the life of an object:

Creation of an object

Destruction of an object

Creation of a binding

Destruction of a binding Dangling reference if these two times are interchanged

Program execution tim

e

Object lifetim

e

Binding

lifetime

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1010

Storage ManagementStorage Management

• Three storage allocation mechanisms– Static– Stack– Heap

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1111

Static AllocationStatic Allocation

• Global variables• Constants

– manifest, declared (parameter variables in Fortran) or identifiedby the compiler

• Variables identified as const in C can be afunction of non constants and therefore cannot bestatically allocated.

• Constant tables generated by the compiler fordebugging and other purposes.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1212

Static Allocation (Cont.)Static Allocation (Cont.)

• In the absence of recursion, all variables can bestatically allocated.

• Also, can be statically allocated:– Arguments and return values (or their addresses). Allocation

can be in processor registers rather than in memory– Temporaries– Bookkeeping information

» return address» saved registers,» debugging information

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1313

Static Allocation (Cont.)Static Allocation (Cont.)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1414

Stack-based Allocation (Cont.)Stack-based Allocation (Cont.)

• Needed when language permits recursion• It could be useful in languages without recursion

because it could save space.• Each subroutine invocation creates a frame or

activation record– arguments– return address– local variables– temporaries– bookkeeping information

• Stack maintained by– calling sequence– prologue– epilogue

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1515

Stack-based Allocation (Cont.)Stack-based Allocation (Cont.)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1616

Heap-based AllocationHeap-based Allocation

• Region of storage in which blocks of memory canbe allocated and deallocated at arbitrary times.

• Because they are not allocated in the stack, thelifetime of objects allocated in the heap is notconfined to the subroutine where they arecreated.

– They can be assigned to parameters (or to components ofobjects accessed via pointers by parameters)

– They can be returned as value of thesubroutine/function/method.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1717

Heap-based Allocation (Cont.)Heap-based Allocation (Cont.)

• There are several strategies to manage space inthe heap.

• An important issue is fragmentation (also anissue in virtual memory management systems)

– Internal fragmentation when space allocated is larger thanneeded.

– External fragmentation when allocated blocks are scatteredthrough the heap. It could be that the total space availablecould is more than requested, but no block has the neededsize.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1818

Heap-based Allocation (Cont.)Heap-based Allocation (Cont.)

• One approach to maintain the free memory spaceis to use a free list.

• Two strategies to find a block for a give request– First fit. Use the first block in the list that is large enough to

satisfy the request– Best fit. Search the entire list to find the smallest block that

satisfy the request

• The free list could be organized (conceptually) asan array of free lists where each list in the arraycontain blocks of the same size.

– Buddy system– Fibonacci heap (better internal fragmentation)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings1919

Garbage collectionGarbage collection

• Programmers can mange memory themselveswith explicit allocation/deallocations.

• However, garbage collection can be appliedautomatically by the run-time system to avoidmemory leaks and difficult to find danglingreferences.

– Lisp– Java

• The disadvantage is cost.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2020

Scope rulesScope rules

• The region of the program in which a binding isactive is its scope.

• Most languages today are lexically scoped• We will also study dynamic scoping for

completeness.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2121

Static ScopeStatic Scope

• A single global scope (basic, awk?)• A separate scope for each program unit (main

program, subroutines, functions) in FORTRAN.• We will discuss two classes of Fortran objects

– variables– common blocks

• Common blocks are blocks of storage that can beshared by several program units.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2222

Static Scope (Cont.)Static Scope (Cont.)

• Common block examplesubroutine first real b(2) logical flag complex c type coordinates seqnece real x, y logical z_0 end type coordinates type (coordinates) p common /reuse/ b,c,flag,p …subroutine second integer I(8) common /reuse/ i

z_0yxflag

i(8)i(7)i(6)i(5)i(4)i(3)

c

i(2)

b(2)

i(1)

b(1)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2323

Static Scope (Cont.)Static Scope (Cont.)

• Lifetime of statically allocated variables andcommon blocks is the duration of the program.

• Most Fortran 77 implementations do allallocations statically.

• If default allocation is not static, variables andcommon blocks ca be saved (I.e. declared assave: save /reuse/,w,r) to force their staticallocation.

• Variables can only be saved in the program unitwhere they are declared.

• If a common block is saved, it has to be saved inall program units where it appears.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2424

Static Scope (Cont.)Static Scope (Cont.)

• The default is that common blocks can go awaywhen there are no active program units thataccess them.

• Saved variables and saved common blocks maycause collisions in parallel executions, but privateentities enable the creation of new copies witheach invocation.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2525

Static Scope - Nested SubroutinesStatic Scope - Nested Subroutines

• In most languages any constant, type, variablesor subroutines declared within a subroutine arenot visible outside the subroutine.

• In the closest nested scope rule: a name is knownin the scope in which it is declared unless it ishidden by another declaration of the same name.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2626

Static Scope - Nested Subroutines (Cont.)Static Scope - Nested Subroutines (Cont.)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2727

Static Scope - Nested Subroutines (Cont.)Static Scope - Nested Subroutines (Cont.)

• To find the frames of surrounding scopes wherethe desired data is a static link could be used.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2828

Static Scope - Nested Subroutines (Cont.)Static Scope - Nested Subroutines (Cont.)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings2929

Static Scope - Nested Subroutines (Cont.)Static Scope - Nested Subroutines (Cont.)

ARB4

ARB1

ARB2

ARB3

{ /* B1 */

{ /* B2 */

{ /* B3 */

{ /* B4 */

}

}

}}

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3030

Static Scope - Nested Subroutines (Cont.)Static Scope - Nested Subroutines (Cont.)

ARP3

ARP1

ARB1

ARB2

ARB3

ARP2

ARP3

P1(){ { /* B1 */ P2() { P3() } { /* B2 */ { /* B3 */ P2() } } } P3() { P3() } }

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3131

Static Scope - ModulesStatic Scope - Modules

• Modularization depends on information hiding.• Functions and subroutines can be used to hide

information. However, this is not flexible enough.• One reason is that persistent data is usually

needed to create abstraction. This can beaddressed in some cases using staticallyallocated values.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3232

Static Scope - Modules (Cont.)Static Scope - Modules (Cont.)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3333

Static Scope - Modules (Cont.)Static Scope - Modules (Cont.)

• But modularization often requires a variety ofoperations on persistent data.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3434

Static Scope - Modules (Cont.) Static Scope - Modules (Cont.)

• Objects inside a module are visible to each other• Objects inside can be hidden explicitly (using a

keyword like private) or implicitly (objects areonly visible outside if they are exported)

• In some language objects outside need to beimported to be visible within the module.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3535

Static Scope - Modules (Cont.)Two Modula 2 examplesStatic Scope - Modules (Cont.)Two Modula 2 examples

VAR a,b: CARDINAL;MODULE M; IMPORT a; EXPORT w,x; VAR u,v,w; CARDINAL; MODULE N; IMPORT u; EXPORT x,y; VAR x,y,z: CARDINAL; (* x,u,y,z visible here *) END N; (* a,u,v,w,x,y visible here *)END M;(* a,b,w,x visible here *)

MODULE M; VAR a: CARDINAL; MODULE N1; EXPORT b; VAR b: CARDINAL; (* only b visible here *) END N1; MODULE N2; EXPORT c; VAR c: CARDINAL; (* only c visible here *) end N2; MODULE N3; IMPORT b,c; (* b,c visible here *) END N3;END M;

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3636

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3737

Static Scope - Modules (Cont.) A Fortran 90 ModuleStatic Scope - Modules (Cont.) A Fortran 90 Module

Module polar_coordinates type polar private real rho, theta end type polar interface operator (*) module procedure polar_mult end interface contains function polar_mult(p1,p2) type (polar), intent(in) :: p1,p2 type (polar) polar_mult polar_mult= polar(p1%rho*p2%rho, & p1%theta+p2%theta) end function polar_mult ...end module polar_coordinates

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3838

Modules as typesModules as types

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings3939

Dynamic scopeDynamic scope

• Early lisp systems were implemented so thatvariables are bound dynamically rather thanstatically.

• In a language with dynamic binding, free variablesin a procedure get their values from theenvironment in which the procedure is calledrather than the environment in which theprocedure is defined.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4040

Dynamic scope (Cont.)Dynamic scope (Cont.)

• Consider the program (define (sum-powers a b n) (define (nth-power x) (expt x n)) (sum nth-power a 1+ b))

• Where sum is defines as follows(define (sum term a next b)

(if (> a b) 0

(+ (term a) (sum term (next a) next b))))

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4141

Dynamic scope (Cont.)Dynamic scope (Cont.)

• Traditionally Lisp systems have beenimplemented so that variables are bounddynamically rather than statically.

• In a language with dynamic binding, free variablesin a procedure get their values from from whichthe procedure is called rather than from theenvironment in which the procedure is defined.

• For example, the free variable n in nth-powerwould get wahtever n had when sum called it.

• In this example, since sum does not rebind n, theonly definition of n is still the one from sum-powers.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4242

Dynamic scope (Cont.)Dynamic scope (Cont.)

• But if we had used n instead of next in thedefinition of sum, then nth-power’s free variablewould refer to sum’s third argument, which is notwhat we intended.

• This would produce an error, since the value of nhere is not a number, as required by nth-power.

• As can be seen from this example, dynamicbinding violates the principle that a procedureshould be regarded as a “black box”, such thatchanging the name of a parameter thourghout aprocedure’s definition will no change theprocedure behavior.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4343

Dynamic scope (Cont.)Dynamic scope (Cont.)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4444

Dynamic scope (Cont.)Dynamic scope (Cont.)

• In a statically bond language, the sum-powersprogram must contain the definition ofnth-power as a local procedure.

• If nth-power represents a common pattern ofusage, its definition must be repeated as aninternal definition in many contexts.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4545

Dynamic scope (Cont.)Dynamic scope (Cont.)

• It should be attractive to be able to move thedefinition of nth-power to a more global context,where it can be shared by many procedures

(define (sum-powers a b n) (sum nth-power a 1+ b))

(define (product-powers a b n) (product nth-power a 1+ b))

(define (nth-power x) (expt x n))

• The attempt to make this work is what motivatedthe development of dynamic binding discipline.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4646

Dynamic scope (Cont.)Dynamic scope (Cont.)

• In general, dynamically bound variables can behelpful in structuring large programs.

• They simplify procedure calls by acting as implicitparameters.

• For example, a low-level procedure nprint calledby the system print procedure for printingnumbers might reference a free variable calledradix that specifies the base in which thenumber is to be printed.

• Procedures that call nprint, such as the systemprint operation, should not need to know aboutthis feature.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4747

Dynamic scope (Cont.)Dynamic scope (Cont.)

• On the other hand, a user might want totemporarily change the radix.

• In a statically bound language, radix would haveto be a global variable.

• After setting radix to a new value, the user wouldhave to explicitly reset it. But the dynamic bindingmechanism could accomplish this setting andresetting automatically, in a structured way

(define print-in-new-radix number radix) (print number)) (define (print frob) < expressions that involve nprint>) (define (nprint number) ... radix ...)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4848

Symbol TablesSymbol Tables

• Symbol tables are used to keep track of scopeand binding information about names.

• The symbol table is searched every time a nameis encountered in the source tex.

• Changes occur when a new name or newinformation about a name is discovered.

• The abstract syntax tree will contain pointers tothe symbol table rather than the actual namesused for objects in the source text.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings4949

Symbol Tables (Cont.)Symbol Tables (Cont.)

• Each symbol table entry contains– the symbol name,– its category (scalar variable, array, constant, type, procedure,

field name, parameter, etc.)– scope number,– type (a pointer to another symbol table entry),– and additional, category specific fields (e.g. rank and shape for

arrays)

• To keep symbol table records uniform, it may beconvenient for some of the information about aname to be kept outside the table entry, with onlya pointer to this information stored in the entry.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5050

Symbol Tables (Cont.)Symbol Tables (Cont.)

• The symbol table may contain the keywords at thebeginning if the lexical scanner searches thesymbol table for each name .

• Alternatively, the lexical scanner can identifykeywords using a separate table or by creating aseparate final state for each keyword.

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5151

Symbol Tables (Cont.)Symbol Tables (Cont.)

• One of the important issues is handling staticscope.

• A simple solution is to create a symbol table foreach scope and attach it to the node in theabstract syntax tree corresponding to the scope.

• An alter native is to use a additional datastructure to keep track of the scope. Thisstructure would resemble a stack:

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5252

Symbol Tables (Cont.)Symbol Tables (Cont.)procedure new_id(id) for index=top to scope_marker(LL - 1) by -1 if id == symbol_table(additional(index)).name then error() k = new_symbol_table_index() symbol_table(k).name=id additional(top++) = k procedure old_id(id) for index= top to 0 by -1 if id == symbol_table(additional(index)).name then return additional(index) error()

procedure scope_entry () scope_marker(LL++)=top

procedure scope_exit() top = scope_marker(--LL)

A

C

A

Badditional

0

2

scope_marker

4 top

2 LL

symbol table

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5353

Symbol Tables (Cont.)Symbol Tables (Cont.)

• A hash table can be added to the previous datastructure to accelerate the search.

• Elements with the same name are linked from top tobottom.

• Search start at the entry of the hash table andproceeds through the linked list until the end of thelist is reached (old_id) or until the link list refers toan element below scope_marker(LL - 1)(new_id)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5454

Symbol Tables (Cont.)Symbol Tables (Cont.)

• This approach does not work in some cases.• Consider the with statement of Pascal and Modula 2.Date = RECORD day: [1..31]; mo: month; yr: CARDINAL ENDd1: Date;

WITH d1 DO day:=10; mo:=Sep; yr:=1981END

is equivalent to

d1.day:=10; d1.mo:=Sep; d1.yr:=1981

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5555

Symbol TablesSymbol Tables

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5656

Symbol Tables (Cont.)Symbol Tables (Cont.)

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5757

Association Lists and Central Reference TablesAssociation Lists and Central Reference Tables

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5858

The binding of referencing environmentsThe binding of referencing environments

• Shallow binding: the referencing environment of aroutine is not created until the subroutine isactually called.

• Deep binding: the program binds the environmentat the time the subroutine is passed as aparameter.

• Deep binding is implemented by creating anexplicit representation of a referencingenvironment and bundling it together with areference to the subroutine. Closure

Names, Scopes, and BindingsNames, Scopes, and BindingsNames, Scopes, and Bindings5959

ARP1

ARB1

ARB2

ARB3

ARP2

ARP3

P1(){ REAL X { /* B1 */ { /* B2 */ { /* B3 */ P2(P3) }

P3() { x } } P2(PX) { PX() }

}}

PX