From a2b423d647fdba6960f8b80c11cf46f12cae2c26 Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Tue, 26 Sep 2017 14:40:45 -0700 Subject: [PATCH] diplomacy: add LazyScope to post-hoc add children to a LazyModule --- src/main/scala/diplomacy/LazyModule.scala | 31 +++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/scala/diplomacy/LazyModule.scala b/src/main/scala/diplomacy/LazyModule.scala index 2d8cc512..f65e4870 100644 --- a/src/main/scala/diplomacy/LazyModule.scala +++ b/src/main/scala/diplomacy/LazyModule.scala @@ -172,16 +172,43 @@ sealed trait LazyModuleImpLike extends BaseModule } } -abstract class LazyModuleImp(val wrapper: LazyModule) extends MultiIOModule with LazyModuleImpLike { +class LazyModuleImp(val wrapper: LazyModule) extends MultiIOModule with LazyModuleImpLike { val (auto, dangles) = instantiate() } -abstract class LazyRawModuleImp(val wrapper: LazyModule) extends RawModule with LazyModuleImpLike { +class LazyRawModuleImp(val wrapper: LazyModule) extends RawModule with LazyModuleImpLike { val (auto, dangles) = withClockAndReset(Bool(false).asClock, Bool(true)) { instantiate() } } +class SimpleLazyModule(implicit p: Parameters) extends LazyModule +{ + lazy val module = new LazyModuleImp(this) +} + +trait LazyScope +{ + this: LazyModule => + def apply[T](body: => T)(implicit p: Parameters) = { + require (!LazyModule.stack.exists(x => x eq this)) + LazyModule.stack = this :: LazyModule.stack + val out = body + require (LazyModule.stack.head eq this) + LazyModule.stack = LazyModule.stack.tail + out + } +} + +object LazyScope +{ + def apply[T](name: String)(body: => T)(implicit p: Parameters) = { + val scope = LazyModule(new SimpleLazyModule with LazyScope) + scope.suggestName(name) + scope { body } + } +} + case class HalfEdge(serial: Int, index: Int) case class Dangle(source: HalfEdge, sink: HalfEdge, flipped: Boolean, name: String, data: Data)