1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// See LICENSE for license details.
package chisel3.internal.firrtl
import chisel3._
import chisel3.internal.sourceinfo.{NoSourceInfo, SourceLine}
private[chisel3] object Emitter {
def emit(circuit: Circuit): String = new Emitter(circuit).toString
}
private class Emitter(circuit: Circuit) {
override def toString: String = res.toString
private def emitPort(e: Port): String =
s"${e.dir} ${e.id.getRef.name} : ${e.id.toType}"
private def emit(e: Command, ctx: Component): String = {
val firrtlLine = e match {
case e: DefPrim[_] => s"node ${e.name} = ${e.op.name}(${e.args.map(_.fullName(ctx)).mkString(", ")})"
case e: DefWire => s"wire ${e.name} : ${e.id.toType}"
case e: DefReg => s"reg ${e.name} : ${e.id.toType}, ${e.clock.fullName(ctx)}"
case e: DefRegInit => s"reg ${e.name} : ${e.id.toType}, ${e.clock.fullName(ctx)} with : (reset => (${e.reset.fullName(ctx)}, ${e.init.fullName(ctx)}))"
case e: DefMemory => s"cmem ${e.name} : ${e.t.toType}[${e.size}]"
case e: DefSeqMemory => s"smem ${e.name} : ${e.t.toType}[${e.size}]"
case e: DefMemPort[_] => s"${e.dir} mport ${e.name} = ${e.source.fullName(ctx)}[${e.index.fullName(ctx)}], ${e.clock.fullName(ctx)}"
case e: Connect => s"${e.loc.fullName(ctx)} <= ${e.exp.fullName(ctx)}"
case e: BulkConnect => s"${e.loc1.fullName(ctx)} <- ${e.loc2.fullName(ctx)}"
case e: Stop => s"stop(${e.clock.fullName(ctx)}, UInt<1>(1), ${e.ret})"
case e: Printf =>
val (fmt, args) = e.pable.unpack(ctx)
val printfArgs = Seq(e.clock.fullName(ctx), "UInt<1>(1)",
"\"" + printf.format(fmt) + "\"") ++ args
printfArgs mkString ("printf(", ", ", ")")
case e: DefInvalid => s"${e.arg.fullName(ctx)} is invalid"
case e: DefInstance => s"inst ${e.name} of ${e.id.modName}"
case w: WhenBegin =>
indent()
s"when ${w.pred.fullName(ctx)} :"
case _: WhenEnd =>
unindent()
s"skip"
}
e.sourceInfo match {
case SourceLine(filename, line, col) => s"${firrtlLine} @[${filename} ${line}:${col}]"
case _: NoSourceInfo => firrtlLine
}
}
// Map of Module FIRRTL definition to FIRRTL name, if it has been emitted already.
private val defnMap = collection.mutable.HashMap[(String, String), Component]()
/** Generates the FIRRTL module declaration.
*/
private def moduleDecl(m: Component): String = m.id match {
case _: BlackBox => newline + s"extmodule ${m.name} : "
case _: Module => newline + s"module ${m.name} : "
}
/** Generates the FIRRTL module definition.
*/
private def moduleDefn(m: Component): String = {
val body = new StringBuilder
withIndent {
for (p <- m.ports)
body ++= newline + emitPort(p)
body ++= newline
m.id match {
case _: BlackBox =>
// TODO: BlackBoxes should be empty, but funkiness in Module() means
// it's not for now. Eventually, this should assert out.
case _: Module => for (cmd <- m.commands) {
body ++= newline + emit(cmd, m)
}
}
body ++= newline
}
body.toString()
}
/** Returns the FIRRTL declaration and body of a module, or nothing if it's a
* duplicate of something already emitted (on the basis of simple string
* matching).
*/
private def emit(m: Component): String = {
// Generate the body.
val defn = moduleDefn(m)
defnMap get (m.id.desiredName, defn) match {
case Some(duplicate) =>
m.id setModName duplicate.name
""
case None =>
defnMap((m.id.desiredName, defn)) = m
m.id setModName m.name
moduleDecl(m) + defn
}
}
private var indentLevel = 0
private def newline = "\n" + (" " * indentLevel)
private def indent(): Unit = indentLevel += 1
private def unindent() { require(indentLevel > 0); indentLevel -= 1 }
private def withIndent(f: => Unit) { indent(); f; unindent() }
private val res = new StringBuilder()
res ++= s";${Driver.chiselVersionString}\n"
res ++= s"circuit ${circuit.name} : "
withIndent { circuit.components.foreach(c => res ++= emit(c)) }
res ++= newline
}
|