Skip to content
Snippets Groups Projects
Commit a3209bba authored by Etienne Kneuss's avatar Etienne Kneuss
Browse files

InjectAssert should not insert trivial asserts

Improve equality to detect equalities between literals
parent ee70f7ad
No related branches found
No related tags found
No related merge requests found
...@@ -280,7 +280,27 @@ object Constructors { ...@@ -280,7 +280,27 @@ object Constructors {
if (a == b && isDeterministic(a)) { if (a == b && isDeterministic(a)) {
BooleanLiteral(true) BooleanLiteral(true)
} else { } else {
Equals(a, b) (a, b) match {
case (a: Literal[_], b: Literal[_]) =>
if (a.value == b.value) {
BooleanLiteral(true)
} else {
BooleanLiteral(false)
}
case _ =>
Equals(a, b)
}
}
}
def assertion(c: Expr, err: Option[String], res: Expr) = {
if (c == BooleanLiteral(true)) {
res
} else if (c == BooleanLiteral(false)) {
Error(res.getType, err.getOrElse("Assertion failed"))
} else {
Assert(c, err, res)
} }
} }
......
...@@ -42,34 +42,34 @@ object InjectAsserts extends SimpleLeonPhase[Program, Program] { ...@@ -42,34 +42,34 @@ object InjectAsserts extends SimpleLeonPhase[Program, Program] {
).setPos(e)) ).setPos(e))
case e @ Division(_, d) => case e @ Division(_, d) =>
Some(Assert(Not(Equals(d, InfiniteIntegerLiteral(0))), Some(assertion(not(equality(d, InfiniteIntegerLiteral(0))),
Some("Division by zero"), Some("Division by zero"),
e e
).setPos(e)) ).setPos(e))
case e @ Remainder(_, d) => case e @ Remainder(_, d) =>
Some(Assert(Not(Equals(d, InfiniteIntegerLiteral(0))), Some(assertion(not(equality(d, InfiniteIntegerLiteral(0))),
Some("Remainder by zero"), Some("Remainder by zero"),
e e
).setPos(e)) ).setPos(e))
case e @ Modulo(_, d) => case e @ Modulo(_, d) =>
Some(Assert(Not(Equals(d, InfiniteIntegerLiteral(0))), Some(assertion(not(equality(d, InfiniteIntegerLiteral(0))),
Some("Modulo by zero"), Some("Modulo by zero"),
e e
).setPos(e)) ).setPos(e))
case e @ BVDivision(_, d) => case e @ BVDivision(_, d) =>
Some(Assert(Not(Equals(d, IntLiteral(0))), Some(assertion(not(equality(d, IntLiteral(0))),
Some("Division by zero"), Some("Division by zero"),
e e
).setPos(e)) ).setPos(e))
case e @ BVRemainder(_, d) => case e @ BVRemainder(_, d) =>
Some(Assert(Not(Equals(d, IntLiteral(0))), Some(assertion(not(equality(d, IntLiteral(0))),
Some("Remainder by zero"), Some("Remainder by zero"),
e e
).setPos(e)) ).setPos(e))
case e @ RealDivision(_, d) => case e @ RealDivision(_, d) =>
Some(Assert(Not(Equals(d, FractionalLiteral(0, 1))), Some(assertion(not(equality(d, FractionalLiteral(0, 1))),
Some("Division by zero"), Some("Division by zero"),
e e
).setPos(e)) ).setPos(e))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment