aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/annotationTests/CleanupNamedTargetsSpec.scala
blob: 873b238337b2d08fbc7c248177cc70608a3a1946 (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
// SPDX-License-Identifier: Apache-2.0

package firrtlTests.annotationTests

import firrtl.{AnnotationSeq, CircuitState, Parser, UnknownForm}
import firrtl.annotations.{
  CircuitName,
  CircuitTarget,
  ComponentName,
  ModuleName,
  MultiTargetAnnotation,
  ReferenceTarget,
  SingleTargetAnnotation,
  Target
}
import firrtl.annotations.transforms.CleanupNamedTargets

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

object CleanupNamedTargetsSpec {

  case class SingleReferenceAnnotation(target: ReferenceTarget) extends SingleTargetAnnotation[ReferenceTarget] {
    override def duplicate(a: ReferenceTarget) = this.copy(target = a)
  }

  case class MultiReferenceAnnotation(targets: Seq[Seq[Target]]) extends MultiTargetAnnotation {
    override def duplicate(a: Seq[Seq[Target]]) = this.copy(targets = a)
  }

}

class CleanupNamedTargetsSpec extends AnyFlatSpec with Matchers {

  import CleanupNamedTargetsSpec._

  class F {

    val transform = new CleanupNamedTargets

    val circuit =
      """|circuit Foo:
         |  module Bar:
         |    skip
         |  module Foo:
         |    inst bar of Bar
         |    wire baz: UInt<1>
         |""".stripMargin

    lazy val foo = CircuitTarget("Foo").module("Foo")

    lazy val barTarget = ComponentName("bar", ModuleName("Foo", CircuitName("Foo"))).toTarget

    lazy val bazTarget = ComponentName("baz", ModuleName("Foo", CircuitName("Foo"))).toTarget

    def circuitState(a: AnnotationSeq): CircuitState = CircuitState(Parser.parse(circuit), UnknownForm, a, None)

  }

  behavior.of("CleanupNamedTargets")

  it should "convert a SingleTargetAnnotation[ReferenceTarget] of an instance to an InstanceTarget" in new F {
    val annotations: AnnotationSeq = Seq(SingleReferenceAnnotation(barTarget))

    transform.transform(circuitState(annotations)).renames.get.get(barTarget) should be {
      Some(Seq(foo.instOf("bar", "Bar")))
    }
  }

  it should "convert a MultiTargetAnnotation with a ReferenceTarget of an instance to an InstanceTarget" in new F {
    val annotations: AnnotationSeq = Seq(MultiReferenceAnnotation(Seq(Seq(barTarget), Seq(bazTarget))))

    val renames = transform.transform(circuitState(annotations)).renames.get

    renames.get(barTarget) should be(Some(Seq(foo.instOf("bar", "Bar"))))

    info("and not touch a true ReferenceAnnotation")
    renames.get(bazTarget) should be(None)

  }

}