Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nix] add sail riscv model builder #780

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ rec {
libspike = final.callPackage ./pkgs/libspike.nix { };
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 All @@ -37,6 +40,11 @@ rec {
mill = let jre = final.jdk21; in
(prev.mill.override { inherit jre; }).overrideAttrs (_: {
passthru = { inherit jre; };
# Using mill without "-i/--interactive" or "--no-server" will run a mill
# build server automatically. The build server doesn't inherit or update
# environment when user invoke mill at command-line, which is hard for
# developers to debug.
#
# --interactive implies --no-server
postInstall = ''wrapProgram $out/bin/mill --add-flags "--interactive"'';
});
Expand Down
192 changes: 192 additions & 0 deletions nix/pkgs/sail-riscv-c-model.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# https://github.com/NixOS/nixpkgs/blob/nixpkgs-unstable/pkgs/applications/virtualization/sail-riscv/default.nix

{ lib
, stdenv
, fetchFromGitHub
, ocamlPackages
, ocaml
, zlib
, z3
, bear

, targetArch ? "rv32"
}:
assert lib.assertMsg (lib.elem targetArch [ "rv32" "rv64" ]) "targetArch `${targetArch}' invalid";

let
ocamlPackages' = ocamlPackages.overrideScope (finalScope: prevScope: {
sail = prevScope.sail.overrideAttrs rec {
version = "0.18";
src = fetchFromGitHub {
owner = "rems-project";
repo = "sail";
rev = version;
hash = "sha256-QvVK7KeAvJ/RfJXXYo6xEGEk5iOmVsZbvzW28MHRFic=";
};

buildInputs = [ finalScope.menhirLib ];
};
});
in
stdenv.mkDerivation (finalAttr: {
pname = "sail-riscv-c-model";
version = "unstable-b90ec78";

src = fetchFromGitHub {
owner = "riscv";
repo = "sail-riscv";
rev = "b90ec7881eb0a0ecea6521e3d6cc03bf0b057e41";
hash = "sha256-vWOKYCkFTYP3t3ZFpmiXOjTpqoX06Ubs2GTncCdj/Cg=";
};

nativeBuildInputs = with ocamlPackages'; [ ocamlbuild findlib ocaml z3 sail ];
buildInputs = with ocamlPackages'; [ zlib linksem ];
strictDeps = true;

postPatch = ''
rm -r prover_snapshots
rm -r handwritten_support
'';

sailArgs = [
"--strict-var"
"-dno_cast"
"-c_preserve"
"_set_Misa_C"
"-O"
"-Oconstant_fold"
"-memo_z3"
"-c"
"-c_no_main"
];

sailSrcs = [
"model/prelude.sail"
] ++ (if targetArch == "rv32" then [
"model/riscv_xlen32.sail"
] else [
"model/riscv_xlen64.sail"
])
++ [
"model/riscv_xlen.sail"
"model/riscv_flen_D.sail"
"model/riscv_vlen.sail"
"model/prelude_mem_metadata.sail"
"model/prelude_mem.sail"
"model/riscv_types_common.sail"
"model/riscv_types_ext.sail"
"model/riscv_types.sail"
"model/riscv_vmem_types.sail"
"model/riscv_reg_type.sail"
"model/riscv_freg_type.sail"
"model/riscv_regs.sail"
"model/riscv_pc_access.sail"
"model/riscv_sys_regs.sail"
"model/riscv_pmp_regs.sail"
"model/riscv_pmp_control.sail"
"model/riscv_ext_regs.sail"
"model/riscv_addr_checks_common.sail"
"model/riscv_addr_checks.sail"
"model/riscv_misa_ext.sail"
"model/riscv_vreg_type.sail"
"model/riscv_vext_regs.sail"
"model/riscv_csr_begin.sail"
"model/riscv_vext_control.sail"
"model/riscv_next_regs.sail"
"model/riscv_sys_exceptions.sail"
"model/riscv_sync_exception.sail"
"model/riscv_next_control.sail"
"model/riscv_softfloat_interface.sail"
"model/riscv_fdext_regs.sail"
"model/riscv_fdext_control.sail"
"model/riscv_sys_control.sail"
"model/riscv_platform.sail"
"model/riscv_mem.sail"
"model/riscv_vmem_common.sail"
"model/riscv_vmem_pte.sail"
"model/riscv_vmem_ptw.sail"
"model/riscv_vmem_tlb.sail"
"model/riscv_vmem.sail"
"model/riscv_types_kext.sail"
"model/riscv_insts_begin.sail"
"model/riscv_insts_base.sail"
"model/riscv_insts_aext.sail"
"model/riscv_insts_zca.sail"
"model/riscv_insts_mext.sail"
"model/riscv_insts_zicsr.sail"
"model/riscv_insts_next.sail"
"model/riscv_insts_hints.sail"
"model/riscv_insts_fext.sail"
"model/riscv_insts_zcf.sail"
"model/riscv_insts_dext.sail"
"model/riscv_insts_zcd.sail"
"model/riscv_insts_svinval.sail"
"model/riscv_insts_zba.sail"
"model/riscv_insts_zbb.sail"
"model/riscv_insts_zbc.sail"
"model/riscv_insts_zbs.sail"
"model/riscv_insts_zcb.sail"
"model/riscv_insts_zfh.sail"
"model/riscv_insts_zfa.sail"
"model/riscv_insts_zkn.sail"
"model/riscv_insts_zks.sail"
"model/riscv_insts_zbkb.sail"
"model/riscv_insts_zbkx.sail"
"model/riscv_insts_zicond.sail"
"model/riscv_insts_vext_utils.sail"
"model/riscv_insts_vext_fp_utils.sail"
"model/riscv_insts_vext_vset.sail"
"model/riscv_insts_vext_arith.sail"
"model/riscv_insts_vext_fp.sail"
"model/riscv_insts_vext_mem.sail"
"model/riscv_insts_vext_mask.sail"
"model/riscv_insts_vext_vm.sail"
"model/riscv_insts_vext_fp_vm.sail"
"model/riscv_insts_vext_red.sail"
"model/riscv_insts_vext_fp_red.sail"
"model/riscv_insts_zicbom.sail"
"model/riscv_insts_zicboz.sail"
"model/riscv_jalr_seq.sail"
"model/riscv_insts_end.sail"
"model/riscv_csr_end.sail"
"model/riscv_step_common.sail"
"model/riscv_step_ext.sail"
"model/riscv_decode_ext.sail"
"model/riscv_fetch.sail"
"model/riscv_step.sail"
"model/main.sail"
];

buildPhase = ''
runHook preBuild

mkdir -p build
sailPhase="sail $sailArgs $sailSrcs -o build/riscv_model_${targetArch}"
echo "$sailPhase"
eval "$sailPhase"

runHook postBuild
'';

installPhase = ''
runHook preInstall

mkdir -p $out/share/
cp -v build/riscv_model_${targetArch}.c $out/share/

runHook postInstall
'';

passthru = {
inherit (ocamlPackages') sail;
inherit targetArch;

dev = finalAttr.finalPackage.overrideAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs ++ [ bear ];
});
};

meta = {
description = "Generated C source of the sail RISC-V (${targetArch}) model";
};
})
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 { };
})
Loading