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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::rc::Rc;

use super::{Attributes, Cell, CombGroup, GetAttributes, Group, Id, Port, RRC};

/// Data for the `seq` control statement.
#[derive(Debug)]
pub struct Seq {
    /// List of `Control` statements to run in sequence.
    pub stmts: Vec<Control>,
    /// Attributes attached to this control statement.
    pub attributes: Attributes,
}

/// Data for the `par` control statement.
#[derive(Debug)]
pub struct Par {
    /// List of `Control` statements to run in parallel.
    pub stmts: Vec<Control>,
    /// Attributes attached to this control statement.
    pub attributes: Attributes,
}

/// Data for the `if` control statement.
#[derive(Debug)]
pub struct If {
    /// Port that connects the conditional check.
    pub port: RRC<Port>,

    /// Optional combinational group attached using `with`.
    pub cond: Option<RRC<CombGroup>>,

    /// Control for the true branch.
    pub tbranch: Box<Control>,

    /// Control for the false branch.
    pub fbranch: Box<Control>,

    /// Attributes attached to this control statement.
    pub attributes: Attributes,
}

/// Data for the `if` control statement.
#[derive(Debug)]
pub struct While {
    /// Port that connects the conditional check.
    pub port: RRC<Port>,

    /// Group that makes the signal on the conditional port valid.
    pub cond: Option<RRC<CombGroup>>,

    /// Control for the loop body.
    pub body: Box<Control>,
    /// Attributes attached to this control statement.
    pub attributes: Attributes,
}

/// Data for the `enable` control statement.
#[derive(Debug)]
pub struct Enable {
    /// List of components to run.
    pub group: RRC<Group>,
    /// Attributes attached to this control statement.
    pub attributes: Attributes,
}

type PortMap = Vec<(Id, RRC<Port>)>;
type CellMap = Vec<(Id, RRC<Cell>)>;

/// Data for an `invoke` control statement.
#[derive(Debug)]
pub struct Invoke {
    /// Cell that is being invoked.
    pub comp: RRC<Cell>,
    /// Mapping from name of input ports in `comp` to the port connected to it.
    pub inputs: PortMap,
    /// Mapping from name of output ports in `comp` to the port connected to it.
    pub outputs: PortMap,
    /// Attributes attached to this control statement.
    pub attributes: Attributes,
    /// Optional combinational group that is active when the invoke is active.
    pub comb_group: Option<RRC<CombGroup>>,
    /// Mapping from name of external cell in 'comp' to the cell connected to it.
    pub ref_cells: CellMap,
}

/// Data for the `empty` control statement.
#[derive(Debug)]
pub struct Empty {}

/// Control AST nodes.
#[derive(Debug)]
pub enum Control {
    /// Represents sequential composition of control statements.
    Seq(Seq),
    /// Represents parallel composition of control statements.
    Par(Par),
    /// Standard imperative if statement
    If(If),
    /// Standard imperative while statement
    While(While),
    /// Invoke a sub-component with the given port assignments
    Invoke(Invoke),
    /// Runs the control for a list of subcomponents.
    Enable(Enable),
    /// Control statement that does nothing.
    Empty(Empty),
}

impl GetAttributes for Control {
    fn get_mut_attributes(&mut self) -> Option<&mut Attributes> {
        match self {
            Self::Seq(Seq { attributes, .. })
            | Self::Par(Par { attributes, .. })
            | Self::If(If { attributes, .. })
            | Self::While(While { attributes, .. })
            | Self::Invoke(Invoke { attributes, .. })
            | Self::Enable(Enable { attributes, .. }) => Some(attributes),
            Self::Empty(..) => None,
        }
    }

    fn get_attributes(&self) -> Option<&Attributes> {
        match self {
            Self::Seq(Seq { attributes, .. })
            | Self::Par(Par { attributes, .. })
            | Self::If(If { attributes, .. })
            | Self::While(While { attributes, .. })
            | Self::Invoke(Invoke { attributes, .. })
            | Self::Enable(Enable { attributes, .. }) => Some(attributes),
            Self::Empty(..) => None,
        }
    }
}

impl Control {
    // ================ Constructor methods ================
    /// Convience constructor for empty.
    pub fn empty() -> Self {
        Control::Empty(Empty {})
    }

    /// Convience constructor for seq.
    pub fn seq(stmts: Vec<Control>) -> Self {
        Control::Seq(Seq {
            stmts,
            attributes: Attributes::default(),
        })
    }

    /// Convience constructor for par.
    pub fn par(stmts: Vec<Control>) -> Self {
        Control::Par(Par {
            stmts,
            attributes: Attributes::default(),
        })
    }

    /// Convience constructor for enable.
    pub fn enable(group: RRC<Group>) -> Self {
        Control::Enable(Enable {
            group,
            attributes: Attributes::default(),
        })
    }

    /// Convience constructor for invoke.
    pub fn invoke(comp: RRC<Cell>, inputs: PortMap, outputs: PortMap) -> Self {
        Control::Invoke(Invoke {
            comp,
            inputs,
            outputs,
            attributes: Attributes::default(),
            comb_group: None,
            ref_cells: Vec::new(),
        })
    }

    /// Convience constructor for if
    pub fn if_(
        port: RRC<Port>,
        cond: Option<RRC<CombGroup>>,
        tbranch: Box<Control>,
        fbranch: Box<Control>,
    ) -> Self {
        Control::If(If {
            port,
            cond,
            tbranch,
            fbranch,
            attributes: Attributes::default(),
        })
    }

    /// Convience constructor for while
    pub fn while_(
        port: RRC<Port>,
        cond: Option<RRC<CombGroup>>,
        body: Box<Control>,
    ) -> Self {
        Control::While(While {
            port,
            cond,
            body,
            attributes: Attributes::default(),
        })
    }

    /// Returns the value of an attribute if present
    pub fn get_attribute<S>(&self, attr: S) -> Option<&u64>
    where
        S: std::fmt::Display + AsRef<str>,
    {
        self.get_attributes().and_then(|attrs| attrs.get(attr))
    }

    /// Returns true if the node has a specific attribute
    pub fn has_attribute<S>(&self, attr: S) -> bool
    where
        S: std::fmt::Display + AsRef<str>,
    {
        self.get_attributes()
            .map(|attrs| attrs.has(attr))
            .unwrap_or(false)
    }
}

impl Control {
    /// Associated clone method the control program. We don't define this using the
    /// [Clone] trait because cloning control is not very common and clones
    /// should be explicit.
    #[allow(clippy::should_implement_trait)]
    pub fn clone(con: &Control) -> Control {
        match con {
            Control::Seq(Seq { stmts, attributes }) => Control::Seq(Seq {
                stmts: stmts.iter().map(Control::clone).collect(),
                attributes: attributes.clone(),
            }),
            Control::Par(Par { stmts, attributes }) => Control::Par(Par {
                stmts: stmts.iter().map(Control::clone).collect(),
                attributes: attributes.clone(),
            }),
            Control::If(If {
                port,
                cond,
                tbranch,
                fbranch,
                attributes,
            }) => Control::If(If {
                port: Rc::clone(port),
                cond: cond.clone().map(|cg| Rc::clone(&cg)),
                tbranch: Box::new(Control::clone(tbranch)),
                fbranch: Box::new(Control::clone(fbranch)),
                attributes: attributes.clone(),
            }),
            Control::While(While {
                port,
                cond,
                body,
                attributes,
            }) => Control::While(While {
                port: Rc::clone(port),
                cond: cond.clone().map(|cg| Rc::clone(&cg)),
                body: Box::new(Control::clone(body)),
                attributes: attributes.clone(),
            }),
            Control::Invoke(Invoke {
                comp,
                inputs,
                outputs,
                attributes,
                comb_group,
                ref_cells,
            }) => Control::Invoke(Invoke {
                comp: Rc::clone(comp),
                inputs: inputs
                    .iter()
                    .map(|(name, port)| (name.clone(), Rc::clone(port)))
                    .collect(),
                outputs: outputs
                    .iter()
                    .map(|(name, port)| (name.clone(), Rc::clone(port)))
                    .collect(),
                comb_group: comb_group.clone().map(|cg| Rc::clone(&cg)),
                attributes: attributes.clone(),
                ref_cells: ref_cells
                    .iter()
                    .map(|(outcell, incell)| {
                        (outcell.clone(), Rc::clone(incell))
                    })
                    .collect(),
            }),
            Control::Enable(Enable { group, attributes }) => {
                Control::Enable(Enable {
                    group: Rc::clone(group),
                    attributes: attributes.clone(),
                })
            }
            Control::Empty(_) => Control::empty(),
        }
    }
}