aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/annotations/TargetUtils.scala
blob: 164c430b5588acda61652ff19018b8ba014b68bb (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
// SPDX-License-Identifier: Apache-2.0

package firrtl.annotations

import firrtl._
import firrtl.analyses.InstanceKeyGraph
import firrtl.analyses.InstanceKeyGraph.InstanceKey
import firrtl.annotations.TargetToken._

object TargetUtils {

  /** Turns an instance path into a corresponding [[IsModule]]
    *
    * @note First InstanceKey is treated as the [[CircuitTarget]]
    * @param path Instance path
    * @param start Module in instance path to be starting [[ModuleTarget]]
    * @return [[IsModule]] corresponding to Instance path
    */
  def instKeyPathToTarget(path: Seq[InstanceKey], start: Option[String] = None): IsModule = {
    val head = path.head
    val startx = start.getOrElse(head.module)
    val top: IsModule = CircuitTarget(head.module).module(startx) // ~Top|Start
    val pathx = path.dropWhile(_.module != startx)
    if (pathx.isEmpty) top
    else pathx.tail.foldLeft(top) { case (acc, key) => acc.instOf(key.name, key.module) }
  }

  /** Calculates all [[InstanceTarget]]s that refer to the given [[IsModule]]
    *
    * {{{
    * ~Top|Top/a:A/b:B/c:C unfolds to:
    * * ~Top|Top/a:A/b:B/c:C
    * * ~Top|A/b:B/c:C
    * * ~Top|B/c:C
    * }}}
    * @note [[ModuleTarget]] arguments return an empty Iterable
    */
  def unfoldInstanceTargets(ismod: IsModule): Iterable[InstanceTarget] = {
    // concretely use List which is fast in practice
    def rec(im: IsModule): List[InstanceTarget] = im match {
      case inst: InstanceTarget => inst :: rec(inst.stripHierarchy(1))
      case _ => Nil
    }
    rec(ismod)
  }
}