comp3190: principle of programming languages names, scopes and bindings
TRANSCRIPT
![Page 1: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/1.jpg)
COMP3190: Principle of Programming Languages
Names, Scopes and Bindings
![Page 2: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/2.jpg)
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
![Page 3: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/3.jpg)
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.
![Page 4: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/4.jpg)
Characteristics of Binding Times
Early binding times Efficiency Compiled languages
Later binding times Flexibility Interpreted languages
![Page 5: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/5.jpg)
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
![Page 6: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/6.jpg)
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
![Page 7: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/7.jpg)
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
![Page 8: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/8.jpg)
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.
![Page 9: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/9.jpg)
Static variables in C
![Page 10: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/10.jpg)
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.
![Page 11: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/11.jpg)
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.
![Page 12: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/12.jpg)
Stack Frame Example
![Page 13: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/13.jpg)
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.
![Page 14: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/14.jpg)
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?
![Page 15: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/15.jpg)
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.
![Page 16: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/16.jpg)
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.
![Page 17: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/17.jpg)
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
![Page 18: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/18.jpg)
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).
![Page 19: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/19.jpg)
Static Scope
Current binding for name: the one encountered most recently in top-to-bottom scan of the program text.
Nested subroutines Modules
![Page 20: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/20.jpg)
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?
![Page 21: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/21.jpg)
Static ChainsNested calls:A, E, B, D, C
Static link:to the frame of the most recentinvocation of the lexically surroundingsubroutine
![Page 22: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/22.jpg)
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)
![Page 23: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/23.jpg)
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.
![Page 24: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/24.jpg)
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
![Page 25: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/25.jpg)
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.
![Page 26: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/26.jpg)
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)
![Page 27: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/27.jpg)
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
![Page 28: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/28.jpg)
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.
![Page 29: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/29.jpg)
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
![Page 30: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/30.jpg)
Outline The Notion of Binding time Object lifetime and storage
management Scope rules Implementation scope The Binding of Referencing
Environments Overloading
![Page 31: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/31.jpg)
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.
![Page 32: COMP3190: Principle of Programming Languages Names, Scopes and Bindings](https://reader036.vdocument.in/reader036/viewer/2022062722/56649f345503460f94c51be9/html5/thumbnails/32.jpg)
Overloading
Subroutine with polymorphic parameters (unconverted). Single body of code. Behaviour is customized.
Generic subroutines (templates). separate, similar, not identical, copies
of code