A custom Rust compiler backend that emits Java Virtual Machine bytecode.
Compile Rust code into a runnable .jar compatible with JVM 8+.
- Demos
- Features
- How It Works
- Prerequisites
- Installation & Build
- Usage
- Running Tests
- Project Structure
- Contributing
- License
These examples are located in tests/binary, compiled to JVM bytecode, and verified on CI during integration testing. Examples include:
- RSA encryption/decryption
- Binary search algorithm
- Fibonacci sequence generator
- Collatz conjecture verifier
- Large prime generator
- Enums and Structs (nested data structures: structs, tuples, arrays, and slices)
- Implementation blocks and Traits (including dynamic dispatch)
- Unions (demonstrating certain
unsafeoperations)
- Optimisations: Constant folding, constant propagation, and dead code elimination to generate clean JVM bytecode.
- Standard Library Support: Basic
coresupport on host target for JVM output. - Arithmetic: Support for integers, floats, and checked operations.
- Operations: Comparisons, bitwise, and logical operations.
- Control Flow: Support for
if/else,match,for,while, andloop. - Type Handling: Type casting (
as) and primitive types. - Functions: Function calls, recursion, and function pointers in multiple contexts (within ADTs, as variables, parameters, return values, or generics).
- Data Structures: Arrays, slices, structs, tuples, and enums (C-like and Rust-style).
- Memory Management: Mutable borrowing, references, and dereferencing.
- Object-Oriented Constructs: Implementations for ADTs, including
self,&self, and&mut self. - Traits & Closures: Dynamic dispatch (
&dyn Trait) and closure capturing. - Unions: Supported for basic types (
bool,i8/u8,i16/u16,i32/u32,f32,f64) and structs containing combinations of these types. - Output: Executable
.jargeneration for binary crates. - Testing: Comprehensive integration tests covering these features in both debug and release modes.
Current Milestone: Full support for the Rust core crate.
- Rustc Frontend → MIR
The standardrustccompiler parses your code into Mid-level IR (MIR). - MIR → OOMIR
A custom "Object-Oriented MIR" layer simplifies MIR into OOP-style constructs (defined insrc/lower1.rs). - OOMIR Optimiser
Optimises OOMIR (defined insrc/optimise1.rs) using:- Constant Folding: Evaluates constant expressions at compile time.
- Constant Propagation: Replaces variables with their constant values.
- Dead Code Elimination: Removes unused execution paths.
- Algebraic Simplification: Simplifies expressions using algebraic identities.
- OOMIR → JVM Classfile
Translates OOMIR to.classfiles usingristretto_classfile(defined insrc/lower2.rs). - R8 Pass
Invokesr8to add stack map frames (required for JVM 8+) and apply further Optimisation passes. - Link & Package
Usesjava-linkerto bundle.classfiles into a runnable.jarwith an appropriateMETA-INF/MANIFEST.MF.
- Rust Nightly (
rustup default nightly) - Gradle 8.5+ (
gradlemust be in system PATH) - JDK 8+ (
javamust be in system PATH, withJAVA_HOMEset) - Python 3 (
python3must be in system PATH)
Clone the repository and build all components using the main build script:
# Clone the repository
git clone https://github.com/IntegralPilot/rustc_codegen_jvm.git
cd rustc_codegen_jvm
# Build all components using Python
# On Linux or macOS:
./build.py all
# On Windows:
python build.py allThis script builds the necessary components in the correct dependency order:
- The Kotlin library shim (
library/) - The shim metadata file (
core.json) - The
java-linkerexecutable - The
rustc_codegen_jvmbackend library - Configuration files (
config.toml,jvm-unknown-unknown.json) - Vendored dependencies (such as R8)
Subsequent runs of build.py check file timestamps and will only rebuild modified components.
-
Configure Your Project
In your target Rust project directory, create or update.cargo/config.tomlby copying the generated template located in the root of this repository.Ensure your
Cargo.tomlcontains the following feature flag to support separate compilation configurations:cargo-features = ["profile-rustflags"]
-
Build with Cargo
cargo build # Debug build cargo build --release # Optimised build
-
Run the JAR File
java -jar target/debug/deps/your_crate*.jar # Run debug build java -jar target/release/deps/your_crate*.jar # Run release build
First, ensure the toolchain is built:
# On Linux/macOS:
./build.py all
# On Windows:
python build.py allRun the test suite with the test runner:
# Run tests in debug mode
python Tester.py
# Run tests in release mode
python Tester.py --releaseTest results will output to the console. Temporary test artifacts are written to .generated/ for debugging.
.
├── src/ # rustc_codegen_jvm compiler backend
│ ├── lib.rs
│ ├── lower1.rs # MIR → OOMIR conversion
│ ├── lower2.rs # OOMIR → JVM bytecode translation
│ └── oomir.rs # OOMIR data definitions
├── java-linker/ # Bundles compiled .class files into .jar archives
├── tests/binary/ # Integration tests and source examples
├── library/ # Kotlin shim implementation for the Rust core library
├── shim-metadata-gen/ # Tool to generate core.json metadata
├── proguard/ # Proguard / R8 configuration rules
├── build.py # Orchestrator build script
├── config.toml.template # Configuration template for cargo projects
├── jvm-unknown-unknown.json.template
├── Tester.py # Automated test runner
└── LICENSE, LICENSE-Apache
Contributions, issues, and pull requests are welcome.
This project is dual-licensed under the MIT License and the Apache License, Version 2.0 at your option: