Implement IR equivalence checker

This commit is contained in:
Jeehoon Kang
2020-06-10 22:27:54 +09:00
parent 6146714112
commit c2380efbb0
5 changed files with 265 additions and 38 deletions

View File

@@ -1,3 +1,7 @@
use itertools::izip;
use core::ops::Deref;
#[macro_export]
/// Ok or executing the given expression.
macro_rules! ok_or {
@@ -51,3 +55,35 @@ pub trait Translate<S> {
fn translate(&mut self, source: &S) -> Result<Self::Target, Self::Error>;
}
pub trait IsEquiv {
fn is_equiv(&self, other: &Self) -> bool;
}
impl<T: IsEquiv> IsEquiv for Box<T> {
fn is_equiv(&self, other: &Self) -> bool {
self.deref().is_equiv(other.deref())
}
}
impl<T: IsEquiv> IsEquiv for &T {
fn is_equiv(&self, other: &Self) -> bool {
(*self).is_equiv(*other)
}
}
impl<T: IsEquiv> IsEquiv for Option<T> {
fn is_equiv(&self, other: &Self) -> bool {
match (self, other) {
(Some(lhs), Some(rhs)) => lhs.is_equiv(rhs),
(None, None) => true,
_ => false,
}
}
}
impl<T: IsEquiv> IsEquiv for Vec<T> {
fn is_equiv(&self, other: &Self) -> bool {
self.len() == other.len() && izip!(self, other).all(|(lhs, rhs)| lhs.is_equiv(rhs))
}
}