Chapter 3
Names, Scopes and Bindings -- Fall 21

Simple Python code example
More complex Python code example
Perl Example 14_39

Binding Time:

Object Lifetime and Storage Management
Runtime memory model
  • Stack allocation
  • Snapshot of Simplesim executing C2 program

  • Automatic variable
  • Deallocation memory when the variable exits the scope
  • Memory usage is predictable
  • LIFO stack
  • Allows recursion
  • Addition field in AR allow each call to a routine to have it's own local data
  • In order to support mutual recursion the definition and declaration must be able to be separated
  • Stack ptr is the register which points to the next free space on the stack (data memory).
    Instruction ptr  is the register which the address of the instruction currently be executed (in this snapshot) The instruction address is in code memory and not shown.  

    Constants

    HEAP Storage

  • Heap (not the data structure heap
  • Memory management hard
  • Dangling Reference Problem -- object deallocated too soon

  • Remember dangling (pointer) reference points to deallocated storage.
  • references
  • Creations of two Dangling Pointers ----old C
    /* ppp is the address of a pointer */
    int * dangle ( int ** ppp) {
    int p = 5; int m = 21;
    *ppp = &p  /* dereference ppp to get the pointer whose address was passed. */
    return &m;
     }
    main () {
    int k = 17;
    int *pm,  *pk = &k;
    pm = dangle(&pk); //both pm and pk point to deallocated memory.
    }
    Fischer&Grodzinsky, The Anatomy of Programming Languages, page 237

    Scope

    Simple Nested Functions example
    Another Nested Function Example
    print outer()
    print i, j
    Perl : local variables in a functions
  • my function creates a variable
  • scope is the block
  • lifetime is from the execution of the my function to the end of the execution of the block.
  • local creates a non-global variable

  • lifetime is the same as a "my" variable
  • scope is the block in which it is defined as well as any function directly or indirectly from that block.
  • local is an unfortunate legacy from earlier versions of Perl and should be avoided.
  • PERL:: Example 14_39

  • Raku - Perl 6 FYI

  • Concepts related to Scope

    Example

    Runtime Support of Nested Blocks and Nested Routines

    int f(void) {
        int x,y,w;

        while( ...) {
           int x,z;
             ...
           while (...) {
               int y;
            ...

           }
           if (...) {
              int x, w;
               ...
           }
        }
        if (...) {
           int a,b,c,d;
            ...
        }

  • Pascal, Modula 2, Haskell have nested routines but not nested blocks
  • Ada and Python have both
  • Simple Nested Functions example
    Figures from Text
  • Nested routines need to be able to find variable declared in the outer routine.
  • Referencing environment
  • Classes of values:

    Implementing Dynamic Scoping see here