calyx_ir/
guard.rs

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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
use crate::Printer;

use super::{Builder, Cell, NumAttr, Port, RRC};
use calyx_utils::Error;
use itertools::Itertools;
use std::collections::HashSet;
use std::fmt::{Debug, Display};
use std::mem;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
use std::{cmp::Ordering, hash::Hash, rc::Rc};

#[derive(Debug, Clone, Default, Eq, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub struct Nothing;

impl Display for Nothing {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "")
    }
}

/// Comparison operations that can be performed between ports by [Guard::CompOp].
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub enum PortComp {
    /// p1 == p2
    Eq,
    /// p1 != p2
    Neq,
    /// p1 > p2
    Gt,
    /// p1 < p2
    Lt,
    /// p1 >= p2
    Geq,
    /// p1 <= p2
    Leq,
}

/// An assignment guard which has pointers to the various ports from which it reads.
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub enum Guard<T> {
    /// Represents `c1 || c2`.
    Or(Box<Guard<T>>, Box<Guard<T>>),
    /// Represents `c1 && c2`.
    And(Box<Guard<T>>, Box<Guard<T>>),
    /// Represents `!c1`
    Not(Box<Guard<T>>),
    #[default]
    /// The constant true
    True,
    /// Comparison operator.
    CompOp(PortComp, RRC<Port>, RRC<Port>),
    /// Uses the value on a port as the condition. Same as `p1 == true`
    Port(RRC<Port>),
    /// Other types of information.
    Info(T),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub struct StaticTiming {
    interval: (u64, u64),
}

impl Display for StaticTiming {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.interval.0 + 1 == self.interval.1 {
            write!(f, "%{}", self.interval.0)
        } else {
            write!(f, "%[{}:{}]", self.interval.0, self.interval.1)
        }
    }
}

impl StaticTiming {
    /// creates a new `StaticTiming` struct
    pub fn new(interval: (u64, u64)) -> Self {
        StaticTiming { interval }
    }

    /// returns the (u64, u64) interval for `struct`
    pub fn get_interval(&self) -> (u64, u64) {
        self.interval
    }

    /// overwrites the current `interval` to be `new_interval`
    pub fn set_interval(&mut self, new_interval: (u64, u64)) {
        self.interval = new_interval;
    }
}

impl<T> Hash for Guard<T>
where
    T: ToString,
{
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            Guard::Or(l, r) | Guard::And(l, r) => {
                l.hash(state);
                r.hash(state)
            }
            Guard::CompOp(_, l, r) => {
                l.borrow().name.hash(state);
                l.borrow().get_parent_name().hash(state);
                r.borrow().name.hash(state);
                r.borrow().get_parent_name().hash(state);
            }
            Guard::Not(inner) => inner.hash(state),
            Guard::Port(p) => {
                p.borrow().name.hash(state);
                p.borrow().get_parent_name().hash(state);
            }
            Guard::True => {}
            Guard::Info(i) => i.to_string().hash(state),
        }
    }
}

impl From<Guard<Nothing>> for Guard<StaticTiming> {
    /// Turns a normal guard into a static guard
    fn from(g: Guard<Nothing>) -> Self {
        match g {
            Guard::Or(left, right) => {
                let l = Self::from(*left);
                let r = Self::from(*right);
                Guard::Or(Box::new(l), Box::new(r))
            }
            Guard::And(left, right) => {
                let l = Self::from(*left);
                let r = Self::from(*right);
                Guard::And(Box::new(l), Box::new(r))
            }
            Guard::Not(c) => {
                let inside = Self::from(*c);
                Guard::Not(Box::new(inside))
            }
            Guard::True => Guard::True,
            Guard::CompOp(pc, left, right) => Guard::CompOp(pc, left, right),
            Guard::Port(p) => Guard::Port(p),
            Guard::Info(_) => {
                unreachable!(
                    "{:?}: Guard<Nothing> should not be of the
                info variant type",
                    g
                )
            }
        }
    }
}

impl<T> Guard<T> {
    /// Returns true definitely `Guard::True`.
    /// Returning false does not mean that the guard is not true.
    pub fn is_true(&self) -> bool {
        match self {
            Guard::True => true,
            Guard::Port(p) => p.borrow().is_constant(1, 1),
            _ => false,
        }
    }

    /// Checks if the guard is always false.
    /// Returning false does not mean that the guard is not false.
    pub fn is_false(&self) -> bool {
        match self {
            Guard::Not(g) => g.is_true(),
            _ => false,
        }
    }

    /// returns true if the self is !cell_name, false otherwise.
    pub fn is_not_done(&self, cell_name: &crate::Id) -> bool {
        if let Guard::Not(g) = self {
            if let Guard::Port(port) = &(**g) {
                return port.borrow().attributes.has(NumAttr::Done)
                    && port.borrow().get_parent_name() == cell_name;
            }
        }
        false
    }

    /// Update the guard in place. Replaces this guard with `upd(self)`.
    /// Uses `std::mem::take` for the in-place update.
    #[inline(always)]
    pub fn update<F>(&mut self, upd: F)
    where
        F: FnOnce(Guard<T>) -> Guard<T>,
    {
        let old = mem::take(self);
        let new = upd(old);
        *self = new;
    }

    /// Return the string corresponding to the guard operation.
    pub fn op_str(&self) -> String {
        match self {
            Guard::And(..) => "&".to_string(),
            Guard::Or(..) => "|".to_string(),
            Guard::CompOp(op, _, _) => match op {
                PortComp::Eq => "==".to_string(),
                PortComp::Neq => "!=".to_string(),
                PortComp::Gt => ">".to_string(),
                PortComp::Lt => "<".to_string(),
                PortComp::Geq => ">=".to_string(),
                PortComp::Leq => "<=".to_string(),
            },
            Guard::Not(..) => "!".to_string(),
            Guard::Port(..) | Guard::True | Guard::Info(_) => {
                panic!("No operator string for Guard::Port/True/Info")
            }
        }
    }

    pub fn port(p: RRC<Port>) -> Self {
        if p.borrow().is_constant(1, 1) {
            Guard::True
        } else {
            Guard::Port(p)
        }
    }

    pub fn and(self, rhs: Guard<T>) -> Self
    where
        T: Eq,
    {
        if rhs == Guard::True {
            self
        } else if self == Guard::True {
            rhs
        } else if self == rhs {
            self
        } else {
            Guard::And(Box::new(self), Box::new(rhs))
        }
    }

    pub fn or(self, rhs: Guard<T>) -> Self
    where
        T: Eq,
    {
        match (self, rhs) {
            (Guard::True, _) | (_, Guard::True) => Guard::True,
            (Guard::Not(n), g) | (g, Guard::Not(n)) => {
                if *n == Guard::True {
                    g
                } else {
                    Guard::Or(Box::new(Guard::Not(n)), Box::new(g))
                }
            }
            (l, r) => {
                if l == r {
                    l
                } else {
                    Guard::Or(Box::new(l), Box::new(r))
                }
            }
        }
    }

    pub fn eq(self, other: Guard<T>) -> Self
    where
        T: Debug + Eq + ToString,
    {
        match (self, other) {
            (Guard::Port(l), Guard::Port(r)) => {
                Guard::CompOp(PortComp::Eq, l, r)
            }
            (l, r) => {
                unreachable!(
                    "Cannot build Guard::Eq using `{}' and `{}'",
                    Printer::guard_str(&l),
                    Printer::guard_str(&r),
                )
            }
        }
    }

    pub fn neq(self, other: Guard<T>) -> Self
    where
        T: Debug + Eq + ToString,
    {
        match (self, other) {
            (Guard::Port(l), Guard::Port(r)) => {
                Guard::CompOp(PortComp::Neq, l, r)
            }
            (l, r) => {
                unreachable!(
                    "Cannot build Guard::Eq using `{}' and `{}'",
                    Printer::guard_str(&l),
                    Printer::guard_str(&r),
                )
            }
        }
    }

    pub fn le(self, other: Guard<T>) -> Self
    where
        T: Debug + Eq + ToString,
    {
        match (self, other) {
            (Guard::Port(l), Guard::Port(r)) => {
                Guard::CompOp(PortComp::Leq, l, r)
            }
            (l, r) => {
                unreachable!(
                    "Cannot build Guard::Eq using `{}' and `{}'",
                    Printer::guard_str(&l),
                    Printer::guard_str(&r),
                )
            }
        }
    }

    pub fn lt(self, other: Guard<T>) -> Self
    where
        T: Debug + Eq + ToString,
    {
        match (self, other) {
            (Guard::Port(l), Guard::Port(r)) => {
                Guard::CompOp(PortComp::Lt, l, r)
            }
            (l, r) => {
                unreachable!(
                    "Cannot build Guard::Eq using `{}' and `{}'",
                    Printer::guard_str(&l),
                    Printer::guard_str(&r),
                )
            }
        }
    }

    pub fn ge(self, other: Guard<T>) -> Self
    where
        T: Debug + Eq + ToString,
    {
        match (self, other) {
            (Guard::Port(l), Guard::Port(r)) => {
                Guard::CompOp(PortComp::Geq, l, r)
            }
            (l, r) => {
                unreachable!(
                    "Cannot build Guard::Eq using `{}' and `{}'",
                    Printer::guard_str(&l),
                    Printer::guard_str(&r),
                )
            }
        }
    }

    pub fn gt(self, other: Guard<T>) -> Self
    where
        T: Debug + Eq + ToString,
    {
        match (self, other) {
            (Guard::Port(l), Guard::Port(r)) => {
                Guard::CompOp(PortComp::Gt, l, r)
            }
            (l, r) => {
                unreachable!(
                    "Cannot build Guard::Eq using `{}' and `{}'",
                    Printer::guard_str(&l),
                    Printer::guard_str(&r),
                )
            }
        }
    }

    /// Returns all the ports used by this guard.
    pub fn all_ports(&self) -> Vec<RRC<Port>> {
        match self {
            Guard::Port(a) => vec![Rc::clone(a)],
            Guard::And(l, r) | Guard::Or(l, r) => {
                let mut atoms = l.all_ports();
                atoms.append(&mut r.all_ports());
                atoms
            }
            Guard::CompOp(_, l, r) => {
                vec![Rc::clone(l), Rc::clone(r)]
            }
            Guard::Not(g) => g.all_ports(),
            Guard::True => vec![],
            Guard::Info(_) => vec![],
        }
    }
}

/// Helper functions for the guard.
impl<T> Guard<T> {
    /// Mutates a guard by calling `f` on every leaf in the
    /// guard tree and replacing the leaf with the guard that `f`
    /// returns.
    pub fn for_each<F>(&mut self, f: &mut F)
    where
        F: FnMut(RRC<Port>) -> Option<Guard<T>>,
    {
        match self {
            Guard::And(l, r) | Guard::Or(l, r) => {
                l.for_each(f);
                r.for_each(f);
            }
            Guard::Not(inner) => {
                inner.for_each(f);
            }
            Guard::CompOp(_, l, r) => {
                match f(Rc::clone(l)) {
                    Some(Guard::Port(p)) => *l = p,
                    Some(_) => unreachable!(
                        "Cannot replace port inside comparison operator"
                    ),
                    None => {}
                }
                match f(Rc::clone(r)) {
                    Some(Guard::Port(p)) => *r = p,
                    Some(_) => unreachable!(
                        "Cannot replace port inside comparison operator"
                    ),
                    None => {}
                }
            }
            Guard::Port(port) => {
                let guard = f(Rc::clone(port))
                    .unwrap_or_else(|| Guard::port(Rc::clone(port)));
                *self = guard;
            }
            Guard::True => {}
            Guard::Info(_) =>
                // Info shouldn't count as port
                {}
        }
    }

    /// runs f(info) on each Guard::Info in `guard`.
    /// if `f(info)` = Some(result)` replaces interval with result.
    /// if `f(info)` = None` does nothing.
    pub fn for_each_info<F>(&mut self, f: &mut F)
    where
        F: FnMut(&mut T) -> Option<Guard<T>>,
    {
        match self {
            Guard::And(l, r) | Guard::Or(l, r) => {
                l.for_each_info(f);
                r.for_each_info(f);
            }
            Guard::Not(inner) => {
                inner.for_each_info(f);
            }
            Guard::True | Guard::Port(_) | Guard::CompOp(_, _, _) => {}
            Guard::Info(timing_interval) => {
                if let Some(new_interval) = f(timing_interval) {
                    *self = new_interval
                }
            }
        }
    }

    /// runs f(info) on each info in `guard`.
    /// f should return Result<(), Error>, meaning that it essentially does
    /// nothing if the `f` returns OK(()), but returns an appropraite error otherwise
    pub fn check_for_each_info<F>(&self, f: &mut F) -> Result<(), Error>
    where
        F: Fn(&T) -> Result<(), Error>,
    {
        match self {
            Guard::And(l, r) | Guard::Or(l, r) => {
                let l_result = l.check_for_each_info(f);
                if l_result.is_err() {
                    l_result
                } else {
                    r.check_for_each_info(f)
                }
            }
            Guard::Not(inner) => inner.check_for_each_info(f),
            Guard::True | Guard::Port(_) | Guard::CompOp(_, _, _) => Ok(()),
            Guard::Info(timing_interval) => f(timing_interval),
        }
    }
}

impl Guard<StaticTiming> {
    /// updates self -> self & interval
    pub fn add_interval(&mut self, timing_interval: StaticTiming) {
        self.update(|g| g.and(Guard::Info(timing_interval)));
    }

    /// Take a static assignment guard and the latency of the group in which the assignment
    /// exists, and provide a list of states, relative to the group's latency, in which
    /// the static assignment should be valid.
    pub fn compute_live_states(&self, group_latency: u64) -> HashSet<u64> {
        match self {
            Self::True => (0..group_latency).collect(),
            Self::And(l, r) => l
                .compute_live_states(group_latency)
                .intersection(&r.compute_live_states(group_latency))
                .cloned()
                .collect(),
            Self::Or(l, r) => l
                .compute_live_states(group_latency)
                .union(&r.compute_live_states(group_latency))
                .cloned()
                .collect(),
            Self::Not(g) => {
                let dont_include = g.compute_live_states(group_latency);
                (0..group_latency)
                    .filter(|state| dont_include.contains(state))
                    .collect()
            }
            Self::Info(static_timing) => {
                let (b, e) = static_timing.interval;
                (b..e).collect()
            }
            Self::CompOp(..) | Self::Port(_) => {
                HashSet::from_iter(0..group_latency)
            }
        }
    }

    /// Replace every interval `[a1, a_n]` in a static timing guard with
    /// `counter.out == a_1 | counter.out == a_2 | ... | counter.out == a_{n-1}`
    pub fn replace_static_timing(
        &mut self,
        builder: &mut Builder,
        counter: &RRC<Cell>,
        width: &u64,
        domain: &u64,
    ) {
        match self {
            Self::True | Self::CompOp(..) | Self::Port(..) => (),
            Self::Not(g) => {
                g.replace_static_timing(builder, counter, width, domain)
            }
            Self::And(l, r) | Self::Or(l, r) => {
                l.replace_static_timing(builder, counter, width, domain);
                r.replace_static_timing(builder, counter, width, domain);
            }
            Self::Info(static_timing) => {
                let (b, e) = static_timing.get_interval();
                let interval = (b..e).collect_vec();
                let complement = (0..b).chain(e..*domain).collect_vec();
                self.update(|_| {
                    if interval.len() < complement.len() {
                        match interval.into_iter().fold(None, |acc, state| {
                            let state_const =
                                builder.add_constant(state, *width);
                            let state_guard = Self::CompOp(
                                PortComp::Eq,
                                counter.borrow().get("out"),
                                state_const.borrow().get("out"),
                            );
                            match acc {
                                None => Some(state_guard),
                                Some(existing_guard) => {
                                    Some(existing_guard.or(state_guard))
                                }
                            }
                        }) {
                            Some(g) => g,
                            None => {
                                let zero = builder.add_constant(0, 1);
                                let out_port = zero.borrow().get("out");
                                Self::port(out_port)
                            }
                        }
                    } else {
                        match complement.into_iter().fold(None, |acc, state| {
                            let state_const =
                                builder.add_constant(state, *width);
                            let state_guard = Self::CompOp(
                                PortComp::Neq,
                                counter.borrow().get("out"),
                                state_const.borrow().get("out"),
                            );
                            match acc {
                                None => Some(state_guard),
                                Some(existing_guard) => {
                                    Some(existing_guard.and(state_guard))
                                }
                            }
                        }) {
                            Some(g) => g,
                            None => Self::True,
                        }
                    }
                });
            }
        }
    }

    /// Take a static assignment guard and get rid of all static timing portions
    /// of the guard. This is useful when we know the cycles `c` at which the assignment
    /// will be active, and we can separately construct the assignment guard
    /// like `dst = c ? src` instead of `dst = beg <= c <= end ? src`.
    pub fn remove_static_timing_info(&mut self) {
        match self {
            Self::Port(_) | Self::CompOp(..) | Self::True => (),
            Self::Info(_) => {
                self.update(|_| Self::True);
            }
            Self::Not(g) => match g.as_mut() {
                Self::Info(_) => self.update(|_| Self::True),
                _ => g.remove_static_timing_info(),
            },
            Self::And(l, r) => match (l.as_mut(), r.as_mut()) {
                (Self::Info(_), Self::Info(_)) => self.update(|_| Self::True),
                (Self::Info(_), _) => {
                    r.remove_static_timing_info();
                    l.update(|_| Self::True);
                }
                (_, Self::Info(_)) => {
                    l.remove_static_timing_info();
                    r.update(|_| Self::True);
                }
                _ => {
                    l.remove_static_timing_info();
                    r.remove_static_timing_info();
                }
            },
            Self::Or(l, r) => match (l.as_mut(), r.as_mut()) {
                (Self::Info(_), Self::Info(_)) => self.update(|_| Self::True),
                (Self::Info(_), _) => {
                    r.remove_static_timing_info();
                    l.update(|_| Self::not(Self::True))
                }
                (_, Self::Info(_)) => {
                    l.remove_static_timing_info();
                    r.update(|_| Self::not(Self::True))
                }
                _ => {
                    l.remove_static_timing_info();
                    r.remove_static_timing_info();
                }
            },
        }
    }
}

impl From<Guard<StaticTiming>> for Guard<Nothing> {
    fn from(guard: Guard<StaticTiming>) -> Guard<Nothing> {
        match guard {
            Guard::True => Guard::True,
            Guard::Port(p) => Guard::Port(p),
            Guard::CompOp(cmp, p1, p2) => Guard::CompOp(cmp, p1, p2),

            Guard::And(l, r) => {
                let l_new = Guard::from(*l);
                let r_new = Guard::from(*r);
                Guard::And(Box::new(l_new), Box::new(r_new))
            }
            Guard::Or(l, r) => {
                let l_new = Guard::from(*l);
                let r_new = Guard::from(*r);
                Guard::Or(Box::new(l_new), Box::new(r_new))
            }
            Guard::Not(g) => {
                let g_new = Guard::from(*g);
                Guard::Not(Box::new(g_new))
            }
            Guard::Info(_) => {
                unreachable!("Guard should not contain any `info` nodes;")
            }
        }
    }
}

/// Construct guards from ports
impl<T> From<RRC<Port>> for Guard<T> {
    fn from(port: RRC<Port>) -> Self {
        Guard::Port(Rc::clone(&port))
    }
}

impl<T> PartialEq for Guard<T>
where
    T: Eq,
{
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Guard::Or(la, ra), Guard::Or(lb, rb))
            | (Guard::And(la, ra), Guard::And(lb, rb)) => la == lb && ra == rb,
            (Guard::CompOp(opa, la, ra), Guard::CompOp(opb, lb, rb)) => {
                (opa == opb)
                    && (la.borrow().get_parent_name(), &la.borrow().name)
                        == (lb.borrow().get_parent_name(), &lb.borrow().name)
                    && (ra.borrow().get_parent_name(), &ra.borrow().name)
                        == (rb.borrow().get_parent_name(), &rb.borrow().name)
            }
            (Guard::Not(a), Guard::Not(b)) => a == b,
            (Guard::Port(a), Guard::Port(b)) => {
                (a.borrow().get_parent_name(), &a.borrow().name)
                    == (b.borrow().get_parent_name(), &b.borrow().name)
            }
            (Guard::True, Guard::True) => true,
            (Guard::Info(i1), Guard::Info(i2)) => i1 == i2,
            _ => false,
        }
    }
}

impl<T> Eq for Guard<T> where T: Eq {}

/// Define order on guards
impl<T> PartialOrd for Guard<T>
where
    T: Eq,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// Define an ordering on the precedence of guards. Guards are
/// considered equal when they have the same precedence.
impl<T> Ord for Guard<T>
where
    T: Eq,
{
    fn cmp(&self, other: &Self) -> Ordering {
        match (self, other) {
            (Guard::Or(..), Guard::Or(..))
            | (Guard::And(..), Guard::And(..))
            | (Guard::CompOp(..), Guard::CompOp(..))
            | (Guard::Not(..), Guard::Not(..))
            | (Guard::Port(..), Guard::Port(..))
            | (Guard::Info(_), Guard::Info(_))
            | (Guard::True, Guard::True) => Ordering::Equal,
            (Guard::Or(..), _) => Ordering::Greater,
            (_, Guard::Or(..)) => Ordering::Less,
            (Guard::And(..), _) => Ordering::Greater,
            (_, Guard::And(..)) => Ordering::Less,
            (Guard::CompOp(PortComp::Leq, ..), _) => Ordering::Greater,
            (_, Guard::CompOp(PortComp::Leq, ..)) => Ordering::Less,
            (Guard::CompOp(PortComp::Geq, ..), _) => Ordering::Greater,
            (_, Guard::CompOp(PortComp::Geq, ..)) => Ordering::Less,
            (Guard::CompOp(PortComp::Lt, ..), _) => Ordering::Greater,
            (_, Guard::CompOp(PortComp::Lt, ..)) => Ordering::Less,
            (Guard::CompOp(PortComp::Gt, ..), _) => Ordering::Greater,
            (_, Guard::CompOp(PortComp::Gt, ..)) => Ordering::Less,
            (Guard::CompOp(PortComp::Eq, ..), _) => Ordering::Greater,
            (_, Guard::CompOp(PortComp::Eq, ..)) => Ordering::Less,
            (Guard::CompOp(PortComp::Neq, ..), _) => Ordering::Greater,
            (_, Guard::CompOp(PortComp::Neq, ..)) => Ordering::Less,
            (Guard::Not(..), _) => Ordering::Greater,
            (_, Guard::Not(..)) => Ordering::Less,
            (Guard::Port(..), _) => Ordering::Greater,
            (_, Guard::Port(..)) => Ordering::Less,
            // maybe we should change this?
            (Guard::Info(..), _) => Ordering::Greater,
            (_, Guard::Info(..)) => Ordering::Less,
        }
    }
}

/////////////// Sugar for convience constructors /////////////

/// Construct a Guard::And:
/// ```
/// let and_guard = g1 & g2;
/// ```
impl<T> BitAnd for Guard<T>
where
    T: Eq,
{
    type Output = Self;

    fn bitand(self, other: Self) -> Self::Output {
        self.and(other)
    }
}

/// Construct a Guard::Or:
/// ```
/// let or_guard = g1 | g2;
/// ```
impl<T> BitOr for Guard<T>
where
    T: Eq,
{
    type Output = Self;

    fn bitor(self, other: Self) -> Self::Output {
        self.or(other)
    }
}

/// Construct a Guard::Or:
/// ```
/// let not_guard = !g1;
/// ```
impl<T> Not for Guard<T> {
    type Output = Self;

    fn not(self) -> Self {
        match self {
            Guard::CompOp(PortComp::Eq, lhs, rhs) => {
                Guard::CompOp(PortComp::Neq, lhs, rhs)
            }
            Guard::CompOp(PortComp::Neq, lhs, rhs) => {
                Guard::CompOp(PortComp::Eq, lhs, rhs)
            }
            Guard::CompOp(PortComp::Gt, lhs, rhs) => {
                Guard::CompOp(PortComp::Leq, lhs, rhs)
            }
            Guard::CompOp(PortComp::Lt, lhs, rhs) => {
                Guard::CompOp(PortComp::Geq, lhs, rhs)
            }
            Guard::CompOp(PortComp::Geq, lhs, rhs) => {
                Guard::CompOp(PortComp::Lt, lhs, rhs)
            }
            Guard::CompOp(PortComp::Leq, lhs, rhs) => {
                Guard::CompOp(PortComp::Gt, lhs, rhs)
            }
            Guard::Not(expr) => *expr,
            _ => Guard::Not(Box::new(self)),
        }
    }
}

/// Update a Guard with Or.
/// ```
/// g1 |= g2;
/// ```
impl<T> BitOrAssign for Guard<T>
where
    T: Eq,
{
    fn bitor_assign(&mut self, other: Self) {
        self.update(|old| old | other)
    }
}

/// Update a Guard with Or.
/// ```
/// g1 &= g2;
/// ```
impl<T> BitAndAssign for Guard<T>
where
    T: Eq,
{
    fn bitand_assign(&mut self, other: Self) {
        self.update(|old| old & other)
    }
}