字节码指令
1.1 什么是字节码
字节码(Byte-code)是一种包含执行程序,由一序列 op 代码/数据对组成的二进制文件,是一种中间码。字节是电脑里的数据量单位。
1.2 javap 工具
自己分析类文件(字节码)太麻烦了,Oracle提供了javap工具来反编译class文件
javap -v HelloWorld.class
/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/bin/javap -v com.jvm.t07_bytecode.T01_HelloWorld
Classfile /Users/lei/IdeaProjects/JvmLearn/target/classes/com/jvm/t07_bytecode/T01_HelloWorld.class
Last modified 2020-8-10; size 586 bytes
MD5 checksum 916c3ebdbfef6b6fab63cac6a085aaa1
Compiled from "T01_HelloWorld.java"
public class com.jvm.t07_bytecode.T01_HelloWorld
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #6.#20 // java/lang/Object."<init>":()V
#2 = Fieldref #21.#22 // java/lang/System.out:Ljava/io/PrintStream;
#3 = String #23 // HelloWorld
#4 = Methodref #24.#25 // java/io/PrintStream.println:(Ljava/lang/String;)V
#5 = Class #26 // com/jvm/t07_bytecode/T01_HelloWorld
#6 = Class #27 // java/lang/Object
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lcom/jvm/t07_bytecode/T01_HelloWorld;
#14 = Utf8 main
#15 = Utf8 ([Ljava/lang/String;)V
#16 = Utf8 args
#17 = Utf8 [Ljava/lang/String;
#18 = Utf8 SourceFile
#19 = Utf8 T01_HelloWorld.java
#20 = NameAndType #7:#8 // "<init>":()V
#21 = Class #28 // java/lang/System
#22 = NameAndType #29:#30 // out:Ljava/io/PrintStream;
#23 = Utf8 HelloWorld
#24 = Class #31 // java/io/PrintStream
#25 = NameAndType #32:#33 // println:(Ljava/lang/String;)V
#26 = Utf8 com/jvm/t07_bytecode/T01_HelloWorld
#27 = Utf8 java/lang/Object
#28 = Utf8 java/lang/System
#29 = Utf8 out
#30 = Utf8 Ljava/io/PrintStream;
#31 = Utf8 java/io/PrintStream
#32 = Utf8 println
#33 = Utf8 (Ljava/lang/String;)V
{
public com.jvm.t07_bytecode.T01_HelloWorld();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 11: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/jvm/t07_bytecode/T01_HelloWorld;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=1, args_size=1
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String HelloWorld
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
LineNumberTable:
line 13: 0
line 14: 8
LocalVariableTable:
Start Length Slot Name Signature
0 9 0 args [Ljava/lang/String;
}
SourceFile: "T01_HelloWorld.java"
Process finished with exit code 0
2.3 图解方法执行流程
1)原始java 代码
/*
演示 字节码指令 和 操作数栈、常量池的关系
*/
public class T02_ByteCommandStackConstantPoolRel {
public static void main(String[] args) {
int a = 10;
int b = Short.MAX_VALUE + 1;
int c = a + b;
System.out.println(c);
}
}
2)编译后的字节码文件
/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/bin/javap -v com.jvm.t07_bytecode.T02_ByteCommandStackConstantPoolRel
Classfile /Users/lei/IdeaProjects/JvmLearn/target/classes/com/jvm/t07_bytecode/T02_ByteCommandStackConstantPoolRel.class
Last modified 2020-8-10; size 709 bytes
MD5 checksum cc8fc12b6e178b8f28e787497e993363
Compiled from "T02_ByteCommandStackConstantPoolRel.java"
public class com.jvm.t07_bytecode.T02_ByteCommandStackConstantPoolRel
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #7.#25 // java/lang/Object."<init>":()V
#2 = Class #26 // java/lang/Short
#3 = Integer 32768
#4 = Fieldref #27.#28 // java/lang/System.out:Ljava/io/PrintStream;
#5 = Methodref #29.#30 // java/io/PrintStream.println:(I)V
#6 = Class #31 // com/jvm/t07_bytecode/T02_ByteCommandStackConstantPoolRel
#7 = Class #32 // java/lang/Object
#8 = Utf8 <init>
#9 = Utf8 ()V
#10 = Utf8 Code
#11 = Utf8 LineNumberTable
#12 = Utf8 LocalVariableTable
#13 = Utf8 this
#14 = Utf8 Lcom/jvm/t07_bytecode/T02_ByteCommandStackConstantPoolRel;
#15 = Utf8 main
#16 = Utf8 ([Ljava/lang/String;)V
#17 = Utf8 args
#18 = Utf8 [Ljava/lang/String;
#19 = Utf8 a
#20 = Utf8 I
#21 = Utf8 b
#22 = Utf8 c
#23 = Utf8 SourceFile
#24 = Utf8 T02_ByteCommandStackConstantPoolRel.java
#25 = NameAndType #8:#9 // "<init>":()V
#26 = Utf8 java/lang/Short
#27 = Class #33 // java/lang/System
#28 = NameAndType #34:#35 // out:Ljava/io/PrintStream;
#29 = Class #36 // java/io/PrintStream
#30 = NameAndType #37:#38 // println:(I)V
#31 = Utf8 com/jvm/t07_bytecode/T02_ByteCommandStackConstantPoolRel
#32 = Utf8 java/lang/Object
#33 = Utf8 java/lang/System
#34 = Utf8 out
#35 = Utf8 Ljava/io/PrintStream;
#36 = Utf8 java/io/PrintStream
#37 = Utf8 println
#38 = Utf8 (I)V
{
public com.jvm.t07_bytecode.T02_ByteCommandStackConstantPoolRel();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 15: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/jvm/t07_bytecode/T02_ByteCommandStackConstantPoolRel;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=4, args_size=1
0: bipush 10
2: istore_1
3: ldc #3 // int 32768
5: istore_2
6: iload_1
7: iload_2
8: iadd
9: istore_3
10: getstatic #4 // Field java/lang/System.out:Ljava/io/PrintStream;
13: iload_3
14: invokevirtual #5 // Method java/io/PrintStream.println:(I)V
17: return
LineNumberTable:
line 17: 0
line 18: 3
line 19: 6
line 20: 10
line 21: 17
LocalVariableTable:
Start Length Slot Name Signature
0 18 0 args [Ljava/lang/String;
3 15 1 a I
6 12 2 b I
10 8 3 c I
}
SourceFile: "T02_ByteCommandStackConstantPoolRel.java"
Process finished with exit code 0
3)常量池载入运行时常量池
4)方法字节码载入方法区
5)main线程开始运行,分配栈帧内存
(stack=2, locals=4)
6)执行引擎开始执行字节码
bipush 10
- 将一个byte压入操作数栈(其长度会补齐4个字节),类似的指令还有
- sipush将一个short压入操作数栈(其长度会补齐4个字节)
- idc将一个int压入操作数栈
- idc2_w将一个long压入操作数栈(分两次压入,因为long是8个字节)
- 这里小的数字都是和字节码指令存在一起,超过short范围的数字存入常量池
istore_1
- 将操作数栈顶数据弹出,存入局部变量表的槽位slot 1中,下图的args/a/b/c就是对应的槽位
idc #3
- idc 从常量池加载 #3 数据到操作数栈
- 注意: Short.MAX_VALUE是32767,所以32768 = Short.MAX_VALUE + 1 超过了Short.MAX_VALUE的值,所以编译器将其放在运行时常量池中。实际是在编译期间计算好的,是一种优化,叫常量折叠优化
istore_2
- 将操作数栈顶数据弹出,存入局部变量表的2号槽位中
iload_1
再接下来,需要执行int c = a + b;执行引擎不能直接在局部变量表进行a+b操作,需要先将a、b进行读取,然后放入操作数栈中才能进行计算分析
- 把局部变量从1号槽位加载数据到操作数栈中
iload 2
- 再把局部变量从2号槽位加载数据到操作数栈中
iadd
- iadd会弹出操作数栈中的2个变量,并进行求和得到32778,最后将结果32778写回到操作数栈中
istore_3
-
将操作数栈的数弹出,存入局部变量表3号槽位。到目前为止,局部变量a、b、c都有值了
getstatic #4
- 去常量池找到成员变量引用,去堆中找到System.out对象。getstatic 会把对象引用放入操作数栈中
iload_3
- 将局部变量表3号槽位的值加载到操作数栈中
invokevirtual #5
- 找到常量池 #5项
- 定位到方法区 java/io/PrintStream.println: (I)V方法
- 生成新的栈帧(分配locals、stack等)
- 传递传数,执行新栈帧中的字节码
- 执行完毕,弹出栈帧
- 清除main操作数栈内容
return
- 完成main方法调用,弹出main栈帧
- 程序结束
文章最后,给大家推荐一些受欢迎的技术博客链接:
- Hadoop相关技术博客链接
- Spark 核心技术链接
- JAVA相关的深度技术博客链接
- 超全干货--Flink思维导图,花了3周左右编写、校对
- 深入JAVA 的JVM核心原理解决线上各种故障【附案例】
- 请谈谈你对volatile的理解?--最近小李子与面试官的一场“硬核较量”
- 聊聊RPC通信,经常被问到的一道面试题。源码+笔记,包懂
欢迎扫描下方的二维码或 搜索 公众号“10点进修”,我们会有更多、且及时的资料推送给您,欢迎多多交流!