1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use super::Attributes;
use crate::Attribute;
use calyx_utils::{CalyxResult, Error, GetName, Id};
use linked_hash_map::LinkedHashMap;
use smallvec::SmallVec;
#[derive(Clone, Debug)]
pub struct Primitive {
pub name: Id,
pub params: Vec<Id>,
pub signature: Vec<PortDef<Width>>,
pub attributes: Attributes,
pub is_comb: bool,
pub latency: Option<std::num::NonZeroU64>,
pub body: Option<String>,
}
impl Primitive {
#[allow(clippy::type_complexity)]
pub fn resolve(
&self,
parameters: &[u64],
) -> CalyxResult<(SmallVec<[(Id, u64); 5]>, Vec<PortDef<u64>>)> {
if self.params.len() != parameters.len() {
let msg = format!(
"primitive `{}` requires {} parameters but instantiation provides {} parameters",
self.name.clone(),
self.params.len(),
parameters.len(),
);
return Err(Error::malformed_structure(msg));
}
let bindings = self
.params
.iter()
.cloned()
.zip(parameters.iter().cloned())
.collect::<LinkedHashMap<Id, u64>>();
let ports = self
.signature
.iter()
.cloned()
.map(|pd| pd.resolve(&bindings))
.collect::<Result<_, _>>()?;
Ok((bindings.into_iter().collect(), ports))
}
pub fn find_all_with_attr<A>(
&self,
attr: A,
) -> impl Iterator<Item = &PortDef<Width>>
where
A: Into<Attribute> + Copy,
{
self.signature
.iter()
.filter(move |&g| g.attributes.has(attr))
}
}
impl GetName for Primitive {
fn name(&self) -> Id {
self.name
}
}
#[derive(Clone, Debug)]
pub struct PortDef<W> {
name: Id,
pub width: W,
pub direction: Direction,
pub attributes: Attributes,
}
impl<W> PortDef<W> {
pub fn new(
name: impl Into<Id>,
width: W,
direction: Direction,
attributes: Attributes,
) -> Self {
assert!(
matches!(direction, Direction::Input | Direction::Output),
"Direction must be either Input or Output"
);
Self {
name: name.into(),
width,
direction,
attributes,
}
}
pub fn name(&self) -> Id {
self.name
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Width {
Const { value: u64 },
Param { value: Id },
}
impl std::fmt::Display for Width {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Width::Const { value } => write!(f, "{}", value),
Width::Param { value } => write!(f, "{}", value),
}
}
}
impl From<u64> for Width {
fn from(value: u64) -> Self {
Width::Const { value }
}
}
impl From<Id> for Width {
fn from(value: Id) -> Self {
Width::Param { value }
}
}
impl PortDef<Width> {
pub fn resolve(
self,
binding: &LinkedHashMap<Id, u64>,
) -> CalyxResult<PortDef<u64>> {
match &self.width {
Width::Const { value } => Ok(PortDef {
name: self.name,
width: *value,
attributes: self.attributes,
direction: self.direction,
}),
Width::Param { value } => match binding.get(value) {
Some(width) => Ok(PortDef {
name: self.name,
width: *width,
attributes: self.attributes,
direction: self.direction,
}),
None => {
let param_name = &self.name;
let msg = format!("Failed to resolve: {param_name}");
Err(Error::malformed_structure(msg))
}
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub enum Direction {
Input,
Output,
Inout,
}
impl Direction {
pub fn reverse(&self) -> Self {
match self {
Direction::Input => Direction::Output,
Direction::Output => Direction::Input,
Direction::Inout => Direction::Inout,
}
}
}