Skip to content

Commit

Permalink
Add weird trait thing (forgot to commit)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanuppal committed Aug 5, 2024
1 parent 445d6e5 commit 20d123d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 6 deletions.
34 changes: 33 additions & 1 deletion tools/calyx-ffi-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use parse::CalyxFFIMacroArgs;
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use quote::quote;
use syn::{parse_macro_input, spanned::Spanned};

mod calyx;
Expand Down Expand Up @@ -116,9 +116,41 @@ pub fn calyx_ffi(attrs: TokenStream, item: TokenStream) -> TokenStream {
}
};

let mut derive_impls = Vec::new();

for derive in args.derives {
let trait_name = derive.name;

let mut getters = Vec::new();
for output in derive.outputs {
getters.push(quote! {
fn #output(&self) -> u64 {
self.#output
}
})
}

let mut setters = Vec::new();
for input in derive.inputs {
setters.push(quote! {
fn #input(&mut self) -> &mut u64 {
&mut self.#input
}
})
}

derive_impls.push(quote! {
impl #trait_name for #name {
#(#getters)*
#(#setters)*
}
});
}

quote! {
#struct_def
#impl_block
#(#derive_impls)*
}
.into()
}
Expand Down
52 changes: 51 additions & 1 deletion tools/calyx-ffi-macro/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
use std::path::PathBuf;

use proc_macro2::{Span, TokenTree};
use syn::parse::{Parse, ParseStream};
use syn::{
bracketed, parenthesized,
parse::{Parse, ParseStream},
};

pub struct CalyxInterface {
pub name: syn::Ident,
pub inputs: Vec<syn::Ident>,
pub outputs: Vec<syn::Ident>,
}

impl Parse for CalyxInterface {
fn parse(input: ParseStream) -> syn::Result<Self> {
let name = input.parse::<syn::Ident>()?;
let inputs;
let outputs;
parenthesized!(inputs in input);
let inputs = inputs
.parse_terminated(syn::Ident::parse, syn::Token![,])?
.into_iter()
.collect();
input.parse::<syn::Token![->]>()?;
parenthesized!(outputs in input);
let outputs = outputs
.parse_terminated(syn::Ident::parse, syn::Token![,])?
.into_iter()
.collect();
Ok(Self {
name,
inputs,
outputs,
})
}
}

pub struct CalyxFFIMacroArgs {
pub src_attr_span: Span,
pub src: PathBuf,
pub comp_attr_span: Span,
pub comp: String,
pub backend: syn::Path,
pub derives: Vec<CalyxInterface>,
}

impl Parse for CalyxFFIMacroArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
syn::custom_keyword!(src);
syn::custom_keyword!(comp);
syn::custom_keyword!(backend);
syn::custom_keyword!(derive);

let src_ident = input.parse::<src>()?;
input.parse::<syn::Token![=]>()?;
Expand All @@ -32,6 +67,20 @@ impl Parse for CalyxFFIMacroArgs {
input.parse::<syn::Token![=]>()?;
let backend_path = input.parse::<syn::Path>()?;

let _ = input.parse::<syn::Token![,]>();

let derives = if input.parse::<derive>().is_ok() {
input.parse::<syn::Token![=]>()?;
let content;
bracketed!(content in input);
content
.parse_terminated(CalyxInterface::parse, syn::Token![,])?
.into_iter()
.collect()
} else {
vec![]
};

if !input.is_empty() {
return Err(syn::Error::new_spanned(
input.parse::<TokenTree>()?,
Expand All @@ -45,6 +94,7 @@ impl Parse for CalyxFFIMacroArgs {
comp_attr_span: comp_ident.span,
comp: comp_lit,
backend: backend_path,
derives,
})
}
}
14 changes: 14 additions & 0 deletions tools/calyx-ffi/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
pub use super::{CalyxFFI, CalyxFFIComponent, CalyxFFIComponentRef};
pub use calyx_ffi_macro::{calyx_ffi, calyx_ffi_test, calyx_ffi_tests};

#[macro_export]
macro_rules! declare_calyx_interface {
($name:ident($($input:ident),*) -> ($($output:ident),*)) => {
pub trait $name: CalyxFFIComponent {
$(
fn $input(&mut self) -> &mut u64;
)*
$(
fn $output(&self) -> u64;
)*
}
};
}
16 changes: 12 additions & 4 deletions tools/tb/examples/calyx/test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use calyx_ffi::declare_calyx_interface;
use calyx_ffi::prelude::*;

use calyx_ffi::cider_ffi_backend;

declare_calyx_interface! {
In2Out1(lhs, rhs) -> (result)
}

#[calyx_ffi(
src = "/Users/ethan/Documents/GitHub/calyx/tools/tb/examples/calyx/adder.futil",
comp = "main",
backend = cider_ffi_backend
backend = cider_ffi_backend,
derive = [
In2Out1(lhs, rhs) -> (result)
]
)]
struct Adder;

Expand All @@ -14,9 +22,9 @@ struct Adder;
mod tests {
use super::*;

fn add(adder: &mut Adder, lhs: u64, rhs: u64) -> u64 {
adder.lhs = lhs;
adder.rhs = rhs;
fn add<I: In2Out1>(adder: &mut I, lhs: u64, rhs: u64) -> u64 {
*adder.lhs() = lhs;
*adder.rhs() = rhs;
adder.go();
adder.result()
}
Expand Down

0 comments on commit 20d123d

Please sign in to comment.