Binding Time and Storage Allocation: The University of North Carolina at Chapel Hill
Binding Time and Storage Allocation: The University of North Carolina at Chapel Hill
Review: Compilation/Interpretation
Source Code
Interpretation
Target Code
2
Phases of Compilation
Ease of Programming
The driving problem in programming language design The rest of this class
Names Control Flow Types Subroutines Object Orientation Concurrency Declarative Programming
Programming Language
Naming
Naming is the process by which the programmer associates a name with a potentially complicated program fragment
The goal is to hide complexity Programming languages use name to designate variables, types, classes, methods, operators,
Data Abstraction allows the programmer to hide data representation details behind abstract operations
ADTs Classes
Binding Time
A binding is an association between two things
E.g. Name of an object and the object
Object Lifetime
Events in the life of an object
Creation Destruction Creation Destruction
A binding to an object that no longer exists is called a dangling reference
Object Lifetime
10
11
Static Allocation
12
In stack-based allocation, (stack) objects are allocated in last-in, first-out data structure, a stack.
E.g. Recursive subroutine parameters
13
Stack-based Allocation
14
Calling Sequence
On procedure (method) call and return compiler generates code that gets executed to manage the runtime stack Setup at call to procedure doFoo(a,b)
15
Calling Sequence
Setup at call to procedure doFoo(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
16
Calling Sequence
Epilog at end of procedure code
Placing return value into slot in frame Restore registers Restore PC to return address
17
sub doFoo { my ($arg1, $arg2, $ref1) = @_ ; # aha! # not quite the same $@_[0] = 4; $@_[1] = $$@_[2][7]; # etc is ok
}
18
In stack-based allocation, (stack) objects are allocated in last-in, first-out data structure, a stack.
E.g. Subroutine parameters
In heap-based allocation, (heap) objects may be allocated and deallocated at arbitrary times
E.g. objects created with C++ new and delete
19
Heap-based Allocation
The heap is a region of storage in which subblock can be allocated and deallocated
This not the heap data structure
Oops no fit
20
External fragmentation
If size of object to allocated is not larger than the size of the available heap, but the available space in the heap is scattered through heap in such a way that no contiguous space fits Comes from variable size blocks
21
malloc, return
Easy to forget return Heap fills with objects no longer in use (dangling refs) Memory leaks heap gets exhausted
22
23
code
Globals, consts
runtime stack
heap
(direction of growth)
foo inDo p1 sum
0000
R1 (pc) R2 (sp) R3 (fp)
6024
3125 217560 218380
6356
975000
24
Reading Assignment
Scotts chapter 3
Section 3.1 Section 3.2
25