复杂点的例子:
Java代码如下:
1561102354042.png编译
$ javac -g HelloWorld.java
汇编指令如下:
$ javap -v HelloWorld.class
Classfile /D:/ideaproject/jvm/HelloWorld.class
Last modified 2019-6-21; size 824 bytes
MD5 checksum 18f78c76b5e4f36814f54b846739fc02
Compiled from "HelloWorld.java"
public class HelloWorld
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #8.#33 // java/lang/Object."<init>":()V
#2 = Class #34 // HelloWorld
#3 = Methodref #2.#33 // HelloWorld."<init>":()V
#4 = Methodref #2.#35 // HelloWorld.record:(D)V
#5 = Methodref #2.#36 // HelloWorld.getAverage:()D
#6 = Fieldref #37.#38 // java/lang/System.out:Ljava/io/PrintStream;
#7 = Methodref #39.#40 // java/io/PrintStream.println:(D)V
#8 = Class #41 // java/lang/Object
#9 = Utf8 <init>
#10 = Utf8 ()V
#11 = Utf8 Code
#12 = Utf8 LineNumberTable
#13 = Utf8 LocalVariableTable
#14 = Utf8 this
#15 = Utf8 LHelloWorld;
#16 = Utf8 record
#17 = Utf8 (D)V
#18 = Utf8 score
#19 = Utf8 D
#20 = Utf8 getAverage
#21 = Utf8 ()D
#22 = Utf8 main
#23 = Utf8 ([Ljava/lang/String;)V
#24 = Utf8 args
#25 = Utf8 [Ljava/lang/String;
#26 = Utf8 calculator
#27 = Utf8 score1
#28 = Utf8 I
#29 = Utf8 score2
#30 = Utf8 avg
#31 = Utf8 SourceFile
#32 = Utf8 HelloWorld.java
#33 = NameAndType #9:#10 // "<init>":()V
#34 = Utf8 HelloWorld
#35 = NameAndType #16:#17 // record:(D)V
#36 = NameAndType #20:#21 // getAverage:()D
#37 = Class #42 // java/lang/System
#38 = NameAndType #43:#44 // out:Ljava/io/PrintStream;
#39 = Class #45 // java/io/PrintStream
#40 = NameAndType #46:#17 // println:(D)V
#41 = Utf8 java/lang/Object
#42 = Utf8 java/lang/System
#43 = Utf8 out
#44 = Utf8 Ljava/io/PrintStream;
#45 = Utf8 java/io/PrintStream
#46 = Utf8 println
{
public HelloWorld(); //构造方法,默认添加
descriptor: ()V //构造方法的描述信息
flags: ACC_PUBLIC //构造方法的修饰符
Code: //Code属性
stack=1, locals=1, args_size=1 //最大栈深1,最大局部变量表容量:1,参数个数:1
0: aload_0 //加载该方法局部变量表索引0的变量:这里是this
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return //返回
LineNumberTable:
line 1: 0
LocalVariableTable: //局部变量表
Start Length Slot Name Signature
0 5 0 this LHelloWorld; //索引0,this
public void record(double);//public void record(double) 方法
descriptor: (D)V //方法参数和返回值信息
flags: ACC_PUBLIC //方法的修饰符
Code:
stack=0, locals=3, args_size=2 //最大栈深0,最大局部变量表容量:3,参数个数:2
0: return
LineNumberTable:
line 4: 0
LocalVariableTable://局部变量表
Start Length Slot Name Signature
0 1 0 this LHelloWorld;//索引0,this,LHelloWorld表示该类的引用
0 1 1 score D //索引1,score,D表示double
public double getAverage();//public double getAverage();
descriptor: ()D
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: dconst_0
1: dreturn
LineNumberTable:
line 7: 0
LocalVariableTable:
Start Length Slot Name Signature
0 2 0 this LHelloWorld;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC //方法的修饰符 public static
Code:
stack=3, locals=6, args_size=1
0: new #2 // class HelloWorld
3: dup //这个后面解释
4: invokespecial #3 // Method "<init>":()V
7: astore_1 //存储局部变量表索引为1的变量
8: iconst_1 //将 int类型 1 压入操作数栈顶
9: istore_2 //将1 存储到 局部变量表索引为2的变量,这里是score1
10: iconst_2
11: istore_3
12: aload_1 //将 局部变量表索引为1 的变量 加载到操作数栈
13: iload_2 //
14: i2d // int类型转换为double类型
15: invokevirtual #4 // Method record:(D)V
18: aload_1
19: iload_3
20: i2d
21: invokevirtual #4 // Method record:(D)V
24: aload_1
25: invokevirtual #5 // Method getAverage:()D
28: dstore 4
30: getstatic #6 // Field java/lang/System.out:Ljava/io/PrintStream;
33: dload 4
35: invokevirtual #7 // Method java/io/PrintStream.println:(D)V
38: return
LineNumberTable:
line 12: 0
line 14: 8
line 15: 10
line 17: 12
line 18: 18
line 20: 24
line 21: 30
line 22: 38
LocalVariableTable:
Start Length Slot Name Signature
0 39 0 args [Ljava/lang/String;
8 31 1 calculator LHelloWorld;
10 29 2 score1 I
12 27 3 score2 I
30 9 4 avg D
}
SourceFile: "HelloWorld.java"
思考题:
用 Java 写一个简单的 class 文件解析工具支持输出函数列表
网友评论