Docsity

Binding Time and Storage: Object Lifetime and Allocation in Programming Languages, Study notes of Programming Languages

Partial preview of the text

Download Binding Time and Storage: Object Lifetime and Allocation in Programming Languages and more Study notes Programming Languages in PDF only on Docsity!

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

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

5 Names Control Flow Types Subroutines Object Orientation Concurrency Declarative Programming

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

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

10 Language Design Time Language Implementation Time Program Writing Time Compile Time Link Time Load Time Run Time

Increasing Flexibility

Increasing Efficiency

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

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

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 Stack growth

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

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