Skip to content

A RISC-V (rv32imc) assembler for JITing on the ESP32

License

Notifications You must be signed in to change notification settings

pixelspark/rv32jit

Repository files navigation

RISC-V 32-bit JIT

An assembler for JIT compilation for use on ESP32-C3 (rv32imc)

Setting up the environment

rustup toolchain install nightly
rustup target add riscv32imc-unknown-none-elf
cargo install ldproxy
cargo install cargo-espflash

Building

cargo build --release

To flash and run:

cargo espflash --release --monitor

If you get a certificate error on macOS:

curl "https://ccadb-public.secure.force.com/mozilla/IncludedRootsPEMTxt?TrustBitsInclude=Websites" > IncludedRootsPEM.txt
SSL_CERT_FILE=$(pwd)/IncludedRootsPEM.txt cargo build

To test locally (replace aarch64-apple-darwin with your system's target triple, use rustup target list to find installed targets):

cargo run --example assemble --target aarch64-apple-darwin -- ~/Desktop/test2.bin

The above will write a binary that you can upload in e.g. this disassembler.

Testing

cd rv32assembler
cargo test --target aarch64-apple-darwin

Usage

fn test_jit_simple() {
	let mut f = Fragment::new();
	f.addi(Register::A0, Register::A0, 42);
	f.ret();
	let program = JitFunction::from(&f);
	let s = unsafe { program.call(88) };
	assert_eq!(s, 88 + 42);
}

If you want to use this crate in your own project, be sure to set the following option in sdkconfig.defaults:

# Required to allow JIT
CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=n

References

Useful reference material:

To create a binary file from "[1, 2, 3]" debug output:

const fs = require("fs");
fs.writeFileSync("foo.bin", Buffer.from([1, 2, 3]));

License

MIT