Search code examples
javajvmbytecodejava-bytecode-asm

ASM bytecode manipulation - istore and istore_<n>


As the documentation says:

public interface Opcodes

Defines the JVM opcodes, access flags and array type codes. This interface does not define all the JVM opcodes because some opcodes are automatically handled. For example, the xLOAD and xSTORE opcodes are automatically replaced by xLOAD_n and xSTORE_n opcodes when possible. The xLOAD_n and xSTORE_n opcodes are therefore not defined in this interface. Likewise for LDC, automatically replaced by LDC_W or LDC2_W when necessary, WIDE, GOTO_W and JSR_W.

Question:

If i have two almost same .class files - one using istore_<n> and one istore with explicit operand. Asm will always tell(ClassReader and ClassVisitor) that there is istore with explicit operand. I want to use asm but i need to know about these differences. It is possible with asm to get real opcode of any instruction?


Solution

  • ASM's ClassWriter will use the most efficient ISTORE opcode it can. If you output ISTORE 1, ASM will output ISTORE_1.

    Because of this, the ClassReader will "deconstruct" opcodes into generic form hiding their original form. It is generally safe to assume that an ISTORE 1 was originally an ISTORE_1,but there are other cases where the actual form of the original bytecode is lost by ASM. If you want to examine original bytecode as it is, you might want to use javassist .