Sie sind auf Seite 1von 38

Chapter 3.

Names, Scopes, Bindings, and Storage Management


1. The notion of binding time
Name A name is mnemonic character string used to represent something else. Binding A binding is an association between a name and the thing it names. Binding time It is the time at which a binding is created or, more generally, the time at which any implementation decision is made.
Organization of Programming Languages CS 3342

There are many dierent binding times: - Language design time: control ow constructs, primitive types, constructors for complex types, aspects of semantics - Language implementation time: precision of certain types, maximum size of stack and heap, run-time exceptions - Program writing time: algorithms, data structures, names - Compile time: constructs in high-level language to machine code, machine addresses for statically dened data
Organization of Programming Languages CS 3342

- Link time: The linker links dierent modules and libraries together and resolves intermodule references. When a name in one module refers to an object in another module, the binding is nalized at link time. - Load time: Load time refers to the point at which the operating system loads the program into memory so that the program can run. In primitive operating systems, the linking of objects to their physical addresses is done at load time. - Run time: Run time covers the entire span from the beginning to the end of execution. Run time includes start-up time, module entry time, elaboration time (the point at which a declaration is rst seen), subroutine call time, block entry time, and statement execution time.
Organization of Programming Languages CS 3342

Both static and dynamic are coarse terms. Static: binding before run time Dynamic: binding at runtime. Bindings lifetime: the period of time between the creation and the destruction of a name-to-object binding Objects lifetime: the time between the creation and the destruction of an object Note that the binding time of a name to an object and the lifetime of the object may not necessarily the same, e.g., a dangling reference.

Organization of Programming Languages

CS 3342

2. Storage management
Object lifetime generally correspond to one of the following three storage allocation mechanisms: 1. Static objects are given an absolute address that is retained throughout the programs execution. 2. Stack objects are allocated and deallocated in last-in, rst-out order, usually in conjunction with subroutine calls and returns. 3. Heap objects may be allocated and deallocated at arbitrary times. They require a more general (and expensive) storage management algorithm.
Organization of Programming Languages CS 3342

Static Allocation This method is used for storing global variables numeric and string-valued constant literals instructions local variables that retain value between calls (e.g., static) tables for debugging, dynamic type checking, garbage collection, exception handling, and other purposes Note that statically allocated objects whose value should not change (e.g., instructions, constants, some tables) are often allocated in protected read-only memory.
Organization of Programming Languages CS 3342

Limitations of this method does not allow recursion data structure cannot be created dynamically

Static allocation of space for subroutines in a language without recursion.

Organization of Programming Languages

CS 3342

Stack-Based Allocation If a language permits recursion, the number of instances of a variable that may exist at the same time is conceptually unbounded. In this case, static allocation is no longer an option. Stack allocation is a natural choice. Maintenance of the stack is the responsibility of the subroutine calling sequence: the code executed by the caller immediately before and after the call and the code executed at the beginning and at the end of the routine itself. Data needed for an activation of a subroutine is collected in a record called an activation record or frame. The record contains storage for local variables, formal parameters, and any additional information needed for the activation.
Organization of Programming Languages CS 3342

Example:

Organization of Programming Languages

CS 3342

Organization of Programming Languages

CS 3342

Heap-Based Allocation A heap is a region of storage in which subblocks can be allocated and deallocated at arbitrary time. Heaps are required for the dynamically allocated objects, e.g., character strings, lists, sets, and dynamically resized objects. Examples: new in C++ and Java and alloc in C. Space concerns can be subdivided into issues of internal and external fragmentation:

Organization of Programming Languages

CS 3342

internal fragmentation * an allocated block is larger that required to hold a given object * the extra space is unused external fragmentation * unused space is composed of multiple blocks * no one piece of it may be large enough to satisfy some future request

Organization of Programming Languages

CS 3342

Allocation algorithms: a single free list * rst t algorithm: select the rst block that is large enough to satisfy the request * best t algorithm: nd the smallest block that is large enough to satisfy the request In either cases, if the chosen block is signicant larger than required, the block is divided into two and the unneeded portion is returned to the free list.

Organization of Programming Languages

CS 3342

When a block is deallocated, it is returned to the free list and coalesced with neighboring free blocks. Best t algorithm may do a better job of reserving larger blocks for larger requests. It may also have a higher cost than the rst t algorithm.

Organization of Programming Languages

CS 3342

pool of standard sizes * buddy system: the standard block sizes are 2k for k 1 * Fibonacci heaps: the sizes are Fibonacci numbers 1, 1, 2, 3, 5, 8, ... In the buddy system, if a block of size 2k is needed, but none is available, a block of size 2k+1 is split into two. When a block is deallocated, it is coalesced with its buddy in a buddy system or a nearest neighbor in a Fibonacci heap.

Organization of Programming Languages

CS 3342

Garbage Collection mechanism to identify and reclaim unreachable objects two types of deallocations: explicit manual deallocation and automatic garbage collection explicit manual deallocation: simple, fast, but error prone Examples: free() with alloc() in C, and delete() with new() in C++ and Java automatic garbage collection: consume nontrivial amount of time, easy for programmers, more reliable Availability: in most functional languages, Modula-3, Java, C#, etc.
Organization of Programming Languages CS 3342

3. Scope Rules
Scope of a binding: the textual region of the program in which a binding is active scope (without a specic binding in mind): a program region of maximal size in which no binding change Typically, a scope is the body of a module, class, subroutine, or a block. elaboration: the process by which declarations become active when control rst enters a scope Elaboration entails the creation of bindings.
Organization of Programming Languages CS 3342

referencing environment: the set of active bindings at any given point in a programs execution Referencing environment is decided by - scope rules (static scope or dynamic scope) - binding rules (deep binding or shallow binding) deep binding: the choice is made when the reference is rst created shallow binding: the choice is made when the reference is nally used

Organization of Programming Languages

CS 3342

Static Scope static (lexical) scope: the binding between names and objects are determined at compile time by examining the text of the program without consideration of the ow of control at run time closest nested scope rule: a name is known in the scope in which it is declared and in each internally nested scope unless it is hidden by another declaration of the same name in one or more nested scopes A collection of built-in or predened objects, such as predened types and functions, are considered to be declared in an extra, outermost scope.
Organization of Programming Languages CS 3342

procedure P1 (A1 : T1); var X : real; ... procedure P2 (A2 : T2); ... procedure P3 (A3 : T3); ... begin ... (* body of P3 *) end; ... begin ... (* body of P2 *) end; ... procedure P4 (A4 : T4); ... function F1 (A5 : T5) : T6; var X : integer; ... begin ... (* body of F1 *) end; ... begin ... (* body of P4 *) end; ... begin ... (* body of P1 *) end;
Organization of Programming Languages

P2 and P4 can only be called within P1. P3 is visible only within P2. Note that F1 could call P2 and P4 could call F1, but P2 could not call F1.

CS 3342

Finding a binding: search from the current, innermost scope outward until nding it (an error occurs if not until the outermost scope) Each frame maintains a static link to its parent frame. If a subroutine is nested k levels deep, then its frames static link, and those of its parent, grandparent, and so on, will form a static chain of length k at run time.

Organization of Programming Languages

CS 3342

Example:

Organization of Programming Languages

CS 3342

Modules A module allows a collection of objects subroutines, variables, types, and so on to be encapsulated in such a way that (1) objects inside are visible to each other, but (2) objects on the inside are not visible on the outside unless explicitly exported, and (3) (in many languages) objects outside are not visible on the inside unless explicitly imported Modules can be found in Clu (which calls them clusters), Modula 1, 2, and 3, Turing, Ada (which calls the packages), C++ (which calls them namespaces), and many others. There are at least two types of modules: module-as-manager and
Organization of Programming Languages CS 3342

module-as-type.

Organization of Programming Languages

CS 3342

Organization of Programming Languages

CS 3342

Organization of Programming Languages

CS 3342

Dynamic Scope the binding between names and objects depend on the ow of control at run time and, in particular, on the order in which subroutines are called. the current binding for a given name is the one encountered most recently during execution, and not yet destroyed by returning from its scope. In the following example, if static scoping is in eect, this program prints a 1. If dynamic scoping is in eect, the program prints a 1 or a 2, depending on the value read at line 8 at run time.

Organization of Programming Languages

CS 3342

Example 1. a : integer 2. procedure rst 3. a := 1 4. procedure second 5. a : integer 6. rst() 7. 8. 9. 10. 11. 12. local declaration global declaration

a := 2 if read integer() > 0 second() else rst() write integer(a)


CS 3342

Organization of Programming Languages

Which a (global or local) the a on line 3 is referring to will depend on whether we enter rst through second or directly from the main program. If we enter through second, we will assign the value 1 to the local a. If we enter from the main program, we will assign the value 1 to the global a. In any case, the write at line 12 will refer to the global a. Languages with dynamic scoping include APL, Snobol, early dialects of Lisp, and Perl.

Organization of Programming Languages

CS 3342

The Binding of Referencing Environments Consider the creation of a reference to a subroutine (e.g., by passing it as a parameter). The scope rules are applied to such a routine: (1) when the reference is rst created, or (2) when the routine is nally called. The late binding of the referencing environment of a subroutine that has been passed as a parameter is known as shallow binding. The early binding of the referencing environment is known as deep binding. Look at the next example.
Organization of Programming Languages CS 3342

Organization of Programming Languages

CS 3342

Implementing Scope Static scope: a compiler relies on a symbol table which maps names to the their information. The most basic operations include (1) placing a new mapping into the table and (2) retrieving the information for a given name. Dynamic scope: perform the similar operations as with static scoping but at run time. Implementations include association list and central reference table. An association list (or A-list) is a list of name/value pairs, which functions as a stack: new declarations are pushed as they are encountered and popped at the end of the scope. Bindings are found by searching down the list from the top.
Organization of Programming Languages CS 3342

A central reference table maintains an explicit mapping from names to their current meanings. Lookup is faster, but scope entry and exit are more complex. It is dicult to save a referencing environment for future use. Subroutine Closures used to implement deep binding consist of (1) an explicit representation of a referencing environment (the one in which the subroutine would execute if called at the present time) and (2) a reference to the routine.

Organization of Programming Languages

CS 3342

With association lists (A-lists) the referencing environment in a closure can be represented by a top-of-stack (beginning of A-list) pointer when a subroutine is called through a closure, the main pointer to the A-list is replaced by the new pointer and the pointer to the closure is saved. It is restored when the subroutine returns.

Organization of Programming Languages

CS 3342

With central reference tables save the environment by copying the main array and the rst entries of the list for the interesting names when the subroutine is called through a closure, these entries can then be pushed onto the beginning of the appropriate lists in the central reference table. Deep scoping. scoping. a 1 with binding is generally the default in languages with static However, binding time does make dierences in static See the following example of a Pascal program. It prints deep binding and a 2 with shallow binding.

Organization of Programming Languages

CS 3342

Organization of Programming Languages

CS 3342

Note that binding rules matter with static scoping only when accessing objects that are neither local nor global. Binding rules are irrelevant in C-family of languages since no nested subroutines are allowed in these languages. First-, Second- and Third-Class Values First class: can be passed as a parameter, returned from a subroutine, or assigned into a variable. Second class: can be passed as a parameter, but not returned from a subroutine or assigned into a variable. Third class: cannot be passed as a parameter.

Organization of Programming Languages

CS 3342

Labels are third-class values in most programming languages but second-class values in Algol. Subroutines are second-class values in most imperative languages but third-calss values in Ada 83. They are rstclass values in all functional languages and also in C#, Perl, and Python.

Organization of Programming Languages

CS 3342

Das könnte Ihnen auch gefallen