YVM
Introduction
This is a toy Java virtual machine(JVM) that supports many of Java's language features, it also comes with an experimental mark-sweep
garbage collector. The implementation of this JVM follows the Java Virtual Machine Specification 8. I don't have enough time to write unit tests for each component, and I don't implement the full Java language features, but as far as pleasing myself, I think it's enough. If you find any bugs, or you want to implement any missing features, you can create an issue or pull request, respectively.
Build and Run
# Note, C++14 is required at least.
$ cd yvm
$ cmake .
$ make
$ ./yvm
Usage:
yvm --lib=<path> <main_class>
--lib=<path> Tells YVM where to find JDK classes(java.lang.String, etc)
<main_class> The full qualified Java class name, e.g. org.example.Foo
$ ./yvm --lib=/path/to/yvm/bytecode ydk.test.QuickSort
0 1 1 1 1 1 4 4 4 5 6 7 7 9 9 9 12 74 96 98 8989
Implemented Features
Advanced language features will support later, you can also create PR to contribute your awesome code. Implemented features are as follows:
- Java arithmetic, flow control, object-oriented programming(virtual method, inherit,etc.)
- Runtime type identification
- String concatenation
- Exception handling
- Async native threads
- Synchronized block with object lock
- Garbage Collection(With mark-and-sweep policy)
You can find some examples at here.
Hacking Guide
1. How does it work
loadJavaClass("org.example.Foo")
- findJavaClass if present
- Otherwise, loadJavaClass from --lib, all classes is stored in ClassSpace
linkJavaClass("org.example.Foo")
- Initialize static fields with default value
initJavaClass("org.example.Foo")
- Invoke
org.example.Foo.<clinit>
- Invoke
invokeByName("org.example.Foo","main","([Ljava/lang/String;)V")
- Prepare execution frame
- Find method from JavaClass
execByteCodede
- Interprete every bytecode on simulated stack
- Recursively call
execByteCodede
when performinginvoke*
bytecodes
2. Code structure
root@ubuntu:~/yvm/src$ tree .
.
βββ classfile
βΒ Β βββ AccessFlag.h # Access flag of class, method, field
βΒ Β βββ ClassFile.h # Parse .class file
βΒ Β βββ FileReader.h # Read .class file
βββ gc
βΒ Β βββ Concurrent.cpp # Concurrency utilities
βΒ Β βββ Concurrent.hpp
βΒ Β βββ GC.cpp # Garbage collector
βΒ Β βββ GC.h
βββ interpreter
βΒ Β βββ CallSite.cpp # Call site to denote a concrete calling
βΒ Β βββ CallSite.h
βΒ Β βββ Internal.h # Types that internally used
βΒ Β βββ Interpreter.cpp # Interpreter
βΒ Β βββ Interpreter.hpp
βΒ Β βββ MethodResolve.cpp # Resolve call site
βΒ Β βββ MethodResolve.h
βββ misc
βΒ Β βββ Debug.cpp # Debugging utilities
βΒ Β βββ Debug.h
βΒ Β βββ NativeMethod.cpp # Java native methods
βΒ Β βββ NativeMethod.h
βΒ Β βββ Option.h # VM arguments and options
βΒ Β βββ Utils.cpp # Tools and utilities
βΒ Β βββ Utils.h
βββ runtime
βΒ Β βββ JavaClass.cpp # Internal representation of java.lang.Class
βΒ Β βββ JavaClass.h
βΒ Β βββ JavaException.cpp # Exception handling
βΒ Β βββ JavaException.h
βΒ Β βββ JavaFrame.cpp # Execution frame
βΒ Β βββ JavaFrame.hpp
βΒ Β βββ JavaHeap.cpp # Java heap, where objects are located
βΒ Β βββ JavaHeap.hpp
βΒ Β βββ JavaType.h # Java type definitions
βΒ Β βββ ClassSpace.cpp # Store JavaClass
βΒ Β βββ ClassSpace.h
βΒ Β βββ ObjectMonitor.cpp # synchronized(){} block implementation
βΒ Β βββ ObjectMonitor.h
βΒ Β βββ RuntimeEnv.cpp # Runtime structures
βΒ Β βββ RuntimeEnv.h
βββ vm
βββ Main.cpp # Parse command line arguments
βββ YVM.cpp # Abstraction of virtual machine
βββ YVM.h
See its wiki for more development details.
License
Code licensed under the MIT License.