|
| 1 | +package analysis.data_structure_analysis |
| 2 | + |
| 3 | +import analysis.solvers.{DSAUnionFindSolver, OffsetUnionFindSolver, UnionFindSolver} |
| 4 | +import ir.eval.BitVectorEval.{bv2SignedInt, isNegative} |
| 5 | +import ir.{BVADD, BinaryExpr, BitVecLiteral, Expr, LocalAssign, Procedure, Program, Register} |
| 6 | +import specification.{ExternalFunction, SymbolTableEntry} |
| 7 | +import util.{DSALogger, IRContext} |
| 8 | + |
| 9 | +import java.io.File |
| 10 | +import scala.collection.{SortedSet, mutable} |
| 11 | + |
| 12 | +/** |
| 13 | + * Integer Interval with top and bottom |
| 14 | + * start (s) <= end (e) |
| 15 | + * For purposes of overlapping and size the interval is inclusive of s and exclusive of e |
| 16 | + */ |
| 17 | +enum DSInterval extends Offsets { |
| 18 | + case Top |
| 19 | + case Bot |
| 20 | + case Value(s: Int, e: Int) |
| 21 | + |
| 22 | + this match |
| 23 | + case DSInterval.Value(s, e) => assert(s <= e, "start of interval should be less than its end") |
| 24 | + case _ => |
| 25 | + |
| 26 | + override def toString: String = |
| 27 | + this match |
| 28 | + case DSInterval.Top => "Top" |
| 29 | + case DSInterval.Bot => "Bot" |
| 30 | + case DSInterval.Value(start, end) => s"${start}_$end" |
| 31 | + |
| 32 | + def start: Option[Int] = |
| 33 | + this match |
| 34 | + case DSInterval.Value(s, e) => Some(s) |
| 35 | + case _ => None |
| 36 | + |
| 37 | + def end: Option[Int] = |
| 38 | + this match |
| 39 | + case DSInterval.Value(s, e) => Some(e) |
| 40 | + case _ => None |
| 41 | + |
| 42 | + def size: Option[Int] = |
| 43 | + this match |
| 44 | + case DSInterval.Value(start, end) => Some(end - start) |
| 45 | + case _ => None |
| 46 | + |
| 47 | + def move(func: Int => Int): DSInterval = |
| 48 | + this match |
| 49 | + case DSInterval.Value(start, end) => Value(func(start), func(end)) |
| 50 | + case x => x |
| 51 | + |
| 52 | + def isEmpty: Boolean = this.size.contains(0) |
| 53 | + |
| 54 | + def growTo(size: Int): DSInterval = |
| 55 | + this match |
| 56 | + case DSInterval.Top => DSInterval.Top |
| 57 | + case DSInterval.Value(start, end) => DSInterval(start, math.max(end, start + size)) |
| 58 | + case _ => this |
| 59 | + |
| 60 | + def contains(offset: Int): Boolean = |
| 61 | + this match |
| 62 | + case DSInterval.Top => true |
| 63 | + case DSInterval.Value(start, end) => start <= offset && (end > offset || end == start) |
| 64 | + case _ => false |
| 65 | + |
| 66 | + def contains(interval: DSInterval): Boolean = |
| 67 | + (this, interval) match |
| 68 | + case (DSInterval.Top, _) => true |
| 69 | + case (_, DSInterval.Top) => false // this is not top |
| 70 | + case (a, b) if a == b => true |
| 71 | + case (DSInterval.Value(s, e), _) if s == e => false |
| 72 | + case (DSInterval.Value(start1, end1), DSInterval.Value(start2, end2)) => |
| 73 | + start1 <= start2 && (end1 > end2 || (start2 < end2 && end1 >= end2)) |
| 74 | + case _ => false |
| 75 | + |
| 76 | + def isOverlapping(other: DSInterval): Boolean = |
| 77 | + (this, other) match |
| 78 | + case (DSInterval.Top, _) => true |
| 79 | + case (_, DSInterval.Top) => true |
| 80 | + case (a, b) if a == b => true |
| 81 | + case (DSInterval.Value(s1, e1), DSInterval.Value(s2, e2)) => |
| 82 | + (s2 < e1 && s2 >= s1) || (s1 < e2 && s1 >= s2) |
| 83 | + case _ => false |
| 84 | + |
| 85 | + def join(other: DSInterval): DSInterval = { |
| 86 | + (this, other) match |
| 87 | + case (DSInterval.Top, _) => DSInterval.Top |
| 88 | + case (_, DSInterval.Top) => DSInterval.Top |
| 89 | + case (DSInterval.Bot, x) => x |
| 90 | + case (x, DSInterval.Bot) => x |
| 91 | + case (DSInterval.Value(start1, end1), DSInterval.Value(start2, end2)) => |
| 92 | + DSInterval(math.min(start1, start2), math.max(end1, end2)) |
| 93 | + } |
| 94 | + |
| 95 | + override def toOffsets: Set[Int] = { |
| 96 | + this match |
| 97 | + case DSInterval.Value(s, e) => Set(s to e: _*) |
| 98 | + case _ => throw Exception("Attempted to retrieve offsets from top/bot") |
| 99 | + } |
| 100 | + |
| 101 | + override def toIntervals: Set[DSInterval] = { |
| 102 | + this match |
| 103 | + case DSInterval.Bot => Set.empty |
| 104 | + case x => Set(x) |
| 105 | + } |
| 106 | + |
| 107 | +} |
| 108 | + |
| 109 | +object DSInterval { |
| 110 | + def apply(start: Int, end: Int) = { |
| 111 | + require(start <= end, "start of interval should be less than it's end") |
| 112 | + DSInterval.Value(start, end) |
| 113 | + } |
| 114 | + def join(interval1: DSInterval, interval2: DSInterval): DSInterval = interval1.join(interval2) |
| 115 | + implicit def orderingByTuple[T <: DSInterval]: Ordering[T] = |
| 116 | + Ordering.by { |
| 117 | + case DSInterval.Value(start, end) => |
| 118 | + (start, end) |
| 119 | + case _ => (Int.MinValue, Int.MinValue) |
| 120 | + } |
| 121 | +} |
0 commit comments