aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/constraint/IsAdd.scala
blob: db2e0f15cbd048e107c3dbdc1f8b6b97bef10ad8 (plain)
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
// SPDX-License-Identifier: Apache-2.0

package firrtl.constraint

// Is case class because writing tests is easier due to equality is not object equality
case class IsAdd private (known: Option[IsKnown], maxs: Vector[IsMax], mins: Vector[IsMin], others: Vector[Constraint])
    extends Constraint
    with MultiAry {

  def op(b1: IsKnown, b2: IsKnown): IsKnown = b1 + b2

  lazy val children: Vector[Constraint] = {
    if (known.nonEmpty) known.get +: (maxs ++ mins ++ others) else maxs ++ mins ++ others
  }

  def addChild(x: Constraint): IsAdd = x match {
    case k:   IsKnown => new IsAdd(merge(Some(k), known), maxs, mins, others)
    case add: IsAdd   => new IsAdd(merge(known, add.known), maxs ++ add.maxs, mins ++ add.mins, others ++ add.others)
    case max: IsMax   => new IsAdd(known, maxs :+ max, mins, others)
    case min: IsMin   => new IsAdd(known, maxs, mins :+ min, others)
    case other => new IsAdd(known, maxs, mins, others :+ other)
  }

  override def serialize: String = "(" + children.map(_.serialize).mkString(" + ") + ")"

  override def map(f: Constraint => Constraint): Constraint = IsAdd(children.map(f))

  def reduce(): Constraint = {
    if (children.size == 1) children.head
    else {
      (known, maxs, mins, others) match {
        case (Some(k), _, _, _) if k.value == 0         => new IsAdd(None, maxs, mins, others).reduce()
        case (Some(k), Vector(max), Vector(), Vector()) => max.map { o => IsAdd(k, o) }.reduce()
        case (Some(k), Vector(), Vector(min), Vector()) => min.map { o => IsAdd(k, o) }.reduce()
        case _                                          => this
      }
    }
  }
}

object IsAdd {
  def apply(left: Constraint, right: Constraint): Constraint = (left, right) match {
    case (l: IsKnown, r: IsKnown) => l + r
    case _ => apply(Seq(left, right))
  }
  def apply(children: Seq[Constraint]): Constraint = {
    children
      .foldLeft(new IsAdd(None, Vector(), Vector(), Vector())) { (add, c) =>
        add.addChild(c)
      }
      .reduce()
  }
}