aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/analyses/IRLookup.scala
blob: e403c1496cfbf95b392500dbbd612c371246d2b9 (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
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
// SPDX-License-Identifier: Apache-2.0

package firrtl.analyses

import firrtl.annotations.TargetToken._
import firrtl.annotations._
import firrtl.ir._
import firrtl.passes.MemPortUtils
import firrtl.{
  DuplexFlow,
  ExpKind,
  Flow,
  InstanceKind,
  Kind,
  MemKind,
  PortKind,
  RegKind,
  SinkFlow,
  SourceFlow,
  UnknownFlow,
  Utils,
  WInvalid,
  WireKind
}

import scala.collection.mutable

object IRLookup {
  def apply(circuit: Circuit): IRLookup = ConnectionGraph(circuit).irLookup
}

/** Handy lookup for obtaining AST information about a given Target
  *
  * @param declarations Maps references (not subreferences) to declarations
  * @param modules      Maps module targets to modules
  */
class IRLookup private[analyses] (
  private val declarations: Map[ModuleTarget, Map[ReferenceTarget, FirrtlNode]],
  private val modules:      Map[ModuleTarget, DefModule]) {

  private val flowCache = mutable.HashMap[ModuleTarget, mutable.HashMap[ReferenceTarget, Flow]]()
  private val kindCache = mutable.HashMap[ModuleTarget, mutable.HashMap[ReferenceTarget, Kind]]()
  private val tpeCache = mutable.HashMap[ModuleTarget, mutable.HashMap[ReferenceTarget, Type]]()
  private val exprCache = mutable.HashMap[ModuleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]]()
  private val refCache =
    mutable.HashMap[ModuleTarget, mutable.LinkedHashMap[Kind, mutable.ArrayBuffer[ReferenceTarget]]]()

  /** @example Given ~Top|MyModule/inst:Other>foo.bar, returns ~Top|Other>foo
    * @return the target converted to its local reference
    */
  def asLocalRef(t: ReferenceTarget): ReferenceTarget = t.pathlessTarget.copy(component = Nil)

  def flow(t: ReferenceTarget): Flow = flowCache
    .getOrElseUpdate(t.moduleTarget, mutable.HashMap[ReferenceTarget, Flow]())
    .getOrElseUpdate(t.pathlessTarget, Utils.flow(expr(t.pathlessTarget)))

  def kind(t: ReferenceTarget): Kind = kindCache
    .getOrElseUpdate(t.moduleTarget, mutable.HashMap[ReferenceTarget, Kind]())
    .getOrElseUpdate(t.pathlessTarget, Utils.kind(expr(t.pathlessTarget)))

  def tpe(t: ReferenceTarget): Type = tpeCache
    .getOrElseUpdate(t.moduleTarget, mutable.HashMap[ReferenceTarget, Type]())
    .getOrElseUpdate(t.pathlessTarget, expr(t.pathlessTarget).tpe)

  /** get expression of the target.
    * It can return None for many reasons, including
    *  - declaration is missing
    *  - flow is wrong
    *  - component is wrong
    *
    * @param t    [[firrtl.annotations.ReferenceTarget]] to be queried.
    * @param flow flow of the target
    * @return Some(e) if expression exists, None if it does not
    */
  def getExpr(t: ReferenceTarget, flow: Flow): Option[Expression] = {
    val pathless = t.pathlessTarget

    inCache(pathless, flow) match {
      case e @ Some(_) => return e
      case None =>
        val mt = pathless.moduleTarget
        val emt = t.encapsulatingModuleTarget
        if (declarations.contains(emt) && declarations(emt).contains(asLocalRef(t))) {
          declarations(emt)(asLocalRef(t)) match {
            case e: Expression =>
              require(e.tpe.isInstanceOf[GroundType])
              exprCache
                .getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())
                .getOrElseUpdate((pathless, Utils.flow(e)), e)
            case d: IsDeclaration =>
              d match {
                case n: DefNode =>
                  updateExpr(mt, Reference(n.name, n.value.tpe, ExpKind, SourceFlow))
                case p: Port =>
                  updateExpr(mt, Reference(p.name, p.tpe, PortKind, Utils.get_flow(p)))
                case w: DefInstance =>
                  updateExpr(mt, Reference(w.name, w.tpe, InstanceKind, SourceFlow))
                case w: DefWire =>
                  updateExpr(mt, Reference(w.name, w.tpe, WireKind, SourceFlow))
                  updateExpr(mt, Reference(w.name, w.tpe, WireKind, SinkFlow))
                  updateExpr(mt, Reference(w.name, w.tpe, WireKind, DuplexFlow))
                case r: DefRegister if pathless.tokens.last == Clock =>
                  exprCache.getOrElseUpdate(
                    pathless.moduleTarget,
                    mutable.HashMap[(ReferenceTarget, Flow), Expression]()
                  )((pathless, SourceFlow)) = r.clock
                case r: DefRegister if pathless.tokens.isDefinedAt(1) && pathless.tokens(1) == Init =>
                  exprCache.getOrElseUpdate(
                    pathless.moduleTarget,
                    mutable.HashMap[(ReferenceTarget, Flow), Expression]()
                  )((pathless, SourceFlow)) = r.init
                  updateExpr(pathless, r.init)
                case r: DefRegister if pathless.tokens.last == Reset =>
                  exprCache.getOrElseUpdate(
                    pathless.moduleTarget,
                    mutable.HashMap[(ReferenceTarget, Flow), Expression]()
                  )((pathless, SourceFlow)) = r.reset
                case r: DefRegister =>
                  updateExpr(mt, Reference(r.name, r.tpe, RegKind, SourceFlow))
                  updateExpr(mt, Reference(r.name, r.tpe, RegKind, SinkFlow))
                  updateExpr(mt, Reference(r.name, r.tpe, RegKind, DuplexFlow))
                case m: DefMemory =>
                  updateExpr(mt, Reference(m.name, MemPortUtils.memType(m), MemKind, SourceFlow))
                case other =>
                  sys.error(s"Cannot call expr with: $t, given declaration $other")
              }
            case _: IsInvalid =>
              exprCache.getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())(
                (pathless, SourceFlow)
              ) = WInvalid
          }
        }
    }

    inCache(pathless, flow)
  }

  /**
    * @param t    [[firrtl.annotations.ReferenceTarget]] to be queried.
    * @param flow flow of the target
    * @return expression of `t`
    */
  def expr(t: ReferenceTarget, flow: Flow = UnknownFlow): Expression = {
    require(contains(t), s"Cannot find\n${t.prettyPrint()}\nin circuit!")
    getExpr(t, flow) match {
      case Some(e) => e
      case None =>
        require(getExpr(t.pathlessTarget, UnknownFlow).isEmpty, s"Illegal flow $flow with target $t")
        sys.error("")
    }
  }

  /** Find [[firrtl.annotations.ReferenceTarget]] with a specific [[firrtl.Kind]] in a [[firrtl.annotations.ModuleTarget]]
    *
    * @param moduleTarget [[firrtl.annotations.ModuleTarget]] to be queried.
    * @param kind         [[firrtl.Kind]] to be find.
    * @return all [[firrtl.annotations.ReferenceTarget]] in this node.
    */
  def kindFinder(moduleTarget: ModuleTarget, kind: Kind): Seq[ReferenceTarget] = {
    def updateRefs(kind: Kind, rt: ReferenceTarget): Unit = refCache
      .getOrElseUpdate(rt.moduleTarget, mutable.LinkedHashMap.empty[Kind, mutable.ArrayBuffer[ReferenceTarget]])
      .getOrElseUpdate(kind, mutable.ArrayBuffer.empty[ReferenceTarget]) += rt

    require(contains(moduleTarget), s"Cannot find\n${moduleTarget.prettyPrint()}\nin circuit!")
    if (refCache.contains(moduleTarget) && refCache(moduleTarget).contains(kind)) refCache(moduleTarget)(kind).toSeq
    else {
      declarations(moduleTarget).foreach {
        case (rt, _: DefRegister) => updateRefs(RegKind, rt)
        case (rt, _: DefWire) => updateRefs(WireKind, rt)
        case (rt, _: DefNode) => updateRefs(ExpKind, rt)
        case (rt, _: DefMemory) => updateRefs(MemKind, rt)
        case (rt, _: DefInstance) => updateRefs(InstanceKind, rt)
        case (rt, _: Port) => updateRefs(PortKind, rt)
        case _ =>
      }
      refCache
        .get(moduleTarget)
        .map(_.getOrElse(kind, Seq.empty[ReferenceTarget]))
        .getOrElse(Seq.empty[ReferenceTarget])
        .toSeq
    }
  }

  /**
    * @param t [[firrtl.annotations.ReferenceTarget]] to be queried.
    * @return the statement containing the declaration of the target
    */
  def declaration(t: ReferenceTarget): FirrtlNode = {
    require(contains(t), s"Cannot find\n${t.prettyPrint()}\nin circuit!")
    declarations(t.encapsulatingModuleTarget)(asLocalRef(t))
  }

  /** Returns the references to the module's ports
    *
    * @param mt [[firrtl.annotations.ModuleTarget]] to be queried.
    * @return the port references of `mt`
    */
  def ports(mt: ModuleTarget): Seq[ReferenceTarget] = {
    require(contains(mt), s"Cannot find\n${mt.prettyPrint()}\nin circuit!")
    modules(mt).ports.map { p => mt.ref(p.name) }
  }

  /** Given:
    * A [[firrtl.annotations.ReferenceTarget]] of ~Top|Module>ref, which is a type of {foo: {bar: UInt}}
    * Return:
    * Seq(~Top|Module>ref, ~Top|Module>ref.foo, ~Top|Module>ref.foo.bar)
    *
    * @return a target to each sub-component, including intermediate subcomponents
    */
  def allTargets(r: ReferenceTarget): Seq[ReferenceTarget] = r.allSubTargets(tpe(r))

  /** Given:
    * A [[firrtl.annotations.ReferenceTarget]] of ~Top|Module>ref and a type of {foo: {bar: UInt}}
    * Return:
    * Seq(~Top|Module>ref.foo.bar)
    *
    * @return a target to each sub-component, excluding intermediate subcomponents.
    */
  def leafTargets(r: ReferenceTarget): Seq[ReferenceTarget] = r.leafSubTargets(tpe(r))

  /** @return Returns ((inputs, outputs)) target and type of each module port. */
  def moduleLeafPortTargets(m: ModuleTarget): (Seq[(ReferenceTarget, Type)], Seq[(ReferenceTarget, Type)]) =
    modules(m).ports.flatMap {
      case Port(_, name, Output, tpe) => Utils.create_exps(Reference(name, tpe, PortKind, SourceFlow))
      case Port(_, name, Input, tpe)  => Utils.create_exps(Reference(name, tpe, PortKind, SinkFlow))
    }.foldLeft((Vector.empty[(ReferenceTarget, Type)], Vector.empty[(ReferenceTarget, Type)])) {
      case ((inputs, outputs), e) if Utils.flow(e) == SourceFlow =>
        (inputs, outputs :+ (ConnectionGraph.asTarget(m, new TokenTagger())(e), e.tpe))
      case ((inputs, outputs), e) =>
        (inputs :+ (ConnectionGraph.asTarget(m, new TokenTagger())(e), e.tpe), outputs)
    }

  /** @param t [[firrtl.annotations.ReferenceTarget]] to be queried.
    * @return whether a ReferenceTarget is contained in this IRLookup
    */
  def contains(t: ReferenceTarget): Boolean = validPath(t.pathTarget) &&
    declarations.contains(t.encapsulatingModuleTarget) &&
    declarations(t.encapsulatingModuleTarget).contains(asLocalRef(t)) &&
    getExpr(t, UnknownFlow).nonEmpty

  /** @param mt [[firrtl.annotations.ModuleTarget]] or [[firrtl.annotations.InstanceTarget]] to be queried.
    * @return whether a ModuleTarget or InstanceTarget is contained in this IRLookup
    */
  def contains(mt: IsModule): Boolean = validPath(mt)

  /** @param t [[firrtl.annotations.ReferenceTarget]] to be queried.
    * @return whether a given [[firrtl.annotations.IsModule]] is valid, given the circuit's module/instance hierarchy
    */
  def validPath(t: IsModule): Boolean = {
    t match {
      case m: ModuleTarget => declarations.contains(m)
      case i: InstanceTarget =>
        val all = i.pathAsTargets :+ i.encapsulatingModuleTarget.instOf(i.instance, i.ofModule)
        all.map { x =>
          declarations.contains(x.moduleTarget) && declarations(x.moduleTarget).contains(x.asReference) &&
          (declarations(x.moduleTarget)(x.asReference) match {
            case DefInstance(_, _, of, _) if of == x.ofModule => validPath(x.ofModuleTarget)
            case _                                            => false
          })
        }.reduce(_ && _)
    }
  }

  /** Updates expression cache with expression. */
  private def updateExpr(mt: ModuleTarget, ref: Expression): Unit = {
    val refs = Utils.expandRef(ref)
    refs.foreach { e =>
      val target = ConnectionGraph.asTarget(mt, new TokenTagger())(e)
      exprCache(target.moduleTarget)((target, Utils.flow(e))) = e
    }
  }

  /** Updates expression cache with expression. */
  private def updateExpr(gt: ReferenceTarget, e: Expression): Unit = {
    val g = Utils.flow(e)
    e.tpe match {
      case _: GroundType =>
        exprCache(gt.moduleTarget)((gt, g)) = e
      case VectorType(t, size) =>
        exprCache(gt.moduleTarget)((gt, g)) = e
        (0 until size).foreach { i => updateExpr(gt.index(i), SubIndex(e, i, t, g)) }
      case BundleType(fields) =>
        exprCache(gt.moduleTarget)((gt, g)) = e
        fields.foreach { f => updateExpr(gt.field(f.name), SubField(e, f.name, f.tpe, Utils.times(g, f.flip))) }
      case other => sys.error(s"Error! Unexpected type $other")
    }
  }

  /** Optionally returns the expression corresponding to the target if contained in the expression cache. */
  private def inCache(pathless: ReferenceTarget, flow: Flow): Option[Expression] = {
    (
      flow,
      exprCache
        .getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())
        .contains((pathless, SourceFlow)),
      exprCache
        .getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())
        .contains((pathless, SinkFlow)),
      exprCache
        .getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())
        .contains(pathless, DuplexFlow)
    ) match {
      case (SourceFlow, true, _, _) =>
        Some(
          exprCache.getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())(
            (pathless, flow)
          )
        )
      case (SinkFlow, _, true, _) =>
        Some(
          exprCache.getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())(
            (pathless, flow)
          )
        )
      case (DuplexFlow, _, _, true) =>
        Some(
          exprCache.getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())(
            (pathless, DuplexFlow)
          )
        )
      case (UnknownFlow, _, _, true) =>
        Some(
          exprCache.getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())(
            (pathless, DuplexFlow)
          )
        )
      case (UnknownFlow, true, false, false) =>
        Some(
          exprCache.getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())(
            (pathless, SourceFlow)
          )
        )
      case (UnknownFlow, false, true, false) =>
        Some(
          exprCache.getOrElseUpdate(pathless.moduleTarget, mutable.HashMap[(ReferenceTarget, Flow), Expression]())(
            (pathless, SinkFlow)
          )
        )
      case _ => None
    }
  }
}