ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 根据执行历史记录,给定指令的操作数堆栈大小是否不同?

2019-07-27 15:00:02  阅读:280  来源: 互联网

标签:java bytecode jvm-bytecode


例如,对于方法

public int f() {
    int k = 1;
    for (int i = 0; i < 10; i++) {
        k += 2;
    }
    return k;
}

javac生成以下字节码:

public int f();
Code:
   0: iconst_1
   1: istore_1
   2: iconst_0
   3: istore_2
   4: iload_2
   5: bipush        10
   7: if_icmpge     19
  10: iinc          1, 2
  13: iinc          2, 1
  16: goto          4
  19: iload_1
  20: ireturn

在标签4处,堆栈具有相同的大小(0),无论先前是哪个指令:3或16.

对于从java代码生成的字节码,这通常是正确的吗?

解决方法:

有关锑的答案的参考,请参阅
JVM specification §4.9.2. Structural Constraints(谢谢Holger!):

  • If an instruction can be executed along several different execution paths, the operand stack must have the same depth (§2.6.2) prior to the execution of the instruction, regardless of the path taken.

在这个答案的第一个版本中,我引用了§4.10.2.1. The Process of Verification by Type Inference,它适用于不包含StackMapTable属性(版本号为49.0或更低版本)的类文件:

… The verifier ensures that at any given point in the program, no matter what code path is taken to reach that point, all of the following are true:

  • The operand stack is always the same size and contains the same types of values.

标签:java,bytecode,jvm-bytecode
来源: https://codeday.me/bug/20190727/1555212.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有