Skip to content

Commit

Permalink
Start FFI and custom calyx testbench
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanuppal committed Jul 17, 2024
1 parent 0e880c7 commit 24b190a
Show file tree
Hide file tree
Showing 22 changed files with 729 additions and 48 deletions.
98 changes: 69 additions & 29 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ members = [
"tools/tb",
"tools/tb/plugins/cocotb",
"tools/tb/plugins/verilator",
"tools/tb/plugins/calyx",
"tools/calyx-ffi-macro",
"tools/calyx-ffi",
"tools/tb/examples/calyx",
]
exclude = ["site"]

Expand Down
25 changes: 25 additions & 0 deletions tools/calyx-ffi-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "calyx-ffi-macro"
authors.workspace = true
license-file.workspace = true
keywords.workspace = true
repository.workspace = true
readme.workspace = true
description.workspace = true
categories.workspace = true
homepage.workspace = true
edition.workspace = true
version.workspace = true
rust-version.workspace = true

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.86"
quote = "1.0.36"
syn = { version = "2.0.69", features = ["full", "visit"] }
calyx-utils.workspace = true
calyx-frontend.workspace = true
calyx-ir.workspace = true
# bigint = "4.4.3"
50 changes: 50 additions & 0 deletions tools/calyx-ffi-macro/src/calyx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::{env, path::PathBuf, rc::Rc};

use proc_macro::TokenStream;

use crate::{parse::CalyxFFIMacroArgs, util};

pub struct CalyxComponent {
ctx: Rc<calyx_ir::Context>,
index: usize,
}

impl CalyxComponent {
pub fn get(&self) -> &calyx_ir::Component {
&self.ctx.components[self.index]
}
}

pub fn parse_calyx_file(
args: CalyxFFIMacroArgs,
) -> Result<CalyxComponent, TokenStream> {
// there has to be a better way to find lib
let home_dir = env::var("HOME").expect("user home not set");
let mut lib_path = PathBuf::from(home_dir);
lib_path.push(".calyx");
let ws = calyx_frontend::Workspace::construct(
&Some(args.src.clone()),
&lib_path,
)
.map_err(|err| util::compile_error(&args.src_attr_span, err.message()))?;
let ctx = calyx_ir::from_ast::ast_to_ir(ws).map_err(|err| {
util::compile_error(&args.src_attr_span, err.message())
})?;

let comp_index = ctx
.components
.iter()
.position(|comp| comp.name == args.comp)
.ok_or(util::compile_error(
&args.comp_attr_span,
format!(
"component '{}' does not exist in '{}'",
args.comp,
args.src.to_string_lossy()
),
))?;
Ok(CalyxComponent {
ctx: Rc::new(ctx),
index: comp_index,
})
}
Loading

0 comments on commit 24b190a

Please sign in to comment.