From a69d79f25a363e2895e682cadd463466964fa889 Mon Sep 17 00:00:00 2001 From: Schuyler Eldridge Date: Sat, 2 Nov 2019 02:01:56 -0400 Subject: Better anonymous and class-in-function desiredName This changes the desired name of a Module to provide non-numeric naming for anonymous Modules and Modules defined inside function bodies. Signed-off-by: Schuyler Eldridge --- chiselFrontend/src/main/scala/chisel3/Module.scala | 28 +++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/chiselFrontend/src/main/scala/chisel3/Module.scala b/chiselFrontend/src/main/scala/chisel3/Module.scala index f88d637f..5642840f 100644 --- a/chiselFrontend/src/main/scala/chisel3/Module.scala +++ b/chiselFrontend/src/main/scala/chisel3/Module.scala @@ -218,10 +218,32 @@ package experimental { // // Chisel Internals // - /** Desired name of this module. Override this to give this module a custom, perhaps parametric, - * name. + + /** The desired name of this module (which will be used in generated FIRRTL IR or Verilog). + * + * The name of a module approximates the behavior of the Java Reflection [[`getSimpleName` method + * https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getSimpleName--]] with some modifications: + * + * - Anonymous modules will get an `"_Anon"` tag + * - Modules defined in functions will use their class name and not a numeric name + * + * @note If you want a custom or parametric name, override this method. */ - def desiredName: String = this.getClass.getName.split("\\.|\\$").last + def desiredName: String = { + /* The default module name is derived from the Java reflection derived class name. */ + val baseName = this.getClass.getName + + /* A sequence of string filters applied to the name */ + val filters: Seq[String => String] = Seq( + ((a: String) => raw"\$$+anon".r.replaceAllIn(a, "_Anon")) // Merge the "$$anon" name with previous name + ) + + filters + .foldLeft(baseName){ case (str, filter) => filter(str) } // 1. Apply filters to baseName + .split("\\.|\\$") // 2. Split string at '.' or '$' + .filterNot(_.forall(_.isDigit)) // 3. Drop purely numeric names + .last // 4. Use the last name + } /** Legalized name of this module. */ final lazy val name = try { -- cgit v1.2.3 From 77d2455a5a9cd1e3baad460d1bf5ce950b58788a Mon Sep 17 00:00:00 2001 From: Schuyler Eldridge Date: Sat, 2 Nov 2019 02:03:35 -0400 Subject: Tests for anonymous/class-in-module desiredName Signed-off-by: Schuyler Eldridge --- src/test/scala/chiselTests/Module.scala | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/scala/chiselTests/Module.scala b/src/test/scala/chiselTests/Module.scala index f730d08b..d8cae510 100644 --- a/src/test/scala/chiselTests/Module.scala +++ b/src/test/scala/chiselTests/Module.scala @@ -158,4 +158,19 @@ class ModuleSpec extends ChiselPropSpec { (the [Exception] thrownBy (Driver.elaborate(() => new NullModuleWrapper))) .getMessage should include ("desiredName of chiselTests.NullModuleWrapper is null") } + property("The name of a module in a function should be sane") { + def foo = { + class Foo1 extends RawModule { + assert(name == "Foo1") + } + new Foo1 + } + Driver.elaborate(() => foo) + } + property("The name of an anonymous module should include '_Anon'") { + trait Foo { this: RawModule => + assert(name.contains("_Anon")) + } + Driver.elaborate(() => new RawModule with Foo) + } } -- cgit v1.2.3