Sponsored Link •
The nitty gritty of tableswitch and lookupswitch
instructions both include one default branch offset and a variable-length set of
case value/branch offset pairs. Both
instructions pop the key (the value of the expression in the
parentheses immediately following the
from the stack. The key is compared with all the case values. If a
match is found, the branch offset associated with the case value is
taken. If no match is found, the default branch offset is taken.
The difference between
lookupswitch is in how they indicate the case values. The
lookupswitch instruction is more general-purpose than
tableswitch is usually more efficient.
Both instructions are followed by zero to three bytes of padding --
enough so that the byte immediately following the padding starts at an
address that is a multiple of four bytes from the beginning of the
method. (These two instructions, by the way, are the only ones in the
entire Java virtual machine instruction set that involve alignment on a
greater than one-byte boundary.) For both instructions, the next four
bytes after the padding is the default branch offset.
After the zero- to three-byte padding and the four-byte default
branch offset, the
lookupswitch opcode is followed by a
four-byte value, npairs, which indicates the number of case
value/branch offset pairs that will follow. The case value is an
int; this highlights the fact that switch statements in Java
require a key expression that is an
byte. If you
attempt to use a
double as a switch key, your program won't compile. The
branch offset associated with each case value is another four-byte
tableswitch instruction, the zero- to three-byte
padding and the four-byte default branch offset are followed by low and
int values. The low and high values indicate the
endpoints of a range of case values included in this
tableswitch instruction. Following the low and high
values are high - low + 1 branch offsets -- one branch offset for high,
one for low, and one for each integer case value in between high and
low. The branch offset for low immediately follows the high value.
Thus, when the Java virtual machine encounters a
lookupswitch instruction, it must check the key against
each case value until it finds a match or runs out of case values. If
it runs out of case values, it uses the default branch offset. On the
other hand, when the JVM encounters a
instruction, it can simply check to see if the key is within the range
defined by low and high. If not, it takes the default branch offset.
If so, it just subtracts low from key to get an offset into the list of
branch offsets. In this manner, it can determine the appropriate branch
offset without having to check each case value.
||<0-3 byte pad>defaultbyte1, defaultbyte2, defaultbyte3, defaultbyte4, npairs1, npairs2, npairs3, npairs4, case value/branch offset pairs...||pop key, match key with case values, if match found jump to associated branch offset, else jump to default branch offset|
||<0-3 byte pad>defaultbyte1, defaultbyte2, defaultbyte3, defaultbyte4, lowbyte1, lowbyte2, lowbyte3, lowbyte4, highbyte1, highbyte2, highbyte3, highbyte4, branch offsets...||pop key, if not in low/high range jump to default branch offset, else get the (key - low) branch offset and jump|
Other than the opcodes described above, the only Java virtual machine instructions that affect control flow are those that deal with throwing and catching exceptions, try-finally clauses, and invoking and returning from methods. The bytecodes for exceptions and try-finally clauses were discussed in the previous two installments of this column (see Resources). The bytecodes that deal with invoking and returning from methods will be treated in a future installment.