2016-08-19 20:08:35 +02:00
|
|
|
// See LICENSE for license details.
|
|
|
|
|
2016-10-04 00:17:36 +02:00
|
|
|
package diplomacy
|
2016-08-19 20:08:35 +02:00
|
|
|
|
|
|
|
import Chisel._
|
2016-09-08 23:41:08 +02:00
|
|
|
import chisel3.internal.sourceinfo.{SourceInfo, SourceLine, UnlocatableSourceInfo}
|
2016-08-19 20:08:35 +02:00
|
|
|
|
2016-08-31 19:25:46 +02:00
|
|
|
abstract class LazyModule
|
2016-08-19 20:08:35 +02:00
|
|
|
{
|
2016-10-04 00:17:36 +02:00
|
|
|
protected[diplomacy] var bindings = List[() => Unit]()
|
|
|
|
protected[diplomacy] var children = List[LazyModule]()
|
|
|
|
protected[diplomacy] var nodes = List[BaseNode]()
|
|
|
|
protected[diplomacy] var info: SourceInfo = UnlocatableSourceInfo
|
|
|
|
protected[diplomacy] val parent = LazyModule.stack.headOption
|
2016-09-02 20:13:43 +02:00
|
|
|
|
|
|
|
LazyModule.stack = this :: LazyModule.stack
|
|
|
|
parent.foreach(p => p.children = this :: p.children)
|
2016-08-19 20:08:35 +02:00
|
|
|
|
2016-09-08 23:41:08 +02:00
|
|
|
def name = getClass.getName.split('.').last
|
2016-09-09 06:11:31 +02:00
|
|
|
def line = sourceLine(info)
|
2016-09-08 23:41:08 +02:00
|
|
|
|
2016-08-31 19:25:46 +02:00
|
|
|
def module: LazyModuleImp
|
2016-08-19 20:08:35 +02:00
|
|
|
|
2016-10-04 00:17:36 +02:00
|
|
|
protected[diplomacy] def instantiate() = {
|
2016-09-02 20:13:43 +02:00
|
|
|
children.reverse.foreach { c =>
|
|
|
|
// !!! fix chisel3 so we can pass the desired sourceInfo
|
|
|
|
// implicit val sourceInfo = c.module.outer.info
|
|
|
|
Module(c.module)
|
2016-08-19 20:08:35 +02:00
|
|
|
}
|
2016-09-02 20:13:43 +02:00
|
|
|
bindings.reverse.foreach { f => f () }
|
2016-08-19 20:08:35 +02:00
|
|
|
}
|
2016-09-26 10:19:28 +02:00
|
|
|
|
|
|
|
def omitGraphML = nodes.isEmpty && children.isEmpty
|
|
|
|
lazy val graphML: String = parent.map(_.graphML).getOrElse {
|
|
|
|
val buf = new StringBuilder
|
|
|
|
buf ++= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
|
|
buf ++= "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:y=\"http://www.yworks.com/xml/graphml\">\n"
|
2016-10-03 23:07:28 +02:00
|
|
|
buf ++= " <key for=\"node\" id=\"n\" yfiles.type=\"nodegraphics\"/>\n"
|
|
|
|
buf ++= " <key for=\"edge\" id=\"e\" yfiles.type=\"edgegraphics\"/>\n"
|
2016-09-26 10:19:28 +02:00
|
|
|
buf ++= " <graph id=\"G\" edgedefault=\"directed\">\n"
|
|
|
|
nodesGraphML(buf, " ")
|
|
|
|
edgesGraphML(buf, " ")
|
|
|
|
buf ++= " </graph>\n"
|
|
|
|
buf ++= "</graphml>\n"
|
|
|
|
buf.toString
|
|
|
|
}
|
|
|
|
|
|
|
|
private val index = { LazyModule.index = LazyModule.index + 1; LazyModule.index }
|
|
|
|
|
|
|
|
private def nodesGraphML(buf: StringBuilder, pad: String) {
|
|
|
|
buf ++= s"""${pad}<node id=\"${index}\">\n"""
|
2016-10-03 23:07:28 +02:00
|
|
|
buf ++= s"""${pad} <data key=\"n\"><y:ShapeNode><y:NodeLabel modelName=\"sides\" modelPosition=\"w\" fontSize=\"10\" borderDistance=\"1.0\" rotationAngle=\"270.0\">${module.name}</y:NodeLabel></y:ShapeNode></data>\n"""
|
2016-09-26 10:19:28 +02:00
|
|
|
buf ++= s"""${pad} <graph id=\"${index}::\" edgedefault=\"directed\">\n"""
|
|
|
|
nodes.filter(!_.omitGraphML).foreach { n =>
|
|
|
|
buf ++= s"""${pad} <node id=\"${index}::${n.index}\"/>\n"""
|
|
|
|
}
|
|
|
|
children.filter(!_.omitGraphML).foreach { _.nodesGraphML(buf, pad + " ") }
|
|
|
|
buf ++= s"""${pad} </graph>\n"""
|
|
|
|
buf ++= s"""${pad}</node>\n"""
|
|
|
|
}
|
|
|
|
private def edgesGraphML(buf: StringBuilder, pad: String) {
|
|
|
|
nodes.filter(!_.omitGraphML) foreach { n => n.outputs.filter(!_.omitGraphML).foreach { o =>
|
|
|
|
buf ++= pad
|
|
|
|
buf ++= "<edge"
|
|
|
|
buf ++= s""" source=\"${index}::${n.index}\""""
|
2016-10-03 23:07:28 +02:00
|
|
|
buf ++= s""" target=\"${o.lazyModule.index}::${o.index}\"><data key=\"e\"><y:PolyLineEdge><y:Arrows source=\"none\" target=\"standard\"/><y:LineStyle color=\"${o.colour}\" type=\"line\" width=\"1.0\"/></y:PolyLineEdge></data></edge>\n"""
|
2016-09-26 10:19:28 +02:00
|
|
|
} }
|
|
|
|
children.filter(!_.omitGraphML).foreach { c => c.edgesGraphML(buf, pad) }
|
|
|
|
}
|
2016-09-02 20:13:43 +02:00
|
|
|
}
|
2016-09-01 01:45:18 +02:00
|
|
|
|
2016-09-02 20:13:43 +02:00
|
|
|
object LazyModule
|
|
|
|
{
|
2016-10-04 00:17:36 +02:00
|
|
|
protected[diplomacy] var stack = List[LazyModule]()
|
2016-09-26 10:19:28 +02:00
|
|
|
private var index = 0
|
2016-09-02 20:13:43 +02:00
|
|
|
|
|
|
|
def apply[T <: LazyModule](bc: T)(implicit sourceInfo: SourceInfo): T = {
|
|
|
|
// Make sure the user put LazyModule around modules in the correct order
|
|
|
|
// If this require fails, probably some grandchild was missing a LazyModule
|
|
|
|
// ... or you applied LazyModule twice
|
2016-09-09 06:11:31 +02:00
|
|
|
require (!stack.isEmpty, s"LazyModule() applied to ${bc.name} twice ${sourceLine(sourceInfo)}")
|
|
|
|
require (stack.head eq bc, s"LazyModule() applied to ${bc.name} before ${stack.head.name} ${sourceLine(sourceInfo)}")
|
2016-09-02 20:13:43 +02:00
|
|
|
stack = stack.tail
|
|
|
|
bc.info = sourceInfo
|
|
|
|
bc
|
|
|
|
}
|
2016-08-19 20:08:35 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 08:46:44 +02:00
|
|
|
abstract class LazyModuleImp(outer: LazyModule) extends Module
|
2016-08-30 02:53:31 +02:00
|
|
|
{
|
2016-09-02 20:13:43 +02:00
|
|
|
// .module had better not be accessed while LazyModules are still being built!
|
2016-09-09 06:11:31 +02:00
|
|
|
require (LazyModule.stack.isEmpty, s"${outer.name}.module was constructed before LazyModule() was run on ${LazyModule.stack.head.name}")
|
2016-09-02 20:13:43 +02:00
|
|
|
|
2016-09-08 23:41:08 +02:00
|
|
|
override def desiredName = outer.name
|
2016-08-31 19:25:46 +02:00
|
|
|
outer.instantiate()
|
2016-08-19 20:08:35 +02:00
|
|
|
}
|