diff options
| author | Adam Izraelevitz | 2017-03-09 15:52:34 -0800 |
|---|---|---|
| committer | Adam Izraelevitz | 2017-03-10 10:33:10 -0800 |
| commit | 1c867150215511930c53c2f15f1d5f4e3fee666c (patch) | |
| tree | c4cb59961e97dae7dc05d1b72a91518fec870125 /src/main/scala/tutorial | |
| parent | 94480ef141fa91ac18b997674aafd92250d1ca3f (diff) | |
Added tutorial pass
Diffstat (limited to 'src/main/scala/tutorial')
| -rw-r--r-- | src/main/scala/tutorial/AnalyzeCircuit.scala | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/main/scala/tutorial/AnalyzeCircuit.scala b/src/main/scala/tutorial/AnalyzeCircuit.scala new file mode 100644 index 00000000..bde75027 --- /dev/null +++ b/src/main/scala/tutorial/AnalyzeCircuit.scala @@ -0,0 +1,49 @@ +package tutorial + +// Compiler Infrastructure +import firrtl.{Transform, LowForm, CircuitState} +// Firrtl IR classes +import firrtl.ir.{Circuit, DefModule, Statement, Expression, Mux} +// Map functions +import firrtl.Mappers._ +// Scala's mutable collections +import scala.collection.mutable + +class Ledger { + var moduleName: Option[String] = None + val moduleMuxMap = mutable.Map[String, Int]() + def foundMux: Unit = moduleName match { + case None => error("Module name not defined in Ledger!") + case Some(name) => moduleMuxMap(name) = moduleMuxMap.getOrElse(name, 0) + 1 + } + def setModuleName(name: String): Unit = { + moduleName = Some(name) + } + def serialize: String = { + moduleMuxMap map { case (module, nMux) => s"$module => $nMux" } mkString "\n" + } +} + +class AnalyzeCircuit extends Transform { + def inputForm = LowForm + def outputForm = LowForm + def execute(state: CircuitState): CircuitState = { + val ledger = new Ledger() + state.circuit map walkModule(ledger) + println(ledger.serialize) + state + } + def walkModule(ledger: Ledger)(m: DefModule): DefModule = { + ledger.setModuleName(m.name) + m map walkStatement(ledger) + } + def walkStatement(ledger: Ledger)(s: Statement): Statement = { + s map walkExpression(ledger) map walkStatement(ledger) + } + def walkExpression(ledger: Ledger)(e: Expression): Expression = e match { + case Mux(cond, tval, fval, tpe) => + ledger.foundMux + e + case e => e map walkExpression(ledger) + } +} |
