jvm 指令详解(下)

九、自增减指令
该指令用于对本地(局部)变量进行自增减操作。该指令 第一参数 为本地变量的编号, 第二个参数 为自增减的数量。
比如对于代码
  1. int d=10;  
  2. d++;  
  3. d+=2;  
  4. d–;  
int d=10;
d++;
d+=2;
d--;
其指令为
  1.  2: bipush 10  
  2.  4: istore_2  //在我的程序中是其所在非静态函数的第2个变量(从0开始计数).  
  3.  5: iinc 21 //在我的程序中是其所在非静态函数的第2个变量(从0开始计数).  
  4.  8: iinc 22  
  5. 11: iinc 2, -1  
   2: bipush 10
   4: istore_2  //在我的程序中是其所在非静态函数的第2个变量(从0开始计数).
   5: iinc 2, 1 //在我的程序中是其所在非静态函数的第2个变量(从0开始计数).
   8: iinc 2, 2
  11: iinc 2, -1
对本地变量所进行的编号,是对所有类型的本地变量进行的(并不按照类型分类)。
对于非静态函数,第一变量是this,它是只读的. 还有函数传入参数也算本地变量,在进行编号时,它是先于函数体的本地变量的。
指令码         助记符                                            说明
0x84                  iinc                将指定int型变量增加指定值(i++, i–, i+=2)

十、类型转化系列
该系列指令负责对栈顶数值类型进行类型转化,并把结果压入栈顶。
指令码         助记符                                           说明
0x85                 i2l                      将栈顶int型数值强制转换成long型数值并将结果压入栈顶
0x86                 i2f                      将栈顶int型数值强制转换成float型数值并将结果压入栈顶
0x87                 i2d                     将栈顶int型数值强制转换成double型数值并将结果压入栈顶
0x88                 l2i                      将栈顶long型数值强制转换成int型数值并将结果压入栈顶
0x89                 l2f                      将栈顶long型数值强制转换成float型数值并将结果压入栈顶
0x8a                 l2d                     将栈顶long型数值强制转换成double型数值并将结果压入栈顶
0x8b                 f2i                      将栈顶float型数值强制转换成int型数值并将结果压入栈顶
0x8c                 f2l                      将栈顶float型数值强制转换成long型数值并将结果压入栈顶
0x8d                 f2d                     将栈顶float型数值强制转换成double型数值并将结果压入栈顶
0x8e                 d2i                     将栈顶double型数值强制转换成int型数值并将结果压入栈顶
0x8f                  d2l                     将栈顶double型数值强制转换成long型数值并将结果压入栈顶
0x90                 d2f                     将栈顶double型数值强制转换成float型数值并将结果压入栈顶
0x91                 i2b                     将栈顶int型数值强制转换成byte型数值并将结果压入栈顶
0x92                 i2c                     将栈顶int型数值强制转换成char型数值并将结果压入栈顶
0x93                 i2s                     将栈顶int型数值强制转换成short型数值并将结果压入栈顶

十二、比较指令系列A
该系列指令用于对栈顶非int型元素进行比较,并把结果压入栈顶。
比如,代码:
  1. void test()  
  2. {  
  3.     long a=11;  
  4.     long b=10;  
  5.     boolean result=(a>b);  
  6. }  
void test()
{
    long a=11;
    long b=10;
    boolean result=(a>b);
}
其指令为:
  1. void test();  
  2.   Code:  
  3.    0: ldc2_w #16;      //long 11l  
  4.    3: lstore_1  
  5.    4: ldc2_w #18;      //long 10l  
  6.    7: lstore_3  
  7.    8: lload_1  
  8.    9: lload_3  
  9.   10: lcmp  
  10.   11: ifle 18  
  11.   14: iconst_1  
  12.   15goto 19  
  13.   18: iconst_0  
  14.   19: istore 5  
  15.   21return  
void test();
  Code:
   0: ldc2_w #16;      //long 11l
   3: lstore_1
   4: ldc2_w #18;      //long 10l
   7: lstore_3
   8: lload_1
   9: lload_3
  10: lcmp
  11: ifle 18
  14: iconst_1
  15: goto 19
  18: iconst_0
  19: istore 5
  21: return
指令码         助记符                                           说明
0x94               lcmp            比较栈顶两long型数值大小,    并将结果(1,0,-1)压入栈顶
0x95               fcmpl           比较栈顶两float型数值大小,    并将结果(1,0,-1)压入栈顶;当其中一个数值为NaN时,将-1压入栈顶
0x96               fcmpg          比较栈顶两float型数值大小,    并将结果(1,0,-1)压入栈顶;当其中一个数值为NaN时,将1压入栈顶
0x97               dcmpl          比较栈顶两double型数值大小,并将结果(1,0,-1)压入栈顶;当其中一个数值为NaN时,将-1压入栈顶
0x98               dcmpg         比较栈顶两double型数值大小,并将结果(1,0,-1)压入栈顶;当其中一个数值为NaN时,将1压入栈顶

十三、有条件跳转指令系列A
该系列指令用于对栈顶int型元素进行比较,根据结果进行跳转。第一个参数为要跳转到的代码的地址(这里的地址是指其指令在函数内是第几个指令)。注意对于boolean型,其实是把它当做int型来处理的。另外对于引用比较使用的时,其实是对存储的对象的地址进行比较。
  1.     void test(){  
  2.         int a=11;  
  3.         int b=10;  
  4.         boolean result=(a>b);  
  5.         if(result)  
  6.             a+=2;  
  7.         if(!result)  
  8.             a+=2;  
  9.         if(a>0)  
  10.             a–;  
  11.     }  
    void test(){
        int a=11;
        int b=10;
        boolean result=(a>b);
        if(result)
            a+=2;
        if(!result)
            a+=2;
        if(a>0)
            a--;
    }
其对应的指令为:
  1. void test();  
  2.     descriptor: ()V  
  3.     flags:  
  4.     Code:  
  5.       stack=2, locals=4, args_size=1  
  6.          0: bipush        11  
  7.          2: istore_1  
  8.          3: bipush        10  
  9.          5: istore_2  
  10.          6: iload_1  
  11.          7: iload_2  
  12.          8: if_icmple     15  //如果比较结果小于0,就跳到第15个指令继续执行  
  13.         11: iconst_1  
  14.         12goto          16  
  15.         15: iconst_0  
  16.         16: istore_3  
  17.         17: iload_3  
  18.         18: ifeq          24  //如果结果为0时(即为false),就跳转到第24个指令继续执行  
  19.         21: iinc          12  
  20.         24: iload_3  
  21.         25: ifne          31  //如果结果不为0时(即为true),就跳转到第31个指令继续执行  
  22.         28: iinc          12  
  23.         31: iload_1  
  24.         32: ifle          38  
  25.         35: iinc          1, -1  
  26.         38return  
void test();
    descriptor: ()V
    flags:
    Code:
      stack=2, locals=4, args_size=1
         0: bipush        11
         2: istore_1
         3: bipush        10
         5: istore_2
         6: iload_1
         7: iload_2
         8: if_icmple     15  //如果比较结果小于0,就跳到第15个指令继续执行
        11: iconst_1
        12: goto          16
        15: iconst_0
        16: istore_3
        17: iload_3
        18: ifeq          24  //如果结果为0时(即为false),就跳转到第24个指令继续执行
        21: iinc          1, 2
        24: iload_3
        25: ifne          31  //如果结果不为0时(即为true),就跳转到第31个指令继续执行
        28: iinc          1, 2
        31: iload_1
        32: ifle          38
        35: iinc          1, -1
        38: return
指令码         助记符                                           说明
0x99                ifeq                              当栈顶int型数值等于0时跳转
0x9a                ifne                              当栈顶int型数值不等于0时跳转
0x9b                iflt                                当栈顶int型数值小于0时跳转
0x9c                ifge                              当栈顶int型数值大于等于0时跳转
0x9d                ifgt                               当栈顶int型数值大于0时跳转
0x9e                ifle                               当栈顶int型数值小于等于0时跳转
0x9f                 if_icmpeq                    比较栈顶两int型数值大小,当结果等于0时跳转
0xa0                if_icmpne                    比较栈顶两int型数值大小,当结果不等于0时跳转
0xa1                if_icmplt                      比较栈顶两int型数值大小,当结果小于0时跳转
0xa2                if_icmpge                    比较栈顶两int型数值大小,当结果大于等于0时跳转
0xa3                if_icmpgt                     比较栈顶两int型数值大小,当结果大于0时跳转
0xa4                if_icmple                     比较栈顶两int型数值大小,当结果小于等于0时跳转
0xa5                if_acmpeq                   比较栈顶两引用型数值,当结果相等时跳转
0xa6                if_acmpne                   比较栈顶两引用型数值,当结果不相等时跳转

十四、无条件跳转指令系列A
该系列指令用于指令的跳转。
指令码         助记符                                           说明
0xa7                goto                                           无条件跳转
0xa8                  jsr                   跳转至指定16位offset位置,并将jsr下一条指令地址压入栈顶
0xa9                 ret                   返回至本地变量指定的index的指令位置(一般与jsr, jsr_w联合使用)
0xaa              tableswitch         用于switch条件跳转,case值连续(可变长度指令)
0xab              lookupswitch      用于switch条件跳转,case值不连续(可变长度指令)

十五、返回指令系列
该系列指令用于从函数中返回。如果有返回值的话,都把函数的返回值放在栈道中,以便它的调用方法取得它。
return 10; 这个语句其实对应的指令是两条:    
9:   bipush 10
11: ireturn
指令码         助记符                                           说明
0xac               ireturn                                从当前方法返回int
0xad               lreturn                                从当前方法返回long
0xae               freturn                                从当前方法返回float
0xaf                dreturn                               从当前方法返回double
0xb0               areturn                               从当前方法返回对象引用
0xb1               return                                 从当前方法返回void

十六、域操作指令系列
该系列指令用于对静态域和非静态域进行读写。该系列命令需要跟一个 表明域编号 的参数,
比如,在函数中对成员变量 m 进行; m++
其指令为:
   0:aload_0
   1:dup
   2:getfield #2; //Field m:I
   5:iconst_1
   6:iadd
   7:putfield #2; //Field m:I
指令码         助记符                                           说明
0xb2              getstatic                    获取指定类的静态域,并将其值压入栈顶
0xb3              putstatic                    用栈顶的值为指定的类的静态域赋值
0xb4              getfield                      获取指定类的实例域,并将其值压入栈顶
0xb5              putfield                      用栈顶的值为指定的类的实例域赋值

十七、方法操作命令系列
该系列指令用于对静态方法和非静方法进行调用。该系列命令需要跟一个表明方法编号的参数。
如果方法有传入参数的话,则需要先压栈到栈顶。另外,方法的返回参数是保存到栈顶的,因此我们可以通过栈道值取得方法的返回值。
  1. void test(){  
  2.     int k=add(12,45);  
  3. }  
    void test(){
        int k=add(12,45);
    }
  1. void test();  
  2.     descriptor: ()V  
  3.     flags:  
  4.     Code:  
  5.       stack=3, locals=2, args_size=1  
  6.          0: aload_0  
  7.          1: bipush        12  
  8.          3: bipush        45  
  9.          5: invokevirtual #20                 // Method add:(II)I  
  10.          8: istore_1  
  11.          9return  
  12.       LineNumberTable:  
  13.         line 80  
  14.         line 99  
  15.       LocalVariableTable:  
  16.         Start  Length  Slot  Name   Signature  
  17.             0      10     0  this   Lthinkinginjava16/Test;  
  18.             9       1     1     k   I  
void test();
    descriptor: ()V
    flags:
    Code:
      stack=3, locals=2, args_size=1
         0: aload_0
         1: bipush        12
         3: bipush        45
         5: invokevirtual #20                 // Method add:(II)I
         8: istore_1
         9: return
      LineNumberTable:
        line 8: 0
        line 9: 9
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      10     0  this   Lthinkinginjava16/Test;
            9       1     1     k   I
指令码         助记符                                           说明
0xb6              invokevirtual                              调用实例方法
0xb7              invokespecial                            调用超类构造方法,实例初始化方法,私有方法
0xb8              invokestatic                               调用静态方法
0xb9              invokeinterface                         调用接口方法

十九、new及数组系列
该系列用于创建一个对象和数组。
  1. package thinkinginjava16;  
  2. class Hello{}  
  3. public class Test {  
  4.     void test(){  
  5.         int ids[] = new int[5];  
  6.         Object objs[] = new Object[5];  
  7.         Object obj = new Object();  
  8.         Hello hello = new Hello();  
  9.         int len = objs.length;  
  10.     }  
  11. }  
package thinkinginjava16;
class Hello{}
public class Test {
    void test(){
        int ids[] = new int[5];
        Object objs[] = new Object[5];
        Object obj = new Object();
        Hello hello = new Hello();
        int len = objs.length;
    }
}
  1.  void test();  
  2.     descriptor: ()V  
  3.     flags:  
  4.     Code:  
  5.       stack=2, locals=6, args_size=1  
  6.          0: iconst_5  
  7.          1: newarray       int  
  8.          3: astore_1  
  9.          4: iconst_5  
  10.          5: anewarray     #3                  // class java/lang/Object  
  11.          8: astore_2  
  12.          9new           #3                  // class java/lang/Object  
  13.         12: dup  
  14.         13: invokespecial #8                  // Method java/lang/Object.”<init>”:()V  
  15.         16: astore_3  
  16.         17new           #15                 // class thinkinginjava16/Hello  
  17.         20: dup  
  18.         21: invokespecial #17                 // Method thinkinginjava16/Hello.”<init>”:()V  
  19.         24: astore        4  
  20.         26: aload_2  
  21.         27: arraylength  
  22.         28: istore        5  
  23.         30return  
  24.       LineNumberTable:  
  25.         line 50  
  26.         line 64  
  27.         line 79  
  28.         line 817  
  29.         line 926  
  30.         line 1030  
  31.       LocalVariableTable:  
  32.         Start  Length  Slot  Name   Signature  
  33.             0      31     0  this   Lthinkinginjava16/Test;  
  34.             4      27     1   ids   [I  
  35.             9      22     2  objs   [Ljava/lang/Object;  
  36.            17      14     3   obj   Ljava/lang/Object;  
  37.            26       5     4 hello   Lthinkinginjava16/Hello;  
  38.            30       1     5   len   I  
 void test();
    descriptor: ()V
    flags:
    Code:
      stack=2, locals=6, args_size=1
         0: iconst_5
         1: newarray       int
         3: astore_1
         4: iconst_5
         5: anewarray     #3                  // class java/lang/Object
         8: astore_2
         9: new           #3                  // class java/lang/Object
        12: dup
        13: invokespecial #8                  // Method java/lang/Object."<init>":()V
        16: astore_3
        17: new           #15                 // class thinkinginjava16/Hello
        20: dup
        21: invokespecial #17                 // Method thinkinginjava16/Hello."<init>":()V
        24: astore        4
        26: aload_2
        27: arraylength
        28: istore        5
        30: return
      LineNumberTable:
        line 5: 0
        line 6: 4
        line 7: 9
        line 8: 17
        line 9: 26
        line 10: 30
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      31     0  this   Lthinkinginjava16/Test;
            4      27     1   ids   [I
            9      22     2  objs   [Ljava/lang/Object;
           17      14     3   obj   Ljava/lang/Object;
           26       5     4 hello   Lthinkinginjava16/Hello;
           30       1     5   len   I
指令码         助记符                                           说明
0xbb              new                          创建一个对象,并将其引用值压入栈顶
0xbc              newarray                  创建一个指定原始类型(如int, float, char…)的数组,并将其引用值压入栈顶
0xbd              anewarray                创建一个引用型(如类,接口,数组)的数组,并将其引用值压入栈顶
0xbe              arraylength               获得数组的长度值并压入栈顶

二十、异常抛出指令
指令码         助记符                                           说明
0xbf                athrow                                将栈顶的异常抛出

二十一、对象操作指令
该系列指令用于操作对象。
指令码         助记符                                           说明
0xc0              checkcast                   检验类型转换,检验未通过将抛出ClassCastException
0xc1              instanceof                  检验对象是否是指定的类的实例,如果是将1压入栈顶,否则将0压入栈顶
0xc2              monitorenter               获得对象的锁,用于同步方法或同步块
0xc3              monitorexit                  释放对象的锁,用于同步方法或同步块

二十二、未归类系列C
此系列暂未归类。
指令码         助记符                                           说明
0xc4                wide                                               <待补充>

二十三、new多维数组系列
指令码      助记符                                           说明
0xc5       multianewarray 创建指定类型和指定维度的多维数组(执行该指令时,操作栈中必须包含各维度的长度值),并将其引用值压入栈顶



二十四、有条件跳转指令系列B

该系列用于根据引用是否为空,来进行相应的指令跳转。
  1. package thinkinginjava16;  
  2. public class Test {  
  3.     void test(){  
  4.         int i=0;  
  5.         Object obj = new Object();  
  6.         if(obj==null){  
  7.             i=0;  
  8.         }  
  9.         if(obj!=null){  
  10.             i=1;  
  11.         }   
  12.     }  
  13. }  
package thinkinginjava16;
public class Test {
    void test(){
        int i=0;
        Object obj = new Object();
        if(obj==null){
            i=0;
        }
        if(obj!=null){
            i=1;
        } 
    }
}
  1. Classfile /D:/eclipse-jee-luna-SR2-win32-x86_64/myworkspace/MyTest/bin/thinkinginjava16/Test.class  
  2.   Last modified 2016-2-3; size 468 bytes  
  3.   MD5 checksum 369317fce1793207da71a53559a65396  
  4.   Compiled from ”Test.java”  
  5. public class thinkinginjava16.Test  
  6.   minor version: 0  
  7.   major version: 51  
  8.   flags: ACC_PUBLIC, ACC_SUPER  
  9. Constant pool:  
  10.    #1 = Class              #2             // thinkinginjava16/Test  
  11.    #2 = Utf8               thinkinginjava16/Test  
  12.    #3 = Class              #4             // java/lang/Object  
  13.    #4 = Utf8               java/lang/Object  
  14.    #5 = Utf8               <init>  
  15.    #6 = Utf8               ()V  
  16.    #7 = Utf8               Code  
  17.    #8 = Methodref          #3.#9          // java/lang/Object.”<init>”:()V  
  18.    #9 = NameAndType        #5:#6          // ”<init>”:()V  
  19.   #10 = Utf8               LineNumberTable  
  20.   #11 = Utf8               LocalVariableTable  
  21.   #12 = Utf8               this  
  22.   #13 = Utf8               Lthinkinginjava16/Test;  
  23.   #14 = Utf8               test  
  24.   #15 = Utf8               i  
  25.   #16 = Utf8               I  
  26.   #17 = Utf8               obj  
  27.   #18 = Utf8               Ljava/lang/Object;  
  28.   #19 = Utf8               StackMapTable  
  29.   #20 = Utf8               SourceFile  
  30.   #21 = Utf8               Test.java  
  31. {  
  32.   public thinkinginjava16.Test();  
  33.     descriptor: ()V  
  34.     flags: ACC_PUBLIC  
  35.     Code:  
  36.       stack=1, locals=1, args_size=1  
  37.          0: aload_0  
  38.          1: invokespecial #8                  // Method java/lang/Object.”<init>”:()V  
  39.          4return  
  40.       LineNumberTable:  
  41.         line 20  
  42.       LocalVariableTable:  
  43.         Start  Length  Slot  Name   Signature  
  44.             0       5     0  this   Lthinkinginjava16/Test;  
  45.   
  46.   
  47.   void test();  
  48.     descriptor: ()V  
  49.     flags:  
  50.     Code:  
  51.       stack=2, locals=3, args_size=1  
  52.          0: iconst_0  
  53.          1: istore_1  
  54.          2new           #3                  // class java/lang/Object  
  55.          5: dup  
  56.          6: invokespecial #8                  // Method java/lang/Object.”<init>”:()V  
  57.          9: astore_2  
  58.         10: aload_2  
  59.         11: ifnonnull     16  
  60.         14: iconst_0  
  61.         15: istore_1  
  62.         16: aload_2  
  63.         17: ifnull        22  
  64.         20: iconst_1  
  65.         21: istore_1  
  66.         22return  
  67.       LineNumberTable:  
  68.         line 40  
  69.         line 52  
  70.         line 610  
  71.         line 714  
  72.         line 916  
  73.         line 1020  
  74.         line 1222  
  75.       LocalVariableTable:  
  76.         Start  Length  Slot  Name   Signature  
  77.             0      23     0  this   Lthinkinginjava16/Test;  
  78.             2      21     1     i   I  
  79.            10      13     2   obj   Ljava/lang/Object;  
  80.       StackMapTable: number_of_entries = 2  
  81.         frame_type = 253 /* append */  
  82.           offset_delta = 16  
  83.           locals = [ intclass java/lang/Object ]  
  84.         frame_type = 5 /* same */  
  85. }  
  86. SourceFile: ”Test.java”  
Classfile /D:/eclipse-jee-luna-SR2-win32-x86_64/myworkspace/MyTest/bin/thinkinginjava16/Test.class
  Last modified 2016-2-3; size 468 bytes
  MD5 checksum 369317fce1793207da71a53559a65396
  Compiled from "Test.java"
public class thinkinginjava16.Test
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Class              #2             // thinkinginjava16/Test
   #2 = Utf8               thinkinginjava16/Test
   #3 = Class              #4             // java/lang/Object
   #4 = Utf8               java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Utf8               Code
   #8 = Methodref          #3.#9          // java/lang/Object."<init>":()V
   #9 = NameAndType        #5:#6          // "<init>":()V
  #10 = Utf8               LineNumberTable
  #11 = Utf8               LocalVariableTable
  #12 = Utf8               this
  #13 = Utf8               Lthinkinginjava16/Test;
  #14 = Utf8               test
  #15 = Utf8               i
  #16 = Utf8               I
  #17 = Utf8               obj
  #18 = Utf8               Ljava/lang/Object;
  #19 = Utf8               StackMapTable
  #20 = Utf8               SourceFile
  #21 = Utf8               Test.java
{
  public thinkinginjava16.Test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #8                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 2: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lthinkinginjava16/Test;


  void test();
    descriptor: ()V
    flags:
    Code:
      stack=2, locals=3, args_size=1
         0: iconst_0
         1: istore_1
         2: new           #3                  // class java/lang/Object
         5: dup
         6: invokespecial #8                  // Method java/lang/Object."<init>":()V
         9: astore_2
        10: aload_2
        11: ifnonnull     16
        14: iconst_0
        15: istore_1
        16: aload_2
        17: ifnull        22
        20: iconst_1
        21: istore_1
        22: return
      LineNumberTable:
        line 4: 0
        line 5: 2
        line 6: 10
        line 7: 14
        line 9: 16
        line 10: 20
        line 12: 22
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      23     0  this   Lthinkinginjava16/Test;
            2      21     1     i   I
           10      13     2   obj   Ljava/lang/Object;
      StackMapTable: number_of_entries = 2
        frame_type = 253 /* append */
          offset_delta = 16
          locals = [ int, class java/lang/Object ]
        frame_type = 5 /* same */
}
SourceFile: "Test.java"
指令码      助记符                                           说明
0xc6            ifnull                                            为null时跳转
0xc7            ifnonnull                                      不为null时跳转

二十五、无条件跳转指令系列B
该系列指令用于进行无条件指令跳转。
指令码      助记符                                           说明
0xc8           goto_w                                 无条件跳转(宽索引)
0xc9           jsr_w                         跳转至指定32位offset位置,并将jsr_w下一条指令地址压入栈顶










九、自增减指令
该指令用于对本地(局部)变量进行自增减操作。该指令 第一参数 为本地变量的编号, 第二个参数 为自增减的数量。
比如对于代码
  1. int d=10;  
  2. d++;  
  3. d+=2;  
  4. d–;  
int d=10;
d++;
d+=2;
d--;
其指令为
  1.  2: bipush 10  
  2.  4: istore_2  //在我的程序中是其所在非静态函数的第2个变量(从0开始计数).  
  3.  5: iinc 21 //在我的程序中是其所在非静态函数的第2个变量(从0开始计数).  
  4.  8: iinc 22  
  5. 11: iinc 2, -1  
   2: bipush 10
   4: istore_2  //在我的程序中是其所在非静态函数的第2个变量(从0开始计数).
   5: iinc 2, 1 //在我的程序中是其所在非静态函数的第2个变量(从0开始计数).
   8: iinc 2, 2
  11: iinc 2, -1
对本地变量所进行的编号,是对所有类型的本地变量进行的(并不按照类型分类)。
对于非静态函数,第一变量是this,它是只读的. 还有函数传入参数也算本地变量,在进行编号时,它是先于函数体的本地变量的。
指令码         助记符                                            说明
0x84                  iinc                将指定int型变量增加指定值(i++, i–, i+=2)

十、类型转化系列
该系列指令负责对栈顶数值类型进行类型转化,并把结果压入栈顶。
指令码         助记符                                           说明
0x85                 i2l                      将栈顶int型数值强制转换成long型数值并将结果压入栈顶
0x86                 i2f                      将栈顶int型数值强制转换成float型数值并将结果压入栈顶
0x87                 i2d                     将栈顶int型数值强制转换成double型数值并将结果压入栈顶
0x88                 l2i                      将栈顶long型数值强制转换成int型数值并将结果压入栈顶
0x89                 l2f                      将栈顶long型数值强制转换成float型数值并将结果压入栈顶
0x8a                 l2d                     将栈顶long型数值强制转换成double型数值并将结果压入栈顶
0x8b                 f2i                      将栈顶float型数值强制转换成int型数值并将结果压入栈顶
0x8c                 f2l                      将栈顶float型数值强制转换成long型数值并将结果压入栈顶
0x8d                 f2d                     将栈顶float型数值强制转换成double型数值并将结果压入栈顶
0x8e                 d2i                     将栈顶double型数值强制转换成int型数值并将结果压入栈顶
0x8f                  d2l                     将栈顶double型数值强制转换成long型数值并将结果压入栈顶
0x90                 d2f                     将栈顶double型数值强制转换成float型数值并将结果压入栈顶
0x91                 i2b                     将栈顶int型数值强制转换成byte型数值并将结果压入栈顶
0x92                 i2c                     将栈顶int型数值强制转换成char型数值并将结果压入栈顶
0x93                 i2s                     将栈顶int型数值强制转换成short型数值并将结果压入栈顶

十二、比较指令系列A
该系列指令用于对栈顶非int型元素进行比较,并把结果压入栈顶。
比如,代码:
  1. void test()  
  2. {  
  3.     long a=11;  
  4.     long b=10;  
  5.     boolean result=(a>b);  
  6. }  
void test()
{
    long a=11;
    long b=10;
    boolean result=(a>b);
}
其指令为:
  1. void test();  
  2.   Code:  
  3.    0: ldc2_w #16;      //long 11l  
  4.    3: lstore_1  
  5.    4: ldc2_w #18;      //long 10l  
  6.    7: lstore_3  
  7.    8: lload_1  
  8.    9: lload_3  
  9.   10: lcmp  
  10.   11: ifle 18  
  11.   14: iconst_1  
  12.   15goto 19  
  13.   18: iconst_0  
  14.   19: istore 5  
  15.   21return  
void test();
  Code:
   0: ldc2_w #16;      //long 11l
   3: lstore_1
   4: ldc2_w #18;      //long 10l
   7: lstore_3
   8: lload_1
   9: lload_3
  10: lcmp
  11: ifle 18
  14: iconst_1
  15: goto 19
  18: iconst_0
  19: istore 5
  21: return
指令码         助记符                                           说明
0x94               lcmp            比较栈顶两long型数值大小,    并将结果(1,0,-1)压入栈顶
0x95               fcmpl           比较栈顶两float型数值大小,    并将结果(1,0,-1)压入栈顶;当其中一个数值为NaN时,将-1压入栈顶
0x96               fcmpg          比较栈顶两float型数值大小,    并将结果(1,0,-1)压入栈顶;当其中一个数值为NaN时,将1压入栈顶
0x97               dcmpl          比较栈顶两double型数值大小,并将结果(1,0,-1)压入栈顶;当其中一个数值为NaN时,将-1压入栈顶
0x98               dcmpg         比较栈顶两double型数值大小,并将结果(1,0,-1)压入栈顶;当其中一个数值为NaN时,将1压入栈顶

十三、有条件跳转指令系列A
该系列指令用于对栈顶int型元素进行比较,根据结果进行跳转。第一个参数为要跳转到的代码的地址(这里的地址是指其指令在函数内是第几个指令)。注意对于boolean型,其实是把它当做int型来处理的。另外对于引用比较使用的时,其实是对存储的对象的地址进行比较。
  1.     void test(){  
  2.         int a=11;  
  3.         int b=10;  
  4.         boolean result=(a>b);  
  5.         if(result)  
  6.             a+=2;  
  7.         if(!result)  
  8.             a+=2;  
  9.         if(a>0)  
  10.             a–;  
  11.     }  
    void test(){
        int a=11;
        int b=10;
        boolean result=(a>b);
        if(result)
            a+=2;
        if(!result)
            a+=2;
        if(a>0)
            a--;
    }
其对应的指令为:
  1. void test();  
  2.     descriptor: ()V  
  3.     flags:  
  4.     Code:  
  5.       stack=2, locals=4, args_size=1  
  6.          0: bipush        11  
  7.          2: istore_1  
  8.          3: bipush        10  
  9.          5: istore_2  
  10.          6: iload_1  
  11.          7: iload_2  
  12.          8: if_icmple     15  //如果比较结果小于0,就跳到第15个指令继续执行  
  13.         11: iconst_1  
  14.         12goto          16  
  15.         15: iconst_0  
  16.         16: istore_3  
  17.         17: iload_3  
  18.         18: ifeq          24  //如果结果为0时(即为false),就跳转到第24个指令继续执行  
  19.         21: iinc          12  
  20.         24: iload_3  
  21.         25: ifne          31  //如果结果不为0时(即为true),就跳转到第31个指令继续执行  
  22.         28: iinc          12  
  23.         31: iload_1  
  24.         32: ifle          38  
  25.         35: iinc          1, -1  
  26.         38return  
void test();
    descriptor: ()V
    flags:
    Code:
      stack=2, locals=4, args_size=1
         0: bipush        11
         2: istore_1
         3: bipush        10
         5: istore_2
         6: iload_1
         7: iload_2
         8: if_icmple     15  //如果比较结果小于0,就跳到第15个指令继续执行
        11: iconst_1
        12: goto          16
        15: iconst_0
        16: istore_3
        17: iload_3
        18: ifeq          24  //如果结果为0时(即为false),就跳转到第24个指令继续执行
        21: iinc          1, 2
        24: iload_3
        25: ifne          31  //如果结果不为0时(即为true),就跳转到第31个指令继续执行
        28: iinc          1, 2
        31: iload_1
        32: ifle          38
        35: iinc          1, -1
        38: return
指令码         助记符                                           说明
0x99                ifeq                              当栈顶int型数值等于0时跳转
0x9a                ifne                              当栈顶int型数值不等于0时跳转
0x9b                iflt                                当栈顶int型数值小于0时跳转
0x9c                ifge                              当栈顶int型数值大于等于0时跳转
0x9d                ifgt                               当栈顶int型数值大于0时跳转
0x9e                ifle                               当栈顶int型数值小于等于0时跳转
0x9f                 if_icmpeq                    比较栈顶两int型数值大小,当结果等于0时跳转
0xa0                if_icmpne                    比较栈顶两int型数值大小,当结果不等于0时跳转
0xa1                if_icmplt                      比较栈顶两int型数值大小,当结果小于0时跳转
0xa2                if_icmpge                    比较栈顶两int型数值大小,当结果大于等于0时跳转
0xa3                if_icmpgt                     比较栈顶两int型数值大小,当结果大于0时跳转
0xa4                if_icmple                     比较栈顶两int型数值大小,当结果小于等于0时跳转
0xa5                if_acmpeq                   比较栈顶两引用型数值,当结果相等时跳转
0xa6                if_acmpne                   比较栈顶两引用型数值,当结果不相等时跳转

十四、无条件跳转指令系列A
该系列指令用于指令的跳转。
指令码         助记符                                           说明
0xa7                goto                                           无条件跳转
0xa8                  jsr                   跳转至指定16位offset位置,并将jsr下一条指令地址压入栈顶
0xa9                 ret                   返回至本地变量指定的index的指令位置(一般与jsr, jsr_w联合使用)
0xaa              tableswitch         用于switch条件跳转,case值连续(可变长度指令)
0xab              lookupswitch      用于switch条件跳转,case值不连续(可变长度指令)

十五、返回指令系列
该系列指令用于从函数中返回。如果有返回值的话,都把函数的返回值放在栈道中,以便它的调用方法取得它。
return 10; 这个语句其实对应的指令是两条:    
9:   bipush 10
11: ireturn
指令码         助记符                                           说明
0xac               ireturn                                从当前方法返回int
0xad               lreturn                                从当前方法返回long
0xae               freturn                                从当前方法返回float
0xaf                dreturn                               从当前方法返回double
0xb0               areturn                               从当前方法返回对象引用
0xb1               return                                 从当前方法返回void

十六、域操作指令系列
该系列指令用于对静态域和非静态域进行读写。该系列命令需要跟一个 表明域编号 的参数,
比如,在函数中对成员变量 m 进行; m++
其指令为:
   0:aload_0
   1:dup
   2:getfield #2; //Field m:I
   5:iconst_1
   6:iadd
   7:putfield #2; //Field m:I
指令码         助记符                                           说明
0xb2              getstatic                    获取指定类的静态域,并将其值压入栈顶
0xb3              putstatic                    用栈顶的值为指定的类的静态域赋值
0xb4              getfield                      获取指定类的实例域,并将其值压入栈顶
0xb5              putfield                      用栈顶的值为指定的类的实例域赋值

十七、方法操作命令系列
该系列指令用于对静态方法和非静方法进行调用。该系列命令需要跟一个表明方法编号的参数。
如果方法有传入参数的话,则需要先压栈到栈顶。另外,方法的返回参数是保存到栈顶的,因此我们可以通过栈道值取得方法的返回值。
  1. void test(){  
  2.     int k=add(12,45);  
  3. }  
    void test(){
        int k=add(12,45);
    }
  1. void test();  
  2.     descriptor: ()V  
  3.     flags:  
  4.     Code:  
  5.       stack=3, locals=2, args_size=1  
  6.          0: aload_0  
  7.          1: bipush        12  
  8.          3: bipush        45  
  9.          5: invokevirtual #20                 // Method add:(II)I  
  10.          8: istore_1  
  11.          9return  
  12.       LineNumberTable:  
  13.         line 80  
  14.         line 99  
  15.       LocalVariableTable:  
  16.         Start  Length  Slot  Name   Signature  
  17.             0      10     0  this   Lthinkinginjava16/Test;  
  18.             9       1     1     k   I  
void test();
    descriptor: ()V
    flags:
    Code:
      stack=3, locals=2, args_size=1
         0: aload_0
         1: bipush        12
         3: bipush        45
         5: invokevirtual #20                 // Method add:(II)I
         8: istore_1
         9: return
      LineNumberTable:
        line 8: 0
        line 9: 9
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      10     0  this   Lthinkinginjava16/Test;
            9       1     1     k   I
指令码         助记符                                           说明
0xb6              invokevirtual                              调用实例方法
0xb7              invokespecial                            调用超类构造方法,实例初始化方法,私有方法
0xb8              invokestatic                               调用静态方法
0xb9              invokeinterface                         调用接口方法

十九、new及数组系列
该系列用于创建一个对象和数组。
  1. package thinkinginjava16;  
  2. class Hello{}  
  3. public class Test {  
  4.     void test(){  
  5.         int ids[] = new int[5];  
  6.         Object objs[] = new Object[5];  
  7.         Object obj = new Object();  
  8.         Hello hello = new Hello();  
  9.         int len = objs.length;  
  10.     }  
  11. }  
package thinkinginjava16;
class Hello{}
public class Test {
    void test(){
        int ids[] = new int[5];
        Object objs[] = new Object[5];
        Object obj = new Object();
        Hello hello = new Hello();
        int len = objs.length;
    }
}
  1.  void test();  
  2.     descriptor: ()V  
  3.     flags:  
  4.     Code:  
  5.       stack=2, locals=6, args_size=1  
  6.          0: iconst_5  
  7.          1: newarray       int  
  8.          3: astore_1  
  9.          4: iconst_5  
  10.          5: anewarray     #3                  // class java/lang/Object  
  11.          8: astore_2  
  12.          9new           #3                  // class java/lang/Object  
  13.         12: dup  
  14.         13: invokespecial #8                  // Method java/lang/Object.”<init>”:()V  
  15.         16: astore_3  
  16.         17new           #15                 // class thinkinginjava16/Hello  
  17.         20: dup  
  18.         21: invokespecial #17                 // Method thinkinginjava16/Hello.”<init>”:()V  
  19.         24: astore        4  
  20.         26: aload_2  
  21.         27: arraylength  
  22.         28: istore        5  
  23.         30return  
  24.       LineNumberTable:  
  25.         line 50  
  26.         line 64  
  27.         line 79  
  28.         line 817  
  29.         line 926  
  30.         line 1030  
  31.       LocalVariableTable:  
  32.         Start  Length  Slot  Name   Signature  
  33.             0      31     0  this   Lthinkinginjava16/Test;  
  34.             4      27     1   ids   [I  
  35.             9      22     2  objs   [Ljava/lang/Object;  
  36.            17      14     3   obj   Ljava/lang/Object;  
  37.            26       5     4 hello   Lthinkinginjava16/Hello;  
  38.            30       1     5   len   I  
 void test();
    descriptor: ()V
    flags:
    Code:
      stack=2, locals=6, args_size=1
         0: iconst_5
         1: newarray       int
         3: astore_1
         4: iconst_5
         5: anewarray     #3                  // class java/lang/Object
         8: astore_2
         9: new           #3                  // class java/lang/Object
        12: dup
        13: invokespecial #8                  // Method java/lang/Object."<init>":()V
        16: astore_3
        17: new           #15                 // class thinkinginjava16/Hello
        20: dup
        21: invokespecial #17                 // Method thinkinginjava16/Hello."<init>":()V
        24: astore        4
        26: aload_2
        27: arraylength
        28: istore        5
        30: return
      LineNumberTable:
        line 5: 0
        line 6: 4
        line 7: 9
        line 8: 17
        line 9: 26
        line 10: 30
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      31     0  this   Lthinkinginjava16/Test;
            4      27     1   ids   [I
            9      22     2  objs   [Ljava/lang/Object;
           17      14     3   obj   Ljava/lang/Object;
           26       5     4 hello   Lthinkinginjava16/Hello;
           30       1     5   len   I
指令码         助记符                                           说明
0xbb              new                          创建一个对象,并将其引用值压入栈顶
0xbc              newarray                  创建一个指定原始类型(如int, float, char…)的数组,并将其引用值压入栈顶
0xbd              anewarray                创建一个引用型(如类,接口,数组)的数组,并将其引用值压入栈顶
0xbe              arraylength               获得数组的长度值并压入栈顶

二十、异常抛出指令
指令码         助记符                                           说明
0xbf                athrow                                将栈顶的异常抛出

二十一、对象操作指令
该系列指令用于操作对象。
指令码         助记符                                           说明
0xc0              checkcast                   检验类型转换,检验未通过将抛出ClassCastException
0xc1              instanceof                  检验对象是否是指定的类的实例,如果是将1压入栈顶,否则将0压入栈顶
0xc2              monitorenter               获得对象的锁,用于同步方法或同步块
0xc3              monitorexit                  释放对象的锁,用于同步方法或同步块

二十二、未归类系列C
此系列暂未归类。
指令码         助记符                                           说明
0xc4                wide                                               <待补充>

二十三、new多维数组系列
指令码      助记符                                           说明
0xc5       multianewarray 创建指定类型和指定维度的多维数组(执行该指令时,操作栈中必须包含各维度的长度值),并将其引用值压入栈顶



二十四、有条件跳转指令系列B

该系列用于根据引用是否为空,来进行相应的指令跳转。
  1. package thinkinginjava16;  
  2. public class Test {  
  3.     void test(){  
  4.         int i=0;  
  5.         Object obj = new Object();  
  6.         if(obj==null){  
  7.             i=0;  
  8.         }  
  9.         if(obj!=null){  
  10.             i=1;  
  11.         }   
  12.     }  
  13. }  
package thinkinginjava16;
public class Test {
    void test(){
        int i=0;
        Object obj = new Object();
        if(obj==null){
            i=0;
        }
        if(obj!=null){
            i=1;
        } 
    }
}
  1. Classfile /D:/eclipse-jee-luna-SR2-win32-x86_64/myworkspace/MyTest/bin/thinkinginjava16/Test.class  
  2.   Last modified 2016-2-3; size 468 bytes  
  3.   MD5 checksum 369317fce1793207da71a53559a65396  
  4.   Compiled from ”Test.java”  
  5. public class thinkinginjava16.Test  
  6.   minor version: 0  
  7.   major version: 51  
  8.   flags: ACC_PUBLIC, ACC_SUPER  
  9. Constant pool:  
  10.    #1 = Class              #2             // thinkinginjava16/Test  
  11.    #2 = Utf8               thinkinginjava16/Test  
  12.    #3 = Class              #4             // java/lang/Object  
  13.    #4 = Utf8               java/lang/Object  
  14.    #5 = Utf8               <init>  
  15.    #6 = Utf8               ()V  
  16.    #7 = Utf8               Code  
  17.    #8 = Methodref          #3.#9          // java/lang/Object.”<init>”:()V  
  18.    #9 = NameAndType        #5:#6          // ”<init>”:()V  
  19.   #10 = Utf8               LineNumberTable  
  20.   #11 = Utf8               LocalVariableTable  
  21.   #12 = Utf8               this  
  22.   #13 = Utf8               Lthinkinginjava16/Test;  
  23.   #14 = Utf8               test  
  24.   #15 = Utf8               i  
  25.   #16 = Utf8               I  
  26.   #17 = Utf8               obj  
  27.   #18 = Utf8               Ljava/lang/Object;  
  28.   #19 = Utf8               StackMapTable  
  29.   #20 = Utf8               SourceFile  
  30.   #21 = Utf8               Test.java  
  31. {  
  32.   public thinkinginjava16.Test();  
  33.     descriptor: ()V  
  34.     flags: ACC_PUBLIC  
  35.     Code:  
  36.       stack=1, locals=1, args_size=1  
  37.          0: aload_0  
  38.          1: invokespecial #8                  // Method java/lang/Object.”<init>”:()V  
  39.          4return  
  40.       LineNumberTable:  
  41.         line 20  
  42.       LocalVariableTable:  
  43.         Start  Length  Slot  Name   Signature  
  44.             0       5     0  this   Lthinkinginjava16/Test;  
  45.   
  46.   
  47.   void test();  
  48.     descriptor: ()V  
  49.     flags:  
  50.     Code:  
  51.       stack=2, locals=3, args_size=1  
  52.          0: iconst_0  
  53.          1: istore_1  
  54.          2new           #3                  // class java/lang/Object  
  55.          5: dup  
  56.          6: invokespecial #8                  // Method java/lang/Object.”<init>”:()V  
  57.          9: astore_2  
  58.         10: aload_2  
  59.         11: ifnonnull     16  
  60.         14: iconst_0  
  61.         15: istore_1  
  62.         16: aload_2  
  63.         17: ifnull        22  
  64.         20: iconst_1  
  65.         21: istore_1  
  66.         22return  
  67.       LineNumberTable:  
  68.         line 40  
  69.         line 52  
  70.         line 610  
  71.         line 714  
  72.         line 916  
  73.         line 1020  
  74.         line 1222  
  75.       LocalVariableTable:  
  76.         Start  Length  Slot  Name   Signature  
  77.             0      23     0  this   Lthinkinginjava16/Test;  
  78.             2      21     1     i   I  
  79.            10      13     2   obj   Ljava/lang/Object;  
  80.       StackMapTable: number_of_entries = 2  
  81.         frame_type = 253 /* append */  
  82.           offset_delta = 16  
  83.           locals = [ intclass java/lang/Object ]  
  84.         frame_type = 5 /* same */  
  85. }  
  86. SourceFile: ”Test.java”  
Classfile /D:/eclipse-jee-luna-SR2-win32-x86_64/myworkspace/MyTest/bin/thinkinginjava16/Test.class
  Last modified 2016-2-3; size 468 bytes
  MD5 checksum 369317fce1793207da71a53559a65396
  Compiled from "Test.java"
public class thinkinginjava16.Test
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Class              #2             // thinkinginjava16/Test
   #2 = Utf8               thinkinginjava16/Test
   #3 = Class              #4             // java/lang/Object
   #4 = Utf8               java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Utf8               Code
   #8 = Methodref          #3.#9          // java/lang/Object."<init>":()V
   #9 = NameAndType        #5:#6          // "<init>":()V
  #10 = Utf8               LineNumberTable
  #11 = Utf8               LocalVariableTable
  #12 = Utf8               this
  #13 = Utf8               Lthinkinginjava16/Test;
  #14 = Utf8               test
  #15 = Utf8               i
  #16 = Utf8               I
  #17 = Utf8               obj
  #18 = Utf8               Ljava/lang/Object;
  #19 = Utf8               StackMapTable
  #20 = Utf8               SourceFile
  #21 = Utf8               Test.java
{
  public thinkinginjava16.Test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #8                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 2: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lthinkinginjava16/Test;


  void test();
    descriptor: ()V
    flags:
    Code:
      stack=2, locals=3, args_size=1
         0: iconst_0
         1: istore_1
         2: new           #3                  // class java/lang/Object
         5: dup
         6: invokespecial #8                  // Method java/lang/Object."<init>":()V
         9: astore_2
        10: aload_2
        11: ifnonnull     16
        14: iconst_0
        15: istore_1
        16: aload_2
        17: ifnull        22
        20: iconst_1
        21: istore_1
        22: return
      LineNumberTable:
        line 4: 0
        line 5: 2
        line 6: 10
        line 7: 14
        line 9: 16
        line 10: 20
        line 12: 22
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      23     0  this   Lthinkinginjava16/Test;
            2      21     1     i   I
           10      13     2   obj   Ljava/lang/Object;
      StackMapTable: number_of_entries = 2
        frame_type = 253 /* append */
          offset_delta = 16
          locals = [ int, class java/lang/Object ]
        frame_type = 5 /* same */
}
SourceFile: "Test.java"
指令码      助记符                                           说明
0xc6            ifnull                                            为null时跳转
0xc7            ifnonnull                                      不为null时跳转

二十五、无条件跳转指令系列B
该系列指令用于进行无条件指令跳转。
指令码      助记符                                           说明
0xc8           goto_w                                 无条件跳转(宽索引)
0xc9           jsr_w                         跳转至指定32位offset位置,并将jsr_w下一条指令地址压入栈顶








猜你喜欢

转载自blog.csdn.net/u010148687/article/details/80062818