|
| 1 | +use calyx_libm_dfg as dfg; |
| 2 | +use cranelift_entity::PrimaryMap; |
| 3 | +use dfg::*; |
| 4 | +use std::ops::{Index, IndexMut}; |
| 5 | +/* |
| 6 | +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq)] |
| 7 | +pub struct BuilderEdgeId(u32); |
| 8 | +entity_impl!(BuilderEdgeId, "be"); |
| 9 | +*/ |
| 10 | + |
| 11 | +pub struct DfgBuilder { |
| 12 | + nodes: PrimaryMap<NodeId, Node>, |
| 13 | + edges: PrimaryMap<EdgeId, Edge>, |
| 14 | +} |
| 15 | + |
| 16 | +impl DfgBuilder { |
| 17 | + pub fn new() -> Self { |
| 18 | + Self { |
| 19 | + nodes: PrimaryMap::new(), |
| 20 | + edges: PrimaryMap::new(), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn finish(self) -> Dfg { |
| 25 | + let mut dfg: Dfg = Dfg::with_nodes(self.nodes); |
| 26 | + |
| 27 | + for edge in self.edges.values() { |
| 28 | + let n_out = edge.output; |
| 29 | + let n_in = edge.input; |
| 30 | + dfg.add_edge(Edge { |
| 31 | + input: n_in, |
| 32 | + output: n_out, |
| 33 | + props: edge.props.clone(), |
| 34 | + edge_type: edge.edge_type.clone(), |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + dfg |
| 39 | + } |
| 40 | + |
| 41 | + pub fn add_node(&mut self, node: Node) -> NodeId { |
| 42 | + self.nodes.push(node) |
| 43 | + } |
| 44 | + |
| 45 | + pub fn connect_nodes( |
| 46 | + &mut self, |
| 47 | + input: NodeId, |
| 48 | + output: NodeId, |
| 49 | + props: Vec<String>, |
| 50 | + edge_type: Type, |
| 51 | + ) -> EdgeId { |
| 52 | + self.edges.push(Edge { |
| 53 | + input, |
| 54 | + output, |
| 55 | + props, |
| 56 | + edge_type, |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +macro_rules! dfg_index_impl { |
| 62 | + ($field:ident, $idx:ty, $out:ty) => { |
| 63 | + impl Index<$idx> for DfgBuilder { |
| 64 | + type Output = $out; |
| 65 | + |
| 66 | + #[inline] |
| 67 | + fn index(&self, index: $idx) -> &Self::Output { |
| 68 | + &self.$field[index] |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + impl IndexMut<$idx> for DfgBuilder { |
| 73 | + #[inline] |
| 74 | + fn index_mut(&mut self, index: $idx) -> &mut Self::Output { |
| 75 | + &mut self.$field[index] |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +dfg_index_impl!(nodes, NodeId, Node); |
0 commit comments