java - 怎么理解JVM中的iload和istore指令
PHPz
PHPz 2017-04-18 10:49:52
0
3
1259

我最近在学习JVM,被istore和iload两条指令困扰了。
以下是我查看《Java虚拟机规范》得到的解释

  • 将一个局部变量加载到操纵栈的指令包括:iload、iload_、lload…

  • 将一个数值从操作数栈存储到局部变量表的指令包括:istore、istore_、lstore…

下面是我的java代码

    public static int add(int a,int b){
        int c=0;
        c=a+b;
        return c;
    }

下面是编译后的字节码,也加上了我的理解,如果解释不恰当,谢谢指出

0: iconst_0        //常量0压入操作数栈
1: istore_2        //弹出操作数栈栈顶元素,保存到局部变量表第2个位置
2: iload_0         //第0个变量压入操作数栈
3: iload_1         //第1个变量压入操作数栈
4: iadd            //操作数栈中的前两个int相加,并将结果压入操作数栈顶
5: istore_2        //弹出操作数栈栈顶元素,保存到局部变量表第2个位置
6: iload_2         //加载局部变量表的第2个变量到操作数栈顶
7: ireturn         //返回

从上面字节码的分析看,指令4已经将计算结果压入到操作数栈了,而指令6又是把结果压入到操作数栈,这不是重复工作吗。
如果存入操作数栈的意义是为了可以store到局部变量表中,那第6步又为什么要load到操作数栈上。
不知道,是不是我哪步理解错了,谢谢指点。

PHPz
PHPz

学习是最好的投资!

reply all(3)
洪涛

If you change the code to

public static int add(int a,int b){
        int c=0;
        return a+b;
    }

Then the corresponding command is:

       0: iconst_0
       1: istore_2
       2: iload_0
       3: iload_1
       4: iadd
       5: ireturn

The compiler is generated according to the code. If you directly return a + b, there will be no extra steps 5 and 6.

黄舟

Correct answer upstairs. In fact, if you look at the code, you can clearly see the reason.

First of all, this method is a static method, so the variables corresponding to the local variable array [0] [1] [2] are a, b, c respectively;

0: iconst_0        //常量0入栈
1: istore_2        //将栈顶出栈,即c=0;
2: iload_0         //复制a变量的值入栈
3: iload_1         //复制b变量的值入栈
4: iadd            //将栈顶两个元素出栈,做加法,然后把结果再入栈(即a,b出栈,将a+b入栈)
5: istore_2        //栈顶元素出栈,即c=和;  此时栈为空
6: iload_2         //将c赋值压入栈
7: ireturn         //返回栈顶元素

伊谢尔伦

A small mistake, the index of the local variable table starts from 0.

The bytecode generated by the compiler is completely generated according to the semantics in the method without much optimization.

Take the value of c in

iadd指令对应的a+b中加法操作,下一步的istore_2对应的就是c=的赋值操作,也就是保存到局部变量表,后面的iload_2对应的就是return.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!