Lecture 7: Binding Time and Storage

COMP 524 Programming Language Concepts Stephen Olivier February 5, 2009

Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts

The University of North Carolina at Chapel Hill Goal of Lecture

•The Goal of this lecture is to discuss object binding and .

The University of North Carolina at Chapel Hill 2 High-Level Programming Languages

•High-Level Programming languages are defined by two characteristics • Machine “independence” • Ease of programming

The University of North Carolina at Chapel Hill 3 Machine “Independence”

•With few exceptions, the code of a high-level language can be compiled on any system • For example cout << “hello world”<< endl; means the same thing on any machine •However, few languages are completely machine independent. •Generally, the more machine dependent a language is the more “efficient” it is.

The University of North Carolina at Chapel Hill 4 Ease of Programming

Names

Control Flow

Types

Subroutines

Object Orientation

Concurrency

Declarative Programming The University of North Carolina at Chapel Hill 5 Naming

•Naming is the process by which a programer associates a name with a potentially complicated program fragment. • Purpose is to hide complexity. • For example, to designate variables, types, classes, operators, etc ...

The University of North Carolina at Chapel Hill 6 Naming

•Naming is the process by which a programer associates a name with a potentially complicated program fragment. By naming an • Purpose is to hide complexity. object we • For example, to designate variables, types, classes, operators, etc ... make an abstraction

The University of North Carolina at Chapel Hill 7 Abstractions

•Control abstractions allows programs to “hide” complex code behind simple interface • and functions • Classes. •Data abstraction allow the programer to hide data representation details behind abstract operations • Abstract Data Types (ADTs) • Classes.

The University of North Carolina at Chapel Hill 8 Binding Time

•A binding is an association between any two things • Name of an object and the object. • A Dook student to a loosing basketball team. •Binding Time is the time at which a binding is created.

The University of North Carolina at Chapel Hill 9 Binding Time

Language Design Time

Language Implementation Time Increasing Efficiency Increasing

Program Writing Time

Compile Time

Link Time Increasing Flexibility

Load Time

Run Time The University of North Carolina at Chapel Hill 10 Object Lifetime

•Object lifetimes have two components • Lifetime of the object. • Lifetime of the binding. •These two don’t necessarily correspond. • For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the object does not exist even though the binding does. • For example in C++, when the value pointed to by an object is deleted the binding is gone before the object.

The University of North Carolina at Chapel Hill 11 Object Lifetimes

•Object Lifetimes correspond to three principal storage allocation mechanisms, • Static objects, which have an absolute address • Stack objects, which are allocated and deallocated in a Last- In First-Out (LIFO) order • Heap objects, which are allocated and deallocated at arbitrary times.

The University of North Carolina at Chapel Hill 12 Static Allocation •Under static allocation, objects are given an absolute address that is retained through the program’s execution • e.g., global variables

Temps. Temps.

Temps. Local Variables Local Variables Local Variables Misc. Bookkeeping Misc. Misc. . . . Bookkeeping Bookkeeping Return Address

Return Address Return Address Arguments & Arguments & Arguments & Returns Returns Returns

SubroutineThe University 1 of NorthSubroutine Carolina at2 Chapel Hill X 13 Stack Allocation

•Under stack-based allocation, objects are allocated in a Last-In First-Out (LIFO) basis called a stack. • e.g., recursive subroutine parameters.

sp Args to called routines. Subroutine D fp Temps

Subroutine C Local Variables

Misc. Subroutine B Bookkeeping fp Subroutine A Return Address (called from main) TheStack growth University of North Carolina at Chapel Hill 14 fp is the “Frame Pointer” Stack Allocation

•Under stack-basedsp is the allocation “Stack ,Pointer objects are” allocated in a Last-In First-Out (LIFO) basis called a stack. • e.g., recursive subroutine parameters.

sp Args to called routines. Subroutine D fp Temps

Subroutine C Local Variables

Misc. Subroutine B Bookkeeping fp Subroutine A Return Address (called from main) TheStack growth University of North Carolina at Chapel Hill 15 Calling Sequence

•On procedure call and return compilers generate code that execute to manage the runtime stack. • Setup at call to procedure foo(a,b). • Prologue before foo code executes. • Epilogue at the end of foo code. • “Teardown” right after calling the code.

The University of North Carolina at Chapel Hill 16 Setup foo(a,b)

•Move sp to allocate a new stack frame •Copy args a,b into frame •Copy return address into frame •Set fp to point to new frame •Maintain static chain or display •Move PC to procedure address

The University of North Carolina at Chapel Hill 17 Setup foo(a,b)

•Move sp to allocate a new stack frame •Copy args x,y into frame •Copy return address into sp frame •Set fp to point to new frame Subroutine Bar fp •Maintain static chain or Subroutine B

display Subroutine A (called from main) •Move PC to procedure address

The University of North Carolina at Chapel Hill 18 Setup foo(a,b)

•Move sp to allocate a new stack frame •Copy args x,y into frame sp Subroutine foo •Copy return address into frame •Set fp to point to new frame Subroutine Bar fp •Maintain static chain or Subroutine B

display Subroutine A (called from main) •Move PC to procedure address

The University of North Carolina at Chapel Hill 19 Setup foo(a,b)

•Move sp to allocate a new stack frame •Copy args x,y into frame sp Subroutine foo •Copy return address into

frame x & y •Set fp to point to new frame Subroutine Bar fp •Maintain static chain or Subroutine B

display Subroutine A (called from main) •Move PC to procedure address

The University of North Carolina at Chapel Hill 20 Setup foo(a,b)

•Move sp to allocate a new stack frame sp •Copy args x,y into frame Subroutine foo fp •Copy return address into Return Ad frame x & y •Set fp to point to new frame Subroutine Bar

•Maintain static chain or Subroutine B

display Subroutine A (called from main) •Move PC to procedure address

The University of North Carolina at Chapel Hill 21 Setup foo(a,b)

•Move sp to allocate a new stack frame sp •Copy args x,y into frame Subroutine foo fp •Copy return address into Return Ad frame x & y •Set fp to point to new frame Subroutine Bar

•Maintain static chain or Subroutine B

display Subroutine A (called from main) •Move PC to procedure address

The University of North Carolina at Chapel Hill 22 Setup foo(a,b)

•Move sp to allocate a new stack frame sp •Copy args x,y into frame Subroutine foo fp •Copy return address into Return Ad frame A & B •Set fp to point to new frame We’llSubroutine ignore Bar this for now •Maintain static chain or Subroutine B

display Subroutine A (called from main) •Move PC to procedure address

The University of North Carolina at Chapel Hill 23 Setup foo(a,b)

•Move sp to allocate a new stack frame sp •Copy args x,y into frame Subroutine foo fp •Copy return address into Return Ad frame This changesA & B •Set fp to point to new frame whereSubroutine the code Bar

•Maintain static chain or is executed.Subroutine B display Subroutine A (called from main) •Move PC to procedure address

The University of North Carolina at Chapel Hill 24 Prologue

•Copy registers into local slots •Object initialization.

The University of North Carolina at Chapel Hill 25 Prologue

•Copy registers into local slots •Object initialization.

Return Ad fp x & y Subroutine Bar

The University of North Carolina at Chapel Hill 26 Prologue

•Copy registers into local slots •Object initialization.

All old reg. Old fp Return Ad fp x & y Subroutine Bar

The University of North Carolina at Chapel Hill 27 Prologue

•Copy registers into local slots •Object initialization.

Objects that areAll old used reg. are initialized.Old fp Return Ad fp x & y Subroutine Bar

The University of North Carolina at Chapel Hill 28 Epilogue

•Place return value into slot in frame. sp •Restore registers. Subroutine foo fp •Restore PC to return Return Ad address. x & y Subroutine Bar

Subroutine B

Subroutine A (called from main)

The University of North Carolina at Chapel Hill 29 Epilogue

•Place return value into slot in frame. sp •Restore registers. Subroutine foo fp •Restore PC to return Return Ad address. x & y Subroutine Bar Return Value

Subroutine B

Subroutine A (called from main)

The University of North Carolina at Chapel Hill 30 Epilogue Registers stored from “foo”’s subroutine are •Place return value into slot registered. in frame. sp •Restore registers. Subroutine foo fp •Restore PC to return Return Ad address. x & y Subroutine Bar Return Value

Subroutine B

Subroutine A (called from main)

The University of North Carolina at Chapel Hill 31 Epilogue The program resumes from where it began. •Place return value into slot in frame. sp •Restore registers. Subroutine foo fp •Restore PC to return Return Ad address. x & y Subroutine Bar Return Value

Subroutine B

Subroutine A (called from main)

The University of North Carolina at Chapel Hill 32 “Teardown”

•Move sp & fp (deallocate frame) sp •Move return values (if in Subroutine foo fp registers) Return Ad x & y Subroutine Bar Return Value

Subroutine B

Subroutine A (called from main)

The University of North Carolina at Chapel Hill 33 “Teardown”

•Move sp & fp (deallocate frame) •Move return values (if in registers) sp

Subroutine Bar Return Value fp Subroutine B

Subroutine A (called from main)

The University of North Carolina at Chapel Hill 34 “Teardown” If the return value was placed in a register, put •Move sp & fp (deallocate it in the stack. frame) •Move return values (if in registers) sp

Subroutine Bar Return Value fp Subroutine B

Subroutine A (called from main)

The University of North Carolina at Chapel Hill 35 Heap-based allocation

•In heap-based allocation, objects may be allocated and deallocated at arbitrary times. • For example, objects created with C++ .

The University of North Carolina at Chapel Hill 36 Heap Space Management

•In general, the heap is allocated sequentially. •This creates fragmentation...

The University of North Carolina at Chapel Hill 37 Internal fragmentation

•Internal fragmentation is caused when extra space within a single block is unused. Used

Unused The University of North Carolina at Chapel Hill 38 Internal fragmentation

•Internal fragmentation is caused when extra space within a single block is unused. Used

Caused by fixed block size.

The University of North Carolina at Chapel Hill 39 External Fragmentation

•External fragmentation occurs when there is sufficient available space for a new object, but there is no single block of free space large enough.

The University of North Carolina at Chapel Hill 40 External Fragmentation

•External fragmentation occurs when there is sufficient available space for a new object, but there is no single block of free space large enough.

The University of North Carolina at Chapel Hill 41 External Fragmentation

•External fragmentation occurs when there is sufficient available space for a new object, but there is no single block of free space large enough.

The University of North Carolina at Chapel Hill 42 External Fragmentation

•External fragmentation occurs when there is sufficient available space for a new object, but there is no single block of free space large enough.

The University of North Carolina at Chapel Hill 43 External Fragmentation

•External fragmentation occurs when there is sufficient available spaceCaused for a by new object, but there is no single block of free spacegaps large enough. between contiguous blocks allocated to existing objects.

The University of North Carolina at Chapel Hill 44 External Fragmentation

•May require heap compaction • Combine in the heap by moving existing objects (expensive) • Similar to defragmentation of a hard drive

The University of North Carolina at Chapel Hill 45 Heap Management

•Some languages (C & C++) require explicit heap management... • In C, malloc and free • In C++, new and delete •Easy to forget free... • Called a memory leak!

The University of North Carolina at Chapel Hill 46 Heap Management

•Some languages (Java) manage the heap for you • new( ) object allocated on heap. • when done, object is reclaimed. •Automatic de-allocation after an object has no binding/ references is called garbage collection. • Some runtime efficiency hit • No memory leaks.

The University of North Carolina at Chapel Hill 47 Sample Memory Layout

Global Code const Runtime stack Heap

0000 6024 6356 275000

pc 3125

sp 217560 Stack frame

fp 218380 The University of North Carolina at Chapel Hill 48