Java Reference
In-Depth Information
bltz
$index,badIndex
# Branch to badIndex if $index<0
lw
$temp,SIZE($array)
# Load size of array into $temp
slt
$temp,$index,$temp
# $temp = $index < size of array
beqz
$temp,badIndex
# Branch to badIndex if
# $index >= size of array
sll
$temp,$index,2
# multiply $index by 4 (size of
# an int) using a left shift
add
$temp,$temp,$array
# Compute $array + 4*$index
lw
$val,OFFSET($temp)
# Load word at
# $array + 4*$index + OFFSET
Figure 13.3: MIPS code for iaload bytecode
bltz
$index,badIndex
# Branch to badIndex if $index<0
lw
$temp,SIZE($array)
# Load size of array into $temp
slt
$temp,$index,$temp
# $temp = $index < size of array
beqz
$temp,badIndex
# Branch to badIndex if
# $index >= size of array
sll
$temp,$index,2
# multiply $index by 4 (size of
# an int) using a left shift
add
$temp,$temp,$array
# Compute $array + 4*$index
sw
$val,OFFSET($temp)
# Load $val into word at
#
$array + 4*$index + OFFSET
Figure 13.4: MIPS code for iastore bytecode
($array) using the index at the top of the stack ($index). We can use the code
shown in Figure 13.4 to implement an iastore instruction:
The MIPS code we have chosen for array indexing looks rather complex
and expensive, especially since arrays are a very commonly used data struc-
ture. Part of this complexity is due to the fact that we have included array
bounds checking, as required in Java. In C and C
, array bounds are rarely
checked at runtime, allowing for faster (but less secure) code.
In many cases it is possible to optimize or entirely eliminate array bounds
checks. On architectures that support unsigned arithmetic, the check for an
index too large and the check for an index too small (less than zero) can be
combined. The trick is to do an unsigned comparison between the array index
and the array size. Anegative indexwill be equivalent to a very large unsigned
value (since its leftmost bit will be one), making it greater than the array size.
In a for loop, it is often possible to determine that a loop index is bounded
++
 
Search WWH ::




Custom Search