Sub topic 3
Calling sequence and frame layout
This chapter assumes you know what an activation record is and how the control stack grows and shrinks, as covered in the previous chapter. It also assumes basic C functions and recursion. Here you will see what each field of a frame holds and which steps the caller and callee perform at a call and at a return.
The fields of a frame
A compiler fixes a layout for the activation record of every procedure. Different machines vary the order, but a common textbook layout, from the caller's side of the frame toward the top of the stack, is shown below.
| Field | Contents | Who fills it |
|---|---|---|
| Actual parameters | Argument values or addresses | Caller |
| Returned value | Slot for the result | Callee writes, caller reads |
| Control link | Pointer to the caller's frame | Caller |
| Access link | Pointer to the frame of the lexically enclosing procedure | Caller |
| Saved machine status | Return address, saved registers | Callee |
| Local data | Variables declared in the procedure | Callee |
| Temporaries | Intermediate results from expression evaluation | Callee |
Placing the parameters and the result slot nearest the caller is deliberate. The caller can reach them with offsets from its own stack top, without knowing how large the callee's locals are. Everything past the saved status is private to the callee, which knows its own sizes at compile time.
The access link only matters for languages with nested procedures. C has none, so a C frame leaves it out. The next chapter covers access links.
Calling and return sequences
A calling sequence is the code that runs when one procedure calls another. It is split between the caller and the callee. The return sequence undoes it. The register top_sp marks the end of the caller's frame.
The steps in order:
- The caller evaluates the actual parameters and places them in the new frame.
- The caller stores the return address and the old
top_sp(the control link) in the new frame, then raisestop_sppast its own locals and temporaries. - The callee saves register values and other machine status.
- The callee initializes its locals and starts its body.
- On return, the callee stores the result next to the parameters, restores
top_spand the registers, and jumps to the return address. - The caller copies the result from the callee's old frame.
Seeing frames in running code
You can observe the stack growing by printing the address of a local variable at each recursion depth. The program below prints each local's offset from the first frame's local. Compile it with gcc -O0 frames.c. An optimizer may turn the tail call into a loop, which removes the frames you want to see.
#include <stdio.h>
#include <stdint.h>
static void probe(int depth, uintptr_t first) {
int local = depth;
uintptr_t here = (uintptr_t)&local;
if (depth == 0) {
first = here;
}
printf("depth %d: local at offset %ld\n", depth, (long)(here - first));
if (depth < 3) {
probe(depth + 1, first);
}
}
int main(void) {
probe(0, 0);
return 0;
}On a typical x86-64 system the offsets are 0, then a negative number, then a more negative one, and so on. The stack grows toward lower addresses, and each step equals one frame size. The exact numbers depend on the compiler and platform. The constant spacing is the point.
Variable-length data
Some data has a size known only at run time, such as a local array whose length is a parameter. The frame cannot reserve a fixed number of bytes for it. Instead, the frame holds a fixed-size pointer to the array. The array's storage goes just above the frame, at top_sp, and top_sp moves up by the array size. Because the pointer sits at a fixed offset, the compiler can still generate fixed-offset code for everything else in the frame. On return, restoring top_sp frees the array along with the frame.
Common mistakes
- Counting frames wrongly.
mainhas a frame too. Forfact(3)the deepest stack holds four frames:main,fact(3),fact(2)andfact(1). - Forgetting that the base case still has a frame.
fact(1)returns without recursing, but its frame exists while it runs. - Mixing up the control link and the access link. The control link always points to the caller. The access link points to the lexically enclosing procedure, which may be a different frame.
- Assuming the stack grows downward in every question. Read the problem statement. The logic is the same either way, but offsets change sign.
Exercise
A C function fact(int n) has a frame with these fields: 4-byte parameter, 4-byte returned value, 4-byte control link, no access link, 8 bytes of saved machine status, no locals and 4 bytes of temporaries. The frame of main is 20 bytes. How many bytes of stack are in use at the deepest point of main calling fact(4), where fact returns 1 for n <= 1 and otherwise calls fact(n - 1)?
Show answer
Each fact frame is 4 + 4 + 4 + 0 + 8 + 0 + 4 = 24 bytes. The deepest point has frames for fact(4), fact(3), fact(2) and fact(1), which is 4 × 24 = 96 bytes. Adding the 20-byte frame of main gives 116 bytes.