Lecture 7: Binding Time and Storage

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

Programming Languages

LECTURE 19. Subroutines and Parameter Passing

Lecture 13: Complex Types and Garbage Collection

Run-time Environments

Run-time Environments

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

G Programming Languages - Fall 2012

Programming Languages

6. Names, Scopes, and Bindings

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

Control Abstraction. Hwansoo Han

G Programming Languages - Fall 2012

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

Chap. 8 :: Subroutines and Control Abstraction

Lecture 23: Object Lifetime and Garbage Collection

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Implementing Procedure Calls

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

Run-time Environment

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

CIT Week13 Lecture

Names, Scope, and Bindings

Chapter 9 :: Subroutines and Control Abstraction

Run-time Environments - 3

Names, Scope, and Bindings

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

Name, Scope, and Binding. Outline [1]

Contents. 8-1 Copyright (c) N. Afshartous

Concepts Introduced in Chapter 7

ECE 2035 Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 1 May 2013

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

CS 314 Principles of Programming Languages. Lecture 11

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

Pointers II. Class 31

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

Names and Abstractions: What s in a Name?

Naming in OOLs and Storage Layout Comp 412

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

Class Information ANNOUCEMENTS

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

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

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Stacks and Function Calls

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

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

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Run-Time Environments

CS61C : Machine Structures

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Names, Scope, and Bindings

Advances in Programming Languages: Regions

Lecture 15: Object-Oriented Programming

Names, Scope, and Bindings

CS399 New Beginnings. Jonathan Walpole

Lecture 7: Procedures and Program Execution Preview

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

Types II. Hwansoo Han

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

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

Pointers, Dynamic Data, and Reference Types

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

Memory Management and Run-Time Systems

Automatic Memory Management

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

Dynamic Memory Management! Goals of this Lecture!

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

The Procedure Abstraction

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Habanero Extreme Scale Software Research Project

Language of the Machine Recursive functions

Arrays and Functions

the gamedesigninitiative at cornell university Lecture 6 C++: Basics

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

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Chapter 3:: Names, Scopes, and Bindings

Anne Bracy CS 3410 Computer Science Cornell University

CS A331 Programming Language Concepts

Runtime Support for Algol-Like Languages Comp 412

Compilers and Code Optimization EDOARDO FUSELLA

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

CSE Lecture In Class Example Handout

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

Lecture 13: Garbage Collection

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

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

Run Time Environments

Principles of Programming Languages

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

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

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Anne Bracy CS 3410 Computer Science Cornell University

Final CSE 131 Winter 2012

Optimizer. Defining and preserving the meaning of the program

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

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e Feb 11, 13, 15, and 25. Winter Session 2018, Term 2

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Transcription:

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

Goal of Lecture The Goal of this lecture is to discuss object binding and memory management. 2

High-Level Programming Languages High-Level Programming languages are defined by two characteristics Machine independence Ease of programming 3

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

Ease of Programming Names Control Flow Types Subroutines Object Orientation Concurrency Declarative Programming 5

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

Naming Naming is the process by which a programer associates a name with a potentially complicated program fragment. By naming an Purpose is to hide complexity. object we For example, to designate variables, types, classes, operators, etc... make an abstraction 7

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

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

Binding Time Increasing Flexibility Language Design Time Language Implementation Time Program Writing Time Compile Time Link Time Load Time Increasing Efficiency Run Time 10

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

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

Static Allocation Under static allocation, objects are given an absolute address that is retained through the program s execution e.g., global variables Temps. Temps. Local Variables Misc. Bookkeeping Temps. Local Variables Misc. Bookkeeping... Local Variables Misc. Bookkeeping Return Address Return Address Arguments & Returns Return Address Arguments & Returns Arguments & Returns Subroutine The University 1 of North Subroutine Carolina at 2Chapel Hill Subroutine X 13

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. sp fp Subroutine D Args to called routines. Temps Stack growth Subroutine C Subroutine B Subroutine A (called from main) Local Variables Misc. Bookkeeping Return Address fp 14

Stack Allocation fp is the Frame Pointer sp is the Stack Pointer Under stack-based allocation, objects are allocated in a Last-In First-Out (LIFO) basis called a stack. e.g., recursive subroutine parameters. sp fp Subroutine D Args to called routines. Temps Stack growth Subroutine C Subroutine B Subroutine A (called from main) Local Variables Misc. Bookkeeping Return Address fp 15

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

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

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 sp fp Subroutine Bar Subroutine B Subroutine A (called from main) 18

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 sp fp Subroutine foo Subroutine Bar Subroutine B Subroutine A (called from main) 19

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 sp fp Subroutine foo x & y Subroutine Bar Subroutine B Subroutine A (called from main) 20

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 sp fp Subroutine foo Return Ad x & y Subroutine Bar Subroutine B Subroutine A (called from main) 21

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 sp fp Subroutine foo Return Ad x & y Subroutine Bar Subroutine B Subroutine A (called from main) 22

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 sp fp Subroutine foo Return Ad A & B We ll ignore Subroutine Bar this for now Subroutine B Subroutine A (called from main) 23

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 sp fp Subroutine foo Return Ad This changes A & B Subroutine Bar where the code is executed. Subroutine B Subroutine A (called from main) 24

Prologue Copy registers into local slots Object initialization. 25

Prologue Copy registers into local slots Object initialization. Return Ad x & y Subroutine Bar fp 26

Prologue Copy registers into local slots Object initialization. All old reg. Old fp Return Ad x & y Subroutine Bar fp 27

Prologue Copy registers into local slots Object initialization. Objects that are All old used reg. are Old fp initialized. Return Ad x & y Subroutine Bar fp 28

Epilogue Place return value into slot in frame. Restore registers. Restore PC to return address. sp fp Subroutine foo Return Ad x & y Subroutine Bar Subroutine B Subroutine A (called from main) 29

Epilogue Place return value into slot in frame. Restore registers. Restore PC to return address. sp fp Subroutine foo Return Ad x & y Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 30

Epilogue Place return value into slot in frame. Restore registers. Restore PC to return address. Registers stored from foo s subroutine are registered. sp fp Subroutine foo Return Ad x & y Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 31

Epilogue Place return value into slot in frame. Restore registers. Restore PC to return address. The program resumes from where it began. sp fp Subroutine foo Return Ad x & y Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 32

Teardown Move sp & fp (deallocate frame) Move return values (if in registers) sp fp Subroutine foo Return Ad x & y Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 33

Teardown Move sp & fp (deallocate frame) Move return values (if in registers) sp fp Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 34

Teardown 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. sp fp Subroutine Bar Return Value Subroutine B Subroutine A (called from main) 35

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

Heap Space Management In general, the heap is allocated sequentially. This creates fragmentation... 37

Internal fragmentation Internal fragmentation is caused when extra space within a single block is unused. Used Unused 38

Internal fragmentation Internal fragmentation is caused when extra space within a single block is unused. Used Caused by fixed block size. 39

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

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

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

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

External Fragmentation External fragmentation occurs when there is sufficient available space Caused for a by new object, but there is no single block of free space large enough. gaps between contiguous blocks allocated to existing objects. 44

External Fragmentation May require heap compaction Combine in the heap by moving existing objects (expensive) Similar to defragmentation of a hard drive 45

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

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

Sample Memory Layout Code Global const Runtime stack Heap 0000 6024 6356 275000 pc 3125 sp 217560 Stack frame fp 218380 48