Names, Scopes and Bindings - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Names, Scopes and Bindings

Description:

In each allocation, search for a block of adequate size. ... x (car set)) true) (else (element-of-set? x (cdr set) (define (adjoin-set x set) ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 39
Provided by: evangelo
Category:
Tags: bindings | names | scopes

less

Transcript and Presenter's Notes

Title: Names, Scopes and Bindings


1
Names, Scopes and Bindings
2
Binding Times
  • Compile time.
  • Mapping of HLL 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.

3
Importance of Binding Times
  • Early binding times
  • Efficiency
  • Compiled languages
  • Later binding times
  • Flexibility
  • Interpreted languages

4
Binding-related Events
  • Creation of objects
  • Creation of bindings
  • References to variables, subroutines, types,
  • Temporary deactivation/reactivation of bindings
  • Destruction of bindings
  • Destruction of objects

5
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

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

7
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.
  • Caller code.
  • Callee code.
  • Prologue.
  • Epilogue.

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

9
Stack Frame Example
10
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.

11
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?
12
Cost of 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 2k1.
  • 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.

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

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

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

16
Local variables in Scheme
  • (let ((x 2) (y 3)) ( x y))
  • (let ((x 2) (y 3)) (let ((x 7)
    (z ( x y))) ( z x)))

17
Nested Functions in Scheme
  • (define (test x y) (let ((fn (lambda (a b)
    ( a b)))) (fn ( x y) ( x y))))
  • (define (test x y) (define (fn a b) ( a
    b)) (fn ( x y) ( x y)))
  • (test 2 3) gt ???

18
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?
19
Static Chains
Nested calls A, E, B, D, C Static link to the
frame of the most recent invocation of the
lexically surrounding subroutine
20
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)

21
Static variables in C
22
Modules in Modula-2
Notice Push, pop visible to each other Push,
pop visible to modules that import thems, top
not visible outside stack module
Main program
23
Module types
Multiple instances of a given abstraction Classes
module types inheritance Every instance of
a module type or class has its own copy of the
module or class variables
24
Data abstraction in Scheme (I)
A set is a collection of distinct
objects. Represent set as a list. (define
(element-of-set? x set) (cond ((null? set)
false) ((equal? x (car set)) true)
(else (element-of-set? x (cdr
set))))) (define (adjoin-set x set) (if
(element-of-set? x set) set
(cons x set))) (define (intersection-set set1
set2) (cond ((or (null? set1) (null? set2))
()) ((element-of-set? (car set1)
set2) (cons (car set1)
(intersection-set (cdr set1) set2)))
(else (intersection-set (cdr set1)
set2)))) What is the drawback of the above? Is
it true data abstraction?
25
Data abstraction in Scheme (II)
  • What is the drawback of the above? Is it true
    data abstraction?
  • Set object is immutable.This can be fixed by
    defining adjoin-set as a mutation on its
    argument).
  • Possible to access list representation directly.
  • Use local state of procedures to hide the state
    of the object.

26
Data abstraction in Scheme (III)
(define (make-account balance) (define
(withdraw amount) (if (gt balance amount)
(begin (set! balance (- balance amount))
balance) "Insufficient
funds")) (define (deposit amount) (set!
balance ( balance amount)) balance)
(define (dispatch m) (cond ((eq? m 'withdraw)
withdraw) ((eq? m 'deposit) deposit)
(else (error "Unknown request -
MAKE-ACCOUNT" m))))
dispatch) (define acc (make-account 50)((acc
deposit) 40) gt ???((acc withdraw) 60) gt
???
27
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.

28
Dynamic scope example
What does the program print? Under static
scoping? 1 regardless of value
read Under dynamic scoping? 2 if
value read is gt0 1 if value read is lt
0 Dynamic scoping is usually a bad idea
29
Static scope implementationSymbol Tables
  • Symbol table as dictionarymaps names to
    information the compiler knows about them (scope
    number, type).
  • Scope representation
  • enter_scope and leave_scope ops
  • Each scope gets a serial number
  • Scope stack scopes in the current referencing
    environment.
  • Scope number
  • Whether scope is closed

30

Hash table
Scope stack
2(T)
find the error in the figure!
1(globals)
4(P1)
3(M)
5(P2)
31
Looking up a name
hardcopy
  • Scan hash chain looking for the name
  • For each matching entry, scan down scope stack to
    see if its scope is visible
  • Imports and exports are made visible outside
    their normal scope by creating additional entries
    for them, which point to real entries.

32
Algorithm for symbol table look-up operation
33
Dynamic Scope implementation
A-list
single stack of bindings
Central reference tbl
stack of entries for each name
34
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.

35
Example
Most appropriate for older_than deep binding
(to get global threshold) print_person shallow
binding (to get locally set line_length) (dyna
mic scoping assumed)
threshold integer
36
Subroutine closures
  • In deep binding, a 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.

37
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

38
Overloading
  • A name that can refer to more than one object in
    a given scop is overloaded.
  • Overloaded arithmetic operators
  • Coercion compiler automatically converts an
    object into another type when required.
  • Subroutine with polymorphic parameters
    (unconverted).
  • Single body of code.
  • Behaviour is customized.
  • Generic subroutines (templates).
  • separate, similar, not identical, copies of code
Write a Comment
User Comments (0)
About PowerShow.com