Chapter 8 control ii procedures and environments
1 / 75

Chapter 8 - Control II: Procedures and Environments - PowerPoint PPT Presentation

  • Uploaded on

Chapter 8 - Control II: Procedures and Environments. Three major parts of a runtime environment:. Static area allocated at load/startup time. Examples: global/static variables and load-time constants.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Chapter 8 - Control II: Procedures and Environments' - cybil

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Three major parts of a runtime environment
Three major parts of a runtime environment:

  • Static area allocated at load/startup time. Examples: global/static variables and load-time constants.

  • Stack area for execution-time data that obeys a last-in first-out lifetime rule. Examples: nested declarations and temporaries.

  • Heap or dynamically allocated area for "fully dynamic" data, i.e. data that does not obey a LIFO rule.

K. Louden, Programming Languages

Procedure overview
Procedure Overview

  • When functions are “first class” data items themselves, they can be dynamically created and used like values just like any other data structure.

  • Thus, we need to be able to pass functions as arguments

  • A procedure is called or activated.

  • Activation record: collection of data needed to maintain a single execution of a procedure.

  • Worry about local and non-local references.

  • Static or dynamic environment (depending on scoping) must be accessible.

  • When a procedure depends only of parameters and fixed language features – closed form.

  • The code for a function together with its defining environment is called closure – as we can resolve all outstanding non-local environments.

K. Louden, Programming Languages

Implementing simple subprograms
Implementing “Simple” Subprograms

  • Call Semantics:

    1. Save the execution status of the caller (calling environment)

    2. Carry out the parameter-passing process by putting the parameters somewhere that the called function can access.

    3. Pass the return address to the callee

    4. Transfer control to the callee

K. Louden, Programming Languages

Implementing simple subprograms1
Implementing “Simple” Subprograms

  • Return Semantics:

    1. If it is a function, move the functional value to a place the caller can get it

    2. Restore the execution status of the caller

    3. Transfer control back to the caller

K. Louden, Programming Languages

Implementing simple subprograms2
Implementing “Simple” Subprograms

  • Required Storage: Status information of the caller, parameters, return address, and functional value (if it is a function)

  • The format, or layout, of the noncode part of an executing subprogram is called an activation record

  • An activation record instance is a concrete example of an activation record (the collection of data for a particular subprogram activation)

K. Louden, Programming Languages

An activation record for simple subprograms no scoping issues
An Activation Record for “Simple” Subprograms (no scoping issues)

K. Louden, Programming Languages

Code and Activation Records of a Program with “Simple” SubprogramsIf there is no recursion, we can have static activation records.If we have no non-local variables, everything we need is local and easy to find.

K. Louden, Programming Languages

Parameter passing
Parameter Passing Subprograms

  • Aliases may be created

  • Type checking parameters

  • a formal reference parameter is a nonlocal variable

  • the same data object passed for two parameters CALL S(X,X)

  • With aliasing, interesting problems in optimizations occur.

K. Louden, Programming Languages

Models of parameter passing
Models of Parameter Passing Subprograms

K. Louden, Programming Languages

  • 1. Pass-by-value (in mode) Subprograms

    • Typically we copy the value in, but can do with a constant reference pointer.

    • Parameters are viewed as local variables of the procedure (with initial values given by values of arguments in the call)

    • Note can still change values outside procedure if we pass a reference.

    • Disadvantages of copy:

      • Requires more storage (duplicated space)

      • Cost of the moves (if the parameter is large)

    • Disadvantages of constant reference:

      • Must write-protect in the called subprogram or compiler check that there are no assignments.

      • Accesses cost more (indirect addressing)

K. Louden, Programming Languages

2. Pass-by-result (out mode) Subprograms

  • function return value

  • Local’s value is passed back to the caller

  • Physical move is usually used (copy to call stack)

  • Disadvantages:

    • If value is passed, time and space

    • order dependence may be a problem

    • If return value is an address, when is it evaluated?


      procedure sub1(y: int, z: int);{y=0;z=5;


      sub1(x, x);

  • Value of x in the caller depends on order of assignments at the return

K. Louden, Programming Languages

Out only parameters
OUT only parameters Subprograms

  • formal parameter is a local variable with no initial value

  • copied back at termination of subprogram Pass by result

  • Explicit function Values: may be considered an extra OUT parameter

  • return(expr)

  • value to be returned by assignment to function name

  • if return is an address (e.g., list[index]), when is it evaluated? time of call? time of return?

K. Louden, Programming Languages

3. Inout mode Subprograms

  • Pass by value-result (aka copy-in copy-out or copy-restore)

    Physical move, both ways

    • value-result (or pass by copy)

    • Disadvantages

      • ordering may be a problem with a call like doit(x,x)

      • time/space issues

  • Need to know whether address is computed again before copying back. doit(i,a[i])

K. Louden, Programming Languages

In out parameters
IN OUT parameters Subprograms

  • Value-restore. Copy value in on call Copy changed value back on return.

  • Used to save cost of indirect access.

  • aliases may be problematic - especially likely if pass same parameter twice. Then if arguments are changed, original values may be changed differently depending on order of change (if value-restore)

K. Louden, Programming Languages

4. In-out mode - pass by reference. Subprograms


  • access is slower (as indirect)

  • passing is faster

  • called program can access parameter through alias

  • Array element collisions:


    sub1(a[i], a[j]); /* if i = j */

    Also, sub2(a, a[i]); (a different one)

  • Collision between formals and globals

  • Root cause of all of these is: The called subprogram is provided wider access to nonlocals than is necessary

  • K. Louden, Programming Languages

    In out parameters1
    IN OUT parameters Subprograms

    • transmission by reference formal parameter is local object of type pointerIf expression is passed as an in/out parameter: a temporary location may be passed(and then the copy is changed, not the original)

    • Disadvantages:

      • access slower as is indirect (always follow a pointer to access), but passing is fast (only copy a pointer, not a whole structure)

      • may make inadvertent changes to parameters (if out only was desired)

    K. Louden, Programming Languages

    Parameter passing methods
    Parameter Passing Methods Subprograms

    5. Pass-by-name (Unevaluated parameters)

    • By textual substitution

    • intended to be an advanced inlining

    • essentially like late evaluation

    • Name of t he argument (or its textual substitution at the point of call) replaces the name of the parameter it corresponds to.

    • Formals are bound to an access method at the time of the call, but actual binding to a value or address takes place at the time of a reference or assignment

    • Purpose: flexibility of late binding

    K. Louden, Programming Languages

    Pass-by-name Subprograms

    Resulting semantics:

    • If actual is a scalar variable, it is pass-by-reference

    • If actual is a constant expression, it is pass-by-value

    • If actual is an array element, it is like nothing else


      procedure sub1(x: int; y: int);


      x := 1; Seems like nothing is happening

      y := 2;with first assignments but it is

      x := 2;

      y := 3;


      sub1(i, a[i]);

    K. Louden, Programming Languages

    5. Pass-by-name (continued) Subprograms

    • If actual is an expression with a reference to a variable that is also accessible in the program, it is also like nothing else

      e.g. (assume k is a global variable)

      procedure sub1(x: int; y: int; z: int);


      k := 1; y := x;k := 5;z := x;


      sub1(k+1, j, a[i]);

      Thunks: pass by name arguments are implemented by little procedure which evaluate the arguments.

      Presumably the image was of little machines that “thunked” into place each time they were needed.

    K. Louden, Programming Languages

    Parameter passing in mode
    Parameter Passing (In Mode) Subprograms

    • Pass-by-name: text for argument is passed to subprogram and expanded in each place parameter is used

      • Roughly same as using macros

      • Note, you can’t evaluate late without having “code” to execute

      • You also need to know a context for evaluating non-local variables

    • Achieves late binding

    K. Louden, Programming Languages

    Pass by name example
    Pass-by-name Example Subprograms

    integer INDEX= 1;

    integer array ARRAY[1:2]

    procedure UPDATE (PARAM);

    integer PARAM


    PARAM := 3;

    INDEX := INDEX + 1;

    PARAM := 5;



    K. Louden, Programming Languages

    Pass by name example1
    Pass-by-name Example Subprograms

    • Previous code puts 3 in ARRAY[1] and 5 in ARRAY[2]

    • How is this accomplished if the compiled code must work for ANY argument that is passed?

    • PARAM must be something that has a value, but can be x, Array[x+y], Array[2*t[6*x]+7]

    • How can you generate code for UPDATE when you don’t know what is passed?

    • If pass by name argument appears on left hand side, need to be able to compute the address.

    • If pass by name argument appears on right hand side (of assignment), need to be able to compute a value.

    K. Louden, Programming Languages

    • New interest in functional languages means more interest in delayed evaluation.

    • Very flexible, but inefficient. Difficult to implement. Confusing to read/write.

    • Some simple operations are not possible with pass by name.

    • Lazy evaluation is another form of late binding. Only evaluate when it becomes necessary.

    • Substitute name or expression (in calling environment) for formal parameter

    • The name location binding is delayed until (and established fresh each time) the formal parameter is encountered.

    • Implemented by passing parameter-less subprograms (thunks) rather than variable name. An expression needs to be evaluated IN the proper environment. Don't have mechanism to do that other than thru procedure call.

    • Whenever formal parameter is referenced, a call is made to thunk, which evaluates the parameter in the proper (caller) environment and returns proper resulting value (or location)

    K. Louden, Programming Languages

    • Example: delayed evaluation.

      procedure R(var i,j: integer);

      begin var


      m := true;

      i := i + 1;

      j := j + 1;



      m := 2;

      for(i=0;i<10;i++) c[i]=10*i;


    • pass by reference: adds 1 to m and c[2]Pass by name: adds 1 to m and c[3]

    K. Louden, Programming Languages

    Example for pass by name
    Example for Pass by Name delayed evaluation.

    b1: begin real x,y;

    y := 0.0;

    procedure G(t): name t;

    begin integer w; integer x;

    w := 10; y := 20; x := 50

    print t x:= 0;

    print t

    end G;

    b2: begin real y;

    y := 0.5; x := 1.0;

    call G(y-x) end


    thunk() return(y-x) end;

    K. Louden, Programming Languages

    Parameter passing methods1
    Parameter Passing Methods delayed evaluation.

    • Disadvantages of pass by name:

      • Very inefficient references

      • Too tricky; hard to read and understand

    K. Louden, Programming Languages

    Parameter passing methods2
    Parameter Passing Methods delayed evaluation.

    • Multidimensional Arrays as Parameters

      • If a multidimensional array is passed to a subprogram and the subprogram is separately compiled, the compiler needs to know the declared size of that array to build the storage mapping function

      • Programmer is required to include the declared sizes of all but the first subscript in the actual parameter

      • This disallows writing flexible subprograms

      • Solution: run time descriptor

    K. Louden, Programming Languages

    Parameter passing methods3
    Parameter Passing Methods delayed evaluation.

    • Design Considerations for Parameter Passing

      1. Efficiency

      2. One-way or two-way

      • These two are in conflict with one another!

      • Good programming => limited access to variables, which means one-way whenever possible

      • Efficiency => pass by reference is fastest way to pass structures of significant size

      • Also, functions should not allow reference parameters

    K. Louden, Programming Languages

    Languages and environments
    Languages and Environments delayed evaluation.

    • Languages differ on where activation records must go in the environment:

      • Fortran is static: all data, including activation records, are statically allocated. (Each function has only one activation record—no recursion!)

      • Functional languages (Scheme,ML) and some OO languages (Smalltalk) are heap-oriented: all (or almost all) data, including activation records, are allocated dynamically.

      • Most languages are in between: data can go anywhere (depending on its properties); activation records go on the stack.

    K. Louden, Programming Languages

    Simple stack based allocation

    x delayed evaluation.



    Simple stack-based allocation

    • Described in Chapter 5.

    • Nested declarations are added to the stack as their code blocks are entered, and removed as their code blocks are exited.

    • Example: Stack at Point 1:{ int x; int y; { int z; } { int w; // Point 1 }}

    • Note ,z has been removed at point 1 as have exited scope

    K. Louden, Programming Languages

    Example c main q p
    Example (C): main delayed evaluation. →q →p

    int x;

    void p( int y)

    { int i = x;

    char c; ...


    void q ( int a)

    { int x;




    { q(2);

    return 0;


    K. Louden, Programming Languages

    Activation record of p

    © 2003 Brooks/Cole - Thomson Learning delayed evaluation. ™

    Activation record of p:

    K. Louden, Programming Languages

    Environment stack during exec of p main q p stack is shown growing down

    © 2003 Brooks/Cole - Thomson Learning delayed evaluation. ™

    Environment stack during exec of p: main →q →p(stack is shown growing down)

    Note: the ep in this picture actually points to the "bottom" of the frame, as do the control links (which are stored old ep values), so ep  top of stack.

    K. Louden, Programming Languages

    Local variable access using the ep
    Local variable access using the ep delayed evaluation.

    • In a typical language with a stack-based runtime environment, the local declarations in a procedure are fixed at compile-time, both in size and in sequence.

    • This information can be used to speed up accesses to local variables, by precomputing these locations as offsets from the ep.

    • Then the local frame need not have a name-based lookup operation (unlike the symbol table).

    • In fact, names can be dispensed with altogether.

    • The next slide shows how that would look for the procedure p of slide 7.

    K. Louden, Programming Languages

    Non local variable access
    Non-local variable access delayed evaluation.

    • Requires that the environment be able to identify frames representing enclosing scopes.

    • Using the control link results in dynamic scope (and also kills the fixed-offset property as you are not sure which method will contain the x. Thus, you can’t depend on a fixed location).

    • If procedures can't be nested (C, C++, Java), the enclosing scope is always locatable by other means: it is either global (accessed directly) or belongs to the current object.

    • If procedures can be nested, to maintain lexical scope a new link must be added to each frame: the access link, pointing to the activation of the defining environment of each procedure.

    K. Louden, Programming Languages

    Example ada like q r p z z
    Example (Ada-like) q delayed evaluation. →r → p → z → z:

    1 procedure q is

    • x,u: integer

    • procedure p (y: integer) is

    • i: integer := x;

    • procedure z(t:float) returns float is

    • m: float;

    • begin

    • if (t >5) return z(t/2);

    • return t+m/i*y + u/x;

    • end z;

    • begin ...

    • end p;

    • procedure r (u:integer) is

    • x: float;

    • begin

    • p(1);...

    • end r;

    • begin

    • r;

    • end q;

    K. Louden, Programming Languages

    Environment during exec of p

    © 2003 Brooks/Cole - Thomson Learning delayed evaluation. ™

    Environment during exec of p:

    K. Louden, Programming Languages

    Nested subprograms
    Nested Subprograms delayed evaluation.

    • The process of locating a nonlocal reference:

      1. Find the correct activation record instance

      2. Determine the correct offset within that activation record instance

    May need to follow several links (access chaining)

    The number of links is known from compile time.

    If used stack of symbol tables, can count how many tables you had to search to find it.

    If used individual stacks for each value, you can record the nesting depth of each variable.

    K. Louden, Programming Languages

    Procedure values as pointer pairs
    Procedure values as pointer pairs delayed evaluation.

    • With nested procedures in lexically scoped languages requiring an access link, when a procedure is called, this access link must be available at the point of call. One way it can be is for the procedure itself to record its access link (necessary if procedures can be parameters).

    • Then each procedure becomes a pair of pointers: a code pointer (called the instruction pointer or ip in the text), and an environment pointer (ep in the text) pointing to the definition environment of the procedure (which will become the access link during a call).

    • Such an <ep,ip> pair is sometimes called a closure.

    K. Louden, Programming Languages

    Fully dynamic environments
    Fully dynamic environments delayed evaluation.

    • Languages with lambdas or where functions can be created locally and returned from a call (ML, Scheme).

    • Activation records cannot in general be popped after a call. Thus, activations no longer behave as LIFO structures and must be allocated in the heap.

    • Control links make little sense, since each control link might have to point far back in the stack.

    • Access links and closures still make perfect sense, however.

    K. Louden, Programming Languages

    Implementing subprograms in algol like languages
    Implementing Subprograms in ALGOL-like Languages delayed evaluation.

    • The collection of dynamic links in the stack at a given time is called the dynamic chain, or call chain

    • Local variables can be accessed by their offset from the beginning of the activation record. This offset is called the local_offset

    • The local_offset of a local variable can be determined by the compiler

      • Assuming all stack positions are the same size, the first local variable declared has an offset of three plus the number of parameters, e.g., in main, the local_offset of Y in A is 3

    K. Louden, Programming Languages

    The process of locating a nonlocal reference
    The Process of Locating a Nonlocal Reference delayed evaluation.

    • Finding the offset is easy

    • Finding the correct activation record instance:

      • Static semantic rules guarantee that all nonlocal variables that can be referenced have been allocated in some activation record instance that is on the stack when the reference is made

    K. Louden, Programming Languages

    Nested subprograms1
    Nested Subprograms delayed evaluation.

    • Technique 1 - Static Chains

    • A static chain is a chain of static links that connects certain activation record instances

    • The static link in an activation record instance for subprogram A points to one of the activation record instances of A's static parent

    • The static chain from an activation record instance connects it to all of its static ancestors

    K. Louden, Programming Languages

    Static chains continued
    Static Chains (continued) delayed evaluation.

    • To find the declaration for a reference to a nonlocal variable:

      • You could chase the static chain until the activation record instance (ari) that has the variable is found, searching each ari as it is found, if variable names were stored in the ari

    • Def: static_depth is an integer associated with a static scope whose value is the depth of nesting of that scope

    K. Louden, Programming Languages

    Static chains show the static dynamic chains when main c a b c
    Static Chains delayed evaluation. Show the static/dynamic chains when main →C →A →B →C

    main ----- static_depth = 0

    A ----- static_depth = 1

    B ----- static_depth = 2

    C ----- static_depth = 1

    K. Louden, Programming Languages

    Static chains continued1
    Static Chains (continued) delayed evaluation.

    • Def: The chain_offset or nesting_depth of a nonlocal reference is the difference between the static_depth of the reference and that of the scope where it is declared

    • A reference can be represented by the pair:

      (chain_offset, local_offset)

      where local_offset is the offset in the activation record of the variable being referenced

    K. Louden, Programming Languages

    Nested subprograms2
    Nested Subprograms delayed evaluation.

    • Static Chain Maintenance

      • At the call :

        • The activation record instance must be built

        • The dynamic link is just the old stack top pointer

        • The static link must point to the most recent ari of the static parent (in most situations)

      • Two Methods to set static chain:

        1. Search the dynamic chain until the first ari for the static parent is found--easy, but slow

    K. Louden, Programming Languages

    Nested subprograms3
    Nested Subprograms delayed evaluation.

    2. Treat procedure calls and definitions like variable references and definitions (have the compiler compute the nesting depth, or number of enclosing scopes between the caller and the procedure that declared the called procedure; store this nesting depth and send it with the call)

    • e.g. Look at MAIN_2 and the stack contents. At the call to SUB1 in SUB3, this nesting depth is 1, which is sent to SUB1 with the call. The static link in the new ari for SUB1 is set to point to the ari that is pointed to by the second static link in the static chain from the ari for SUB3

    K. Louden, Programming Languages

    Nested subprograms4
    Nested Subprograms delayed evaluation.

    • Evaluation of the Static Chain Method

      • Problems:

        1. A nonlocal reference is slow if the number of scopes between the reference and the declaration of the referenced variable is large

        2. Time-critical code is difficult, because the costs of nonlocal references are not equal, and can change with code upgrades and fixes

    K. Louden, Programming Languages

    Nested subprograms5
    Nested Subprograms delayed evaluation.

    • Technique 2 (for locating non-local variables) - Displays

      • The idea: Put the static links in a separate stack called a display. The entries in the display are pointers to the ari's that have the variables in the referencing environment.

      • Represent references as

        (display_offset, local_offset)

        where display_offset is the same as chain_offset

      • Can access via computation. display offset of 10 is one lookup (not a chain of length 10)

    K. Louden, Programming Languages

    Main – level 1 delayed evaluation.


    p level 1

    p level 2

    t level 2

    q level 4

    s level 3

    r level 3

    K. Louden, Programming Languages

    Display contains pointers to each delayed evaluation.

    activation record at each reachable level








    main-> t -> s-> q


    When s calls q, a single element is added to the


    K. Louden, Programming Languages

    100 delayed evaluation.







    main-> t -> s-> q-> p


    old level 2 is 200

    When q calls p, a new level 2 entry is needed.

    Store the old one, so you can get it back.

    Level 3 and level 4 are unused (but unchanged)



    K. Louden, Programming Languages

    100 delayed evaluation.








    main-> t -> s-> q-> p->t

    old level 2 is 200


    When p calls t, a new level 2 entry is needed

    Level 3 and level 4 are unused (but unchanged)


    old level 2 is 500



    K. Louden, Programming Languages

    Displays continued
    Displays (continued) delayed evaluation.

    • Mechanics of references:

      • Use the display_offset to get the pointer into the display to the ari with the variable

      • Use the local_offset to get to the variable within the ari

    K. Louden, Programming Languages

    Displays continued1
    Displays (continued) delayed evaluation.

    • Display maintenance (assuming no parameters that are subprograms and no pass-by-name parameters)

      • Note that display_offset depends only on the static depth of the procedure whose ari is being built. It is exactly the static_depth of the procedure.

      • There are k+1 entries in the display, where k is the static depth of the currently executing unit (k=0 is for the main program)

    K. Louden, Programming Languages

    Displays continued2
    Displays (continued) delayed evaluation.

    • For a call to procedure P with a static_depth of k:

      a. Save, in the new ari, a copy of the display pointer at position k (you will need a stack)

      b. Put the link to the ari for P at position k in the display

    • At an exit, move the saved display pointer from the ari back into the display at position k

    K. Louden, Programming Languages

    Displays continued3
    Displays (continued) delayed evaluation.

    • To see that this works:

      • Let Psd be the static_depth of P, and Qsd be the static_depth of Q

      • Assume Q calls P

      • There are three possible cases:

        1. Qsd = Psd

        2. Qsd < Psd (would only differ by one)

        3. Qsd > Psd (ignore higher levels)

        Show example where each of these could happen.

    K. Louden, Programming Languages

    Blocks delayed evaluation.

    • Two Methods:

      1. Treat blocks as parameterless subprograms

      • Use activation records

        2. Allocate locals on top of the ari of the subprogram

      • Must use a different method to access locals

      • A little more work for the compiler writer

    K. Louden, Programming Languages

    Implementing dynamic scoping
    Implementing Dynamic Scoping delayed evaluation.

    1. Deep Access (search) - nonlocal references are found by searching the activation record instances on the dynamic chain

    • Length of chain cannot be statically determined

    • Every activation record instance must have variable names recorded

    K. Louden, Programming Languages

    Implementing dynamic scoping1
    Implementing Dynamic Scoping delayed evaluation.

    2. Shallow Access - put locals in a central place

    • Methods:

      a. One stack for each variable name

      b. Central referencing table with an entry for each variable name

      At subprogram entry, add location for each variable.

      At subprogram exit, remove location for each variable.

    K. Louden, Programming Languages

    Using shallow access to implement dynamic scoping
    Using Shallow Access to Implement Dynamic Scoping delayed evaluation.

    K. Louden, Programming Languages

    Fundamentals of subprograms
    Fundamentals of Subprograms delayed evaluation.

    • Actual/Formal Parameter Correspondence:

      1. Positional (this is what we are used to)

      2. Keyword

      • e.g. SORT(LIST => A, LENGTH => N);

      • Advantage: order is irrelevant

      • Disadvantage: user must know the formal parameter’s names

      • Default Values:

        e.g. procedure SORT(LIST : LIST_TYPE;

        LENGTH : INTEGER := 100);


        SORT(LIST => A);

    K. Louden, Programming Languages

    Overloaded subprograms
    Overloaded Subprograms delayed evaluation.

    • Def: An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment

    • C++ and Ada have overloaded subprograms built-in, and users can write their own overloaded subprograms

    K. Louden, Programming Languages

    Generic subprograms
    Generic Subprograms delayed evaluation.

    • A generic or polymorphic subprogram is one that takes parameters of different types on different activations

    • Overloaded subprograms provide ad hoc polymorphism

    • A subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism

    K. Louden, Programming Languages

    Separate independent compilation
    Separate & Independent Compilation delayed evaluation.

    • Def: Independent compilation is compilation of some of the units of a program separately from the rest of the program, without the benefit of interface information

    • Def: Separate compilation is compilation of some of the units of a program separately from the rest of the program, using interface information to check the correctness of the interface between the two parts.

    K. Louden, Programming Languages

    Benefit (of system controlled storage management): delayed evaluation. ability to delay the binding of a storage segment's size and/or location

    • reuse of a storage segment for different jobs (from system supervisor point of view)

    • reuse of storage for different data structures

    • increased generality, not have to specify maximum data structure size

    • dynamic data structures

    • recursive procedures - garbage collection is automatic

      Benefits of programmer controlled storage management

    • Disadvantage: burden on programmer & may interfere with necessary system controlMay lead to subtle errorsMay interfere with system-controlled storage management

    • Advantage: difficult for system to determine when storage may be most effectively allocated and freed

    K. Louden, Programming Languages

    Heap management delayed evaluation.

    • Single-size cells vs. variable-size cells

    • Reference counters (eager approach) vs. garbage collection (lazy approach)

      1. Reference counters: maintain a counter in every cell that store the number of pointers currently pointing at the cell

    • Disadvantages: space required, complications for cells connected circularly

      Expensive - when making a pointer assignment p=q

    • decrement count for old value of p

    • if 0, return to free storage. Check if contains references to other blocks. Could be recursive

    • do pointer assignment

    • Increment reference count for q

    K. Louden, Programming Languages

    One bit reference counting
    One-bit reference counting delayed evaluation.

    • Another variation on reference counting, called one-bit reference counting, uses a single bit flag to indicate whether each object has either "one" or "many" references.

    • If a reference to an object with "one" reference is removed, then the object can be recycled.

    • If an object has "many" references, then removing references does not change this, and that object will never be recycled. It is possible to store the flag as part of the pointer to the object, so no additional space is required in each object to store the count.

    • One-bit reference counting is effective in practice because most actual objects have a reference count of one.

    K. Louden, Programming Languages

    2. Garbage collection: allocate until all available cells allocated; then begin gathering all garbage

    • Every heap cell has an extra bit used by collection algorithm

    • All cells initially set to garbage

    • All pointers traced into heap, and reachable cells marked as not garbage

    • All garbage cells returned to list of available cells

      Disadvantages: when you need it most, it works worst (takes most time when program needs most of cells in heap)

    K. Louden, Programming Languages

    Mark-Sweep - Java uses allocated; then begin gathering all garbage

    • In a mark-sweep collection, the collector first examines the program variables; any blocks of memory pointed to are added to a list of blocks to be examined.

    • For each block on that list, it sets a flag (the mark) on the block to show that it is still required, and also that it has been processed. It also adds to the list any blocks pointed to by that block that have not yet been marked. In this way, all blocks that can be reached by the program are marked.

    • In the second phase, the collector sweeps all allocated memory, searching for blocks that have not been marked. If it finds any, it returns them to the allocator for reuse

    • Can find circular references.

    • Easy if regular use of pointers (Like in LISP)

    • All elements must be reachable by a chain of pointers which begins outside the heap

    • Have to be able to know where all pointers are - both inside the heap and outside. How can a chain be followed from a pointer if there is no predefined location for that pointer in the pointed-to cell?

    K. Louden, Programming Languages

    Conservative garbage collection allocated; then begin gathering all garbage

    • Although garbage collection was first invented in 1958, many languages have been designed and implemented without the possibility of garbage collection in mind. It is usually difficult to add normal garbage collection to such a system, but there is a technique, known as conservative garbage collection, that can be used.

    • The usual problem with such a language is that it doesn't provide the collector with information about the data types, and the collector cannot therefore determine what is a pointer and what isn't.

    • A conservative collector assumes that anything might be a pointer. It regards any data value that looks like a pointer to or into a block of allocated memory as preventing the recycling of that block.

    • You might think that conservative garbage collection could easily perform quite poorly, leaving a lot of garbage uncollected. In practice, it does quite well, and there are refinements that improve matters further.

    • Because references are ambiguous, some objects may be retained despite being actually unreachable. In practice, this happens rarely, and refinements such as black-listing can further reduce the odds.

    K. Louden, Programming Languages

    Incremental Garbage Collection allocated; then begin gathering all garbage

    • Older garbage collection algorithms relied on being able to start collection and continue working until the collection was complete, without interruption. This makes many interactive systems pause during collection, and makes the presence of garbage collection obtrusive.

    • Fortunately, there are modern techniques (known as incremental collection) to allow garbage collection to be performed in a series of small steps while the program is never stopped for long. In this context, the program that uses and modifies the blocks is sometimes known as the mutator. While the collector is trying to determine which blocks of memory are reachable by the mutator, the mutator is busily allocating new blocks, modifying old blocks, and changing the set of blocks it is actually looking at.

    K. Louden, Programming Languages

    Heap Storage Management - Variable Sized Elements allocated; then begin gathering all garbage

    • Memory operations

    • Initially one large block

    • Free space list, as space is recovered

    • allocate from free list: first fit, best fit, worst fit

    • compact: maintain a length of each block

    • recover via explicit, garbage collection or reference countsNeed length of each to locate pieces and coalesce

    • fragmentation

    • partial compaction (coalescing of adjacent free blocks)full compaction (move blocks) Show how

    K. Louden, Programming Languages