Skip to content

Commit 571bf53

Browse files
committed
Improve standard library linking and variable typing process, to make it fully adaptive to any function/variable and not hardcoded.
- Added a new `shim-metadata-gen` crate to automatically generate JSON metadata (added to Makefile) for shim methods in the compiled stdlib JAR, which is used through include_bytes! to allow the codegen backend to use it. - Added types to OOMIR variables and modified lower1 to provide this info and lower2 to use this to actually use the appropriate instructions for different types and deal with variables in the correct way. - Removed unsigned types from OOMIR and adjusted type mappings accordingly, as the JVM doesn't support them (this means we deal with u -> i coversion at the lower1 stage, which is more idiomatic to do this at then lower2).
1 parent d551beb commit 571bf53

File tree

12 files changed

+1812
-445
lines changed

12 files changed

+1812
-445
lines changed

Cargo.lock

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
regex = "1.11.1"
78
ristretto_classfile = { version = "0.16.0" }
9+
serde = { version = "1.0.219", features = ["derive"] }
10+
serde_json = "1.0.140"
811

912
[lib]
1013
crate-type = ["dylib"]

Makefile

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,77 @@
1-
# Makefile
1+
# === Phony Targets ===
2+
.PHONY: all clean help rust clean-rust java-linker clean-java-linker \
3+
shim-metadata-gen clean-shim-metadata-gen \
4+
asm-processor clean-asm-processor \
5+
library clean-library
26

3-
.PHONY: all rust java-linker asm-processor clean clean-rust clean-java-linker clean-asm-processor library clean-library
7+
# === Terminal Colors ===
8+
GREEN := \033[1;32m
9+
CYAN := \033[1;36m
10+
RESET := \033[0m
411

5-
all: rust java-linker asm-processor library
12+
# === Default Target ===
13+
all: rust java-linker asm-processor
14+
@echo "$(GREEN)✨ Build complete! ✨$(RESET)"
615

7-
# --- Rust root project ---
8-
rust:
16+
# === Help ===
17+
help:
18+
@echo "$(CYAN)🛠️ Makefile for building the project$(RESET)"
19+
@echo ""
20+
@echo "Available targets:"
21+
@echo " make all - Build all components"
22+
@echo " make clean - Clean all components"
23+
@echo " make rust - Build the Rust root project"
24+
@echo " make java-linker - Build the Java Linker subproject"
25+
@echo " make shim-metadata-gen - Generate library shim metadata"
26+
@echo " make asm-processor - Build the ASM processor"
27+
@echo " make library - Build the standard library shim"
28+
@echo " make clean-* - Clean individual components"
29+
30+
# === Rust root project (Cargo) ===
31+
rust: shim-metadata-gen
32+
@echo "$(CYAN)📦 Building Rust root project...$(RESET)"
933
cargo build
1034

1135
clean-rust:
36+
@echo "$(CYAN)🧹 Cleaning Rust root project...$(RESET)"
1237
cargo clean
1338

14-
# --- Java Linker Rust Subproject ---
39+
# === Java Linker Subproject ===
1540
java-linker:
41+
@echo "$(CYAN)📦 Building Java Linker...$(RESET)"
1642
cd java-linker && cargo build
1743

1844
clean-java-linker:
45+
@echo "$(CYAN)🧹 Cleaning Java Linker...$(RESET)"
1946
cd java-linker && cargo clean
2047

21-
# --- ASM Processor (Gradle) ---
48+
# === Library shim metadata generator ===
49+
shim-metadata-gen: library
50+
@echo "$(CYAN)🔧 Generating shim metadata...$(RESET)"
51+
cd shim-metadata-gen && rm -f core.json && cargo run -- ../library/build/libs/library-0.1.0.jar ./core.json
52+
53+
clean-shim-metadata-gen:
54+
@echo "$(CYAN)🧹 Cleaning shim-metadata-gen...$(RESET)"
55+
cd shim-metadata-gen && cargo clean
56+
57+
# === ASM Processor (Gradle) ===
2258
asm-processor:
59+
@echo "$(CYAN)⚙️ Building ASM processor...$(RESET)"
2360
cd asm-processor && gradle shadowJar
2461

2562
clean-asm-processor:
63+
@echo "$(CYAN)🧹 Cleaning ASM processor...$(RESET)"
2664
cd asm-processor && gradle clean
2765

28-
# --- Standard Library Shim (Gradle) ---
66+
# === Standard Library Shim (Gradle) ===
2967
library:
68+
@echo "$(CYAN)📚 Building standard library shim...$(RESET)"
3069
cd library && gradle build
70+
3171
clean-library:
72+
@echo "$(CYAN)🧹 Cleaning library shim...$(RESET)"
3273
cd library && gradle clean
3374

34-
# --- Clean everything ---
35-
clean: clean-rust clean-java-linker clean-asm-processor clean-library
75+
# === Clean All ===
76+
clean: clean-rust clean-java-linker clean-asm-processor clean-library clean-shim-metadata-gen
77+
@echo "$(GREEN)🧼 All clean!$(RESET)"

Readme.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ This backend currently supports a subset of Rust features:
2929

3030
* ✅ Compiling minimal `no_std` & `no_core` Rust programs (like an empty `main`) using the `jvm-unknown-unknown` target.
3131
* ✅ Compiling simple programs using basic `core` features (like the `is_even_plus_one` test) using the host target.
32-
* ✅ Basic integer arithmetic operations on `i32`:
32+
* ✅ Basic integer arithmetic operations on all types of numbers:
3333
* Addition (`+`), Subtraction (`-`), Multiplication (`*`), Division (`/`), Remainder (`%`).
3434
* Checked addition and subtraction (`checked_add`, `checked_sub`) returning `(result, overflowed_bool)` tuples.
3535
* ✅ Comparisons (`==`, `!=`, `<`, `<=`, `>`, `>=`).
3636
* ✅ Bitwise operations (`&`, `|`, `^`, `<<`, `>>`).
3737
* ✅ Basic control flow: `if`/`else` statements, `panic!` / `assert!` (with simple string messages).
3838
* ✅ Calling other `static` functions within the same crate (including recursion).
3939
* ✅ Basic variable assignment (`let x = y;`).
40+
* ✅ Arrays and slices.
4041
* ✅ Generating executable `.jar` files for binary crates.
4142

4243
### Next Milestone:
@@ -62,10 +63,6 @@ This backend currently supports a subset of Rust features:
6263
```bash
6364
# Using Make
6465
make all
65-
66-
# OR Using the build script
67-
chmod +x build.sh
68-
./build.sh
6966
```
7067
* This will:
7168
* Build the main `rustc_codegen_jvm` library (`target/debug/librustc_codegen_jvm.dylib`).
@@ -113,7 +110,7 @@ To compile *your own* Rust project using this backend:
113110

114111
This project includes integration tests managed by a Python script.
115112

116-
1. **Ensure Toolchain is Built:** Build the project using `make all` or `./build.sh`.
113+
1. **Ensure Toolchain is Built:** Build the project using `make all`.
117114
2. **Check Target JSON:** Make sure the `jvm-unknown-unknown.json` file in the *root* of this repository has the **relative paths** starting with `../../../` for the linker and backend, as the tester expects this structure when running tests from subdirectories. If you changed them to absolute paths for external use, change them back temporarily.
118115
3. **Run the Tester:**
119116
```bash
@@ -135,9 +132,11 @@ This project includes integration tests managed by a Python script.
135132
* `tests/`: Integration tests.
136133
* `binary/`: Tests for compiling executable Rust crates.
137134
* `library/`: Source code for a minimal Kotlin-based implementation of the Rust core library. This serves as a temporary substitute to bootstrap the project until the backend can fully compile the Rust core library itself.
135+
* `shim-metadata-gen`: A tool for generating metadata for the Kotlin core library, called at compiletime to provide nessecary info (descriptors) to the codegen backend. It's generated metadata is embedded in the generated library, so not needed at runtime.
136+
* `core.json`: Metadata for the core library, generated by this tool.
138137
* `jvm-unknown-unknown.json`: The Rust target specification file.
139138
* `Tester.py`: Python script for running integration tests.
140-
* `Makefile`, `build.sh`: Scripts for building the entire toolchain.
139+
* `Makefile` Scripts for building the entire toolchain.
141140
* `setup.sh`: Script to install Rust components.
142141
* `Cargo.toml`, `Cargo.lock`: Rust package definition and dependencies for the backend.
143142
* `LICENSE`, `LICENSE-Apache`: Project licenses.

library/src/main/kotlin/org/rustlang/core/Core.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ package org.rustlang.core
66
public object Core {
77

88
@JvmStatic // Ensures static JVM methods are generated
9-
public fun rust_panic_fmt(message: String?) {
9+
public fun panic_fmt(message: String?) {
1010
// note to future self: consider using a more specific Rust exception type if needed
1111
throw RuntimeException("Rust panic: " + (message ?: "<no message>"))
1212
}
1313

1414
@JvmStatic
15-
public fun rust_fmt_arguments_new_const_stub(messagePiece: String?): String? {
16-
return messagePiece
15+
public fun arguments_new_const(pieces: Array<String>): String {
16+
// Concatenate all the string pieces together.
17+
// This mimics the simplest formatting scenario where pieces are just joined.
18+
// If the array is empty, it correctly returns an empty string.
19+
return pieces.joinToString("")
1720
}
1821
}

0 commit comments

Comments
 (0)