编译原理 第七章 Runtime Environments

本书以 编译原理及实践 为textbook

Chapter Seven. Runtime Environments

Three kinds of runtime environments:

  1. Fully static environment; FORTRAN77
  2. stack-based environment; C C++
  3. fully dynamic environment; LISP

Memory Organization During Program Execution

The general organization of runtime storage:

Code area
Global/static area
Stack
Free space
heap

Procedure activation record:

Space for arguments (parameters)
Space for bookkeeping information, including return address
Space for local data
Space for local temporaries

Processor registers:
Processor registers are also part of the structure of the runtime environment.
Special-purpose registers to keep track of execution:

  1. PC program counter;
  2. SP stack pointer;
  3. FP frame pointer;
  4. AP argument pointer

calling sequence:

  1. The allocation of memory for the activation record;
  2. The computation and storing of the arguments;
  3. The storing and setting of necessary registers to affect the call

returning sequence:

  1. The placing of the return value where it can be accessed by the caller;
  2. The readjustment of registers;
  3. The possible releasing for activation record memory.

The design of the calling sequence:

  1. how to divide the calling sequence operations between the caller and callee
  2. to what extent to rely on processor support for calls rather that generating explicit code for each step of the calling sequence

The caller: computing the arguments and placing them in locations where they may be found by the callee.
The callee : the return address, the registers must be saved.

Fully Static Runtime Environments:
All data are static, remaining fixed in memory for the duration of program execution.
The entire program memory:

  1. The global variables and all variables are allocated statically.
  2. Each procedure has only a single activation record.
  3. All variables ( local or global) can be accessed directly via fixed address.
  4. No extra information about the environment needs to be kept in an activation record.

The calling sequence (simple):

  1. each argument is computed and stored into its appropriate parameter location in the activation of the procedure being called.
  2. the return address in the code of the caller is saved.
  3. a jump is made to the beginning of the code of the called procedure.
  4. On return, a simple jump is made to the return address.

In FORTRAN77, parameter values are implicitly memory references, so the locations of the arguments of the call(TABLE, 3, and TEMP) are copied into the parameter locations of QUADMEAN.

  1. an extra dereference is required to access parameter values.
  2. array parameters do not need to be reallocated and copied.
  3. constant arguments must be stored to a memory location and this location used during the call.
    The unnamed location is used to store temporary value during the computation of arithmetic expression.

Stack-based runtime environments:
For a language, in which

  1. Recursive calls are allowed
  2. Local variables are newly allocated at each call
  3. Activation records cannot be allocated statically. Instead, activation records must be allocated in a stack-based fashion.

The stack of activation records grows and shrinks with the main of calls in the executing program.
Each procedure may have several different activation records on the call stack at one time.
More complex strategy for bookkeeping and variable access.

Stack-Based Environments Without Local Procedures
In a language where all procedures are global( such as C language), the stack-based environment requires two things:

  1. Frame pointer or fp, a pointer to the current activation record to allow access to local variable.
    control link or dynamic link, a point to a record of the immediately preceding activation.
  2. Stack pointer or sp, a point to the last location allocated on the call stack.

A General AR:

Actual parameters
Control link
Returned values
Saved machine status
Local data
Temporaries

In each new activation record, the control link points to the control link of the previous activation record.
The fp points to the control link of the current activation record.

Activation tree
Access to Names:

  1. parameters and local variable must be found by offset from the current frame pointer.
  2. In most language, the offset can be statically computable by the compiler.

The calling sequence:

  1. Compute the arguments and store them in their correct positions in the new activation record of the procedure.
  2. Store the fp as the control link in the new activation record;
  3. Change the fp so that it points to the beginning of the new activation record;
  4. Store the return address in the new activation record;
  5. Perform a jump to the code of the procedure to be called.

When a procedure exits:

  1. Copy the fp to the sp.
  2. Load the control link into the fp.
  3. Perform a jump to the return address.
  4. Change the sp to pop the arguments.

Dealing with variable-length data
Data may vary both in the number of data objects and in the size of each object:

  1. The number of arguments in a call may vary from call to call.
  2. The size of an array parameter or a local array variable may vary from call to call

solution:

  1. C compiler typically deal with this by pushing the arguments to a call in reverse order onto the runtime stack. The first parameter is always located at a fixed offset from the fp in the implementation described above.
  2. Another option is to use a processor mechanism such as ap(argument pointer) in VAX architecture.

Local Temporaries and Nested Declarations
Local temporaries are partial results of computations that must be saved across procedure calls
Nested Declarations:

  1. A compiler could treat a block just like a procedure and create a new activation record each time a block is entered and discard it on exit.
    this would be inefficient.
  2. A simpler method is to treat them in a similar way to temporary expression.

Stack-Based Environment with local Procedures
Consider the non-local and non-global references
To solve the above problem about variable access, we add an extra piece of bookkeeping information called the access link to each activation record

A General AR:

Actual parameters
Access link
Control link
Returned values
Saved machine status
Local data
Temporaries

Access link represents the defining environment of the procedure; access link is sometimes also called the static link.
Control link represents the calling environment of the procedure.

(1) The access link must be pushed onto the runtime stack just before the fp during a call
(2) The sp must be adjusted by an extra amount to remove the access link after an exit.

Fully Dynamic Environment
It can deallocate activation records only when all references to them have disappeared
garbage collection:

  1. the tracking of references during execution .
  2. the ability to find and deallocate in accessible areas of memory at arbitrary times during execution.

When control is returned to the caller, the exited activation record remains in memory, to be de-allocated at some later time.

Dynamic Memory in Object-Oriented Languages
An object in memory can be viewed as a cross between a traditional record structure and an activation record.

  1. One straightforward mechanism :
    for initialization code to copy all the currently inherited features ( and methods) directly into the record structure. This is extremely wasteful of space.
  2. keep a complete description of the class structure in memory at each point during execution.
    inheritance maintained by superclass pointers all method pointers kept as fields in the class structure.
  3. compute the list of code pointers for available methods of each class , and store this in ( static) memory as a virtual function table ( in c++ ).
    it can be arranged so that each method has a predictable offset
    a traversal of the class hierarchy with a series of table lookups is no longer necessary.
    Each object contains a pointer to the appropriate virtual function table.

Heap Management

  1. allocate
    take a size parameter
    return a pointer to a block of memory of the correct size, return a null pointer if none exists.
  2. free
    takes a pointer to an allocated block of memory and marks it as being free again.

A standard method for maintaining the heap and implementing these functions:

  1. a circular linked list of free blocks.
  2. memory is taken by malloc.
  3. memory is return by free.
    drawbacks:
  4. The free operation can not tell if the pointer is legal or not.
  5. Care must be taken to coalesce blocks, otherwise, the heap can quickly become fragmented.

Automatic Management of the heap

  1. manual method
    the use of malloc and free to perform dynamic allocation and de-allocation of pointer .
  2. garbage collection
    the process of reclamation of allocated but no longer used storage without an explicit call to free.

mark and sweep garbage collection
no memory is freed until a call to malloc fails, which does this in two passes.

  1. Follows all pointers recursively,starting with all currently accessible pointer values and marks each block of storage reached.
  2. Sweeps linearly through memory.
    returning unmarked blocks to free memory.
    perform memory compaction to leave only one large block of contiguous free space at the other end.

The Mark and Sweep garbage collection has several drawbacks:

  1. Requires extra storage;
  2. The double pass through memory cause a significant delay in processing

stop-and-copy or two-space garbage collection

  1. During the marking pass, all reached blocks are immediately copied to the second half of storage not in use;
  2. No extra mark bit is required and only one pass is required;
  3. It also performs compaction automatically.
  4. It does little to improve processing delays during storage reclamation.

Generational garbage collection
Allocated objects that survive long enough are simply copied into permanent space and are never deallocated during subsequent storage reclamations.

Parameter Passing Mechanisms
The common parameter passing mechanisms:

  1. pass by value
  2. pass by reference
  3. pass by value-result
  4. pass by name

Pass by Value:
The arguments are expressions that are evaluated at the time of the call.
their values becomes the values of the parameter during the execution of the procedure.

Pass by Reference:
pass by reference passes the location of the variable.
the parameter becomes an alias for the argument.

Pass by Value-Result:
The mechanism achieves a similar result to pass by reference, except that no actual alias is established.
Known as copy-in, copy-out, or copy- restore.
The value of the argument is copied and used in the procedure.
the final value of the parameter is copied back out to the location of the argument.

The unspecified issues in this mechanism:

  1. The order in which results are copied back to the arguments.
  2. The locations of the arguments are calculated only on entry or recalculated on exit.

Pass by Name:
the argument is not evaluated until its actual use in the called program.

The interpretation of pass by name is as follows:

  1. The text of an argument at point of call is viewed as a function in its own right.
  2. The arguments are evaluated every time the parameter name is reached in the procedure.
  3. The argument will always be evaluated in the caller’s environment.

你可能感兴趣的:(编译原理)