A custom Rust compiler backend that emits Java Virtual Machine bytecode.
Compile your Rust code into a runnable .jar
on JVM 8+!
- Demos
- Features
- How It Works
- Prerequisites
- Installation & Build
- Usage
- Running Tests
- Project Structure
- Contributing
- License
All examples live in tests/binary
and are compiled to JVM bytecode & run/tested on the CI on every commit. Some exciting demos made in pure-Rust include:
- RSA encryption/decryption
- Binary search algorithm
- Fibonacci sequence generator
- Collatz conjecture verifier
- Large prime generator
- Use of nested data structures: enums, structs, tuples, arrays, slices (enums, structs - both tests use arrays and tuples)
- …and more!
- Minimal
no_std
&no_core
programs viajvm-unknown-unknown
- Optimisations including constant folding and propogation, dead code elimination, and more to generate efficient JVM bytecode
- Basic
core
support on host target for JVM output - Arithmetic (integers + floats, incl. checked ops)
- Comparisons, bitwise & logical ops
- Control flow:
if
/else
,match
,for
,while
,loop
- Type casting (
as
), primitive types - Function calls (recursion supported)
- Arrays & slices with nested indexing
- Structs, tuples, enums (both C‑like and Rust‑style)
- Executable
.jar
generation for binary crates - Mutable borrowing, references, and dereferencing
- Implementations for ADTs, including using and returning
self
,&self
,&mut self
- Traits!
- Integration tests for all features, in debug and release modes
🚧 Next Milestone: Full support for the Rust core
crate.
- Rustc Frontend → MIR
Standardrustc
parses your code into Mid‑level IR (MIR). - MIR → OOMIR
Custom “Object‑Oriented MIR” simplifies MIR into OOP‑style constructs.
(seesrc/lower1.rs
) - OOMIR optimiser
Optimises OOMIR using constant folding, dead code elimination, and more.
(seesrc/optimise1.rs
)- Constant Folding: Evaluates constant expressions at compile time.
- Constant Propagation: Replaces variables with their constant values.
- Dead Code Elimination: Removes unused code paths.
- Algebraic Simplification: Simplifies expressions using algebraic identities.
- OOMIR → JVM Classfile
Translate to.class
files usingristretto_classfile
.
(seesrc/lower2.rs
) - R8 pass
r8
adds stack map frames (neeeded to run on JVM 8+) and applies some further optimisations. - Link & Package
java-linker
bundles.class
files into a runnable.jar
withMETA-INF/MANIFEST.MF
.
- Rust Nightly (
rustup default nightly
) - Gradle 8.5+ (
gradle
in PATH) - JDK 8+ (
java
in PATH, and theJAVA_HOME
environment variable set) - Python 3 (
python3
in PATH)
# Clone & enter repo
git clone https://github.com/IntegralPilot/rustc_codegen_jvm.git
cd rustc_codegen_jvm
# Build everything
make all
This will compile:
rustc_codegen_jvm
backend libraryjava-linker
- Kotlin shim for
core
(once core support is reached, this will no longer be needed) - Generate
config.toml
&jvm-unknown-unknown.json
If you relocate the repo, re-run:
make gen-files
-
Configure your project
In your Rust project directory, create or update.cargo/config.toml
by copying the generated template (will be at the root of this repo after running make). Also, yourCargo.toml
needs to contain the following (used to pass flags differentiating between debug and release builds to the linker):cargo-features = ["profile-rustflags"]
-
Build with Cargo
cargo build # debug cargo build --release # optimized
-
Run the
.jar
java -jar target/debug/deps/your_crate*.jar
Ensure the toolchain is built:
make all
# If you moved the repo:
make gen-files
Then:
python3 Tester.py
# or with --release for release‑mode tests
Look for ✅ All tests passed!
or inspect .generated
files on failure.
.
├── src/ # rustc_codegen_jvm backend
│ ├── lib.rs
│ ├── lower1.rs # MIR → OOMIR
│ ├── lower2.rs # OOMIR → JVM bytecode
│ └── oomir.rs # OOMIR definitions
├── java-linker/ # Bundles .class files into .jar
├── tests/binary/ # Integration tests
├── library/ # Kotlin shim for Rust core library
├── shim-metadata-gen/ # Generates core.json metadata
├── proguard/ # .pro rules used for r8
├── Makefile # build & gen-files
├── config.toml.template
├── jvm-unknown-unknown.json.template
├── Tester.py # test runner
├── GenerateFiles.py # regenerates config & target spec
└── LICENSE, LICENSE-Apache
Contributions, issues & PRs welcome! :)
Dual‑licensed under MIT OR Apache 2.0 at your option:
https://opensource.org/licenses/MIT
https://www.apache.org/licenses/LICENSE-2.0