Skip to content

Commit

Permalink
enable doctests and remove unused macro
Browse files Browse the repository at this point in the history
  • Loading branch information
EclecticGriffin committed Jul 15, 2024
1 parent 0a70ea6 commit 48806db
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 345 deletions.
5 changes: 0 additions & 5 deletions interp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ authors = ["The Calyx authors"]
edition = "2021"
rust-version = "1.73"

[lib]
doctest = false # Don't run doc tests

[[bin]]
name = "cider"
path = "src/main.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
change-based-sim = []
default = ["change-based-sim"]

[dependencies]
smallvec = { workspace = true, features = ["union", "const_generics"] }
Expand Down
340 changes: 0 additions & 340 deletions interp/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,343 +1,3 @@
/// Define a primitive
///
/// ```
/// # use interp::comb_primitive;
/// # use interp::values::Value;
/// # use interp::primitives::Primitive;
/// comb_primitive!(StdAdd[width](left: width, right: width) -> (out: width) {
/// let left_64 = left.as_u64();
/// let right_64 = right.as_u64();
/// let init_val = left_64 + right_64;
/// let bitwidth = left.width();
/// Ok(Value::from(init_val, bitwidth))
/// });
///
/// ```
/// The macro implements the [Primitive](crate::primitives::Primitive) trait
/// for the struct as well as `StdAdd::new(bindings: ir::Params)` and
/// `StdAdd::from_constants(ports)`
///
/// TODO(rachit): $out_width is never used. (TODO: Griffin) fix the copy-paste
#[macro_export]
macro_rules! comb_primitive {
($name:ident[
$( $param:ident ),+
]( $( $port:ident : $width:ident ),+ ) ->
( $( $out:ident : $out_width:ident ),+ ) $execute:block
) => {
comb_primitive!(LOG; NAME; $name[$( $param ),+]($( $port : $width ),+) -> ($($out : $out_width),+ ) $execute);
};


(FLAG : $flag:ident; LOG: $log:ident; $name:ident[
$( $param:ident ),+
]( $( $port:ident : $width:ident ),+ ) ->
( $( $out:ident : $out_width:ident ),+ ) $execute:block
) => {
comb_primitive!($flag; $log; NAME; $name[$( $param ),+]($( $port : $width ),+) -> ($($out : $out_width),+ ) $execute);
};

(LOG: $log:ident; FLAG : $flag:ident; $name:ident[
$( $param:ident ),+
]( $( $port:ident : $width:ident ),+ ) ->
( $( $out:ident : $out_width:ident ),+ ) $execute:block
) => {
comb_primitive!($flag; $log; NAME; $name[$( $param ),+]($( $port : $width ),+) -> ($($out : $out_width),+ ) $execute);
};

(FLAG : $flag:ident; $name:ident[
$( $param:ident ),+
]( $( $port:ident : $width:ident ),+ ) ->
( $( $out:ident : $out_width:ident ),+ ) $execute:block
) => {
comb_primitive!($flag; LOG; NAME; $name[$( $param ),+]($( $port : $width ),+) -> ($($out : $out_width),+ ) $execute);
};

(FLAG : $flag:ident; NAME : $full_name:ident; $name:ident[
$( $param:ident ),+
]( $( $port:ident : $width:ident ),+ ) ->
( $( $out:ident : $out_width:ident ),+ ) $execute:block
) => {
comb_primitive!($flag; LOG; $full_name; $name[$( $param ),+]($( $port : $width ),+) -> ($($out : $out_width),+ ) $execute);
};


(LOG : $log:ident; $name:ident[
$( $param:ident ),+
]( $( $port:ident : $width:ident ),+ ) ->
( $( $out:ident : $out_width:ident ),+ ) $execute:block
) => {
comb_primitive!($log; NAME; $name[$( $param ),+]($( $port : $width ),+) -> ($($out : $out_width),+ ) $execute);
};

(NAME : $full_name:ident; $name:ident[
$( $param:ident ),+
]( $( $port:ident : $width:ident ),+ ) ->
( $( $out:ident : $out_width:ident ),+ ) $execute:block
) => {
comb_primitive!(LOG; $full_name; $name[$( $param ),+]($( $port : $width ),+) -> ($($out : $out_width),+ ) $execute);
};

($log:ident; $full_name:ident; $name:ident[
$( $param:ident ),+
]( $( $port:ident : $width:ident ),+ ) ->
( $( $out:ident : $out_width:ident ),+ ) $execute:block
) => {
#[derive(Clone, Debug)]
#[allow(non_snake_case)]
pub struct $name {
$($param: u64),+,
name: calyx_ir::Id,
logger: $crate::logging::Logger,
}

impl Default for $name {
fn default() -> Self {
Self {
name: "".into(),
logger: $crate::logging::new_sublogger(""),
$($param: 0),+,
}
}
}


impl $name {
pub fn new(params: &calyx_ir::Binding, name: calyx_ir::Id) -> Self {
let mut base = Self::default();
for (param, value) in params {
match param.as_ref() {
$( $crate::in_fix!($param) => base.$param = *value ),+,
p => unreachable!("Unknown parameter: {}", p),
}
}
base.logger = $crate::logging::new_sublogger(&name);
base.name = name;
base
}

#[allow(non_snake_case)]
pub fn from_constants($( $param: u64 ),+, name: calyx_ir::Id) -> Self {
$name {
$($param),+,
logger: $crate::logging::new_sublogger(&name),
name
}
}
}

impl $crate::primitives::Named for $name {
fn get_full_name(&self) -> &calyx_ir::Id {
&self.name
}
}


impl $crate::primitives::Primitive for $name {

//null-op; comb don't use do_tick()
fn do_tick(&mut self) -> $crate::errors::InterpreterResult<Vec<(calyx_ir::Id, $crate::values::Value)>>{
Ok(vec![])
}

fn is_comb(&self) -> bool { true }

fn validate(
&self,
inputs: &[(calyx_ir::Id, &$crate::values::Value)]
) {
for (id, v) in inputs {
match id.as_ref() {
$( $crate::in_fix!($port) => assert_eq!(v.len() as u64, self.$width) ),+,
p => unreachable!("Unknown port: {}", p),
}
}
}

#[allow(non_snake_case,unused)]
fn execute(
&mut self,
inputs: &[(calyx_ir::Id, &$crate::values::Value)],
) -> $crate::errors::InterpreterResult<Vec<(calyx_ir::Id, $crate::values::Value)>> {

#[derive(Default)]
struct Ports<'a> {
$( $port: Option<&'a $crate::values::Value> ),+
}

let mut base = Ports::default();

for (id, v) in inputs {
match id.as_ref() {
$( $crate::in_fix!($port) => base.$port = Some(v) ),+,
p => unreachable!("Unknown port: {}", p),
}
}

let exec_func = |$($param: u64),+, $( $port: &Value ),+, $log: &$crate::logging::Logger, $full_name:&calyx_ir::Id| -> $crate::errors::InterpreterResult<Value> {
$execute
};

#[allow(unused_parens)]
let ($( $out ),+) = exec_func(
$(self.$param),+,
$( base
.$port
.expect(&format!("No value for port: {}", $crate::in_fix!($port)).to_string()) ),+,
&self.logger,
$crate::primitives::Named::get_full_name(self),
)?;

return Ok(vec![
$( ($crate::in_fix!($out).into(), $out) ),+
])

}

// Combinational components cannot be reset
fn reset(
&mut self,
inputs: &[(calyx_ir::Id, &$crate::values::Value)],
) -> $crate::errors::InterpreterResult<Vec<(calyx_ir::Id, $crate::values::Value)>> {
self.execute(inputs)
}

}
};

($flag: ident; $log:ident; $full_name:ident; $name:ident[
$( $param:ident ),+
]( $( $port:ident : $width:ident ),+ ) ->
( $( $out:ident : $out_width:ident ),+ ) $execute:block
) => {
#[derive(Clone, Debug)]
#[allow(non_snake_case)]
pub struct $name {
$($param: u64),+,
name: calyx_ir::Id,
logger: $crate::logging::Logger,
$flag: bool
}

impl Default for $name {
fn default() -> Self {
Self {
name: "".into(),
logger: $crate::logging::new_sublogger(""),
$flag: false,
$($param: 0),+,
}
}
}


impl $name {
pub fn new(params: &calyx_ir::Binding, name: calyx_ir::Id, $flag: bool) -> Self {
let mut base = Self::default();
for (param, value) in params {
match param.as_ref() {
$( $crate::in_fix!($param) => base.$param = *value ),+,
p => unreachable!("Unknown parameter: {}", p),
}
}
base.logger = $crate::logging::new_sublogger(&name);
base.name = name;
base.$flag = $flag;
base
}

#[allow(non_snake_case)]
pub fn from_constants($( $param: u64 ),+, name: calyx_ir::Id, $flag: bool) -> Self {
$name {
$($param),+,
logger: $crate::logging::new_sublogger(&name),
name,
$flag
}
}
}

impl $crate::primitives::Named for $name {
fn get_full_name(&self) -> &calyx_ir::Id {
&self.name
}
}


impl $crate::primitives::Primitive for $name {

//null-op; comb don't use do_tick()
fn do_tick(&mut self) -> $crate::errors::InterpreterResult<Vec<(calyx_ir::Id, $crate::values::Value)>>{
Ok(vec![])
}

fn is_comb(&self) -> bool { true }

fn validate(
&self,
inputs: &[(calyx_ir::Id, &$crate::values::Value)]
) {
for (id, v) in inputs {
match id.as_ref() {
$( $crate::in_fix!($port) => assert_eq!(v.len() as u64, self.$width) ),+,
p => unreachable!("Unknown port: {}", p),
}
}
}

#[allow(non_snake_case,unused)]
fn execute(
&mut self,
inputs: &[(calyx_ir::Id, &$crate::values::Value)],
) -> $crate::errors::InterpreterResult<Vec<(calyx_ir::Id, $crate::values::Value)>> {

#[derive(Default)]
struct Ports<'a> {
$( $port: Option<&'a $crate::values::Value> ),+
}

let mut base = Ports::default();

for (id, v) in inputs {
match id.as_ref() {
$( $crate::in_fix!($port) => base.$port = Some(v) ),+,
p => unreachable!("Unknown port: {}", p),
}
}

let exec_func = |$($param: u64),+, $( $port: &Value ),+, $log: &$crate::logging::Logger, $full_name:&calyx_ir::Id, $flag: bool| -> $crate::errors::InterpreterResult<Value> {
$execute
};

#[allow(unused_parens)]
let ($( $out ),+) = exec_func(
$(self.$param),+,
$( base
.$port
.expect(&format!("No value for port: {}", $crate::in_fix!($port)).to_string()) ),+,
&self.logger,
$crate::primitives::Named::get_full_name(self),
self.$flag
)?;

return Ok(vec![
$( ($crate::in_fix!($out).into(), $out) ),+
])

}

// Combinational components cannot be reset
fn reset(
&mut self,
inputs: &[(calyx_ir::Id, &$crate::values::Value)],
) -> $crate::errors::InterpreterResult<Vec<(calyx_ir::Id, $crate::values::Value)>> {
self.execute(inputs)
}

}
};
}

#[macro_export]
/// Internal macro used to homogenize representation for raw identifiers in
/// port names.
Expand Down

0 comments on commit 48806db

Please sign in to comment.