Contents

Introduction

In this set of notes we will consider:

Storage Layout

There are many possible ways to organize memory. We will concentrate mainly on the standard Unix approach, illustrated below:

image/svg+xml Code Static Data(e.g. constants) Heap(grows towards high memory) Stack(grows towards low memory) Low address High address Free (unallocated) memory
Usually, the stack is used to store one activation record for each currently active method, and the heap is used for dynamically allocated memory (i.e., memory allocated as a result of using the new operator). An activation record is a data structure used to hold information relevant to one method call. The exact structure of an activation record depends both on the language in use and on the particular implementation; a typical organization is shown in the following picture (the individual fields will be discussed in some detail below and in the next set of notes).
image/svg+xml Parameters Access link Return value(for non-void methods) Saved registers Control link Return address Local variables High address Low address
As mentioned above, activation records are usually stored on the stack. A new record is pushed onto the stack when a method is called, and is popped when the method returns. However, for some languages, activation records may be stored in the heap (this might be done, for example, in a concurrent language, in which method calls do not obey the last-in-first-out protocol of a stack) or in the static data area. We will briefly consider the latter approach, then look at the most common case of stack allocation. In both cases, we will consider what must be done when a method is called, when it starts executing, and when it returns.

Static Allocation

Some old implementations of Fortran used this approach: there is no heap or stack, and all allocation records are in the static data area, one per method. This means that every time a method is called, its parameters and local variables are stored in the same locations (which are known at compile time). This approach has some advantages and disadvantages when compared with stack or heap allocation of activation records:

ADVANTAGES

DISADVANTAGES Using this approach, when a method is called, the calling method: The called method: When the called method is ready to return, it: Back in the calling method, the code that follows that call does the following:

TEST YOURSELF #1

Assume that static allocation is used, and that each activation record contains local variables, parameters, the return address, and (for non-void methods) the return value. Trace the execution of the following code by filling in the appropriate fields of the activation records of the three methods. Also think about where the string literals would be stored.

solution


Stack Allocation

Stack allocation is used to implement most modern programming languages. The basic idea is that:

When a method is called, the calling method: The called method: When the method returns, it:

Activation Records

Consider the following code:

The following pictures show the activation records on the stack at different points during the code's execution (only the control link, parameter, and local variable fields are shown).
  1. When the program starts:
    image/svg+xml ControlLink a 1 ? Main'sActivationRecord FP SP
  2. After main calls f1:
    image/svg+xml ControlLink a x ControlLink 1 1 ? Main'sActivationRecord f1'sActivationRecord FP SP
  3. After f1 calls f2:
    image/svg+xml ControlLink a x ControlLink y ControlLink 1 1 ? 0 Main'sActivationRecord f1'sActivationRecord f2'sActivationRecord FP SP
  4. After f2 calls f1:
    image/svg+xml ControlLink a x ControlLink y ControlLink x ControlLink 1 1 ? 0 0 Main'sActivationRecord f1'sActivationRecord f2'sActivationRecord f1'sActivationRecord FP SP
After this, f1 returns (and its AR is popped), then f2 returns, then the first call to f1 returns, then the whole program ends.

TEST YOURSELF #2

Assume that stack allocation is used. Trace the execution of the following code by filling in the local variables, parameters, and control link fields of the activation records (recall that dynamically allocated storage is stored in the heap, not on the stack).

solution