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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//! IR Builder. Provides convience methods to build various parts of the internal
//! representation.
use crate::{self as ir, LibrarySignatures, Nothing, RRC, WRC};
use calyx_frontend::BoolAttr;
use calyx_utils::CalyxResult;
use std::rc::Rc;

use super::{CellType, PortDef};

/// IR builder.
/// Uses internal references to the component to construct and validate
/// constructs when needed.
/// By default, assumes that the cells are being added by a pass and marks
/// them with the `@generated` attribute.
///
/// In order to disable this behavior, call [[ir::Builder::not_generated()]].
pub struct Builder<'a> {
    /// Component for which this builder is constructing.
    pub component: &'a mut ir::Component,
    /// Library signatures.
    lib: &'a LibrarySignatures,
    /// Enable validation of components.
    /// Useful for debugging malformed AST errors.
    validate: bool,
    /// Cells added are generated during a compiler pass.
    generated: bool,
}

impl<'a> Builder<'a> {
    /// Instantiate a new builder using for a component.
    pub fn new(
        component: &'a mut ir::Component,
        lib: &'a LibrarySignatures,
    ) -> Self {
        Self {
            component,
            lib,
            validate: false,
            // By default, assume that builder is called from a pass
            generated: true,
        }
    }

    /// Enable the validation flag on a builder.
    pub fn validate(mut self) -> Self {
        self.validate = true;
        self
    }

    /// Disable the generated flag on the builder
    pub fn not_generated(mut self) -> Self {
        self.generated = false;
        self
    }

    pub fn add_continuous_assignments(
        &mut self,
        assigns: Vec<ir::Assignment<Nothing>>,
    ) {
        self.component.continuous_assignments.extend(assigns);
    }

    /// Construct a new group and add it to the Component.
    /// The group is guaranteed to start with `prefix`.
    /// Returns a reference to the group.
    pub fn add_group<S>(&mut self, prefix: S) -> RRC<ir::Group>
    where
        S: Into<ir::Id>,
    {
        let prefix: ir::Id = prefix.into();
        assert!(
            prefix != "",
            "Cannot construct group with empty name prefix"
        );
        let name = self.component.generate_name(prefix);

        // Check if there is a group with the same name.
        let group = ir::rrc(ir::Group::new(name));

        // Add default holes to the group.
        for (name, width) in &[("go", 1), ("done", 1)] {
            let hole = ir::rrc(ir::Port {
                name: ir::Id::from(*name),
                width: *width,
                direction: ir::Direction::Inout,
                parent: ir::PortParent::Group(WRC::from(&group)),
                attributes: ir::Attributes::default(),
            });
            group.borrow_mut().holes.push(hole);
        }

        // Add the group to the component.
        self.component.get_groups_mut().add(Rc::clone(&group));

        group
    }

    /// Construct a new static group and add it to the Component.
    /// The group is guaranteed to start with `prefix`.
    /// Returns a reference to the group.
    pub fn add_static_group<S>(
        &mut self,
        prefix: S,
        latency: u64,
    ) -> RRC<ir::StaticGroup>
    where
        S: Into<ir::Id>,
    {
        let prefix: ir::Id = prefix.into();
        assert!(
            prefix != "",
            "Cannot construct group with empty name prefix"
        );
        let name = self.component.generate_name(prefix);

        // Check if there is a group with the same name.
        let group = ir::rrc(ir::StaticGroup::new(name, latency));

        // Add default holes to the group.
        // Static Groups don't need a done hole.
        // May be beneficial to have a go hole, though (although maybe not)
        let (name, width) = ("go", 1);
        let hole = ir::rrc(ir::Port {
            name: ir::Id::from(name),
            width,
            direction: ir::Direction::Inout,
            parent: ir::PortParent::StaticGroup(WRC::from(&group)),
            attributes: ir::Attributes::default(),
        });
        group.borrow_mut().holes.push(hole);

        // Add the group to the component.
        self.component
            .get_static_groups_mut()
            .add(Rc::clone(&group));

        group
    }

    /// Construct a combinational group
    pub fn add_comb_group<S>(&mut self, prefix: S) -> RRC<ir::CombGroup>
    where
        S: Into<ir::Id> + ToString + Clone,
    {
        let name = self.component.generate_name(prefix);

        // Check if there is a group with the same name.
        let group = ir::rrc(ir::CombGroup {
            name,
            attributes: ir::Attributes::default(),
            assignments: vec![],
        });

        // Add the group to the component.
        self.component.comb_groups.add(Rc::clone(&group));

        group
    }

    /// Return reference for a constant cell associated with the (val, width)
    /// pair, building and adding it to the component if needed..
    /// If the constant does not exist, it is added to the Context.
    pub fn add_constant(&mut self, val: u64, width: u64) -> RRC<ir::Cell> {
        // Ensure that the value can fit within the width
        assert!(
            // This calculates the position of the most significant 1 bit which
            // tells us the minimum number of bits required to represent the
            // constant. Note that this will not work for constants that require
            // more than 64 bits as those currently cannot be parsed
            (64 - val.leading_zeros()) as u64 <= width,
            "Constant value {} cannot fit in {} bits",
            val,
            width
        );
        let name = ir::Cell::constant_name(val, width);
        // If this constant has already been instantiated, return the relevant
        // cell.
        if let Some(cell) = self.component.cells.find(name) {
            return Rc::clone(&cell);
        }

        // Construct this cell if it's not already present in the context.
        let cell = Self::cell_from_signature(
            name,
            ir::CellType::Constant { val, width },
            vec![ir::PortDef::new(
                ir::Id::from("out"),
                width,
                ir::Direction::Output,
                ir::Attributes::default(),
            )],
        );

        // Add constant to the Component.
        self.component.cells.add(Rc::clone(&cell));

        cell
    }

    /// Consturcts a primitive cell of type `primitive`.
    /// The name of the cell is guaranteed to start with `prefix`.
    /// Adds this cell to the underlying component and returns a reference
    /// to the Cell.
    ///
    /// For example:
    /// ```
    /// // Construct a std_reg.
    /// builder.add_primitive("fsm", "std_reg", vec![32]);
    /// ```
    pub fn add_primitive<Pre, Prim>(
        &mut self,
        prefix: Pre,
        primitive: Prim,
        param_values: &[u64],
    ) -> RRC<ir::Cell>
    where
        Pre: Into<ir::Id> + ToString + Clone,
        Prim: Into<ir::Id>,
    {
        self.try_add_primitive(prefix, primitive, param_values)
            .expect("failed to add primitive:")
    }

    /// Result variant of [[ir::Builder::add_primitive()]].
    pub fn try_add_primitive<Pre, Prim>(
        &mut self,
        prefix: Pre,
        primitive: Prim,
        param_values: &[u64],
    ) -> CalyxResult<RRC<ir::Cell>>
    where
        Pre: Into<ir::Id> + ToString + Clone,
        Prim: Into<ir::Id>,
    {
        let prim_id = primitive.into();
        let prim = &self.lib.get_primitive(prim_id);
        let (param_binding, ports) = prim.resolve(param_values)?;

        let name = self.component.generate_name(prefix);
        let cell = Self::cell_from_signature(
            name,
            ir::CellType::Primitive {
                name: prim_id,
                param_binding: Box::new(param_binding),
                is_comb: prim.is_comb,
                latency: prim.latency,
            },
            ports,
        );
        if self.generated {
            cell.borrow_mut().add_attribute(BoolAttr::Generated, 1);
        }
        self.component.cells.add(Rc::clone(&cell));
        Ok(cell)
    }

    /// Add a component instance to this component using its name and port
    /// signature.
    pub fn add_component<Pre>(
        &mut self,
        prefix: Pre,
        component: Pre,
        sig: Vec<PortDef<u64>>,
    ) -> RRC<ir::Cell>
    where
        Pre: Into<ir::Id> + ToString + Clone,
    {
        let name = self.component.generate_name(prefix);
        let cell = Self::cell_from_signature(
            name,
            CellType::Component {
                name: component.into(),
            },
            sig,
        );
        if self.generated {
            cell.borrow_mut().add_attribute(BoolAttr::Generated, 1);
        }
        self.component.cells.add(Rc::clone(&cell));
        cell
    }

    /// Construct an assignment.
    pub fn build_assignment<T>(
        &self,
        dst: RRC<ir::Port>,
        src: RRC<ir::Port>,
        guard: ir::Guard<T>,
    ) -> ir::Assignment<T> {
        // Valid the ports if required.
        if self.validate {
            self.is_port_well_formed(&dst.borrow());
            self.is_port_well_formed(&src.borrow());
            guard
                .all_ports()
                .into_iter()
                .for_each(|p| self.is_port_well_formed(&p.borrow()));
        }
        // If the ports have different widths, error out.
        debug_assert!(
            src.borrow().width == dst.borrow().width,
            "Invalid assignment. `{}.{}' and `{}.{}' have different widths",
            src.borrow().get_parent_name(),
            src.borrow().name,
            dst.borrow().get_parent_name(),
            dst.borrow().name,
        );
        // If ports have the wrong directions, error out.
        debug_assert!(
            // Allow for both Input and Inout ports.
            src.borrow().direction != ir::Direction::Input,
            "Not an ouput port: {}.{}",
            src.borrow().get_parent_name(),
            src.borrow().name
        );
        debug_assert!(
            // Allow for both Input and Inout ports.
            dst.borrow().direction != ir::Direction::Output,
            "Not an input port: {}.{}",
            dst.borrow().get_parent_name(),
            dst.borrow().name
        );

        ir::Assignment {
            dst,
            src,
            guard: Box::new(guard),
            attributes: ir::Attributes::default(),
        }
    }

    ///////////////////// Internal functions/////////////////////////////////
    /// VALIDATE: Check if the component contains the cell/group associated
    /// with the port exists in the Component.
    /// Validate methods panic! in order to generate a stacktrace to the
    /// offending code.
    fn is_port_well_formed(&self, port: &ir::Port) {
        match &port.parent {
            ir::PortParent::Cell(cell_wref) => {
                let cell_ref = cell_wref.internal.upgrade().expect("Weak reference to port's parent cell points to nothing. This usually means that the Component did not retain a pointer to the Cell.");

                let cell = &cell_ref.borrow();
                self.component.find_cell(cell.name()).expect("Port's parent cell not present in the component. Add the cell to the component before using the Port.");
            }
            ir::PortParent::Group(group_wref) => {
                let group_ref = group_wref.internal.upgrade().expect("Weak reference to hole's parent group points to nothing. This usually means that the Component did not retain a pointer to the Group.");

                let group = &group_ref.borrow();
                self.component.find_group(group.name()).expect("Hole's parent cell not present in the component. Add the group to the component before using the Hole.");
            }
            ir::PortParent::StaticGroup(group_wref) => {
                let group_ref = group_wref.internal.upgrade().expect("Weak reference to hole's parent group points to nothing. This usually means that the Component did not retain a pointer to the Group.");

                let group = &group_ref.borrow();
                self.component.find_static_group(group.name()).expect("Hole's parent cell not present in the component. Add the static group to the component before using the Hole.");
            }
        };
    }
    /// Construct a cell from input/output signature.
    /// Input and output port definition in the form (name, width).
    pub(super) fn cell_from_signature(
        name: ir::Id,
        typ: ir::CellType,
        ports: Vec<ir::PortDef<u64>>,
    ) -> RRC<ir::Cell> {
        let cell = ir::rrc(ir::Cell::new(name, typ));
        ports.into_iter().for_each(|pd| {
            let port = ir::rrc(ir::Port {
                name: pd.name(),
                width: pd.width,
                direction: pd.direction,
                parent: ir::PortParent::Cell(WRC::from(&cell)),
                attributes: pd.attributes,
            });
            cell.borrow_mut().ports.push(port);
        });
        cell
    }
}