diff options
| author | Jack Koenig | 2020-03-22 18:13:58 -0700 |
|---|---|---|
| committer | Jack Koenig | 2020-03-25 19:17:15 -0700 |
| commit | fbf5e6f1a0e8bf535d465b748ad554575fe62156 (patch) | |
| tree | 578858ab6d219ca6daf44cf87b73f75054989097 /chiselFrontend/src/main/scala/chisel3/internal/Namer.scala | |
| parent | b2e004fb615a3c931d910a338b9faa99c1c975d7 (diff) | |
Rename subprojects to more canonical names
* Rename coreMacros to macros
* Rename chiselFrontend to core
Also make each subproject publish with "chisel3-" as a prefix
Diffstat (limited to 'chiselFrontend/src/main/scala/chisel3/internal/Namer.scala')
| -rw-r--r-- | chiselFrontend/src/main/scala/chisel3/internal/Namer.scala | 154 |
1 files changed, 0 insertions, 154 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/internal/Namer.scala b/chiselFrontend/src/main/scala/chisel3/internal/Namer.scala deleted file mode 100644 index 999971a4..00000000 --- a/chiselFrontend/src/main/scala/chisel3/internal/Namer.scala +++ /dev/null @@ -1,154 +0,0 @@ -// See LICENSE for license details. - -// This file contains part of the implementation of the naming static annotation system. - -package chisel3.internal.naming -import chisel3.experimental.NoChiselNamePrefix - -import scala.collection.mutable.Stack -import scala.collection.mutable.ListBuffer - -import scala.collection.JavaConversions._ - -import java.util.IdentityHashMap - -/** Recursive Function Namer overview - * - * In every function, creates a NamingContext object, which associates all vals with a string name - * suffix, for example: - * val myValName = SomeStatement() - * produces the entry in items: - * {ref of SomeStatement(), "myValName"} - * - * This is achieved with a macro transforming: - * val myValName = SomeStatement() - * statements into a naming call: - * val myValName = context.name(SomeStatement(), "myValName") - * - * The context is created from a global dynamic context stack at the beginning of each function. - * At the end of each function call, the completed context is added to its parent context and - * associated with the return value (whose name at an enclosing function call will form the prefix - * for all named objects). - * - * When the naming context prefix is given, it will name all of its items with the prefix and the - * associated suffix name. Then, it will check its descendants for sub-contexts with references - * matching the item reference, and if there is a match, it will (recursively) give the - * sub-context a prefix of its current prefix plus the item reference suffix. - * - * Note that for Modules, the macro will insert a naming context prefix call with an empty prefix, - * starting the recursive naming process. - */ - -/** Base class for naming contexts, providing the basic API consisting of naming calls and - * ability to take descendant naming contexts. - */ -sealed trait NamingContextInterface { - /** Suggest a name (that will be propagated to FIRRTL) for an object, then returns the object - * itself (so this can be inserted transparently anywhere). - * Is a no-op (so safe) when applied on objects that aren't named, including non-Chisel data - * types. - */ - def name[T](obj: T, name: String): T - - /** Gives this context a naming prefix (which may be empty, "", for a top-level Module context) - * so that actual naming calls (HasId.suggestName) can happen. - * Recursively names descendants, for those whose return value have an associated name. - */ - def namePrefix(prefix: String) -} - -/** Dummy implementation to allow for naming annotations in a non-Builder context. - */ -object DummyNamer extends NamingContextInterface { - def name[T](obj: T, name: String): T = obj - - def namePrefix(prefix: String): Unit = { - } -} - -/** Actual namer functionality. - */ -class NamingContext extends NamingContextInterface { - val descendants = new IdentityHashMap[AnyRef, ListBuffer[NamingContext]]() - val anonymousDescendants = ListBuffer[NamingContext]() - val items = ListBuffer[(AnyRef, String)]() - var closed = false // a sanity check to ensure no more name() calls are done after name_prefix - - /** Adds a NamingContext object as a descendant - where its contained objects will have names - * prefixed with the name given to the reference object, if the reference object is named in the - * scope of this context. - */ - def addDescendant(ref: Any, descendant: NamingContext) { - ref match { - case ref: AnyRef => - descendants.getOrElseUpdate(ref, ListBuffer[NamingContext]()) += descendant - case _ => anonymousDescendants += descendant - } - } - - def name[T](obj: T, name: String): T = { - assert(!closed, "Can't name elements after name_prefix called") - obj match { - case _: NoChiselNamePrefix => // Don't name things with NoChiselNamePrefix - case ref: AnyRef => items += ((ref, name)) - case _ => - } - obj - } - - def namePrefix(prefix: String): Unit = { - closed = true - for ((ref, suffix) <- items) { - // First name the top-level object - chisel3.internal.Builder.nameRecursively(prefix + suffix, ref, (id, name) => id.suggestName(name)) - - // Then recurse into descendant contexts - if (descendants.containsKey(ref)) { - for (descendant <- descendants.get(ref)) { - descendant.namePrefix(prefix + suffix + "_") - } - descendants.remove(ref) - } - } - - for (descendant <- descendants.values().flatten) { - // Where we have a broken naming link, just ignore the missing parts - descendant.namePrefix(prefix) - } - for (descendant <- anonymousDescendants) { - descendant.namePrefix(prefix) - } - } -} - -/** Class for the (global) naming stack object, which provides a way to push and pop naming - * contexts as functions are called / finished. - */ -class NamingStack { - val namingStack = Stack[NamingContext]() - - /** Creates a new naming context, where all items in the context will have their names prefixed - * with some yet-to-be-determined prefix from object names in an enclosing scope. - */ - def pushContext(): NamingContext = { - val context = new NamingContext - namingStack.push(context) - context - } - - /** Called at the end of a function, popping the current naming context, adding it to the - * enclosing context's descendants, and passing through the prefix naming reference. - * Every instance of push_context() must have a matching pop_context(). - * - * Will assert out if the context being popped isn't the topmost on the stack. - */ - def popContext[T <: Any](prefixRef: T, until: NamingContext): Unit = { - assert(namingStack.top == until) - namingStack.pop() - if (!namingStack.isEmpty) { - namingStack.top.addDescendant(prefixRef, until) - } - } - - def length() : Int = namingStack.length -} |
