Skip to content

Commit 08ce4c5

Browse files
committed
Implement the GC in Rust
This implements the current garbage collector in Rust. No changes were made to the GC design -- it's just ports the one implemented in code generator to Rust. The goals are: - Evaluate Rust for Motoko's RTS implementation - Make the collector easier to read, understand, modify, and extend. Current status: the code is complete in the sense that there aren't any missing features/passes etc., but it's has bugs. I'm not sure how to debug Wasm yet. There are also lots of TODOs in the code, mostly for documentation. Submitting a PR to get early feedback.
1 parent 4cbf21c commit 08ce4c5

File tree

16 files changed

+741
-159
lines changed

16 files changed

+741
-159
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
_out
44
_output
55
_build
6+
target
67

78
**/*~
89
/result*

default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ let
2626
nixpkgs.clang_10 # for native/wasm building
2727
nixpkgs.lld_10 # for wasm building
2828
nixpkgs.rustc
29+
nixpkgs.cargo
2930
];
3031

3132
rtsEnv = ''

rts/Makefile

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
SHELL:=bash -O globstar
2+
13
CLANG ?= clang-10
24
WASM_CLANG ?= clang-10
35
WASM_LD ?= wasm-ld-10
@@ -138,7 +140,6 @@ vpath %.c $(MUSLSRC)/src/math $(MUSLSRC)/src/stdio $(MUSLSRC)/src/string $(MUSLS
138140
#
139141

140142
RTSFILES=rts idl bigint float char buf utf8_valid closure-table text blob url
141-
RTS_RUST_FILES=to_hex_digit
142143

143144
_build/wasm/%.o: %.c rts.h buf.h | _build/wasm
144145
$(WASM_CLANG) $(CLANG_FLAGS) $(WASM_FLAGS) $(TOMMATH_FLAGS) -I$(TOMMATHSRC) $< --output $@
@@ -155,16 +156,22 @@ _build/native/tommath_%.o: %.c rts.h buf.h | _build/native
155156
_build/wasm/musl_%.o: %.c | _build/wasm
156157
$(WASM_CLANG) $(CLANG_FLAGS) $(WASM_FLAGS) $(MUSL_FLAGS) $< --output $@
157158

158-
_build/wasm/%_rs.o: %.rs | _build/wasm
159-
rustc $< --target=wasm32-unknown-emscripten -O --crate-type=lib -Crelocation-model=pic --emit=obj -o $@
159+
.PHONY: _build/wasm/motoko_rts.o
160+
_build/wasm/motoko_rts.o: | _build/wasm
161+
cd motoko-rts && \
162+
cargo rustc --target=wasm32-unknown-emscripten --release -v -- -Crelocation-model=pic --emit=obj
163+
cp motoko-rts/target/wasm32-unknown-emscripten/release/deps/motoko_rts*.o $@
160164

161-
_build/native/%_rs.o: %.rs | _build/native
162-
rustc $< -O --crate-type=lib -Crelocation-model=pic --emit=obj -o $@
165+
.PHONY: _build/native/motoko_rts.o
166+
_build/native/motoko_rts.o: | _build/native
167+
cd motoko-rts && \
168+
cargo rustc --release -v -- -Crelocation-model=pic --emit=obj
169+
cp motoko-rts/target/release/deps/motoko_rts*.o $@
163170

164171
RTS_WASM_O=$(RTSFILES:%=_build/wasm/%.o)
165172
RTS_NATIVE_O=$(RTSFILES:%=_build/native/%.o)
166-
RTS_RUST_WASM_O=$(RTS_RUST_FILES:%=_build/wasm/%_rs.o)
167-
RTS_RUST_NATIVE_O=$(RTS_RUST_FILES:%=_build/native/%_rs.o)
173+
RTS_RUST_WASM_O=_build/wasm/motoko_rts.o
174+
RTS_RUST_NATIVE_O=_build/native/motoko_rts.o
168175

169176
#
170177
# The actual RTS, as we ship it with the compiler
@@ -187,4 +194,4 @@ test_leb128: test_leb128.c $(RTS_NATIVE_O) $(RTS_RUST_NATIVE_O) $(TOMMATH_NATIVE
187194
$(CLANG) -o $@ $+
188195

189196
clean:
190-
rm -rf _build mo-rts.wasm test_rts test_leb128
197+
rm -rf _build mo-rts.wasm test_rts test_leb128 motoko-rts/target

rts/motoko-rts/.vim/coc-settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rust-analyzer.cargo.target": "wasm32-unknown-emscripten"
3+
}

rts/motoko-rts/Cargo.lock

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

rts/motoko-rts/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "motoko-rts"
3+
version = "0.1.0"
4+
authors = ["Ömer Sinan Ağacan <omeragacan@gmail.com>"]
5+
edition = "2018"
6+
7+
[dependencies]

rts/motoko-rts/src/array.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::common::rts_trap_with;
2+
use crate::types::{skew, Array, SkewedPtr};
3+
4+
/// Returns address of Nth payload field of an array
5+
#[no_mangle]
6+
pub unsafe extern "C" fn array_field_addr(array: SkewedPtr, idx: u32) -> SkewedPtr {
7+
let array_ptr = array.unskew() as *const Array;
8+
9+
if idx >= (*array_ptr).len {
10+
rts_trap_with("Array index out of bounds\0".as_ptr());
11+
}
12+
13+
let payload_begin = array_ptr.offset(1) as *const u32;
14+
skew(payload_begin.offset(idx as isize) as usize)
15+
}
16+
17+
/// Index an array. Does not check bounds.
18+
pub unsafe fn array_idx_unchecked(array_ptr: *const Array, idx: u32) -> SkewedPtr {
19+
let payload_begin = array_ptr.offset(1) as *const u32;
20+
SkewedPtr(*payload_begin.offset(idx as isize) as usize)
21+
}

rts/motoko-rts/src/common.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern "C" {
2+
pub fn rts_trap_with(msg: *const u8) -> !;
3+
}

0 commit comments

Comments
 (0)