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

A lecture note from the university of north carolina at chapel hill's comp 524 programming language concepts course, delivered by stephen olivier on february 5, 2009. The lecture focuses on object binding, memory management, and the concept of binding time in programming languages. It covers topics such as naming, abstractions, static and stack allocation, and heap-based allocation.

Typology: Study notes

Pre 2010

Uploaded on 03/16/2009

koofers-user-tpr
koofers-user-tpr 🇺🇸

10 documents

1 / 48

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

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