Access jump table by index and jump
Bytecode
Type | Description |
u1 | tableswitch opcode = 0xAA (170) |
- | ...0-3 bytes of padding ... |
u4 | default_offset |
u4 | <low> |
u4 |
<low> + N - 1 |
u4 | offset_1 |
u4 | offset_2 |
... | ... |
u4 | offset_N |
Description
A tableswitch is a variable-length instruction. Immediately after the tableswitch opcode, between zero and three null bytes (zeroed bytes, not the null object) are inserted as padding. The number of null bytes is chosen so that the following byte begins at an address that is a multiple of four bytes from the start of the current method (the opcode of its first instruction). Immediately after the padding follow the bytes constituting a series of signed 32-bit values: default, low, high, and then high - low + 1 further signed 32-bit offsets. The value low must be less than or equal to high. The high - low + 1 signed 32-bit offsets are treated as a 0-based jump table. Each of these signed 32-bit values is constructed as (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4.
The index must be of type int
and is popped
from the operand stack. If index is less than low or index is greater
than high, then a target address is calculated by adding default to the
address of the opcode of this tableswitch instruction. Otherwise, the offset at
position index - low of the jump table is extracted. The target address is
calculated by adding that offset to the address of the opcode of this tableswitch
instruction. Execution then continues at the target address.
The target address which can be calculated from each jump table offset, as well as the ones that can be calculated from default, must be the address of an opcode of an instruction within the method that contains this tableswitch instruction.
The alignment required of the 4-byte operands of the tableswitch instruction guarantees 4-byte alignment of those operands if and only if the method that contains the tableswitch starts on a 4-byte boundary.
Addresses are measured in bytes from the start of the bytecode (i.e. address 0 is the first byte in the bytecode of the currently executing method).