Skip to content

Commit

Permalink
Change prefix stack to List[String] (bp #1617) (#1628)
Browse files Browse the repository at this point in the history
* Change prefix stack to List[String] (#1617)

This allows for sharing of the prefix stacks between Data, and removes a
boxing per prefix stack element that is no longer necessary.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit fef0b68)

* Add binary compatibility waivers

Co-authored-by: Jack Koenig <koenig@sifive.com>
  • Loading branch information
mergify[bot] and jackkoenig authored Oct 20, 2020
1 parent 6e75f67 commit 8dd2ed1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ lazy val core = (project in file("core")).
ProblemFilters.exclude[IncompatibleResultTypeProblem]("chisel3.internal.Builder.pushPrefix"),
ProblemFilters.exclude[IncompatibleResultTypeProblem]("chisel3.internal.Builder.exception"),
ProblemFilters.exclude[IncompatibleResultTypeProblem]("chisel3.internal.Builder.pushPrefix"),
ProblemFilters.exclude[IncompatibleMethTypeProblem]("chisel3.internal.Builder.pushPrefix")
ProblemFilters.exclude[IncompatibleMethTypeProblem]("chisel3.internal.Builder.pushPrefix"),
ProblemFilters.exclude[IncompatibleResultTypeProblem]("chisel3.internal.Builder.popPrefix"),
ProblemFilters.exclude[IncompatibleResultTypeProblem]("chisel3.internal.ChiselContext.prefixStack")
)
).
settings(
Expand Down
39 changes: 18 additions & 21 deletions core/src/main/scala/chisel3/internal/Builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private[chisel3] trait HasId extends InstanceId {
private val construction_prefix: Prefix = Builder.getPrefix()

// Prefix when the latest [[suggestSeed]] or [[autoSeed]] is called
private var prefix_seed: Prefix = List.empty[Either[String, Data]]
private var prefix_seed: Prefix = Nil

// Post-seed hooks called to carry the suggested seeds to other candidates as needed
private val suggest_postseed_hooks = scala.collection.mutable.ListBuffer.empty[String=>Unit]
Expand Down Expand Up @@ -156,21 +156,18 @@ private[chisel3] trait HasId extends InstanceId {
*/
def buildName(seed: String, prefix: Prefix): String = {
val builder = new StringBuilder()
prefix.foreach {
case Left(s: String) => builder ++= s + "_"
case other => Builder.exception(s"Only Strings should exist in Prefixes, got $other")
}
prefix.foreach(builder ++= _ + "_")
builder ++= seed
builder.toString
}

if (hasSeed) {
Some(buildName(seedOpt.get, prefix_seed))
Some(buildName(seedOpt.get, prefix_seed.reverse))
} else {
defaultSeed.map { default =>
defaultPrefix match {
case Some(p) => buildName(default, Left(p) +: construction_prefix)
case None => buildName(default, construction_prefix)
case Some(p) => buildName(default, p :: construction_prefix.reverse)
case None => buildName(default, construction_prefix.reverse)
}
}
}
Expand Down Expand Up @@ -295,7 +292,7 @@ private[chisel3] class ChiselContext() {
val bundleStack: ArrayBuffer[(Bundle, String, String, Int)] = ArrayBuffer()

// Records the different prefixes which have been scoped at this point in time
val prefixStack: ArrayBuffer[Either[String, HasId]] = ArrayBuffer()
var prefixStack: Prefix = Nil
}

private[chisel3] class DynamicContext() {
Expand All @@ -322,7 +319,7 @@ private[chisel3] class DynamicContext() {
private[chisel3] object Builder {

// Represents the current state of the prefixes given
type Prefix = List[Either[String, Data]]
type Prefix = List[String]

// All global mutable state must be referenced via dynamicContextVar!!
private val dynamicContextVar = new DynamicVariable[Option[DynamicContext]](None)
Expand Down Expand Up @@ -361,7 +358,8 @@ private[chisel3] object Builder {

// Puts a prefix string onto the prefix stack
def pushPrefix(d: String): Unit = {
chiselContext.get().prefixStack += Left(d)
val context = chiselContext.get()
context.prefixStack = d :: context.prefixStack
}

/** Pushes the current name of a data onto the prefix stack
Expand Down Expand Up @@ -399,31 +397,30 @@ private[chisel3] object Builder {
}
}
buildAggName(d).map { name =>
chiselContext.get().prefixStack += Left(name)
pushPrefix(name)
}.isDefined
}

// Remove a prefix from top of the stack
def popPrefix(): Either[String, HasId] = {
val ps = chiselContext.get().prefixStack
ps.remove(ps.size - 1)
def popPrefix(): List[String] = {
val context = chiselContext.get()
val tail = context.prefixStack.tail
context.prefixStack = tail
tail
}

// Removes all prefixes from the prefix stack
def clearPrefix(): Unit = {
val ps = chiselContext.get().prefixStack
ps.clear()
chiselContext.get().prefixStack = Nil
}

// Clears existing prefixes and sets to new prefix stack
def setPrefix(prefix: Prefix): Unit = {
val ps = chiselContext.get().prefixStack
clearPrefix()
ps.insertAll(0, prefix)
chiselContext.get().prefixStack = prefix
}

// Returns the prefix stack at this moment
def getPrefix(): Prefix = chiselContext.get().prefixStack.toList.asInstanceOf[Prefix]
def getPrefix(): Prefix = chiselContext.get().prefixStack

def currentModule: Option[BaseModule] = dynamicContextVar.value match {
case Some(dyanmicContext) => dynamicContext.currentModule
Expand Down

0 comments on commit 8dd2ed1

Please sign in to comment.