Java字节码错误指令

我目前正在为自己的DSL编写字节码编译器.但是,当执行我用ASM构造的字节码时,我收到以下错误:

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)

这些是执行的指令:

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

问题似乎是SIPUSH.如果我用BIPUSH,10替换指令,一切都按预期工作.我从字节码大纲得到的字节码使用SIPUSH没有问题,所以我做错了什么?

最佳答案 解决方案很简单,我使用了错误的方法:

请使用visitIntInsn(SIPUSH,1000),而不是使用visitVarInsn(SIPUSH,1000).

点赞