• Stars
    star
    248
  • Rank 162,923 (Top 4 %)
  • Language
    C++
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[yvm] low performance garbage-collectable jvm

YVM

Introduction

δΈ­ζ–‡ | English

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:

You can find some examples at here.

Hacking Guide

1. How does it work

  1. loadJavaClass("org.example.Foo")
    • findJavaClass if present
    • Otherwise, loadJavaClass from --lib, all classes is stored in ClassSpace
  2. linkJavaClass("org.example.Foo")
    • Initialize static fields with default value
  3. initJavaClass("org.example.Foo")
    • Invoke org.example.Foo.<clinit>
  4. 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 performing invoke* 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.