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
Goal of Lecture The Goal of this lecture is to discuss object binding and memory management. 2
High-Level Programming Languages High-Level Programming languages are defined by two characteristics Machine independence Ease of programming 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. 4
Ease of Programming Names Control Flow Types Subroutines Object Orientation Concurrency Declarative Programming 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... 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 7
Abstractions Control abstractions allows programs to hide complex code behind simple interface Subroutines and functions Classes. Data abstraction allow the programer to hide data representation details behind abstract operations Abstract Data Types (ADTs) Classes. 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. 9
Binding Time Increasing Flexibility Language Design Time Language Implementation Time Program Writing Time Compile Time Link Time Load Time Increasing Efficiency Run Time 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. 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. 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. Local Variables Misc. Bookkeeping Temps. Local Variables Misc. Bookkeeping... Local Variables Misc. Bookkeeping Return Address Return Address Arguments & Returns Return Address Arguments & Returns Arguments & Returns Subroutine The University 1 of North Subroutine Carolina at 2Chapel Hill Subroutine 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 fp Subroutine D Args to called routines. Temps Stack growth Subroutine C Subroutine B Subroutine A (called from main) Local Variables Misc. Bookkeeping Return Address fp 14
Stack Allocation fp is the Frame Pointer sp is the Stack Pointer Under stack-based allocation, objects are allocated in a Last-In First-Out (LIFO) basis called a stack. e.g., recursive subroutine parameters. sp fp Subroutine D Args to called routines. Temps Stack growth Subroutine C Subroutine B Subroutine A (called from main) Local Variables Misc. Bookkeeping Return Address fp 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. 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 17
Setup foo(a,b) Move sp to allocate a new stack frame Copy args x,y into frame Copy return address into frame Set fp to point to new frame Maintain static chain or display Move PC to procedure address sp fp Subroutine Bar Subroutine B Subroutine A (called from main) 18
Setup foo(a,b) Move sp to allocate a new stack frame Copy args x,y into frame Copy return address into frame Set fp to point to new frame Maintain static chain or display Move PC to procedure address sp fp Subroutine foo Subroutine Bar Subroutine B Subroutine A (called from main) 19
Setup foo(a,b) Move sp to allocate a new stack frame Copy args x,y into frame Copy return address into frame Set fp to point to new frame Maintain static chain or display Move PC to procedure address sp fp Subroutine foo x & y Subroutine Bar Subroutine B Subroutine A (called from main) 20
Setup foo(a,b) Move sp to allocate a new stack frame Copy args x,y into frame Copy return address into frame Set fp to point to new frame Maintain static chain or display Move PC to procedure address sp fp Subroutine foo Return Ad x & y Subroutine Bar Subroutine B Subroutine A (called from main) 21
Setup foo(a,b) Move sp to allocate a new stack frame Copy args x,y into frame Copy return address into frame Set fp to point to new frame Maintain static chain or display Move PC to procedure address sp fp Subroutine foo Return Ad x & y Subroutine Bar Subroutine B Subroutine A (called from main) 22
Setup foo(a,b) Move sp to allocate a new stack frame Copy args x,y into frame Copy return address into frame Set fp to point to new frame Maintain static chain or display Move PC to procedure address sp fp Subroutine foo Return Ad A & B We ll ignore Subroutine Bar this for now Subroutine B Subroutine A (called from main) 23
Setup foo(a,b) Move sp to allocate a new stack frame Copy args x,y into frame Copy return address into frame Set fp to point to new frame Maintain static chain or display Move PC to procedure address sp fp Subroutine foo Return Ad This changes A & B Subroutine Bar where the code is executed. Subroutine B Subroutine A (called from main) 24
Prologue Copy registers into local slots Object initialization. 25
Prologue Copy registers into local slots Object initialization. Return Ad x & y Subroutine Bar fp 26
Prologue Copy registers into local slots Object initialization. All old reg. Old fp Return Ad x & y Subroutine Bar fp 27
Prologue Copy registers into local slots Object initialization. Objects that are All old used reg. are Old fp initialized. Return Ad x & y Subroutine Bar fp 28
Epilogue Place return value into slot in frame. Restore registers. Restore PC to return address. sp fp Subroutine foo Return Ad x & y Subroutine Bar Subroutine B Subroutine A (called from main) 29
Epilogue Place return value into slot in frame. Restore registers. Restore PC to return address. sp fp Subroutine foo Return Ad x & y Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 30
Epilogue Place return value into slot in frame. Restore registers. Restore PC to return address. Registers stored from foo s subroutine are registered. sp fp Subroutine foo Return Ad x & y Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 31
Epilogue Place return value into slot in frame. Restore registers. Restore PC to return address. The program resumes from where it began. sp fp Subroutine foo Return Ad x & y Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 32
Teardown Move sp & fp (deallocate frame) Move return values (if in registers) sp fp Subroutine foo Return Ad x & y Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 33
Teardown Move sp & fp (deallocate frame) Move return values (if in registers) sp fp Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 34
Teardown Move sp & fp (deallocate frame) Move return values (if in registers) If the return value was placed in a register, put it in the stack. sp fp Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 35
Heap-based allocation In heap-based allocation, objects may be allocated and deallocated at arbitrary times. For example, objects created with C++ new and delete. 36
Heap Space Management In general, the heap is allocated sequentially. This creates fragmentation... 37
Internal fragmentation Internal fragmentation is caused when extra space within a single block is unused. Used Unused 38
Internal fragmentation Internal fragmentation is caused when extra space within a single block is unused. Used Caused by fixed block size. 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. 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. 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. 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. 43
External Fragmentation External fragmentation occurs when there is sufficient available space Caused for a by new object, but there is no single block of free space large enough. gaps between contiguous blocks allocated to existing objects. 44
External Fragmentation May require heap compaction Combine in the heap by moving existing objects (expensive) Similar to defragmentation of a hard drive 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! 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. 47
Sample Memory Layout Code Global const Runtime stack Heap 0000 6024 6356 275000 pc 3125 sp 217560 Stack frame fp 218380 48