Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP3190: Principle of Programming Languages

Similar presentations


Presentation on theme: "COMP3190: Principle of Programming Languages"— Presentation transcript:

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

2 Outline The Notion of Binding time
Object lifetime and storage management Scope rules Implementation scope The Binding of Referencing Environments Overloading

3 Binding Times Compile time. Link time. Load time. Run 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.

4 Characteristics of Binding Times
Early binding times Efficiency Compiled languages Later binding times Flexibility Interpreted languages

5 Outline The Notion of Binding time
Object lifetime and storage management Scope rules Implementation scope The Binding of Referencing Environments Overloading

6 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

7 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

8 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.

9 Static variables in C

10 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.

11 Stack Frame Specified at compile time. Specified at run 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.

12 Stack Frame Example

13 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.

14 Heap Fragmentation Problem
Internal fragmentation: part of a block is unused External fragmentation: unused space consists of many small blocks Which approach, first fit or best fit, results in lower external fragmentation?

15 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.

16 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.

17 Outline The Notion of Binding time
Object lifetime and storage management Scope rules Implementation scope The Binding of Referencing Environments Overloading

18 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).

19 Static Scope Current binding for name: the one encountered most recently in top-to-bottom scan of the program text. Nested subroutines Modules

20 Nested Subroutines in Pascal
Visible: P2, P4 within P1 P3 within P2 F1 within P4 Can: F1 call P2? P4 call F1? P2 call F1? Which X: In F1? In P4? In P2?

21 Static Chains Nested calls: A, E, B, D, C Static link: to the frame of
the most recent invocation of the lexically surrounding subroutine

22 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)

23 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.

24 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 usually a bad idea

25 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.

26 Example Most appropriate for: older_than: deep binding
(to get global threshold) print_person: shallow binding (to get locally set line_length) (dynamic scoping assumed) threshold: integer

27 Outline The Notion of Binding time
Object lifetime and storage management Scope rules Implementation scope The Binding of Referencing Environments Overloading

28 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.

29 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

30 Outline The Notion of Binding time
Object lifetime and storage management Scope rules Implementation scope The Binding of Referencing Environments Overloading

31 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.

32 Overloading Subroutine with polymorphic parameters (unconverted).
Single body of code. Behaviour is customized. Generic subroutines (templates). separate, similar, not identical, copies of code


Download ppt "COMP3190: Principle of Programming Languages"

Similar presentations


Ads by Google