Coder Social home page Coder Social logo

y1yang0 / yvm Goto Github PK

View Code? Open in Web Editor NEW
248.0 15.0 54.0 3.9 MB

[yvm] low performance garbage-collectable jvm

License: MIT License

CMake 0.19% C 9.72% C++ 85.85% Java 4.20% Batchfile 0.04%
jvm java classloader java-virtual-machine cpp14 homemade garbage-collector mark-and-sweep

yvm's Introduction

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.

yvm's People

Contributors

entropyfeng avatar y1yang0 avatar y1yang1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yvm's Issues

which version of jvm or any other version of jvm source code you referred for the development.

Background

By reviewing your implementation, I found that your initial codes including code execution for generic data type are all implemented using java.

I can not clearly see how did you build ast and convert them to op codes for the moment. But I think I can follow your submission step by step so that I can clearly see how you made changes.

But only 15 days ago: 04e75b5 -> racaljk committed on 4 Oct 2017, you changed all codes into c++ version when you wanted to implement a gc algorithm "mark and sweep".

It is a little bit like a full time project(java, c++ , 6 months project) rather than a project for demo, or for interests soly. Are you supported by any foundation or just writing it for fun?

I also noticed that you made a specific version of algorithm like RBTree, without delivering it as an independent package or library: no benchmark testing (you might be over confident to your implementation). At least I assume that you can rely on boost , google libraries for development: glog, boost.asio, boost.threads and so on.

here is what think about the programme

In YVM.cpp, your main thread creates a code execution engine for op code generated by some java semantic analyzer for a specific operating system (or some existing jvm? I am not clear about that). Then you create runtime "yrc" to load model and find execution entry point "main". When the main thread finishes codes execution, you fire gc(unreasonable).

I thought most of your efforts in 2017 is about implementing a loading relevant functions. After refactorization of your codes, your project does not have language relevant programmes like: symbol table, java language parser generator, and analyzer (ast). Have you tested octal, unicode parsing, it is not that trivial.

Question 1

How did you make sure that your line search over allocated memory blocks as java runtime frames stack living in heap, successfully be marked if the number of their refs' countered down to 0, and not marked if they don't. And then delete them if you have a scheduler

Question 2

Where do you manage different threads and pass mutex from parent to children thread? It seems that you don't have scheduler implementation for threads.

Question 3

Could you explain more about your ObjectMonitor.h/,cc? Generic object cast is a very challenging problem (@see python). I am curious about how you computed your memory block for type dynamic casting?

Question 4

For research complete, could you tell me, which version of java jvm or any other materials including c++ codes, java codes(your initial commit on some day in Sep, 2017) , you once referred for the project.

Is it possible to implements the GC by using visitor pattern ?

code maybe like this:

class JavaTypeVisitor;
struct JavaType {
  // ... other fields
  virtual void accept(JavaTypeVisitor* visitor) = 0;
};
struct JavaInt : public JavaType {
  virtual void accept(JavaTypeVisitor* visitor) override;
};

struct JavaLong : public JavaType {
  virtual void accept(JavaTypeVisitor* visitor) override;
};

struct JavaObject : public JavaType {
  virtual void accept(JavaTypeVisitor* visitor) override;
};

// ... other JavaType struct definitions

// visitor
class JavaTypeVisitor {
public:
  virtual void visit(JavaInt* ji) = 0;
  virtual void visit(JavaLong* jl) = 0;
  virtual void visit(JavaObject* jo) = 0;
  // ... visit other JavaTypes
};

// region gc

class GC {
public:
  virtual void gc() = 0;
protected:
  // ... some fields
};

class CMS_GC : public GC, public JavaTypeVisitor {
// ... some implementations
};

class G1_GC : public GC, public JavaTypeVisitor {
// ... some implementations
};

// ... other GC Implementation, eg: zgc

// endregion gc

CMakeLists SOURCE_FILES missing SymbolicRef.cpp

make -j2
[ 5%] Linking CXX executable yvm.exe
CMakeFiles/yvm.dir/src/interpreter/Interpreter.cpp.o:Interpreter.cpp:(.text+0x512e): undefined reference to `parseFieldSymbolicReference(JavaClass const*, unsigned short)'

编译失败

环境:
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
boost_1_65_0
cmake-3.15.3
CLion 2019.3.6
C++语言不熟悉,这种情况,大家有没有见过?想知道大家有谁知道是什么原因不?

====================[ Build | yvm | Debug ]=====================================
/opt/CLion-2019.3.6/clion-2019.3.6/bin/cmake/linux/bin/cmake --build /home/aaron/桌面/yvm --target yvm -- -j 2
Scanning dependencies of target yvm
[ 5%] Building CXX object CMakeFiles/yvm.dir/src/vm/Main.cpp.o
[ 10%] Building CXX object CMakeFiles/yvm.dir/src/runtime/MethodArea.cpp.o
[ 15%] Building CXX object CMakeFiles/yvm.dir/src/runtime/JavaFrame.cpp.o
[ 21%] Building CXX object CMakeFiles/yvm.dir/src/interpreter/Interpreter.cpp.o
[ 26%] Building CXX object CMakeFiles/yvm.dir/src/interpreter/SymbolicRef.cpp.o
[ 31%] Building CXX object CMakeFiles/yvm.dir/src/misc/Debug.cpp.o
[ 36%] Building CXX object CMakeFiles/yvm.dir/src/runtime/JavaClass.cpp.o
[ 42%] Building CXX object CMakeFiles/yvm.dir/src/runtime/JavaHeap.cpp.o
[ 47%] Building CXX object CMakeFiles/yvm.dir/src/interpreter/MethodResolve.cpp.o
[ 52%] Building CXX object CMakeFiles/yvm.dir/src/misc/NativeMethod.cpp.o
[ 57%] Building CXX object CMakeFiles/yvm.dir/src/vm/YVM.cpp.o
[ 63%] Building CXX object CMakeFiles/yvm.dir/src/misc/Utils.cpp.o
[ 68%] Building CXX object CMakeFiles/yvm.dir/src/runtime/JavaException.cpp.o
/home/aaron/桌面/yvm/src/runtime/JavaException.cpp: In member function ‘void StackTrace::printStackTrace()’:
/home/aaron/桌面/yvm/src/runtime/JavaException.cpp:13:37: warning: zero-length gnu_printf format string [-Wformat-zero-length]
: printf("");
^
[ 73%] Building CXX object CMakeFiles/yvm.dir/src/gc/GC.cpp.o
[ 78%] Building CXX object CMakeFiles/yvm.dir/src/interpreter/CallSite.cpp.o
[ 84%] Linking CXX executable yvm
/usr/bin/ld: 找不到 -llpthread
collect2: error: ld returned 1 exit status
CMakeFiles/yvm.dir/build.make:341: recipe for target 'yvm' failed
make[3]: *** [yvm] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/yvm.dir/all' failed
make[2]: *** [CMakeFiles/yvm.dir/all] Error 2
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/yvm.dir/rule' failed
make[1]: *** [CMakeFiles/yvm.dir/rule] Error 2
Makefile:129: recipe for target 'yvm' failed
make: *** [yvm] Error 2

oh my god

As a college student, you can write such a code, which is amazing!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.