Learning By Doing.
A new pit has recently been opened to implement a simple interpreted JVM in Java 8. Currently, more than 100 commits have been posted scattered. The goal is self-starter. So far, about 60% progress is made. Basic ClassFile parsing, class loading, bytecode execution, method calls, object instantiation, polymorphism, interfaces are basically available.
0 x00 background
Motivation for the Mini-JVM project.
- That’s kind of interesting.
- I mainly use Java in my daily work, which requires some understanding of JVM, but Hotspot source code is really hard to read.
- Learning By Doing.
The purpose of writing this article.
- A little record for later review,
- Spread the word so that those interested in the JVM will know that there is such a project.
- Looking for friends, I hope interested students can contact me, in order to continue to improve this project.
0x01
In the process of implementation, we have ensured that the code is simple, readable and testable. So far the code is just over 5000 lines, I think, for students who want to understand the fundamentals of the JVM is a good entry project. On this basis, to facilitate understanding of concepts such as stack-based virtual machine implementation, a special edge feature is implemented for quick understanding.
0x02 Basic implementation of a Stack-based VM.
If you know anything about JVM bytecode execution engines, you should know something about stack frames
Stack frames are data structures used to support method invocation and method execution by virtual machines. Stack frames are created with method invocation and destroyed with method completion. The storage space of Stack frames is allocated in the Java VIRTUAL machine Stack. Each Stack frame has its own Local Variables Operand Stack.
If you have tried javap-v classfile, the Method block is also present.
Let’s look at a simple example
public class Hello {
public static int return1(a) {
return 1; }}Copy the code
Compile and use
cat <<EOF > Hello.java
public class Hello {
public static int return1() {
return 1;
}
}
EOF
If return1 is called, the result must be 1.
javac Hello.java
javap -v Hello.class
Copy the code
The output is longer, and only the methods section is below
public static int return(1); descriptor: ()I flags: ACC_PUBLIC, ACC_STATIC Code: stack=1, locals=0, args_size=0 0: iconst_1 1: ireturnCopy the code
Stack =1, the operand stack size is 1. Locals =0, the local variable table size is 0. Args_size, the number of parameters for this method is 0
0: iconst_1
1: ireturn
Copy the code
This is the bytecode of the method 0: iconst_1, indicating that the instruction at position 0 of the method’s instruction set is iconst_1, and iconst_1 means to push the int value 1 onto the operand stack. Ireturn: indicates that the instruction at position 1 of the instruction set of this method is IRETURN. The meaning of ireturn is to eject the int value at the top of the current operand stack, and eject the int value from the operand stack to the operand stack of the current stack frame.
The JVM specification defines 200 + directives like iconst_1.
For ease of understand. the project implements a simple assembly like language to describe the bytecode and explain its execution.
return1 1 0 0
0 iconst_1
1 ireturn
Copy the code
Compare the javap-v output above to remove some redundant information. The following is an actual screenshot
The slightly more complex SUM10
A little bit more complicated sumN
0 x03 end
That concludes the introduction, but more information can be found at the Github repo. If you are interested in participating, please feel free to contact me.
Contact: echo “Z3V4aW5na2VfCg = =” | base64 – d mail: echo “YWRtaW5AZ3V4aW5na2UuY29tCg = =” | base64 – d
Project Address: github: github.com/guxingke/mi…