summaryrefslogtreecommitdiff
path: root/core/src/main/scala/chisel3/internal/plugin
diff options
context:
space:
mode:
authorJack Koenig2020-11-10 18:40:51 -0800
committerGitHub2020-11-11 02:40:51 +0000
commit1260f7c89f1b95bdb00e56e49edb73dc2eac3a0e (patch)
tree8f349d91946fd43b7bf0d8ed9987404c0a49b7a1 /core/src/main/scala/chisel3/internal/plugin
parent8187318e7aef42d541ce307f93d9fc946ed4c38d (diff)
Refine autonaming to have more intuitive behavior (#1660)
* Refine autonaming to have more intuitive behavior Last name in an Expression wins, while the first Statement to name wins. This is done via checking the _id of HasIds during autonaming and only applying a name if the HasId was created in the scope of autonaming. There is no change to .autoSeed or .suggestName behavior. Behavior of chisel3-plugins from before this change is maintained. * Update docs with naming plugin changes
Diffstat (limited to 'core/src/main/scala/chisel3/internal/plugin')
-rw-r--r--core/src/main/scala/chisel3/internal/plugin/package.scala51
1 files changed, 51 insertions, 0 deletions
diff --git a/core/src/main/scala/chisel3/internal/plugin/package.scala b/core/src/main/scala/chisel3/internal/plugin/package.scala
index dd091ccc..c17baf22 100644
--- a/core/src/main/scala/chisel3/internal/plugin/package.scala
+++ b/core/src/main/scala/chisel3/internal/plugin/package.scala
@@ -10,6 +10,7 @@ package object plugin {
* @param nameMe The thing to be named
* @tparam T The type of the thing to be named
* @return The thing, but now named
+ * @note This is the version called by chisel3-plugin prior to v3.4.1
*/
def autoNameRecursively[T <: Any](name: String, nameMe: T): T = {
chisel3.internal.Builder.nameRecursively(
@@ -19,4 +20,54 @@ package object plugin {
)
nameMe
}
+
+ // The actual implementation
+ // Cannot be unified with (String, T) => T (v3.4.0 version) because of special behavior of ports
+ // in .autoSeed
+ private def _autoNameRecursively[T <: Any](prevId: Long, name: String, nameMe: T): T = {
+ chisel3.internal.Builder.nameRecursively(
+ name,
+ nameMe,
+ (id: chisel3.internal.HasId, n: String) => {
+ // Name override only if result was created in this scope
+ if (id._id > prevId) {
+ id.forceAutoSeed(n)
+ }
+ }
+ )
+ nameMe
+ }
+
+ /** Used by Chisel's compiler plugin to automatically name signals
+ * DO NOT USE in your normal Chisel code!!!
+ *
+ * @param name The name to use
+ * @param nameMe The thing to be named
+ * @tparam T The type of the thing to be named
+ * @return The thing, but now named
+ */
+ def autoNameRecursively[T <: Any](name: String)(nameMe: => T): T = {
+ // The _id of the most recently constructed HasId
+ val prevId = Builder.idGen.value
+ val result = nameMe
+ _autoNameRecursively(prevId, name, result)
+ }
+
+ /** Used by Chisel's compiler plugin to automatically name signals
+ * DO NOT USE in your normal Chisel code!!!
+ *
+ * @param names The names to use corresponding to interesting fields of the Product
+ * @param nameMe The [[Product]] to be named
+ * @tparam T The type of the thing to be named
+ * @return The thing, but with each member named
+ */
+ def autoNameRecursivelyProduct[T <: Product](names: List[Option[String]])(nameMe: => T): T = {
+ // The _id of the most recently constructed HasId
+ val prevId = Builder.idGen.value
+ val result = nameMe
+ for ((name, t) <- names.iterator.zip(result.productIterator) if name.nonEmpty) {
+ _autoNameRecursively(prevId, name.get, t)
+ }
+ result
+ }
}