17
1 Binding Time and Storage Binding Time and Storage Allocation Allocation (Section 3.1-3.2) (Section 3.1-3.2) CSCI 431 Programming Languages CSCI 431 Programming Languages Fall 2003 Fall 2003 A modification of slides A modification of slides developed by Felix Hernandez- developed by Felix Hernandez- Campos at UNC Chapel Hill Campos at UNC Chapel Hill

1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

Embed Size (px)

Citation preview

Page 1: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

11

Binding Time and Storage Binding Time and Storage AllocationAllocation

(Section 3.1-3.2)(Section 3.1-3.2)

CSCI 431 Programming LanguagesCSCI 431 Programming Languages

Fall 2003Fall 2003

A modification of slides developed by Felix A modification of slides developed by Felix Hernandez-Campos at UNC Chapel HillHernandez-Campos at UNC Chapel Hill

Page 2: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

22

Review: Compilation/InterpretationReview: Compilation/Interpretation

Compiler or InterpreterCompiler or Interpreter

Translation Translation ExecutionExecution

Source CodeSource Code

Target CodeTarget Code

Interpre-Interpre-tationtation

Page 3: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

33

Phases of CompilationPhases of Compilation

Page 4: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

44

High-Level Programming LanguagesHigh-Level Programming Languages

• Two main goals:Two main goals:– Machine independenceMachine independence– Ease of programmingEase of programming

• High-level programming languages are independent High-level programming languages are independent of any particular instruction setof any particular instruction set

– But compilation to assembly code requires a target But compilation to assembly code requires a target instruction setinstruction set

– There is a trade-off between machine independence and There is a trade-off between machine independence and efficiencyefficiency

» E.g.E.g. Java vs. C Java vs. C

Page 5: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

55

Ease of ProgrammingEase of Programming

• The driving problem in programming language The driving problem in programming language designdesign

• The rest of this classThe rest of this class– NamingNaming– Control FlowControl Flow– TypesTypes– SubroutinesSubroutines– Object OrientationObject Orientation– ConcurrencyConcurrency– Functional ProgrammingFunctional Programming– Declarative ProgrammingDeclarative Programming

Programming Programming LanguageLanguage

Page 6: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

66

NamingNaming

• NamingNaming is the process by which the programmer is the process by which the programmer associates a name with a potentially complicated associates a name with a potentially complicated program fragmentprogram fragment

– The goal is to hide complexityThe goal is to hide complexity– Programming languages use name to designate variables, Programming languages use name to designate variables,

types, classes, methods, operators,…types, classes, methods, operators,…

• Naming provides Naming provides abstractionabstraction – E.g.E.g. Mathematics is all about the formal notation (i.e. Mathematics is all about the formal notation (i.e.

naming) that lets us explore more and more abstract naming) that lets us explore more and more abstract conceptsconcepts

Page 7: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

77

Control and Data AbstractionControl and Data Abstraction

• Control abstractionControl abstraction allows the programmer to hide allows the programmer to hide an arbitrarily complicated code behind a simple an arbitrarily complicated code behind a simple interfaceinterface

– SubroutinesSubroutines– ModulesModules

• Data AbstractionData Abstraction allows the programmer to hide allows the programmer to hide data representation details behind abstract operationsdata representation details behind abstract operations

– ADTsADTs– ClassesClasses

Page 8: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

88

Binding TimeBinding Time

• A A bindingbinding is an association between two things is an association between two things– E.g. Name of an object and the objectE.g. Name of an object and the object

• Binding timeBinding time is the time at which a binding is is the time at which a binding is createdcreated

– Language design timeLanguage design time– Language implementationLanguage implementation– Program writing timeProgram writing time– Compile TimeCompile Time– Link TimeLink Time– Load TimeLoad Time– Run TimeRun Time

Page 9: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

99

Binding Time ImpactBinding Time Impact

• Binding times have a Binding times have a crucialcrucial impact in programming impact in programming languageslanguages

– They are a fundamental design decisionThey are a fundamental design decision

• In general, early binding is associated with greater In general, early binding is associated with greater efficiencyefficiency

– E.g.E.g. compilers translate a program once, interpreters compilers translate a program once, interpreters translate it every time it is runtranslate it every time it is run

• In general, late binding is associated with greater In general, late binding is associated with greater flexibilityflexibility

– E.g. E.g. Class method vs. SubroutineClass method vs. Subroutine

• Compiled languages tend to bind names earlier than Compiled languages tend to bind names earlier than interpreted languagesinterpreted languages

Page 10: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

1010

• Events in the life of a bindingEvents in the life of a binding– CreationCreation– DestructionDestruction

» A binding to an object that no longer exists is called a A binding to an object that no longer exists is called a dangling dangling referencereference

– Deactivation and ReactivationDeactivation and Reactivation

Object LifetimeObject Lifetime

Object LifetimeObject Lifetime

• Events in the life of an objectEvents in the life of an object– CreationCreation– DestructionDestruction

Page 11: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

1111

Storage Allocation MechanismsStorage Allocation Mechanisms

• In In static allocationstatic allocation, objects are given an absolute , objects are given an absolute address that is retained throughout the program’s address that is retained throughout the program’s executionexecution

– E.g. E.g. Global variables, Non-recursive Subroutine Global variables, Non-recursive Subroutine Parameters, constant literals, run-time tablesParameters, constant literals, run-time tables

– Some objects are placed in protected, read-only memorySome objects are placed in protected, read-only memory

Page 12: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

1212

Static AllocationStatic Allocation

Page 13: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

1313

Storage Allocation MechanismsStorage Allocation Mechanisms

• In In static allocationstatic allocation, (static) objects are given an , (static) objects are given an absolute address that is retained throughout the absolute address that is retained throughout the program’s executionprogram’s execution

– E.g. E.g. Global variables, Non-recursive Subroutine Global variables, Non-recursive Subroutine ParametersParameters

• In In stack-based allocationstack-based allocation, (stack) objects are , (stack) objects are allocated in last-in, first-out data structure, a allocated in last-in, first-out data structure, a stackstack..

– E.g.E.g. Recursive subroutine parameters Recursive subroutine parameters

Page 14: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

1414

Stack-based AllocationStack-based Allocation

Page 15: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

1515

Storage Allocation MechanismsStorage Allocation Mechanisms

• In In static allocationstatic allocation, (static) objects are given an , (static) objects are given an absolute address that is retained throughout the absolute address that is retained throughout the program’s executionprogram’s execution

– E.g. E.g. Global variables , Non-recursive Subroutine Global variables , Non-recursive Subroutine ParametersParameters

• In In stack-based allocationstack-based allocation, (stack) objects are , (stack) objects are allocated in last-in, first-out data structure, a allocated in last-in, first-out data structure, a stackstack..

– E.g.E.g. Subroutine parameters Subroutine parameters

• In In heap-based allocationheap-based allocation, (heap) objects may be , (heap) objects may be allocated and deallocated at arbitrary timesallocated and deallocated at arbitrary times

– E.g.E.g. objects created with C++ objects created with C++ newnew and and deletedelete

Page 16: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

1616

Heap-based AllocationHeap-based Allocation

• The The heapheap is a region of storage in which blocks of is a region of storage in which blocks of memory can be allocated and deallocatedmemory can be allocated and deallocated

– This This notnot the the heap data structureheap data structure

Page 17: 1 Binding Time and Storage Allocation (Section 3.1-3.2) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos

1717

Heap Space ManagementHeap Space Management

• In general, the heap is allocated sequentiallyIn general, the heap is allocated sequentially

• This creates space This creates space fragmentationfragmentation problems problems– Internal fragmentationInternal fragmentation

» If size of object to allocated is smaller than the size of the If size of object to allocated is smaller than the size of the memory chunk available in the heapmemory chunk available in the heap

– External fragmentationExternal fragmentation» If size of object to allocated is not larger than the size of the 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 available heap, but the available space in the heap is scattered through the heap in such a way that no contiguous allocation is through the heap in such a way that no contiguous allocation is possiblepossible

• Automatic deallocation after an object has no Automatic deallocation after an object has no bindings is called bindings is called garbage collectiongarbage collection

– E.g.E.g. Java, Ada, Scheme, SmallTalk, etc. Java, Ada, Scheme, SmallTalk, etc.