Oberon || Library || Module Index || Search Engine || Definition || Module

Ulm's Oberon Library:


CompilerStackAllocations - static allocations for stack frames


CONST growsup = 1; growsdown = 2;
TYPE Direction = SHORTINT; (* growsup, growsdown *)

TYPE Stack = POINTER TO StackRec; TYPE StackRec = RECORD (Disciplines.ObjectRec) align: Types.Size; END;

PROCEDURE Init(stack: Stack; baseoff, align: Types.Size; dir: Direction);

PROCEDURE Allocate(stack: Stack; size, align: Types.Size; autofree: BOOLEAN) : Types.Size; PROCEDURE Free(stack: Stack; offset: Types.Size);

PROCEDURE Close(stack: Stack); PROCEDURE AllocateClosed(stack: Stack; size: Types.Size) : Types.Size;

PROCEDURE GetSize(stack: Stack) : Types.Size;


Stack frames provide space for parameters, saved registers, local variables, and temporary values. All offsets relative to the frame pointer can be computed at compile time with the exception of dynamic arrays. CompilerStackAllocations supports the code generator in allocating static space within a stack frame for items that are not known from the signature of a procedure and its local variable list. Typical examples are the construction of constants that do not fit into a register, passing constants or register values to a procedure that expects an address, and cases where we run out of registers.

Init initializes the stack frame object stack for the direction dir (usually growsdown) with an initial offset of baseoff. The frame pointer and baseoff are considered to be multiplies of align. All alignment parameters given to Allocate must be divisors of this alignment.

Allocate allocates size bytes (must be positive) for the stack frame associated with stack and returns the offset which is a multiply of align. If autofree is set to TRUE, the area remains allocated until Close gets called. Otherwise, the area has to be freed using Free.

Free allows to deallocate the area at offset which was formerly returned by Allocate with autofree set to FALSE.

Close closes the regular allocation phase of the stack. Neither Allocate nor Free may be called for stack afterwards. All offsets returned by Allocate with autofree set to FALSE must have been freed before.

AllocateClosed allows to allocate space (with the alignment given to Init) that is available from begin to end of the procedure. In contrast, areas allocated by Allocate are only available from the point where Allocate was called until the area is either freed using Free or automatically closed by Close. AllocateClosed is useful if we want to save only those registers that are actually used. But this is not known until the code for the whole block is generated. Please note that AllocateClosed must not be called before closing the stack and not after GetSize was called for the stack.

GetSize returns the next aligned offset beyond the allocated area. This is the size of the allocated area if baseoff was 0. This procedure must not be called unless Close has already been called for stack.


The procedure GetSize is not properly named if baseoff is non-zero.

Currently, CompilerStackAllocations has no way of generating CompilerErrors events for the current compilation process. Hence, CompilerStackAllocations has to resort to assertions to assure that offsets are kept within the limits of the representation of Types.Size.


This module was written by Christian Ehrhardt. The manual page is due to Andreas Borchert.
Edited by: borchert, last change: 2005/08/26, revision: 1.1, converted to HTML: 2005/08/26

Oberon || Library || Module Index || Search Engine || Definition || Module