Skip to content

Commit c91755d

Browse files
committed
New pass - Optimise2 (runs on the Jar) built with R8. Reduces JAR size in the tests from ~1M to ~300K.
1 parent c9dee95 commit c91755d

File tree

17 files changed

+1218
-750
lines changed

17 files changed

+1218
-750
lines changed

CompareSize.py

+236-361
Large diffs are not rendered by default.

Makefile

+32-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# === Phony Targets ===
22
.PHONY: all help clean rust-components rust clean-rust java-linker clean-java-linker \
3-
shim-metadata-gen clean-shim-metadata-gen asm-processor clean-asm-processor \
4-
library clean-library gen-files clean-gen-files ci
3+
shim-metadata-gen clean-shim-metadata-gen stackmapadder clean-stackmapadder \
4+
library clean-library gen-files clean-gen-files ci optimise2 clean-optimise2
55

66
# === Terminal Colors ===
77
GREEN := \033[1;32m
@@ -11,17 +11,18 @@ RESET := \033[0m
1111
# === Directory Variables ===
1212
JAVA_LINKER_DIR := java-linker
1313
SHIM_METADATA_GEN_DIR := shim-metadata-gen
14-
ASM_PROCESSOR_DIR := asm-processor
14+
STACKMAPADDER_DIR := stackmapadder
15+
OPTIMISE2_DIR := optimise2
1516
LIBRARY_DIR := library
1617
LIBRARY_JAR := $(LIBRARY_DIR)/build/libs/library-0.1.0.jar
1718
RUST_SOURCES := $(shell find $(SHIM_METADATA_GEN_DIR)/src -type f -name '*.rs')
1819

1920
# === Default Target ===
2021
ifeq ($(IS_CI),1)
21-
all: rust java-linker asm-processor
22+
all: rust java-linker stackmapadder optimise2
2223
@echo "$(GREEN)✨ Build complete in CI mode! ✨$(RESET)"
2324
else
24-
all: rust gen-files java-linker asm-processor
25+
all: rust gen-files java-linker stackmapadder optimise2
2526
@echo "$(GREEN)✨ Build complete! ✨$(RESET)"
2627
endif
2728

@@ -40,7 +41,7 @@ help:
4041
@echo " make rust-components - Install needed Rust components"
4142
@echo " make rust - Build the Rust root project"
4243
@echo " make java-linker - Build the Java Linker subproject"
43-
@echo " make asm-processor - Build the ASM processor"
44+
@echo " make stackmapadder - Build the ASM processor"
4445
@echo " make library - Build the standard library shim"
4546
@echo " make gen-files - Generate necessary files from templates"
4647
@echo " make clean-* - Clean individual components"
@@ -95,21 +96,37 @@ clean-shim-metadata-gen-json-files:
9596
rm -f $(SHIM_METADATA_GEN_DIR)/*.json; \
9697
fi
9798

98-
# === ASM Processor (Gradle) ===
99-
asm-processor:
100-
@echo "$(CYAN)⚙️ Building ASM processor...$(RESET)"
99+
# === ASM Processors (Gradle) ===
100+
stackmapadder:
101+
@echo "$(CYAN)⚙️ Building Stack map adder...$(RESET)"
101102
ifeq ($(IS_CI),1)
102-
cd $(ASM_PROCESSOR_DIR) && gradle --no-daemon shadowJar
103+
cd $(STACKMAPADDER_DIR) && gradle --no-daemon shadowJar
103104
else
104-
cd $(ASM_PROCESSOR_DIR) && gradle shadowJar
105+
cd $(STACKMAPADDER_DIR) && gradle shadowJar
105106
endif
106107

107-
clean-asm-processor:
108+
clean-stackmapadder:
108109
@echo "$(CYAN)🧹 Cleaning ASM processor...$(RESET)"
109110
ifeq ($(IS_CI),1)
110-
cd $(ASM_PROCESSOR_DIR) && gradle --no-daemon clean
111+
cd $(STACKMAPADDER_DIR) && gradle --no-daemon clean
111112
else
112-
cd $(ASM_PROCESSOR_DIR) && gradle clean
113+
cd $(STACKMAPADDER_DIR) && gradle clean
114+
endif
115+
116+
optimise2:
117+
@echo "$(CYAN)⚙️ Building optimise2...$(RESET)"
118+
ifeq ($(IS_CI),1)
119+
cd $(OPTIMISE2_DIR) && gradle --no-daemon shadowJar
120+
else
121+
cd $(OPTIMISE2_DIR) && gradle shadowJar
122+
endif
123+
124+
clean-optimise2:
125+
@echo "$(CYAN)🧹 Cleaning optimise2...$(RESET)
126+
ifeq ($(IS_CI),1)
127+
cd $(OPTIMISE2_DIR) && gradle --no-daemon clean
128+
else
129+
cd $(OPTIMISE2_DIR) && gradle clean
113130
endif
114131

115132
# === Standard Library Shim (Gradle) ===
@@ -142,5 +159,5 @@ clean-gen-files:
142159
rm -f jvm-unknown-unknown.json config.toml
143160

144161
# === Clean All ===
145-
clean: clean-rust clean-java-linker clean-asm-processor clean-library clean-shim-metadata-gen clean-gen-files
162+
clean: clean-rust clean-java-linker clean-stackmapadder clean-library clean-shim-metadata-gen clean-gen-files
146163
@echo "$(GREEN)🧼 All clean!$(RESET)"

Readme.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ All examples live in `tests/binary` and are compiled to JVM bytecode & run/teste
7272
Translate to `.class` files using `ristretto_classfile`.
7373
_(see `src/lower2.rs`)_
7474
5. **Post‑Process Stack Map Frames**
75-
Kotlin `asm-processor` (ASM library) adds verification frames.
75+
Kotlin `stackmapadder` (built with ASM library) adds verification frames and applys some further optimisations to the class files.
76+
6. **JAR Optimisation**
77+
`optimise2` (based on `r8`) optimises the final jar file.
7678
6. **Link & Package**
7779
`java-linker` bundles `.class` files into a runnable `.jar` with `META-INF/MANIFEST.MF`.
7880

@@ -102,7 +104,8 @@ This will compile:
102104

103105
- `rustc_codegen_jvm` backend library
104106
- `java-linker`
105-
- `asm-processor`
107+
- `stackmapadder`
108+
- `optimise2`
106109
- Kotlin shim for `core` (once core support is reached, this will no longer be needed)
107110
- Generate `config.toml` & `jvm-unknown-unknown.json`
108111

@@ -162,7 +165,8 @@ Look for `✅ All tests passed!` or inspect `.generated` files on failure.
162165
│ ├── lower2.rs # OOMIR → JVM bytecode
163166
│ └── oomir.rs # OOMIR definitions
164167
├── java-linker/ # Bundles .class files into .jar
165-
├── asm-processor/ # Kotlin ASM post‑processor
168+
├── stackmapadder/ # Kotlin StackMapFrame generator
169+
├── optimise2/ # Jar optimiser
166170
├── tests/binary/ # Integration tests
167171
├── library/ # Kotlin shim for Rust core library
168172
├── shim-metadata-gen/ # Generates core.json metadata

asm-processor/src/main/kotlin/asmprocessor/Processor.kt

-46
This file was deleted.

config.toml.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
rustflags = [
33
"-Z", "codegen-backend=../../../target/debug/librustc_codegen_jvm.dylib",
44
"-C", "linker=../../../java-linker/target/debug/java-linker",
5-
"-C", "link-args=../../../library/build/distributions/library-0.1.0/lib/library-0.1.0.jar ../../../library/build/distributions/library-0.1.0/lib/kotlin-stdlib-2.1.20.jar --asm-processor ../../../asm-processor/build/libs/asm-processor-1.0-SNAPSHOT-all.jar --known-good kotlin-stdlib"
5+
"-C", "link-args=../../../library/build/distributions/library-0.1.0/lib/library-0.1.0.jar ../../../library/build/distributions/library-0.1.0/lib/kotlin-stdlib-2.1.20.jar --stackmapadder ../../../stackmapadder/build/libs/stackmapadder-1.0-SNAPSHOT-all.jar --known-good kotlin-stdlib --optimizer ../../../optimise2/build/libs/optimise2-1.0-SNAPSHOT-all.jar --proguard-config ../../../proguard/default.pro"
66
]
77

88
# Throwing a JVM exception will unwind and give a stack trace, no need for rust to handle unwinding.

0 commit comments

Comments
 (0)