lecture 7: binding time and storageolivier/comp524/lecture07.pdf · •for example in c++, when a...

48
The University of North Carolina at Chapel Hill 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

Upload: others

Post on 01-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 2: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 3: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 4: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 5: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

The University of North Carolina at Chapel Hill

Ease of Programming

5

Names

Control Flow

Types

Subroutines

Object Orientation

Concurrency

Declarative Programming

Page 6: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 7: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 8: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 9: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 10: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 11: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 12: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 13: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

. . .

Page 14: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 15: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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”

Page 16: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 17: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 18: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 19: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 20: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 21: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 22: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 23: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 24: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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.

Page 25: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

The University of North Carolina at Chapel Hill

Prologue

•Copy registers into local slots

•Object initialization.

25

Page 26: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

The University of North Carolina at Chapel Hill

•Copy registers into local slots

•Object initialization.

26

fp

Subroutine Bar

x & y

Return Ad

Prologue

Page 27: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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.

Page 28: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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.

Page 29: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 30: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 31: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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.

Page 32: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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.

Page 33: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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)

Page 34: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 35: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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.

Page 36: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 37: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

The University of North Carolina at Chapel Hill

Heap Space Management

•In general, the heap is allocated sequentially.

•This creates fragmentation...

37

Page 38: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 39: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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.

Page 40: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 41: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 42: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 43: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 44: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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.

Page 45: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 46: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 47: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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

Page 48: Lecture 7: Binding Time and Storageolivier/comp524/Lecture07.pdf · •For example in C++, when a variable is passed by “reference”, i.e., using “&”, then the name of the

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