tableswitch <low> [<high>] <label1> <label2> ... <labelN> default : <defaultLabel><low> is a 32-bit signed integer. <label1>, <label2>, etc. are label names. <high> is a 32-bit signed integer which is greater than or equal to <low>. There must be <high>-<low>+1 labels given in the table. If <high> is omitted, Jasmin calculates it for you based on the number of entries in the table.
To define the location of the label, use the <label> name followed by a colon:
<label>:<label> then becomes associated the address of the following instruction. Labels can only be assigned one location in a method. On the other hand, a single <label> can be the target of multiple branch instructions.
Stack Before
Description After val ... ...
tableswitch is used to perform computed jump. The jump table used by tableswitch is given in the bytecode after the tableswitch opcode.
An integer, val, is popped off the top of the operand stack. If this value is less than <low>, or if it is greater than <high>, execution branches to the address (pc + default_offset), where pc is the address of the tableswitch opcode in the bytecode, and default_offset is taken from the bytecode and is the relative address of the instruction at <defaultLabel>.
If val is in the range <low> to <high>, execution branches to the i'th entry in the table, where i is (val - <low>) and the first entry in the table is index 0. i.e. pc becomes the address (pc + table[val - <low>]).
The following pseudo-code illustrates the computation performed by tableswitch:
int val = pop(); // pop an int from the stack if (val < low || val > high) { // if its less than <low> or greater than <high>, pc += default; // branch to default } else { // otherwise pc += table[val - low]; // branch to entry in table }Notice that all addresses stored in the table are relative to the address of the tableswitch opcode in the bytecode. If you are using Jasmin, these addresses are computed for you from the address of the given labels.
Example
iload_1 ; push local variable 1 onto the stack ; if the variable contains 0, jump to ZeroLabel ; if the variable contains 1, jump to OneLabel ; if the variable contains 2, jump to TwoLabel ; otherwis jump to DefaultLabel tableswitch 0 2 ZeroLabel OneLabel TwoLabel default: DefaultLabel ZeroLabel: ; the variable contained 0 ... ldc 100 ireturn ; return the result 100 OneLabel: ; the variable contained 1 ... bipush 200 ireturn ; return the result 200 TwoLabel: ; the variable contained 2 ... bipush 300 ireturn ; return the result 300 DefaultLabel: ; the variable contained something else ... bipush 0 ireturn ; return the result 0
Bytecode
tableswitch is a variable length instruction. Immediately after the tableswitch opcode, between 0 to 3 bytes of padding zeros are inserted, so that the default_offset parameter starts at an offset in the bytecode which is a multiple of 4. Next, two signed 32-bit integers are given - low and high, followed by (low - high + 1) 32-bit integer offsets.
Type | Description |
u1 | tableswitch opcode = 0xAA (170) |
- | ...0-3 bytes of padding ... |
s4 | default_offset |
s4 | <low> |
s4 | <low> + N - 1 |
s4 | offset_1 |
s4 | offset_2 |
... | ... |
s4 | offset_N |
lookupswitch
Notes
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).