name binding and object lifetimes prepared by manuel e. bermúdez, ph.d. associate professor...

34
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 10

Upload: griselda-alicia-gallagher

Post on 27-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Name Binding and Object Lifetimes

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language ConceptsLecture 10

Page 2: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Names

• Pervasive in programming languages.• Not limited to identifiers ('+' can be a

name)• Refer to variables, constants,

operations, types, files, functions, procedures, modules, etc.

• Must be tracked in any compiler, usually in a symbol table.

Page 3: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Name Binding

• Association between a name and the object its represents.

• The term "object" denotes an entity or concept in the language.

Page 4: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Binding Can Occur at Various Times

• Language design time.• Example: the type referred to by the

name int.

• Language implementation time.• Example: the names of library

routines in C, e.g. printf.

Page 5: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Binding Can Occur at Various Times (cont’d)

• Program writing time. Names of data structures, modules.• Example: names of debugging flags

for C preprocessor:

#define DEBUGGING 1 ...

#if DEBUGGING

printf( ... );

#endif

Page 6: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Binding Can Occur at Various Times (cont’d)

• Compile time: most bindings take place here.

• Link time: modules compiled separately are linked together, inter-module references resolved. • Example: location of library functions.

• Load time:• program given a load point,• translate virtual memory addresses into

physical memory addresses.

Page 7: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Binding Can Occur at Various Times (cont’d)

• Run time. Variables bound to values (memory allocated).

• Sub-categories include:• program start-up time,• module entry time,• elaboration time (allocate

memory for a declared variable),• routine call time (set up

activation record),• execution time.

Page 8: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Terminology

• Static usually means "before run time".

• Dynamic usually refers to run time.

• Tradeoff: In general,

• Early binding -> greater efficiency, less flexibility.

• Late binding -> more flexibility, less efficiency.

Page 9: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Examples

• Early binding: most compiled implementations: C, C++, Java.

• Compilers analyze semantics, allocate memory in advance, generate smarter code.

• Can't predict location of a local variable at run time.

• Can arrange for a variable to live at a fixed offset from a run-time register.

Page 10: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Examples (cont’d)

• Late binding: most interpreted languages, e.g. RPAL, BASIC, perl, shell script languages, Smalltalk.

• More analysis done at run time, less efficient.

Page 11: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Static Type-Checking

• a.k.a. static semantic analysis,• a.k.a. contextual constraint analysis:• A context-sensitive issue, handled by name

associations.• Must span arbitrary distances across the

tree.• Context-free grammar can't do this.• Whenever X is used in a program,

• must find declaration for X,• must connect X's USE with its

declaration.

Page 12: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture
Page 13: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Scope Rules

• Govern which names are visible in which program segments.

• Declaration: Binding of name and a "descriptor": information about type, value, address, etc.

Page 14: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Examplesconst x=7;

Binds x to (7,type integer).

var x:integer; Binds x to (address, type integer), if global Binds x to (stack offset, type integer), if local

type T = record y: integer; x: real; end; Binds y to (record offset, type integer). Binds x to (record offset, type real)

Binds T to (is_record_type, list of fields)

Page 15: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Object Lifetime and Storage Management

• Issues:

• Object creation.• Binding creation.• Name references (binding usages).• Activation, deactivation, reactivation of

bindings (mostly due to scope rules).• Binding destruction• Object destruction.

Page 16: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Definitions

• Binding Lifetime:• Period of time between creation and

destruction of a binding.

• Object lifetime: • Period between creation and

destruction of an object.

• Binding lifetime usually shorter than object lifetime.

Page 17: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Example: Passing a Variable by Reference.

main() {

int n=3; // object n exists

f(&n); // throughout, but ...

}

void f(int * p) {

*p = 4;

// binding of p is temporary

}

Page 18: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Binding Destruction Can Be Trouble

• Example: (classic no-no in C)

int *f() {

int p;

return(&p);

// binding of p is destroyed,

// but object (address) stills

// exists.

}

Page 19: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Binding Lifetime Can Be Longer Than Object Lifetime

• Example (in C):

char *p = malloc(4); strcpy(p, "abc"); free(p); // object gone, but // binding of p, to a // useless address, lives on. strcpy(p, "abc"); // Bad things can happen.

Called a dangling reference: binding of a name to an object that no longer exists.

Page 20: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Storage Allocation Mechanisms

• Three main storage allocation mechanisms:

1. Static objects:• Retain absolute address throughout.• Examples: global variables, literal

constants: "abc", 3.

Page 21: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Storage Allocation Mechanisms (cont’d)

2. Stack objects: • Addresses relative to a stack (segment)

base, usually in conjunction with fcn/proc calls.

3. Heap objects. Allocated and deallocated at programmer's discretion.

Page 22: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Static Space Allocation

• Special case: No recursion.

• Original Fortran, most BASICs.

• Variables local to procedures allocated statically, not on a stack. Procedures can share their local variables !

• No more since Fortran 90.

Page 23: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Stack-Based Space Allocation

• Necessary if recursion is allowed.

• Each instance of an active procedure occupies one activation record (or frame) on the stack.

• Frame organization varies by language and implementation.

Page 24: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

General Scheme

int g; main()

{A();}

fcn A() bp: Base pointer. {A(); B();} For global

references. proc B() fp: Frame pointer.

{C();} For local references. proc C() sp: Stack pointer.

{} Top of stack.

Page 25: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture
Page 26: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Example: (see diagram)

int g=2; // g: global address 0

main() { int m=1; // m: local address 0 print(A(m)); // return address 1} int A(int p) { // p: local address 1 int a; // a: local address 3 if p=1 return 1+A(2); // return address 2 else return B(p+1); // return address 3}int B(int q) { // q: local address 1 int b=4; // b: local address 3 print(q+b+g); // situation depicted HERE}

Page 27: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture
Page 28: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Heap-Based Allocation

• Heap: Memory market. • Memory region where memory

blocks can be bought and sold (allocated and deallocated).

• Many strategies for managing the heap.

Page 29: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Heap-Based Allocation (cont’d)

• Main problem: fragmentation. • After many allocations and

deallocations, many small free blocks scattered and intermingled with used blocks.

Heap

Allocation request

Page 30: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Heap-Based Allocation (cont’d)

• Internal fragmentation: • Allocate larger block than needed.

Space wasted.• External fragmentation:

• Can't handle a request for a large block. Plenty of free space, but no large blocks available. Need compaction, expensive.

• Often use a linked list of free blocks.• First fit strategy: allocate first block

that suffices. More efficient, but more fragmentation.

Page 31: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Heap-Based Allocation (cont’d)

• Best fit strategy: allocate smallest block that suffices. Less efficient, less fragmentation.

• Maintain "pools" of blocks, of various sizes.• "Buddy system": blocks of size 2k.

Allocate blocks of nearest power of two. If no blocks available, split up one of size 2k+1. When freed later, "buddy it" back, if possible.

Page 32: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Heap-Based Allocation (cont’d)

• Fibonacci heap: Use block sizes that increase as Fibonacci numbers do:

f(n)=f(n-1)+f(n-2) instead of doubling.

• Allocation is usually explicit in PL's:• malloc, new.

Page 33: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Heap Deallocation

• Implicit: Garbage Collection (Java).• Automatic, but expensive (getting

better).

• Explicit: free (C, C++), dispose (Pascal).• Risky. • Very costly errors, memory leaks, but

efficient.

Page 34: Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture

Name Binding and Object Lifetimes

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language ConceptsLecture 10