Java Reference
In-Depth Information
start of the frame. This o
set is a fixed constant, determined at compile-time.
Because we normally store the start of the frame in a register, each piece of
data can be addressedas a (
Register
,
O
ff
set
) pair, which is a standard addressing
mode in almost all computer architectures. For example, if register
R
points
to the beginning of p's frame, variable b can be addressed as (
R
,
ff
16) , with
the o
set value of 16 actually being added to the contents of
R
at runtime as
instructions are decoded and executed. Normally, the literal 2.51 of procedure
p is not stored in p's frame because the values of local data that are stored in a
frame disappear with it at the end of a call. If 2.51 were stored in the frame,
its value would have to be initialized before each call. It is easier and more
e
ff
cient to allocate literals in a static area, often called a
literal pool
or
constant
pool
. Java uses a constant pool to store literals, type, method, and interface
information as well as class and field names.
Just as local variables are assigned an o
set within the current frame, fields
within a class or struct definition are also assigned o
ff
sets relative to the be-
ginning of the data object. Consider the following struct definition:
ff
struct s {
int a;
double b;
double c[10];
}
As each field is processed it is assigned an o
ff
set, starting at zero. Thus a is
given an o
set is determined by the size of fields that precede it,
augmented with alignment restrictions. Integers typically require 4 bytes, and
doubles must be allocated at addresses that are a multiple of 8, so b gets an
o
ff
set of 0. b's o
ff
ff
set of 8. Array c is assigned an o
ff
set of 16, and the size of the entire struct
is 96 bytes.
Using this simple scheme, we may use the following formula to compute
the address of a field within a class or struct:
address
(
struct
.
field
)
=
address
(
struct
)
+
o
ff
set
(
field
)
For example, if we declare:
struct s
var;
and s is assigned a static address of 4000, then the address of var.b is 4000
+
8
=
4008.