• Stars
    star
    248
  • Rank 163,560 (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.