From 2b977a74293a49e9e2a5d960a6a9c07df22430ce Mon Sep 17 00:00:00 2001 From: mergify[bot] Date: Wed, 6 Jul 2022 00:28:02 +0000 Subject: Implement trait for Chisel compiler to name arbitrary non-Data types (#2610) (#2617) Co-authored-by: Jack Koenig Co-authored-by: Megan Wachs (cherry picked from commit 3ab34cddd8b87c22d5fc31020f10ddb2f1990d51) Co-authored-by: Jared Barocsi <82000041+jared-barocsi@users.noreply.github.com>--- .../chisel3/internal/plugin/ChiselComponent.scala | 33 ++++++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'plugin/src/main/scala') diff --git a/plugin/src/main/scala/chisel3/internal/plugin/ChiselComponent.scala b/plugin/src/main/scala/chisel3/internal/plugin/ChiselComponent.scala index b3bbdbe4..dd9f24fb 100644 --- a/plugin/src/main/scala/chisel3/internal/plugin/ChiselComponent.scala +++ b/plugin/src/main/scala/chisel3/internal/plugin/ChiselComponent.scala @@ -82,9 +82,17 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) // Checking for all chisel3.internal.NamedComponents, but since it is internal, we instead have // to match the public subtypes private val shouldMatchNamedComp: Type => Boolean = - shouldMatchGen(tq"chisel3.Data", tq"chisel3.MemBase[_]", tq"chisel3.VerificationStatement") + shouldMatchGen( + tq"chisel3.Data", + tq"chisel3.MemBase[_]", + tq"chisel3.VerificationStatement" + ) private val shouldMatchModule: Type => Boolean = shouldMatchGen(tq"chisel3.experimental.BaseModule") private val shouldMatchInstance: Type => Boolean = shouldMatchGen(tq"chisel3.experimental.hierarchy.Instance[_]") + private val shouldMatchChiselPrefixed: Type => Boolean = + shouldMatchGen( + tq"chisel3.experimental.AffectsChiselPrefix" + ) // Given a type tree, infer the type and return it private def inferType(t: Tree): Type = localTyper.typed(t, nsc.Mode.TYPEmode).tpe @@ -171,25 +179,38 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) // Check if a subtree is a candidate case dd @ ValDef(mods, name, tpt, rhs) if okVal(dd) => val tpe = inferType(tpt) + val isData = shouldMatchData(tpe) + val isNamedComp = isData || shouldMatchNamedComp(tpe) + val isPrefixed = isNamedComp || shouldMatchChiselPrefixed(tpe) + // If a Data and in a Bundle, just get the name but not a prefix - if (shouldMatchData(tpe) && inBundle(dd)) { + if (isData && inBundle(dd)) { val str = stringFromTermName(name) val newRHS = transform(rhs) // chisel3.internal.plugin.autoNameRecursively val named = q"chisel3.internal.plugin.autoNameRecursively($str)($newRHS)" treeCopy.ValDef(dd, mods, name, tpt, localTyper.typed(named)) } // If a Data or a Memory, get the name and a prefix - else if (shouldMatchNamedComp(tpe)) { + else if (isData || isPrefixed) { val str = stringFromTermName(name) // Starting with '_' signifies a temporary, we ignore it for prefixing because we don't // want double "__" in names when the user is just specifying a temporary val prefix = if (str.head == '_') str.tail else str val newRHS = transform(rhs) val prefixed = q"chisel3.experimental.prefix.apply[$tpt](name=$prefix)(f=$newRHS)" - val named = q"chisel3.internal.plugin.autoNameRecursively($str)($prefixed)" + + val named = + if (isNamedComp) { + // Only name named components (not things that are merely prefixed) + q"chisel3.internal.plugin.autoNameRecursively($str)($prefixed)" + } else { + prefixed + } + treeCopy.ValDef(dd, mods, name, tpt, localTyper.typed(named)) - // If an instance, just get a name but no prefix - } else if (shouldMatchModule(tpe)) { + } + // If an instance, just get a name but no prefix + else if (shouldMatchModule(tpe)) { val str = stringFromTermName(name) val newRHS = transform(rhs) val named = q"chisel3.internal.plugin.autoNameRecursively($str)($newRHS)" -- cgit v1.2.3