Java Reference
In-Depth Information
p(int a) {
double b;
double c[10];
b = c[a] * 2.51;
}
Figure 12.1: A Simple Subprogram
Total size= 104
Space for c
Offset = 24
Space for b
Offset = 16
Padding
Space for a
Offset = 8
Control Information
Offset = 0
Figure 12.2: Frame for Procedure p
routine's local data. To see how stack allocation works, consider the C
++
subprogram shown in Figure 12.1. Procedure p requires space for the param-
eter a as well as the local variables b and c. It also needs space for control
information, such as the return address (a subprogram may be called from
many di
erent places). As the procedure is compiled, the space requirements
of the procedure are recorded (in the procedure's symbol table). In particular,
the o ff set of each data item relative to the beginning of the frame is stored in
the symbol table. The total amount of space needed, and thus the size of the
frame, is also recorded. The memory required for each individual variable
(and hence an entire frame) is machine dependent. Di
ff
ff
erent architectures
may assume di
erent sizes for primitive values like integers or addresses. It
is wise to avoid “hard coding” machine dependent quantities in a compiler.
Instead, calls to a target environment class can be made.
In our example, assume p's control information requires 8 bytes (this size
is usually the same for all methods and subprograms). Assume parameter a
requires 4 bytes, local variable b requires 8 bytes, and local array c requires
80 bytes. Because many machines require that word and doubleword data be
aligned, it is common practice to pad a frame (if necessary) so that its size is
a multiple of 4 or 8 bytes. This guarantees a useful invariant: at all times the
top of the stack is properly aligned. Figure 12.2 shows p's frame.
Within p, each local data object is addressed by its o
ff
ff
set relative to the
 
Search WWH ::




Custom Search