diff options
| author | Jack Koenig | 2020-11-10 18:40:51 -0800 |
|---|---|---|
| committer | GitHub | 2020-11-11 02:40:51 +0000 |
| commit | 1260f7c89f1b95bdb00e56e49edb73dc2eac3a0e (patch) | |
| tree | 8f349d91946fd43b7bf0d8ed9987404c0a49b7a1 /core | |
| parent | 8187318e7aef42d541ce307f93d9fc946ed4c38d (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')
| -rw-r--r-- | core/src/main/scala/chisel3/internal/Builder.scala | 5 | ||||
| -rw-r--r-- | core/src/main/scala/chisel3/internal/plugin/package.scala | 51 |
2 files changed, 55 insertions, 1 deletions
diff --git a/core/src/main/scala/chisel3/internal/Builder.scala b/core/src/main/scala/chisel3/internal/Builder.scala index 56a85fb6..d665b7bc 100644 --- a/core/src/main/scala/chisel3/internal/Builder.scala +++ b/core/src/main/scala/chisel3/internal/Builder.scala @@ -59,6 +59,7 @@ private[chisel3] class IdGen { counter += 1 counter } + def value: Long = counter } /** Public API to access Node/Signal names. @@ -119,7 +120,9 @@ private[chisel3] trait HasId extends InstanceId { * @param seed Seed for the name of this component * @return this object */ - private [chisel3] def autoSeed(seed: String): this.type = { + private[chisel3] def autoSeed(seed: String): this.type = forceAutoSeed(seed) + // Bypass the overridden behavior of autoSeed in [[Data]], apply autoSeed even to ports + private[chisel3] def forceAutoSeed(seed: String): this.type = { auto_seed = Some(seed) for(hook <- auto_postseed_hooks) { hook(seed) } prefix_seed = Builder.getPrefix() 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 + } } |
