Basic Java Bytecode Instruction

I am currently writing a bytecode compiler for my own DSL. However, when I execute the bytecode that I built using 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 look like SIPUSH. If you replace the command with BIPUSH, 10, everything will work as expected. The bytecode that I got from the bytecode loop uses SIPUSHwithout problems, so what am I doing wrong?

+4
source share
1 answer

The solution is simple, I used the wrong method:

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

+2
source

Source: https://habr.com/ru/post/1656184/


All Articles