lecture 7: binding time and storageolivier/comp524/lecture07.pdf · •for example in c++, when a...
TRANSCRIPT
The University of North Carolina at Chapel Hill
Lecture 7: Binding Time and Storage
COMP 524 Programming Language Concepts Stephen OlivierFebruary 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 memory management.
2
The University of North Carolina at Chapel Hill
High-Level Programming Languages
•High-Level Programming languages are defined by two characteristics
• Machine “independence”
• Ease of programming
3
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
Ease of Programming
5
Names
Control Flow
Types
Subroutines
Object Orientation
Concurrency
Declarative Programming
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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 ...
7
By naming an object we make an
abstraction
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
Binding Time
10
Language Design Time
Language Implementation Time
Program Writing Time
Compile Time
Link Time
Load Time
Run Time
Incr
easi
ng F
lexi
bilit
y Increasing E
fficiency
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
Static Allocation
•Under static allocation, objects are given an absolute address that is retained through the program’s execution
• e.g., global variables
13
Return Address
Arguments & Returns
Misc. Bookkeeping
Local Variables
Temps.
Return Address
Arguments & Returns
Misc. Bookkeeping
Local Variables
Temps.
Return Address
Arguments & Returns
Misc. Bookkeeping
Local Variables
Temps.
Subroutine 1 Subroutine 2 Subroutine X
. . .
The University of North Carolina at Chapel Hill
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.
14
Subroutine B
Subroutine A (called from main)
Subroutine C
Subroutine D
Return Address
Misc. Bookkeeping
Local Variables
Temps
Args to called routines.
sp
fp
fp
Sta
ck g
row
th
The University of North Carolina at Chapel Hill
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.
15
Subroutine B
Subroutine A (called from main)
Subroutine C
Subroutine D
Return Address
Misc. Bookkeeping
Local Variables
Temps
Args to called routines.
sp
fp
fp
Sta
ck g
row
thfp is the “Frame Pointer”
sp is the “Stack Pointer”
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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
18
Subroutine B
Subroutine A (called from main)
Subroutine Bar
sp
fp
The University of North Carolina at Chapel Hill
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
19
Subroutine B
Subroutine A (called from main)
Subroutine Bar
sp
fp
Subroutine foo
The University of North Carolina at Chapel Hill
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
20
Subroutine B
Subroutine A (called from main)
Subroutine Bar
sp
fp
Subroutine foo
x & y
The University of North Carolina at Chapel Hill
Setup foo(a,b)
21
Subroutine B
Subroutine A (called from main)
Subroutine Bar
sp
x & y
•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
Return Ad
Subroutine foo
fp
The University of North Carolina at Chapel Hill
Setup foo(a,b)
22
Subroutine B
Subroutine A (called from main)
Subroutine Bar
sp
fp
x & y
Return Ad
Subroutine foo
•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
The University of North Carolina at Chapel Hill
Setup foo(a,b)
23
Subroutine B
Subroutine A (called from main)
Subroutine Bar
sp
A & B
Return Ad
Subroutine foo
•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
fp
We’ll ignore this for now
The University of North Carolina at Chapel Hill
Setup foo(a,b)
24
Subroutine B
Subroutine A (called from main)
Subroutine Bar
sp
A & B
Return Ad
Subroutine foo
•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
fp
This changes where the code
is executed.
The University of North Carolina at Chapel Hill
Prologue
•Copy registers into local slots
•Object initialization.
25
The University of North Carolina at Chapel Hill
•Copy registers into local slots
•Object initialization.
26
fp
Subroutine Bar
x & y
Return Ad
Prologue
The University of North Carolina at Chapel Hill
•Copy registers into local slots
•Object initialization.
27
fp
Subroutine Bar
x & y
Return Ad
Prologue
Old fp
All old reg.
The University of North Carolina at Chapel Hill 28
fp
Subroutine Bar
x & y
Return Ad
Prologue
Old fp
All old reg.
•Copy registers into local slots
•Object initialization.
Objects that are used are
initialized.
The University of North Carolina at Chapel Hill
Epilogue
•Place return value into slot in frame.
•Restore registers.
•Restore PC to return address.
29
Subroutine B
Subroutine A (called from main)
Subroutine Bar
x & y
Return Ad
Subroutine foosp
fp
The University of North Carolina at Chapel Hill
Epilogue
•Place return value into slot in frame.
•Restore registers.
•Restore PC to return address.
30
Subroutine B
Subroutine A (called from main)
Subroutine Bar
x & y
Return Ad
Subroutine foosp
fp
Return Value
The University of North Carolina at Chapel Hill
Epilogue
•Place return value into slot in frame.
•Restore registers.
•Restore PC to return address.
31
Subroutine B
Subroutine A (called from main)
Subroutine Bar
x & y
Return Ad
Subroutine foosp
fp
Return Value
Registers stored from “foo”’s subroutine are
registered.
The University of North Carolina at Chapel Hill
Epilogue
•Place return value into slot in frame.
•Restore registers.
•Restore PC to return address.
32
Subroutine B
Subroutine A (called from main)
Subroutine Bar
x & y
Return Ad
Subroutine foosp
fp
Return Value
The program resumes from where it began.
The University of North Carolina at Chapel Hill
“Teardown”
33
Subroutine B
Subroutine A (called from main)
Subroutine Bar
x & y
Return Ad
Subroutine foosp
fp
Return Value
•Move sp & fp (deallocate frame)
•Move return values (if in registers)
The University of North Carolina at Chapel Hill
“Teardown”
•Move sp & fp (deallocate frame)
•Move return values (if in registers)
34
Subroutine B
Subroutine A (called from main)
Subroutine Bar
sp
fpReturn Value
The University of North Carolina at Chapel Hill
“Teardown”
35
Subroutine B
Subroutine A (called from main)
Subroutine Bar
sp
fpReturn Value
•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.
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
Heap Space Management
•In general, the heap is allocated sequentially.
•This creates fragmentation...
37
The University of North Carolina at Chapel Hill
Internal fragmentation
•Internal fragmentation is caused when extra space within a single block is unused.
38
Used
Unused
The University of North Carolina at Chapel Hill
Internal fragmentation
•Internal fragmentation is caused when extra space within a single block is unused.
39
Used
Caused by fixed block
size.
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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.
44
Caused by gaps
between contiguous
blocks allocated to
existing objects.
The University of North Carolina at Chapel Hill
External Fragmentation
•May require heap compaction
• Combine in the heap by moving existing objects (expensive)
• Similar to defragmentation of a hard drive
45
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
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
The University of North Carolina at Chapel Hill
Sample Memory Layout
48
pc 3125
sp 217560
fp 218380
0000 6024 6356 275000
Code
Global const Runtime stack Heap
Stack frame