JAVA opcode instruction related description (1)

Previous article simply to introduce the introduction and use of various locks (with a corresponding end of the text links), the underlying operating current chapter after speaking to unlock the realization of the first to lay down the foundation.

JAVA basic type conversion

JAVABasic type of conversion is not involved in the operand, the converted value is obtained directly from the top of the stack, JAVAthe virtual machine pop a value from the stack, convert him, and then the conversion result onto the stack.

1.int, conversion between long, float, double type

Opcode Number of operations Remark
i2l no The value is converted to type int long class type
i2f no The value is converted to type int class type float
i2d no The value is converted to type int class type double
l2i no The value is converted to long int type class type
l2f no The conversion value is a float type long class type
l2d no The conversion value for the type long double class type
f2i no The float int type value into the type of class
f2l no The float type value into long class type
f2d no The float type converted to double the value of class type
D2i no The value is converted to a double class type int
D2L no The conversion value for the long type double class type
d2f no The conversion value is a float type double class type

2.int the data type byte, char, short type conversion

Opcode Number of operations Remark
i2b no The value is converted to type int byte type value, a sign-extended recovery pushed onto the stack as an int
i2c no The value of the char is converted to type int type value, zero-extended to restore an int onto the stack
i2s no The value is converted to type int type value short performed with sign extension to restore an int onto the stack

It does not exist than inttype takes up less space in the data type into intthe type of the opcode. The above three kinds of time because only be pushed onto the stack, it has been converted to inttype, and then to intthe type of value calculation, to give the final inttype of the value.

Demo:

    public static void main(String[] args) {
		byte a =2;
		byte b =1;
		byte c = (byte) (a+b);
	}
复制代码
public static void main(java.lang.String[]);
    Code:
       0: iconst_2  //常量2入栈
       1: istore_1  //弹出栈顶元素存入位置1的局部变量
       2: iconst_1  
       3: istore_2  
       4: iload_1   //取出位置1的局部变量值入栈
       5: iload_2 
       6: iadd      //加法运算
       7: i2b       //int类型转化为byte类型值
       8: istore_3  //弹出栈顶元素存入位置3的局部变量
       9: return
}
复制代码

JAVA constant operation

1. The constant value implied operation code contained in the interior

Opcode Number of operations Remark
iconst_m1 no The value of type int -1 onto the stack
iconst_0 no The int type value onto the stack 0
iconst_1 no The int type value onto the stack 1
iconst_2 no The int type value onto the stack 2
iconst_3 no The int type value onto the stack 3
fconst_0 no A float onto the stack type value 0
fconst_1 no The float type value onto the stack 1
fconst_2 no The value of type float onto the stack 2
lconst_0 no The long type value onto the stack 0
lconst_1 no The long type value onto the stack 1
dconst_0 no The value of 0 onto the stack type double
dconst_1 no The double type value onto the stack 1
aconst_null no Empty (null) the object onto the stack

2. After the constant value following the opcode in the bytecode

Opcode Number of operations Remark
bipush A byte type number Converting the byte number of the number of types of type int and pushed onto the stack
sipush Type a short number Converting the number of atoms of type short int type, and then pushed onto the stack

3. Remove the constant from the constant pool

Opcode Number of operations Remark
ldc Unsigned 8-bit indexbyte A word length of the value taken from the group consisting indexbyte pointed constant pool entry, and then pushed onto the stack
ldc_w Unsigned 16-bit number indexshort Value is taken from the group consisting indexbyte word length constant pool entry point, is then pushed onto the stack

General Operation JAVA stack and local variables

General Operation 1. Stack

Opcode Number of operations Remark Demo
nop no Do nothing Front: ..., word
after: ..., word, word
pop no A pop top stack word from the operand stack Front: ..., word
after: ...
pop2 no Pop the top two words from the top of the stack number
swap no Exchange two words at the top of the stack Front: ..., word2, word1
after: ..., word1, word2
after no Copy a word at the top of the stack
dup2 no Copy the word at the top of the stack
dup2 no Copy the word at the top of the stack
dup_x1 no A word copy top of the stack,
copy the contents of the original and two pop-up
content of a word onto the stack
Front: ..., word2, word1
after: ..., word1, word2, word1
dup_x2 no 复制栈顶部的一个字,
将复制内容以及原来弹出的三
个字长的内容压入栈
前:.,word3,word2,word1
后:.,word1,word3,word2,word1
dup2_x1 复制栈顶部的两个字,
将复制内容以及原来弹出的三
个字长的内容压入栈
dup2_x2 复制栈顶部的两个字,
将复制内容以及原来弹出的四
个字长的内容压入栈

2.局部变量压入栈

1.将一个字长的局部变量压入栈

操作码 操作数 备注
iload vindex 将位置为vindex的int类型的局部变量压入栈
iload_0 将位置为0的int类型的局部变量压入栈
iload_1 将位置为1的int类型的局部变量压入栈
iload_2 将位置为2的int类型的局部变量压入栈
iload_3 将位置为3的int类型的局部变量压入栈
fload vindex 将位置为vindex的float类型的局部变量压入栈
fload_0 将位置为0的float类型的局部变量压入栈
fload_1 将位置为1的float类型的局部变量压入栈
fload_2 将位置为2的float类型的局部变量压入栈
fload_3 将位置为3的float类型的局部变量压入栈

2.将两个字长的局部变量压入栈

操作码 操作数 备注
lload vindex 将位置为vindex和(vindex+1)的long类型的局部变量压入栈
lload_0 将位置为0和1的long类型的局部变量压入栈
lload_1 将位置为1和2的long类型的局部变量压入栈
lload_2 将位置为2和3的long类型的局部变量压入栈
lload_3 将位置为3和4的long类型的局部变量压入栈
dload vindex 将位置为vindex和(vindex+1)的double类型的局部变量压入栈
dload_0 将位置为0和1的double类型的局部变量压入栈
dload_1 将位置为1和2的double类型的局部变量压入栈
dload_2 将位置为2和3的double类型的局部变量压入栈
dload_3 将位置为3和4的double类型的局部变量压入栈

3.将对象引用局部变量压入栈

操作码 操作数 备注
aload vindex 将位置为vindex的对象引用局部变量压入栈
aload_0 将位置为0的对象引用局部变量压入栈
aload_1 将位置为1的对象引用局部变量压入栈
aload_2 将位置为2的对象引用局部变量压入栈
aload_3 将位置为3的对象引用局部变量压入栈

4.弹出栈顶元素,赋值局部变量

移动一个字长的操作:

操作码 操作数 备注
istore vindex 从栈中弹出int类型值,然后存到位置为vindex的局部变量中
istore_0 从栈中弹出int类型值,然后存到位置为0的局部变量中
istore_1 从栈中弹出int类型值,然后存到位置为1的局部变量中
istore_2 从栈中弹出int类型值,然后存到位置为2的局部变量中
istore_3 从栈中弹出int类型值,然后存到位置为3的局部变量中
fstore vindex 从栈中弹出float类型值,然后存到位置为vindex的局部变量中
fstore_0 从栈中弹出float类型值,然后存到位置为0的局部变量中
fstore_1 从栈中弹出float类型值,然后存到位置为1的局部变量中
fstore_2 从栈中弹出float类型值,然后存到位置为2的局部变量中
fstore_3 从栈中弹出float类型值,然后存到位置为3的局部变量中

移动两个字长的操作:

操作码 操作数 备注
lstore vindex 从栈中弹出long类型值,然后存到位置为vindex和(vindex+1)的局部变量中
lstore_0 从栈中弹出long类型值,然后存到位置为0和1的局部变量中
lstore_1 从栈中弹出long类型值,然后存到位置为1和2的局部变量中
lstore_2 从栈中弹出long类型值,然后存到位置为2和3的局部变量中
lstore_3 从栈中弹出long类型值,然后存到位置为3和4的局部变量中
dstore vindex 从栈中弹出double类型值,然后存到位置为vindex和(vindex+1)的局部变量中
dstore_0 从栈中弹出double类型值,然后存到位置为0和1的局部变量中
dstore_1 从栈中弹出double类型值,然后存到位置为1和2的局部变量中
dstore_2 从栈中弹出double类型值,然后存到位置为2和3的局部变量中
dstore_3 从栈中弹出double类型值,然后存到位置为3和4的局部变量中

移动引用的操作: 移动两个字长的操作:

操作码 操作数 备注
astore vindex 从栈中弹出对象引用,然后存到位置为vindex的局部变量中
astore_0 从栈中弹出对象引用,然后存到位置为0的局部变量中
astore_1 从栈中弹出对象引用,然后存到位置为1的局部变量中
astore_2 从栈中弹出对象引用,然后存到位置为2的局部变量中
astore_3 从栈中弹出对象引用,然后存到位置为3的局部变量中

5.wide指令

无符号8位局部变量索引,把方法中局部变量数的限制在256以下。一条单独的wide指令可以将8位的索引再扩展8位。跳转指令并不允许直接跳转到被wide指令修改过的操作码。

操作码 操作数 备注
wide iload,index 从局部变量位置为index的地方取出int类型值,并将其压入栈
wide lload,index 从局部变量位置为index的地方取出long类型值,并将其压入栈
wide fload,index 从局部变量位置为index的地方取出float类型值,并将其压入栈
wide dload,index 从局部变量位置为index的地方取出double类型值,并将其压入栈
wide aload,index 从局部变量位置为index的地方取出对象引用,并将其压入栈
wide istore,index 从栈中弹出int类型值,将其存入位置为index的局部变量中
wide lstore,index 从栈中弹出long类型值,将其存入位置为index的局部变量中
wide lstore,index 从栈中弹出long类型值,将其存入位置为index的局部变量中
wide fstore,index 从栈中弹出float类型值,将其存入位置为index的局部变量中
wide dstore,index 从栈中弹出double类型值,将其存入位置为index的局部变量中
wide astore,index 从栈中弹出对象引用,将其存入位置为index的局部变量中

以上先介绍一部分JAVA字节码中的一些命令,接下来还有一些方法调用等等指令,下一篇文章给大家介绍下。

转载于:https://juejin.im/post/5d020857e51d454d1d6284fe

Guess you like

Origin blog.csdn.net/weixin_33946020/article/details/93176319