Skip to content

Commit

Permalink
Split mut and ref impl blocks for interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanuppal committed Nov 11, 2024
1 parent 48f2cc6 commit 454b29b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
9 changes: 8 additions & 1 deletion tools/calyx-ffi/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ macro_rules! declare_interface {
($name:ident($($input:ident: $input_width:literal),*)
-> ($($output:ident: $output_width:literal),*)
$(impl {
$(fn $fn2:ident(& $self2:ident $(, $arg2:ident: $argty2:ty)* $(,)?) $(-> $ret2:ty)? $body2:block)*
})?
$(mut impl {
$(fn $fn:ident(&mut $self:ident $(, $arg:ident: $argty:ty)* $(,)?) $(-> $ret:ty)? $body:block)*
})? ) => {
})?
) => {
calyx_ffi::prelude::paste::paste! {
pub trait $name: CalyxFFIComponent {
$(
Expand All @@ -25,6 +29,9 @@ macro_rules! declare_interface {

fn $output(&self) -> u64;
)*
$($(
fn $fn2(&$self2, $($arg2: $argty2),*) $(-> $ret2)* {$body2}
)*)*
$($(
fn $fn(&mut $self, $($arg: $argty),*) $(-> $ret)* {$body}
)*)*
Expand Down
4 changes: 2 additions & 2 deletions tools/calyx-ffi/tests/fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ enum QueueStatus {

calyx_ffi::declare_interface! {
Queue(cmd: 1, value: 32) -> (ans: 32, err: 1) impl {
fn status(&mut self) -> QueueStatus {
fn status(&self) -> QueueStatus {
if self.err() == 0 { QueueStatus::Ok } else { QueueStatus::Err }
}

} mut impl {
fn assert_no_error(&mut self) {
assert_eq!(QueueStatus::Ok, self.status(), "queue underflowed or overflowed");
}
Expand Down
4 changes: 2 additions & 2 deletions tools/calyx-ffi/tests/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ enum StackCommand {
const STACK_CAPACITY: u64 = 16;

calyx_ffi::declare_interface! {
Stack(cmd: 1, value: 32) -> (out: 32, length: 4) impl {
fn push(&mut self, value: u32) {
Stack(cmd: 1, value: 32) -> (out: 32, length: 4) mut impl {
fn push(&mut self, value: u32,) {
assert!(self.length() < STACK_CAPACITY, "tried to push when length={}", STACK_CAPACITY);
println!("stack has length {} before push", self.length());
let old_length = self.length();
Expand Down

0 comments on commit 454b29b

Please sign in to comment.