Java Reference
In-Depth Information
Work registers may be used at any time by any code-generation routine.
Work registers may safely be used only in local code sequences, over which
the code generator has complete control. That is, if we were generating code
to do an indexing operation on an array (say, a[i+j]), it would be wrong to
use a work register to hold the address of the array because computation of
i+j might also use the same work register. An allocatable register would, of
course, be protected. Work registers are useful in several circumstances:
Sometimes we need a register for a very brief time. (For example, in
compiling a=bwe load b into a register and then immediately store
the register into a.) Using awork register saves the overhead of allocating
and then immediately freeing a register.
Many instructions require that their operands be in registers. Since work
registers are always free, we need not worry about there being no free
registers. If necessary, we can load values from memory into work
registers, execute an instruction or two, then save needed values back
into memory.
We can pretendwe have more registers than we really do. Such registers,
sometimes called virtual registers or pseudo-registers ,canbesimulated
by allocating them in memory and placing their values into work regis-
ters when they are used in instructions.
In general, reserved registers are identified in advance by hardware and op-
erating systems conventions. Sometimes work registers are also established
in advance, and if they are not, we can choose three or four for this purpose.
The remaining registers can be marked allocatable. They will hold tempo-
rary values, and may also be used to hold frequently accessed variables and
constants.
The
get
R
eg
and
free
R
eg
methods
To allocate and free registers, we will create twomethods,
get
R
eg
and
free
R
eg
.
get
will allocate a single allocatable register and return its index. (If we
have both integer and float registers, we will create
R
eg
get
R
eg
and
get
F
loat
R
eg
.)
A register allocated by a call to
get
R
eg
remains allocated to the caller until it
is returned.
What happens if no more registers are available for allocation? In simple
compilers we can simply terminate compilation with a message that the pro-
gram requires more registers than are available. Modern computers routinely
have 20 or more allocatable registers. Unless registers are used aggressively
to hold program variables and constants across a relatively large section of a
program (Section 13.3.2), the chances are remote that a “real-life” programwill
exhaust the allocatable registers.
 
Search WWH ::




Custom Search