CSc 520 - Principles of Programming Languages
5 : Memory Management -- Stack Allocation

Christian Collberg

Department of Computer Science

University of Arizona

1 Questions

2 Stack Allocation

Local Variables:
stored on the run-time stack.
Actual parameters:
stored on the stack or in special argument registers.

3 Storage Allocation...

 
  • When a procedure call is made the caller and the callee cooperate to set up the new frame. When the call returns, the frame is removed from the stack.
returned value
actual parameter 1
actual parameter 2
...
return address
static link
control link
saved registers, etc
local variable 1
local variable 2
...


Recursion


4 Recursion Examples

Example I (Factorial function):
$R_0$ and $R_1$ are registers that hold temporary results.
Example II (Fibonacci function):
We show the status of the stack after the first call to B(1) has completed and the first call to B(0) is almost ready to return.

The next step will be to pop B(0)'s AR, return to B(2), and then for B(2) to return with the sum B(1)+B(0).

acttree

5 Recursion Example

1mm
 

\begin{gprogram}
XX\=\kill
PROCEDURE F (n:INTEGER) :INTEGER; \\
\x VAR L:INTEGE...
...N L; \\
END F; \\
BEGIN \\
\x (9) C:=F(3); \\
\x (10) \\
END
\end{gprogram}
 
\begin{astack}{60}{20}{20}{0}
\arput{
\begin{arecord}{F(1)}
\arentry{n = 1}
\a...
...\end{arecord}\begin{arecord}{main}
\arentry{C = ?}
\end{arecord}}
\end{astack}

6 Recursion Example

1mm
 

\begin{gprogram}
X\=(9)XX\=X\=\kill
PROCEDURE B (n:INTEGER):INTEGER; \\
\x VAR ...
...N L; \\
END B; \\
BEGIN \\
\x (9) C:=B(4); \\
\x (10) \\
END
\end{gprogram}
 
\begin{astack}{60}{20}{20}{0}
\arput{
\begin{arecord}{B(0)}
\arentry{n = 1; L =...
...\end{arecord}\begin{arecord}{main}
\arentry{C = ?}
\end{arecord}}
\end{astack}


Calling Conventions


7 Procedure Call Conventions

Work During Call Sequence:
Allocate Activation Record, Set up Control Link and Static Link. Store Return Address. Save registers.
Work During Return Sequence:
Deallocate Activation Record, Restore saved registers, Return function result Jump to code following the call-site.

8 Example Call/Return Sequence

The Call Sequence

The caller:
Allocates the activation record, Evaluates actuals, Stores the return address, Adjusts the stack pointer, and Jumps to the start of the callee's code.
The callee:
Saves register values, Initializes local data, Begins execution.

The Return Sequence

The callee:
Stores the return value, Restores registers, Returns to the code following the call instr.
The caller:
Restores the stack pointer, Loads the return value.


The Control Link


9 The Control Link

10 The Control Link...

FramePointer

11 The Control Link...


1mm

\rotatebox{90}{\begin{tabular}{@{}l@{}l}
\begin{minipage}[c]{3cm}
\begin{tt}
\be...
...{minipage}&
\begin{minipage}[c]{5cm}
\loadfig{Call}
\end{minipage}\end{tabular}}


Procedure Call on the MIPS


12 MIPS Activation Record

\rotatebox{90}{\loadfig{mips}}

13 MIPS Procedure Call

14 MIPS Procedure Call...

15 MIPS Procedure Call...

16 MIPS Procedure Call...

\rotatebox{90}{\loadfig{MipsCall}}

17 MIPS Procedure Returns

18 MIPS Procedure Returns...

\rotatebox{90}{\loadfig{MipsReturn}}

19 Readings and References

  • Read Scott, pp. 106-111, 410-413

20 Summary

  • Each procedure call pushes a new activation record on the run-time stack. The AR contains local variables, actual parameters, a static (access) link, a dynamic (control) link, the return address, saved registers, etc.
  • The frame pointer (FP) (which is usually kept in a register) points to a fixed place in the topmost activation record. Each local variable and actual parameter is at a fixed offset from FP.

21 Summary...

  • The dynamic link is used to restore the FP when a procedure call returns.
  • The static link is used to access non-local variables, i.e. local variables which are declared within a procedure which statically encloses the current one.



Christian Collberg 2008-01-30