在写一个关于JVM字节码偏移的问题的答案时,我注意到javac的行为以及我无法解释的结果类文件:
编译这样的类时
class FarJump
{
public static void main(String args[])
{
call(0, 1);
}
public static void call(int x, int y)
{
if (x < y)
{
y++;
y++;
// ... (10921 times - too much code to post here!)
y++;
y++;
}
System.out.println(y);
}
}
Run Code Online (Sandbox Code Playgroud)
然后生成的字节代码将包含以下if_icmpge指令:
public static void call(int, int);
Code:
0: iload_0
1: iload_1
2: if_icmpge 32768
5: iinc 1, 1
8: iinc 1, 1
...
Run Code Online (Sandbox Code Playgroud)
根据跳转指令的文档,偏移量(在这种情况下为32768)计算如下:
如果比较成功,则使用无符号branchbyte1和branchbyte2 …