# JVM Backend for `rustc` ## Abstract Implement a JVM backend for `rustc` that is capable of generating Rust data structures in JVM bytecode. ## Description Currently, Rust only has three backends, which are LLVM, Cranelift, and GCC. The goal of this proposal is to add a new experimental `rust_codegen_jvm` backend that generates Rust internal representations into JVM bytecode, and verify the bytecode by invoking `java` on generated class files. This will allow Rust to eliminate `Java Native Interface (JNI)`, `Java Native Access (JNA)` and `Foreign Function Interface (FFI)` layers for Java application to integerating with Rust libraries / applications. ## Expected Result The minimum viable product will be able to turn `rustc` data structures into java classes / interfaces / enums, and write the output byte arrays as class files to the location specified by `--out-dir`. The first step will only involved with class generation. Method generation is not considered to be done in this proposal due to complex generation algorithm behind methods (This includes `<init>`, known as constructor method, and `<clinit>`, known as static initialize method). The second step is to validate each class files with `java` program, the validation output should be other than `main method not found` error. ## Project Size Large ## Difficulty Large --- **Revision 1** ## Abstract Incubating a JVM bytecode manipulation library purely in Rust that is capable of running without Java Virtual Machine. ## Description Currently, Rust only has three backends, which are LLVM, Cranelift, and GCC. The goal of this proposal is to incubate a native JVM bytecode manipulation library before fully putting effort into JVM backend implementation. Incubating a native JVM bytecode manipulation library not only makes the JVM bytecode generation possible without having JVM on the machine but also paves the path for future JVM backend for `rustc`. There are several JVM bytecode manipulation libraries to take as an implementation reference: - [Objectweb ASM](https://en.wikipedia.org/wiki/ObjectWeb_ASM) Widely used in several industry projects, such as OpenJDK, Groovy compiler, Kotlin compiler, and even some other bytecode manipulation libraries such as ByteBuddy. - [Javaassist](https://github.com/jboss-javassist/javassist) - [ByteBuddy](https://bytebuddy.net/) It is important to notice that after some investigation on JVM bytecode manipulation libraries, the Objectweb ASM would be the optimal implementation reference for this proposal due to the minimal dependency on JVM itself. The only part that depends on JVM is the `ClassLoader`, which is used to determine the common superclass for `StackMapTable` generation. This part could be resolved by building a minimal class file analyzing application to construct a class hierarchy for common superclass lookup. ## Expected result The minimum viable product will be able to generate a class file with a static `main` method that is capable of being executed by JVM, and also pass the unit tests written in Objectweb ASM. The first part of MVP is expected to be variable, for example, verifying the generation of `for loop` is valid and the output is correct. The second step is to validate the generation of basic data structures defined in Rust, e.g. `struct`, `enum` etc. Notice that `union` type cannot be generated into JVM bytecode due to lack of manipulation of object layout. ## Project Size Large ## Difficulty Medium