Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Similar documents
Lecture 7: Binding Time and Storage

Programming Languages

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

CS61C : Machine Structures

Heap Management. Heap Allocation

Chapter 3:: Names, Scopes, and Bindings

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Name, Scope, and Binding. Outline [1]

G Programming Languages - Fall 2012

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

G Programming Languages - Fall 2012

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Run-time Environments - 3

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Run-time Environments

Run-time Environments

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Names, Scope, and Bindings

Concepts Introduced in Chapter 7

Chapter 5. Names, Bindings, and Scopes

Functional Programming

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

CSC 1600 Memory Layout for Unix Processes"

Run-Time Environments/Garbage Collection

Programming Languages Third Edition. Chapter 7 Basic Semantics

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

Lecture 13: Garbage Collection

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Principles of Programming Languages

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Advanced Programming & C++ Language

6. Names, Scopes, and Bindings

Chapter 9 :: Subroutines and Control Abstraction

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Requirements, Partitioning, paging, and segmentation

Control Abstraction. Hwansoo Han

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

o Code, executable, and process o Main memory vs. virtual memory

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Qualifying Exam in Programming Languages and Compilers

Types II. Hwansoo Han

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Run-time Environments -Part 3

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Dynamic Memory Management

CS558 Programming Languages

In Java we have the keyword null, which is the value of an uninitialized reference type

ECE 598 Advanced Operating Systems Lecture 10

A program execution is memory safe so long as memory access errors never occur:

High-Level Language VMs

Run Time Environments

Today: Segmentation. Last Class: Paging. Costs of Using The TLB. The Translation Look-aside Buffer (TLB)

Dynamic Storage Allocation

Classifying Information Stored in Memory! Memory Management in a Uniprogrammed System! Segments of a Process! Processing a User Program!

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

StackVsHeap SPL/2010 SPL/20

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Names, Scope, and Bindings

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Dynamic Memory Management! Goals of this Lecture!

CS399 New Beginnings. Jonathan Walpole

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru Department of Electronics and Communication Engineering

12: Memory Management

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

Lecture 13: Complex Types and Garbage Collection

Lectures 13 & 14. memory management

Virtual Memory. Today. Segmentation Paging A good, if common example

Limitations of the stack

CSC 533: Organization of Programming Languages. Spring 2005

Lecture 23: Object Lifetime and Garbage Collection

NOTE: Answer ANY FOUR of the following 6 sections:

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Memory Management (Chaper 4, Tanenbaum)

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Chapter 12. File Management

Dynamic Memory Management

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

Engine Support System. asyrani.com

[07] SEGMENTATION 1. 1

CSE 504: Compiler Design. Runtime Environments

Chapter 8 :: Composite Types

Operating Systems (2INC0) 2017/18

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

Names and Abstractions: What s in a Name?

15 Sharing Main Memory Segmentation and Paging

Memory Management Basics

Events, Memory Management

Chapter 5 Names, Binding, Type Checking and Scopes

CS558 Programming Languages

CMSC 330: Organization of Programming Languages

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Object-Oriented Programming

Transcription:

Binding and Storage Björn B. Brandenburg The University of North Carolina at Chapel Hill Based in part on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts.

What s the most striking difference? Java Interface definition x86 Assembly 2

What s the most striking difference? Java Interface definition Names! High-level languages have rich facilities for naming things. x86 Assembly 3

Names Required for abstraction. Assembly only has values & addresses & registers. Machine dependence! Names enable abstraction. Can refer to something without knowing the details (e.g., exact address, exact memory layout). Let the compiler worry about the details. Can refer to things that do not yet exist! E.g., during development, we can (and often do) write code for (Java) interfaces that have not yet been implemented. 4

Abstraction Control Abstraction vs. Data Abstraction 5

Abstraction Control Abstraction vs. Data Abstraction Control Abstraction Can hide arbitrary complex code behind a simple name. For example, addition can be simple (int) or difficult (vector). 6

Abstraction Control Abstraction vs. Data Abstraction Data Abstraction Abstract Data Types (ADTs) Reason about concepts instead of impl. details. Programmer doesnʼt know memory layout, address, whether other interfaces are implemented, what invariants need to be ensured, etc. 7

Binding Associating a name with some entity. (or object, but not the Java notion of an object) Binding vs. Abstraction. Introducing a name creates an abstraction. Binding a name to an entity resolves an abstraction. Binding time: When is a name resolved? 8

Binding Time Language Design Time Increasing Flexibility Language Impl. Time Program Writing Time Compile Time Link Time Load Time Increasing Efficiency Run Time 9

Binding Time Keywords, e.g. if Language Design Time Increasing Flexibility Tool vendor chooses predefined symbols and std. library impl. Programmer chooses names, data structures, and algorithms. Compiler determines exact memory layout, code order, etc. Resolve names in static library, e.g., printf in the C library. OS loads dynamic libraries, e.g., Windows loads DLLs, Unix SOs. Name resolved based on input. E.g., plugins, interfaces. Language Impl. Time Program Writing Time Compile Time Link Time Load Time Run Time Increasing Efficiency 10

11 Binding Time Binding may change Keywords, e.g. if Increasing Flexibility Tool vendor chooses predefined symbols and std. library impl. Programmer chooses names, data structures, and algorithms. Compiler determines exact memory layout, code order, etc. Resolve names in static library, e.g., printf in the C library. OS loads dynamic libraries, e.g., Windows loads DLLs, Unix SOs. Name resolved based on input. E.g., plugins, interfaces. Language Design Time Language Impl. Time Program Writing Time Compile Time Link Time Load Time Run Time if language is revised, e.g., Java 1.1, 1.2,, 1.6 if new tool version is released, e.g., GCC 2.95 vs. GCC 4.0. Increasing Efficiency if source code is changed. on every compile (but hopefully doesnʼt). every time the final program is linked after a library update. every time the app is launched. (e.g., Windows compatibility mode, DLL injection, debug libraries). at any time during execution.

Binding Time Called static or early binding. Increasing Flexibility Language Design Time Language Impl. Time Program Writing Time Compile Time Link Time Load Time Run Time Increasing Efficiency Called dynamic or late binding. 12

Object Lifetime vs. Binding Lifetime Entity A Entity B Name time t0 t1 t2 t3 t4 t5 Lifetime Entity: alife if memory is allocated (and initialized). Binding: alife if the name refers to some entity. 13

Object Lifetime vs. Binding Lifetime Entity A Entity B Name initially unbound: no binding exists. Name time t0 t1 t2 t3 t4 t5 Lifetime Entity: alife if memory is allocated (and initialized). Binding: alife if the name refers to some entity. 14

Object Lifetime vs. Binding Lifetime Entity A Entity B B is allocated and bound to Name Name time t0 t1 t2 t3 t4 t5 Lifetime Entity: alife if memory is allocated (and initialized). Binding: alife if the name refers to some entity. 15

A is allocated and bound to Name. Object Lifetime vs. Binding Lifetime B still exists! Entity A Entity B Name time t0 t1 t2 t3 t4 t5 Lifetime Entity: alife if memory is allocated (and initialized). Binding: alife if the name refers to some entity. 16

Object Lifetime vs. Binding Lifetime Entity A Entity B New binding: Name refers to B again. Name time t0 t1 t2 t3 t4 t5 Lifetime Entity: alife if memory is allocated (and initialized). Binding: alife if the name refers to some entity. 17

Object Lifetime vs. Binding Lifetime Entity A Entity B A continues to exist for some time until it is deallocated. Name time t0 t1 t2 t3 t4 t5 Lifetime Entity: alife if memory is allocated (and initialized). Binding: alife if the name refers to some entity. 18

Object Lifetime vs. Binding Lifetime Entity A Entity B The binding from Name to B is shadowed by a new binding to A. Name time t0 t1 t2 t3 t4 t5 Lifetime Entity: alife if memory is allocated (and initialized). Binding: alife if the name refers to some entity. 19

Object Lifetimes Defined by three principal storage allocation mechanisms: Static objects, which have a fixed address and are not deallocated before program termination. 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. Simplified 32-bit Memory Model Code Static Runtime stack Heap 0x0 Increasing Virtual Addresses 0xffffffff 20

Static Allocation Some memory is required throughout program execution. Multi-byte constants. Strings ( hello world ). Lookup tables. Global variables. Caution: this is not the same as Java s static. The program code. Must be allocated before program execution starts. Requirements specified in program file. Allocated by OS as part of program loading. The size of static allocation is constant between runs. Code Constants Variables Variables generated by compiler initialized with some value initialized with some value not initialized 21

Static Allocation Some memory is required throughout program execution. Multi-byte constants. Strings ( hello world ). Lookup tables. Global variables. The program code. Read-only data. Attempt to update illegal in many operating systems. Caution: this is not the same as Java s static. Must be allocated before program execution starts. Requirements specified in program file. Allocated by OS as part of program loading. The size of static allocation is constant between runs. Code Constants Variables Variables generated by compiler initialized with some value initialized with some value not initialized 22

Static Allocation Some memory is required throughout program execution. Multi-byte constants. Strings ( hello world ). Lookup tables. Global variables. The program code. Caution: this is not the same as Java s static. Global Initialized Variables. E.g., int meaning = 42; Must be allocated before program execution starts. Requirements specified in program file. Allocated by OS as part of program loading. The size of static allocation is constant between runs. Code Constants Variables Variables generated by compiler initialized with some value initialized with some value not initialized 23

Static Allocation Some memory is required throughout program execution. Multi-byte constants. Strings ( hello world ). Lookup tables. Global variables. The program code. Caution: this is not the same as Java s static. Global Uninitialized Variables. E.g., int last_error; (For historic reasons called the.bss segment.) Must be allocated before program execution starts. Requirements specified in program file. Allocated by OS as part of program loading. The size of static allocation is constant between runs. Code Constants Variables Variables generated by compiler initialized with some value initialized with some value not initialized 24

Static Allocation Some memory is required throughout program execution. Multi-byte constants. Strings ( hello world ). Compile-time constants. Lookup tables. Value must be known at compile time. Global variables. The program code. Must be allocated before program execution starts. Requirements specified in program file. Allocated by OS as part of program loading. The size of static allocation is constant between runs. Caution: this is not the same as Java s static. Elaboration-time constants. Value computed at runtime; compiler disallows subsequent updates. Code Constants Variables Variables generated by compiler initialized with some value initialized with some value not initialized 25

Advantages & Disadvantages Advantages. No allocation/deallocation runtime overhead. Static addresses. Compiler can optimize accesses. Limitations. Allocation size fixed; cannot depend on input. Wasteful; memory is always allocated. Global variables are error-prone. Advice: avoid global variables. 26

Runtime Stack Hardware-supported allocation area. Essential for subprogram calls. Grows top-down in many architectures. Size limit: stack overflow if available space is exhausted. Max. size of stack can be adjusted at runtime. OS is often involved in stack management. Simplified 32-bit Memory Model Code Static Runtime stack Heap growth 0x0 Increasing Virtual Addresses 0xffffffff 27

Subroutines & Static Allocation Calling a function/method/subroutine requires memory. Arguments. Local variables. Return address (where to continue execution after subroutine completes). Some bookkeeping information. E.g., to support exceptions and call stack traces. Where should this memory be allocated? 28

Static Allocation of Subroutine Memory Return Value(s) Arguments & Return Address Bookkeeping Misc. Local Variables Temps. Arguments & Return Value(s) Return Address Misc. Bookkeeping Local Variables Temps. Subroutine 1 Subroutine X Increasing Virtual Addresses One approach: statically allocate memory for each subroutine. (e.g. early versions of Fortran) 29

Static Allocation of Subroutine Memory Return Value(s) Arguments & Return Address Bookkeeping Misc. Local Variables Temps. Arguments & Return Value(s) Return Address Misc. Bookkeeping Local Variables Temps. Subroutine 1 Subroutine X Increasing Virtual Addresses Problem: Waste Most of the allocations will be unused most of the time. One approach: statically allocate memory for each subroutine. (e.g. early versions of Fortran) 30

Static Allocation of Subroutine Memory Return Value(s) Arguments & Return Address Bookkeeping Misc. Local Variables Temps. Arguments & Return Value(s) Return Address Misc. Bookkeeping Local Variables Temps. Subroutine 1 Increasing Virtual Addresses Problem: Waste Most of the allocations will be unused most of the time. Subroutine X Problem: No Recursion Subroutines may not be called while their memory is already in use. One approach: statically allocate memory for each subroutine. (e.g. early versions of Fortran) 31

Static Allocation of Subroutine Memory Return Value(s) Arguments & Return Address Bookkeeping Misc. Local Variables Temps. Arguments & Return Value(s) Return Address Misc. Bookkeeping Local Variables Temps. Subroutine 1 Increasing Virtual Addresses Problem: Waste Most of the allocations will be unused most of the time. Subroutine X Problem: No Recursion Subroutines may not be called while their memory is already in use. One approach: statically allocate memory for each subroutine. Limited recursion (e.g. depth early can versions be allowed of Fortran) by allocating memory for multiple subroutine instances. But this increases waste 32

Runtime Stack Idea: allocate memory for subroutines on demand. Reserve (large) area for subroutine calls. Allocate new frame for each call. Deallocate on return. Subroutine calls must be fast: need efficient memory management. Observation: last-in first-out allocation pattern. A routine returns only after all called subroutines have returned. Thus, allocations can be piled on top of each other. Subroutine memory is allocated on-demand from the runtime stack. 33

Stack Frames On a subroutine call: New stack frame pushed onto stack. Stack frames differ in size (depending on local variables). Recursion only limited by total size of stack. Reduced waste: unused subroutines only consume memory for code, not variables. Stack frame popped from stack on return. Temps. Local Variables Misc. Bookkeeping Return Address Arguments & Return Value(s) top of stack bottom Stack growth Subroutine D Subroutine C Subroutine B Subroutine A (program entry) Increasing Virtual Addresses 34

Calling Sequence Compilers generate code to manage the runtime stack. Setup, before call to subroutine. Prologue, before subroutine body executes. Epilogue, after subroutine body completes (the return). Cleanup, right after subroutine call. 35

Calling Sequence Compilers generate code to manage the runtime stack. Setup, before call to subroutine. Prologue, before subroutine body executes. Epilogue, after subroutine body completes (the return). Cleanup, right after subroutine call. 36

Calling Sequence Example C program 37

Calling Sequence Example C program, x86-64 assembly 38

Calling Sequence Example C Call program, of add() x86-64 from assembly main(). 39

Prologue Calling Sequence Example push stack & load arguments C program, x86-64 assembly 40

Prologue Calling Sequence Example push stack & load arguments C program, x86-64 assembly Epilogue setup return value & restore stack 41

Prologue Calling Sequence Example push stack & load arguments Call Setup prepare arguments C program, x86-64 assembly Epilogue setup return value & restore stack 42

Prologue Calling Sequence Example push stack & load arguments Call Setup prepare arguments Cleanup save return value C program, x86-64 assembly Epilogue setup return value & restore stack 43

Stack Trace Walking the stack top of stack bottom Stack growth NFA.<init> NFA.<init> Show.showNFA Show.main Increasing Virtual Addresses 44

Advantages & Disadvantages Advantages. Negligible (in most cases) runtime overhead. Efficient use of space. Recursion possible. Offset of local variable within frame usually constant. Limitations. Stack space is a limited resource. Stack frame size fixed (in many languages). Some offset computations required at runtime. Object lifetime limited to one subroutine invocation. Advice: use stack allocation when possible. (except for large buffers) 45

The Heap (no relation to the data structure of the same name) Arbitrary object lifetimes. Allocation and deallocation at any point in time. Can persists after subroutine completion. Very flexible; required for dynamic allocations. Most expensive to manage. Simplified 32-bit Memory Model Code Static Runtime stack Heap 0x0 Increasing Virtual Addresses 0xffffffff 46

Memory Management Allocation. Often explicit. C++: new Compiler can generate implicit calls to allocator. E.g., Prolog. Deallocation. Often explicit. C++: delete Sometimes done automatically. Increasing Virtual Addresses 47

Memory Management Allocation. Often explicit. C++: new Compiler can generate implicit calls to allocator. E.g., Prolog. Deallocation. Often explicit. C++: delete Sometimes done automatically. Increasing Virtual Addresses Allocated Free 48

Memory Management Allocation. Often explicit. C++: new Compiler can generate implicit calls to allocator. E.g., Prolog. Deallocation. Often explicit. C++: delete Sometimes done automatically. Allocation Problem: Given a size S, find a contiguous region of unallocated memory of length at least S. (and be very, very quick about it) Increasing Virtual Addresses Allocated Free 49

Common Techniques Allocator implementation. Variable size. Heuristics: First-fit, best-fit, last-fit, worst-fit. List traversals, slow coalescing when deallocated. Fixed-size blocks. 2 n or Fibonacci sequence. Buddy allocator, slab allocator Memory blocks are split until desired block size is reached. Quick coalescing: on free block is merged with its buddy. In practice. Most modern OSs use fixed-size blocks. Allocator performance crucial to many workloads. Allocators for multicore systems are still being researched. 50

Internal Fragmentation Negative impact of fixed-size blocks. Block-size usually too large. Some memory is wasted. new 51

Internal Fragmentation Negative impact of fixed-size blocks. Block-size usually too large. Some memory is wasted. Allocated but partially unused. 52

External Fragmentation Non-contiguous free memory. In total, there is sufficient available space but there is none of the free blocks is large enough by itself. new 53

External Fragmentation Non-contiguous free memory. In total, there is sufficient available space but there is none of the free blocks is large enough by itself. new 54

External Fragmentation Non-contiguous free memory. In total, there is sufficient available space but there is none of the free blocks is large enough by itself. new 55

External Fragmentation Non-contiguous free memory. In total, there is sufficient available space but there is none of the free blocks is large enough by itself. new 56

Compacting the Heap Merge free space. Copy existing allocations & update all references. Very difficult to implement new new 57

Dangling References Binding / object lifetime mismatch. Binding exists longer than object. Object de-allocated too early; access now illegal. Use-after-free bug (free is the C deallocation routine) Dangling pointer or reference. Object Name time t0 t1 t2 t3 58

Dangling References Binding / object lifetime mismatch. Binding exists longer than object. Object de-allocated too early; access now illegal. Use-after-free bug (free is the C deallocation routine) Dangling pointer or reference. Name bound, but object no longer exists. Reference is stale and dangles. Object Name time t0 t1 t2 t3 59

Memory Leaks Omitted deallocation. Objects that live forever. Even if no longer required. Possibly no longer referenced. Waste memory; can bring system down. A problem in virtually every non-trivial project. Object Name time t0 t1 t2 60

Memory Leaks Omitted deallocation. Objects that live forever. Even if no longer required. Objects forgotten about, but stick around and waste space until program termination. Possibly no longer referenced. Waste memory; can bring system down. A problem in virtually every non-trivial project. Object Name time t0 t1 t2 61

Garbage Collection Manual deallocation is error-prone. Dangling references. Use after free. Possibly unnecessarily conservative. Memory leaks. Garbage collection. Automatically deallocates objects when it is safe to do so. Automated heap management; programmer can focus on solving real problem. We will focus on garbage collection techniques later in the semester. 62

Summary & Advise Static Allocation Not dynamically sizable; lifetime spans virtually whole program execution; use only sparingly. Stack Allocation Lifetime restricted to subroutine invocation; allocation and deallocation is cheap; use whenever possible. Heap Allocation Arbitrary lifetimes; use garbage collection whenever possible; use for large buffers and long-living objects. 63