summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/BetterNamingTests.scala
blob: 8fd1c11fac4a460f2072bdb2a8f6d559f4b0535f (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
// SPDX-License-Identifier: Apache-2.0

package chiselTests

import chisel3._
import chisel3.stage.ChiselStage
import chisel3.util._

import scala.collection.mutable

// Defined outside of the class so we don't get $ in name
class Other(w: Int) extends Module {
  val io = IO(new Bundle {
    val a = UInt(w.W)
  })
}

// Check the names of the Modules (not instances)
class PerNameIndexing(count: Int) extends NamedModuleTester {
  def genModName(prefix: String, idx: Int): String = if (idx == 0) prefix else s"${prefix}_$idx"
  val wires = Seq.tabulate(count) { i =>
    expectModuleName(Module(new Other(i)), genModName("Other", i))
  }
  val queues = Seq.tabulate(count) { i =>
    expectModuleName(Module(new Queue(UInt(i.W), 16)), genModName("Queue", i))
  }
}

// Note this only checks Iterable[Chisel.Data] which excludes Maps
class IterableNaming extends NamedModuleTester {
  val seq = Seq.tabulate(3) { i =>
    Seq.tabulate(2) { j => expectName(WireDefault((i * j).U), s"seq_${i}_${j}") }
  }
  val optSet = Some(
    Set(
      expectName(WireDefault(0.U), "optSet_0"),
      expectName(WireDefault(1.U), "optSet_1"),
      expectName(WireDefault(2.U), "optSet_2"),
      expectName(WireDefault(3.U), "optSet_3")
    )
  )

  val stack = {
    val s = mutable.Stack[Module]()
    for (i <- 0 until 4) {
      val j = 3 - i
      s.push(expectName(Module(new Other(i)), s"stack_$j"))
    }
    s
  }
  def streamFrom(x: Int): Stream[Module] =
    expectName(Module(new Other(x)), s"list_$x") #:: streamFrom(x + 1)
  val stream = streamFrom(0) // Check that we don't get into infinite loop
  val list = stream.take(8).toList
}

class DigitFieldNamesInRecord extends NamedModuleTester {
  val wire = Wire(new CustomBundle("0" -> UInt(32.W), "1" -> UInt(32.W)))
  expectName(wire("0"), "wire.0")
  expectName(wire("1"), "wire.1")
}

/* Better Naming Tests
 *
 * These tests are intended to validate that Chisel picks better names
 */
class BetterNamingTests extends ChiselFlatSpec {

  behavior.of("Better Naming")

  it should "provide unique counters for each name" in {
    var module: PerNameIndexing = null
    ChiselStage.elaborate { module = new PerNameIndexing(4); module }
    assert(module.getNameFailures() == Nil)
  }

  it should "provide names for things defined in Iterable[HasId] and Option[HasId]" in {
    var module: IterableNaming = null
    ChiselStage.elaborate { module = new IterableNaming; module }
    assert(module.getNameFailures() == Nil)
  }

  it should "allow digits to be field names in Records" in {
    var module: DigitFieldNamesInRecord = null
    ChiselStage.elaborate { module = new DigitFieldNamesInRecord; module }
    assert(module.getNameFailures() == Nil)
  }

  "Literals" should "not impact temporary name suffixes" in {
    class MyModule(withLits: Boolean) extends Module {
      val io = IO(new Bundle {})
      if (withLits) {
        List(8.U, -3.S, 1.25.F(2.BP))
      }
      WireDefault(3.U)
    }
    val withLits = ChiselStage.emitChirrtl(new MyModule(true))
    val noLits = ChiselStage.emitChirrtl(new MyModule(false))
    withLits should equal(noLits)
  }
}