Search code examples
javabytecodejava-bytecode-asm

Java Bytecode Bad Instruction


I am currently writing a bytecode compiler for my own DSL. However, when executing the bytecode, which I constructed with ASM, I get the following error:

Exception in thread "main" java.lang.VerifyError: Bad instruction
Exception Details:
  Location:
    ForClass.doLoop()V @14: wide
  Reason:
    Error exists in the bytecode



Bytecode:
    0x0000000: 043c b200 101b b600 161b 0460 3c1b c411
    0x0000010: 03e8 a4ff f0b1                      

at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at Test3.main(Test3.java:28)

These are the instructions that are executed:

mv.visitVarInsn(ILOAD, 1);
mv.visitVarInsn(SIPUSH, 1000);
mv.visitJumpInsn(IF_ICMPLE, l1);

The problems seems to be the SIPUSH. If I replace the instruction with BIPUSH, 10, everything works as expected. The bytecode I got from the bytecode outline uses SIPUSHwith no problems, so what am I doing wrong?


Solution

  • The solution is simple, I used a wrong method:

    Instead of using visitVarInsn(SIPUSH, 1000), use visitIntInsn(SIPUSH, 1000).