COMP3190: Principle of Programming Languages Names, Scopes and Bindings.
-
Upload
brianne-bryan -
Category
Documents
-
view
217 -
download
3
Transcript of COMP3190: Principle of Programming Languages Names, Scopes and Bindings.
COMP3190: Principle of Programming Languages
Names, Scopes and Bindings
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
Binding Times
Compile time. Mapping of high level constructs to machine
code. Layout of static data in memory.
Link time. Resolves intermodule references.
Load time. Machine addresses assigned.
Run time. Bindings of values to variables.
Characteristics of Binding Times
Early binding times Efficiency Compiled languages
Later binding times Flexibility Interpreted languages
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
Binding-related Events life time Creation of objects Creation of bindings References to variables,
subroutines, types,… Temporary deactivation/reactivation
of bindings Destruction of bindings Destruction of objects
Storage Allocation
Static: objects get fixed absolute address
Stack: objects allocated on the stack in connection with subroutine calls
Heap: objects allocated/deallocated at arbitrary times Explicitly by the programmer Implicitly by the garbage collector
Static Objects Global variables. Variables local to a subroutine, but
retain value across invocations. Constant literals. Tables for run-time support (e.g.
debugging, type checking, etc.). Space for subroutines, incl. local
variables, in language with no recursion.
Static variables in C
Stack-based Allocation Space for subroutines in a language
that permits recursion (stack frame or activation record). Arguments, local variables. Return values.
Subroutine calling sequence.
Stack Frame Specified at compile time.
Offsets of objects within a frame. Frame pointer (register pointing to a
known location of the current stack frame).
Stack pointer (register pointing to the first unused location on the stack).
Specified at run time. The absolute location of stack frame in
memory.
Stack Frame Example
Heap-based Allocation
Heap: region of storage in which blocks can be allocated/deallocated at arbitrary times.
Storage management: Free list: linked list of free blocks. In each allocation, search for a block of
adequate size. First fit: grab first block that is large enough. Best fit: grab smallest block that is large enough.
Heap Fragmentation Problem
Internal fragmentation: part of a block is unusedExternal fragmentation: unused space consists of many small blocks
Which approach, first fit or best fit, results in lower external fragmentation?
Heap-based Allocation Single free list: linear cost in the number of
free blocks. Separate free lists for blocks of different
sizes. Buddy system.
If block of size 2k is unavailable, split a block of size 2k+1.
If block of size 2k is deallocated, it may be coalesced with the other block (buddy).
Fibonacci heap. To eliminate external fragmentation, heap
can be compacted.
Heap-based Deallocation
Explicitly by the programmer. Efficient. May lead to very nasty bugs.
Dangling pointers/references (deallocate too soon).
Memory leaks (deallocate too late).
Automatically by the garbage collector.
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
Scope Rules Scope of a binding: a program region
(textually) in which binding is active. Scope: a program region of maximal
size where no bindings change. Scoping can be:
Lexical or static (bindings known at compile time).
Dynamic (bindings depend on flow of execution at run time).
Static Scope
Current binding for name: the one encountered most recently in top-to-bottom scan of the program text.
Nested subroutines Modules
Nested Subroutines in Pascal
Visible:P2, P4 within P1P3 within P2F1 within P4Can:F1 call P2?P4 call F1?P2 call F1?Which X:In F1?In P4?In P2?
Static ChainsNested calls:A, E, B, D, C
Static link:to the frame of the most recentinvocation of the lexically surroundingsubroutine
Information Hiding
Make objects and algorithms invisible to portions of the software system that do not need them.
Information hiding Static variables in C (permanent state)
Module (effectively a single instance of class)
Module type (effectively a class with no inheritance)
Class (module type + inheritance)
Dynamic Scope
The current binding for a given name is
the one encountered most recently during execution, and
not yet destroyed by returning from its scope.
Dynamic scope example
What does the program print?
Under static scoping? 1 regardless of value read
Under dynamic scoping? 2: if value read is >0 1: if value read is <= 0
Dynamic scoping is usuallya bad idea
Shallow & Deep binding
When subroutine is passed as a parameter, when is the referencing environment bound?
Shallow binding: when the routine is called
Deep binding: when the routine is first passed as a parameter.
Important in both dynamic and static scoping.
Example
threshold: integer
Most appropriate for:
older_than: deep binding (to get global threshold)
print_person: shallow binding (to get locally set line_length)
(dynamic scoping assumed)
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
Subroutine closures
In deep binding, a subroutine closure is a bundle of: A referencing environment Reference to the subroutine
Deep binding is: an option in dynamically scoped
langs. The default in statically scoped langs.
First- and second-class subroutines
First class subroutines Passed as parameter Returned from a subroutine Assigned into a variable
Second class subroutines Passed as parameter only
Third class subroutines None of the above
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
Overloading
A name that can refer to more than one object in a given scope is overloaded. Overloaded arithmetic operators Coercion: compiler automatically
converts an object into another type when required.
Overloading
Subroutine with polymorphic parameters (unconverted). Single body of code. Behaviour is customized.
Generic subroutines (templates). separate, similar, not identical, copies
of code