Skip to content

Commit afe9b2c

Browse files
authored
Rename and fix firrtl emission for HierarchicalModuleName (#4872)
1 parent 3687bbc commit afe9b2c

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

core/src/main/scala/chisel3/Printable.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ object Printable {
193193
/** Resolve Printables that are resolved at Chisel-time */
194194
private[chisel3] def resolve(pable: Printable, ctx: Component)(implicit info: SourceInfo): Printable =
195195
pable.map {
196-
case Name(data) => PString(data.ref.name)
197-
case FullName(data) => PString(data.ref.fullName(ctx))
198-
case other => other
196+
case Name(data) => PString(data.ref.name)
197+
case FullName(data) => PString(data.ref.fullName(ctx))
198+
case HierarchicalModuleName => PString("{{HierarchicalModuleName}}")
199+
case other => other
199200
}
200201
}
201202

@@ -377,7 +378,7 @@ case object Percent extends Printable {
377378
}
378379

379380
/** Represents the hierarchical name in the Verilog (`%m`) */
380-
case object HierarchicalName extends Printable {
381+
case object HierarchicalModuleName extends Printable {
381382
@deprecated("Use unpack with no arguments instead.", "Chisel 7.0.0")
382383
final def unpack(ctx: Component)(implicit info: SourceInfo): (String, Iterable[String]) = ("%m", List.empty)
383384
@deprecated("Use unpack with no arguments instead.", "Chisel 7.0.0")

core/src/main/scala/chisel3/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ package object chisel3 {
247247
} else if (s(end + 1) == '%') {
248248
Percent
249249
} else if (s(end + 1) == 'm') {
250-
HierarchicalName
250+
HierarchicalModuleName
251251
} else {
252252
throw new UnknownFormatConversionException("Un-escaped %")
253253
}

docs/src/explanations/printing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ printf(cf"myUInt = $myUInt%c") // myUInt = !
6161

6262
There are special values you can include in your `cf` interpolated string:
6363

64-
* `HierarchicalName` (`%m`): The hierarchical name of the signal
64+
* `HierarchicalModuleName` (`%m`): The hierarchical name of the current module
6565
* `Percent` (`%%`): A literal `%`
6666

6767
```scala mdoc:compile-only
68-
printf(cf"hierarchical path = $HierarchicalName\n") // hierarchical path = <verilog.module.path>
68+
printf(cf"hierarchical path = $HierarchicalModuleName\n") // hierarchical path = <verilog.module.path>
6969
printf(cf"hierarchical path = %m\n") // equivalent to the above
7070

7171
printf(cf"100$Percent\n") // 100%

src/test/scala-2/chiselTests/PrintableSpec.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,20 @@ class PrintableSpec extends AnyFlatSpec with Matchers with FileCheck {
318318
it should "support all legal format specifiers" in {
319319
class MyModule extends Module {
320320
val in = IO(Input(UInt(8.W)))
321-
printf(cf"$HierarchicalName $in%d $in%x $in%b $in%c %%\n")
321+
printf(cf"$HierarchicalModuleName $in%d $in%x $in%b $in%c %%\n")
322322
}
323323
ChiselStage
324324
.emitCHIRRTL(new MyModule)
325325
.fileCheck()(
326-
"""CHECK: printf(clock, UInt<1>(0h1), "%m %d %x %b %c %%\n", in, in, in, in)"""
326+
"""CHECK{LITERAL}: printf(clock, UInt<1>(0h1), "{{HierarchicalModuleName}} %d %x %b %c %%\n", in, in, in, in)"""
327327
)
328+
// Also check Verilog
329+
ChiselStage
330+
.emitSystemVerilog(new MyModule)
331+
.fileCheck()(
332+
"""CHECK: $fwrite(`PRINTF_FD_, "%m %d %x %b %c %%\n", in, in, in, in);"""
333+
)
334+
328335
}
329336

330337
it should "support modifiers to format specifiers" in {
@@ -383,7 +390,7 @@ class PrintableSpec extends AnyFlatSpec with Matchers with FileCheck {
383390
(Character(x), ("%c", Seq(x))),
384391
(PString("foo"), ("foo", Seq())),
385392
(Percent, ("%%", Seq())),
386-
(HierarchicalName, ("%m", Seq())),
393+
(HierarchicalModuleName, ("%m", Seq())),
387394
(Name(x), ("%n", Seq(x))),
388395
(FullName(x), ("%N", Seq(x)))
389396
)

src/test/scala-2/chiselTests/Printf.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ class PrintfSpec extends AnyFlatSpec with Matchers with FileCheck {
8181
ChiselStage
8282
.emitCHIRRTL(new MyModule)
8383
.fileCheck()(
84-
"""CHECK: printf(clock, UInt<1>(0h1), "%m %d %x %b %c %%\n", in, in, in, in)"""
84+
"""CHECK{LITERAL}: printf(clock, UInt<1>(0h1), "{{HierarchicalModuleName}} %d %x %b %c %%\n", in, in, in, in)"""
85+
)
86+
// Also check Verilog
87+
ChiselStage
88+
.emitSystemVerilog(new MyModule)
89+
.fileCheck()(
90+
"""CHECK: $fwrite(`PRINTF_FD_, "%m %d %x %b %c %%\n", in, in, in, in);"""
8591
)
8692
}
8793

0 commit comments

Comments
 (0)