Skip to content

Commit

Permalink
[core] Use State.guard for Module.apply
Browse files Browse the repository at this point in the history
Change the way that `Module.apply` works to use the new `State.guard`
method that automates the process of changing the "append point" for where
commands will be added.

Signed-off-by: Schuyler Eldridge <[email protected]>
  • Loading branch information
seldridge committed Jan 24, 2025
1 parent 897c742 commit c8a9b09
Showing 1 changed file with 22 additions and 57 deletions.
79 changes: 22 additions & 57 deletions core/src/main/scala/chisel3/ModuleImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,63 +69,31 @@ private[chisel3] trait ObjectModuleImpl {
}
Builder.readyForModuleConstr = true

val parent = Builder.currentModule
val parentWhenStack = Builder.whenStack
val parentBlockStack = Builder.blockStack
val parentLayerStack = Builder.layerStack

// Save then clear clock and reset to prevent leaking scope, must be set again in the Module
// Note that Disable is a function of whatever the current reset is, so it does not need a port
// and thus does not change when we cross module boundaries
val (saveClock, saveReset) = (Builder.currentClockDelayed, Builder.currentResetDelayed)
val savePrefix = Builder.getPrefix
Builder.clearPrefix()
Builder.currentClock = None
Builder.currentReset = None

// Save the currently enabled layer. Clear any enabled layers.
val saveEnabledLayers = Builder.enabledLayers
Builder.enabledLayers = LinkedHashSet.empty

// Execute the module, this has the following side effects:
// - set currentModule
// - unset readyForModuleConstr
// - reset whenStack to be empty
// - reset blockStack to body or empty if none
// - reset layerStack to be root :: nil
// - set currentClockAndReset
val module: T = bc // bc is actually evaluated here
val module = Builder.State.guard(Builder.State.default) {
val module: T = bc

require(Builder.blockDepth == module.getBody.size, "block leftover")
if (Builder.whenDepth != 0) {
throwException("Internal Error! when() scope depth is != 0, this should have been caught!")
}
if (Builder.readyForModuleConstr) {
throwException(
"Error: attempted to instantiate a Module, but nothing happened. " +
"This is probably due to rewrapping a Module instance with Module()." +
sourceInfo.makeMessage(" See " + _)
)
}
if (Builder.whenDepth != 0) {
throwException("Internal Error! when() scope depth is != 0, this should have been caught!")
}
if (Builder.readyForModuleConstr) {
throwException(
"Error: attempted to instantiate a Module, but nothing happened. " +
"This is probably due to rewrapping a Module instance with Module()." +
sourceInfo.makeMessage(" See " + _)
)
}

// Only add the component if the module generates one
val componentOpt = module.generateComponent()
for (component <- componentOpt) {
Builder.components += component
}
// Only add the component if the module generates one
val componentOpt = module.generateComponent()
for (component <- componentOpt) {
Builder.components += component
}

if (module.localModulePrefix.isDefined) {
Builder.popModulePrefix() // Pop localModulePrefix if it was defined
}

// Reset Builder state *after* generating the component, so any atModuleBodyEnd generators are still within the
// scope of the current Module.
Builder.currentModule = parent // Back to parent!
Builder.whenStack = parentWhenStack
Builder.blockStack = parentBlockStack
Builder.layerStack = parentLayerStack
Builder.currentClock = saveClock // Back to clock and reset scope
Builder.currentReset = saveReset
Builder.setPrefix(savePrefix)
Builder.enabledLayers = saveEnabledLayers
if (module.localModulePrefix.isDefined) {
Builder.popModulePrefix() // Pop localModulePrefix if it was defined
module
}

module.moduleBuilt()
Expand Down Expand Up @@ -509,10 +477,7 @@ package experimental {
readyForModuleConstr = false

Builder.currentModule = Some(this)
Builder.whenStack = Nil
Builder.blockStack = Nil
getBody.foreach(Builder.pushBlock(_))
Builder.layerStack = layer.Layer.root :: Nil
}

//
Expand Down

0 comments on commit c8a9b09

Please sign in to comment.