comp3190: principle of programming languages names, scopes and bindings

32
COMP3190: Principle of Programming Languages Names, Scopes and Bindings

Upload: brianne-bryan

Post on 05-Jan-2016

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: COMP3190: Principle of Programming Languages Names, Scopes and Bindings

COMP3190: Principle of Programming Languages

Names, Scopes and Bindings

Page 2: 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

Page 3: COMP3190: Principle of Programming Languages Names, Scopes and Bindings

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

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

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

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

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

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

Static variables in C

Page 10: COMP3190: Principle of Programming Languages Names, Scopes and Bindings

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

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

Stack Frame Example

Page 13: COMP3190: Principle of Programming Languages Names, Scopes and Bindings

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Overloading

Subroutine with polymorphic parameters (unconverted). Single body of code. Behaviour is customized.

Generic subroutines (templates). separate, similar, not identical, copies

of code