Skip to content

Commit

Permalink
[sail-riscv] add sail-riscv Rust binder
Browse files Browse the repository at this point in the history
Disambiguation:

* sail: the official sail language, library, API, headers that belongs
  to rem-project.
* sail-riscv: the riscv spec definition describe using sail.

Build step:

1. Use rust-bindgen to create Rust FFI to the sail header, so that we
   can always keep integrity of the sail library.
2. Extern declare all necessary C function define in generated model
   from sail and sail-riscv.
3. Define or override necessary functions from our Rust side.

Current issues:

* Implement our Rust emulator using above strategy inevitably create a
  programming model that functions will be called back and forth from
  both C and Rust side. This will also make the code hard to read. Hope
  this situation could be improved in upcomming rational documents.
* Although sail support using different prefix for its model declaration
  when generating C sources, I am still afraid that these global machine
  state in sail-riscv model will cause problem in our future development.

Signed-off-by: Avimitin <[email protected]>
  • Loading branch information
Avimitin committed Oct 8, 2024
1 parent 80f4038 commit ab28447
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 0 deletions.
1 change: 1 addition & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rec {
libspike_interfaces = final.callPackage ../difftest/spike_interfaces { };

sail-riscv-c-model = final.callPackage ./pkgs/sail-riscv-c-model.nix { };
sail-riscv-rust-emu = final.callPackage ../sail-riscv { };

# DynamoCompiler doesn't support python 3.12+ yet
buddy-mlir = final.callPackage ./pkgs/buddy-mlir.nix { python3 = final.python311; };
Expand Down
4 changes: 4 additions & 0 deletions sail-riscv/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ lib, newScope }:
lib.makeScope newScope (scope: {
sail-riscv-sys = scope.callPackage ./sail-riscv-sys { };
})
286 changes: 286 additions & 0 deletions sail-riscv/sail-riscv-sys/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions sail-riscv/sail-riscv-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "sail-riscv-sys"
version = "0.1.0"
edition = "2021"

[dependencies]

[build-dependencies]
bindgen = "0.70.1"
19 changes: 19 additions & 0 deletions sail-riscv/sail-riscv-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::env;
use std::path::PathBuf;

fn main() {
let sail_install_path = env::var("SAIL_INSTALL_PATH").expect("$SAIL_INSTALL_PATH not set");

let bindings = bindgen::Builder::default()
.header("sail_include/wrapper.h")
.clang_arg(format!("-I{sail_install_path}/share/sail/lib"))
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("sail_h.rs"))
.expect("Couldn't write bindings!");
}
47 changes: 47 additions & 0 deletions sail-riscv/sail-riscv-sys/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{ lib
, rustPlatform
, sail-riscv-c-model
, rustfmt
, rust-analyzer
, gmp
}:

let
finalPkg = rustPlatform.buildRustPackage {
name = "sail-riscv-rs-emulator";
src = with lib.fileset; toSource {
root = ./.;
fileset = unions [
./src
./build.rs
./sail_include
./Cargo.lock
./Cargo.toml
];
};

nativeBuildInputs = [
rustPlatform.bindgenHook
];

buildInputs = [ gmp ];

env = {
SAIL_INSTALL_PATH = toString sail-riscv-c-model.sail;
};

cargoLock = {
lockFile = ./Cargo.lock;
};

passthru = {
devShell = finalPkg.overrideAttrs (prev: {
nativeBuildInputs = prev.nativeBuildInputs ++ [
rustfmt
rust-analyzer
];
});
};
};
in
finalPkg
1 change: 1 addition & 0 deletions sail-riscv/sail-riscv-sys/sail_include/wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <sail.h>
Loading

0 comments on commit ab28447

Please sign in to comment.