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
|
// SPDX-License-Identifier: Apache-2.0
package firrtlTests
import firrtl._
import firrtl.ir._
import firrtl.Utils
import org.scalatest.matchers.should.Matchers._
import org.scalatest.flatspec.AnyFlatSpec
class UtilsSpec extends AnyFlatSpec {
behavior.of("Utils.expandPrefix")
val expandPrefixTests = List(
("return a name without prefixes", "_", "foo", Set("foo")),
("expand a name ending with prefixes", "_", "foo__", Set("foo__")),
("expand a name with on prefix", "_", "foo_bar", Set("foo_bar", "foo_")),
(
"expand a name with complex prefixes",
"_",
"foo__$ba9_9X__$$$$$_",
Set("foo__$ba9_9X__$$$$$_", "foo__$ba9_9X__", "foo__$ba9_", "foo__")
),
("expand a name starting with a delimiter", "_", "__foo_bar", Set("__", "__foo_", "__foo_bar")),
("expand a name with a $ delimiter", "$", "foo$bar$$$baz", Set("foo$", "foo$bar$$$", "foo$bar$$$baz")),
("expand a name with a multi-character delimiter", "FOO", "fooFOOFOOFOObar", Set("fooFOOFOOFOO", "fooFOOFOOFOObar"))
)
for ((description, delimiter, in, out) <- expandPrefixTests) {
it should description in { Utils.expandPrefixes(in, delimiter).toSet should be(out) }
}
"expandRef" should "return intermediate expressions" in {
val bTpe = VectorType(Utils.BoolType, 2)
val topTpe = BundleType(Seq(Field("a", Default, Utils.BoolType), Field("b", Default, bTpe)))
val wr = WRef("out", topTpe, PortKind, SourceFlow)
val expected = Seq(
wr,
WSubField(wr, "a", Utils.BoolType, SourceFlow),
WSubField(wr, "b", bTpe, SourceFlow),
WSubIndex(WSubField(wr, "b", bTpe, SourceFlow), 0, Utils.BoolType, SourceFlow),
WSubIndex(WSubField(wr, "b", bTpe, SourceFlow), 1, Utils.BoolType, SourceFlow)
)
(Utils.expandRef(wr)) should be(expected)
}
def combineTest(circuits: Seq[String], expected: String) = {
(Utils.orderAgnosticEquality(Utils.combine(circuits.map(c => Parser.parse(c))), Parser.parse(expected))) should be(
true
)
}
"combine" should "merge multiple module circuits" in {
val input = Seq(
"""|circuit Top:
| extmodule External:
| output foo: UInt<32>
| defname = External
|
| extmodule Child1:
| output foo: UInt<32>
| defname = Child1
|
| extmodule Child2:
| output foo: UInt<32>
| defname = Child2
|
| module Top:
| output foo: UInt<32>
| inst c1 of Child1
| inst c2 of Child2
| inst e of External
| foo <= tail(add(add(c1.foo, c2.bar), e.foo), 1)
|""".stripMargin,
"""|circuit Child1:
| extmodule External:
| output foo: UInt<32>
| defname = External
|
| module Child1:
| output foo: UInt<32>
| inst e of External
| foo <= e.foo
|""".stripMargin,
"""|circuit Child2:
| extmodule External:
| output foo: UInt<32>
| defname = External
|
| module Child2:
| output bar: UInt<32>
| inst e of External
| bar <= e.foo
|""".stripMargin
)
val output =
"""|circuit Top:
| module Top:
| output foo: UInt<32>
| inst c1 of Child1
| inst c2 of Child2
| inst e of External
| foo <= tail(add(add(c1.foo, c2.bar), e.foo), 1)
|
| module Child1:
| output foo: UInt<32>
| inst e of External
| foo <= e.foo
|
| module Child2:
| output bar: UInt<32>
| inst e of External
| bar <= e.foo
|
| extmodule External:
| output foo: UInt<32>
| defname = External
|""".stripMargin
combineTest(input, output)
}
"combine" should "dedup ExtModules if an implementation exists" in {
val input = Seq(
"""|circuit Top:
| extmodule Child:
| output foo: UInt<32>
| defname = Child
|
| module Top:
| output foo: UInt<32>
| inst c of Child
| foo <= c.foo
|""".stripMargin,
"""|circuit Child:
| module Child:
| output foo: UInt<32>
|
| skip
|""".stripMargin
)
val output =
"""|circuit Top:
| module Top:
| output foo: UInt<32>
| inst c of Child
| foo <= c.foo
|
| module Child:
| output foo: UInt<32>
|
| skip
|""".stripMargin
combineTest(input, output)
}
"combine" should "support lone ExtModules" in {
val input = Seq(
"""|circuit Top:
| extmodule External:
| output foo: UInt<32>
| defname = External
|
| module Top:
| output foo: UInt<32>
| inst e of External
| foo <= e.foo
|""".stripMargin
)
val output =
"""|circuit Top:
| extmodule External:
| output foo: UInt<32>
| defname = External
|
| module Top:
| output foo: UInt<32>
| inst e of External
| foo <= e.foo
|""".stripMargin
combineTest(input, output)
}
"combine" should "fail with multiple lone Modules" in {
val input = Seq(
"""|circuit Top:
| extmodule External:
| output foo: UInt<32>
| defname = External
|
| module Top:
| output foo: UInt<32>
| inst e of External
| foo <= e.foo
|""".stripMargin,
"""|circuit Top2:
| extmodule External:
| output foo: UInt<32>
| defname = External
|
| module Top2:
| output bar: UInt<32>
| inst e of External
| bar <= e.foo
|""".stripMargin
)
a[java.lang.AssertionError] shouldBe thrownBy { combineTest(input, "") }
}
}
|