Why does the format of JVM tableswitch/lookupswitch instruction has between 0 and 3 bytes padding?

Viewed 80

I have understanded the format of tableswitch/lookupswitch. But,what is the purpose of between 0 and 3 bytes padding?

1 Answers

The padding exists for historical reasons. The first Java Virtual Machines did not have JIT compilation, they interpreted bytecode instructions one by one. To allow interpreters read 32-bit offsets directly from the bytecode stream, the offsets are 32-bit aligned - their addresses are exact multiple of 4 bytes.

RISC processors (like SPARC, ARMv5 and earlier, etc.) permitted only aligned memory access. E.g. to read a 32-bit value from memory with a single CPU instruction, the address must be 32-bit aligned. If the address is not aligned, getting a 32-bit value requires four 8-bit memory reads, which is, of course, slower.

Nowadays the optimization is not useful anymore.

Related