Java Reference
In-Depth Information
lw
$t0,16($fp)
# Load b, at 16+$fp, into $t0
lw
$t1,20($fp)
# Load c, at 20+$fp, into $t1
add
$t2,$t0,$t1
# Add $t0 and $t1 into $t2
lw
$t3,24($fp)
# Load d, at 24+$fp, into $t3
sub
$t4,$t2,$t3
# Subtract $t3 from $t2 into $t4
sw
$t4,12($fp)
# Store result into a, at 12+$fp
Figure 13.2: MIPS code for a=b+c-d;
Continuing with our example, assume a, b, c,andd are assigned frame
o
sets 12, 16, 20, and 24 respectively (we will discuss memory allocation
for locals and fields in Section 13.1.1). These four variables are given o
ff
sets
because local variables in a procedure or method are allocated as part of a
frame —a block of memory allocated on the runtime stack whenever a call is
made. Thus, rather than push or pop individual data values, as bytecodes do,
we prefer to push a single large block of memory once per call.
ff
Let us assume the temporaries we allocate areMIPS registers, denoted $t0,
$t1,
.Eachtimewegeneratecodeforabytecodeinstructionthatpushesa
value onto the stack, we will call
...
(discussed in Section 13.3.1) to allocate
a result register. Whenever we generateMIPS code for a bytecode that accesses
stack values, we will use the registers already allocated to hold stack values.
The net e
get
R
eg
ect is to use registers rather than stack locations to hold operands,
which yields fast and compact instruction sequences. For our above example
we might generate the MIPS code shown in Figure 13.2 (“#” begins a one line
comment in MIPS source code).
ff
The lw instruction loads a word of memory into a register. Addresses of
locals are computed relative to $fp, the frame pointer, which always points to
the currently active frame. Similarly, sw stores a register into aword of memory.
The add and sub instructions add and subtract two registers, respectively,
putting the result into a third register.
Bytecodes that push constants, like bipush n, can be implemented as an
immediate load of a literal into a register. As an optimization, we can delay
loading the constant value into the register until we are sure it is actually
needed there. To achieve this e
ect, we note that a particular stack location
associated with a MIPS register will hold a known literal value. When that
register is used, we determine if the constant value must be its own register, or
if an immediate instruction can be used instead. For example, we may replace
a register-to-register add with an add-immediate instruction.
ff
 
Search WWH ::




Custom Search