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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
// SPDX-License-Identifier: Apache-2.0
package chisel3.internal.firrtl
import chisel3._
import chisel3.experimental._
import chisel3.internal.sourceinfo.{NoSourceInfo, SourceInfo, SourceLine, UnlocatableSourceInfo}
import firrtl.{ir => fir}
import chisel3.internal.{castToInt, throwException, HasId}
import scala.annotation.{nowarn, tailrec}
import scala.collection.immutable.{Queue, VectorBuilder}
import scala.collection.immutable.LazyList // Needed for 2.12 alias
@nowarn("msg=class Port") // delete when Port becomes private
private[chisel3] object Converter {
// TODO modeled on unpack method on Printable, refactor?
def unpack(pable: Printable, ctx: Component): (String, Seq[Arg]) = pable match {
case Printables(pables) =>
val (fmts, args) = pables.map(p => unpack(p, ctx)).unzip
(fmts.mkString, args.flatten.toSeq)
case PString(str) => (str.replaceAll("%", "%%"), List.empty)
case format: FirrtlFormat =>
("%" + format.specifier, List(format.bits.ref))
case Name(data) => (data.ref.name, List.empty)
case FullName(data) => (data.ref.fullName(ctx), List.empty)
case Percent => ("%%", List.empty)
}
private def reportInternalError(msg: String): Nothing = {
val link = "https://github.com/chipsalliance/chisel3/issues/new"
val fullMsg = s"Internal Error! $msg This is a bug in Chisel, please file an issue at '$link'"
throwException(fullMsg)
}
def getRef(id: HasId, sourceInfo: SourceInfo): Arg =
id.getOptionRef.getOrElse {
val module = id._parent.map(m => s" '$id' was defined in module '$m'.").getOrElse("")
val loc = sourceInfo.makeMessage(" " + _)
reportInternalError(s"Could not get ref for '$id'$loc!$module")
}
private def clonedModuleIOError(mod: BaseModule, name: String, sourceInfo: SourceInfo): Nothing = {
val loc = sourceInfo.makeMessage(" " + _)
reportInternalError(s"Trying to convert a cloned IO of $mod inside of $mod itself$loc!")
}
def convert(info: SourceInfo): fir.Info = info match {
case _: NoSourceInfo => fir.NoInfo
case SourceLine(fn, line, col) => fir.FileInfo(fir.StringLit(s"$fn $line:$col"))
}
def convert(op: PrimOp): fir.PrimOp = firrtl.PrimOps.fromString(op.name)
def convert(dir: MemPortDirection): firrtl.MPortDir = dir match {
case MemPortDirection.INFER => firrtl.MInfer
case MemPortDirection.READ => firrtl.MRead
case MemPortDirection.WRITE => firrtl.MWrite
case MemPortDirection.RDWR => firrtl.MReadWrite
}
// TODO
// * Memoize?
// * Move into the Chisel IR?
def convert(arg: Arg, ctx: Component, info: SourceInfo): fir.Expression = arg match {
case Node(id) =>
convert(getRef(id, info), ctx, info)
case Ref(name) =>
fir.Reference(name, fir.UnknownType)
case Slot(imm, name) =>
fir.SubField(convert(imm, ctx, info), name, fir.UnknownType)
case OpaqueSlot(imm) =>
convert(imm, ctx, info)
case Index(imm, ILit(idx)) =>
fir.SubIndex(convert(imm, ctx, info), castToInt(idx, "Index"), fir.UnknownType)
case Index(imm, value) =>
fir.SubAccess(convert(imm, ctx, info), convert(value, ctx, info), fir.UnknownType)
case ModuleIO(mod, name) =>
if (mod eq ctx.id) fir.Reference(name, fir.UnknownType)
else fir.SubField(fir.Reference(getRef(mod, info).name, fir.UnknownType), name, fir.UnknownType)
case ModuleCloneIO(mod, name) =>
if (mod eq ctx.id) clonedModuleIOError(mod, name, info)
else fir.Reference(name)
case u @ ULit(n, UnknownWidth()) =>
fir.UIntLiteral(n, fir.IntWidth(u.minWidth))
case ULit(n, w) =>
fir.UIntLiteral(n, convert(w))
case slit @ SLit(n, w) =>
fir.SIntLiteral(n, convert(w))
val unsigned = if (n < 0) (BigInt(1) << slit.width.get) + n else n
val uint = convert(ULit(unsigned, slit.width), ctx, info)
fir.DoPrim(firrtl.PrimOps.AsSInt, Seq(uint), Seq.empty, fir.UnknownType)
// TODO Simplify
case fplit @ FPLit(n, w, bp) =>
val unsigned = if (n < 0) (BigInt(1) << fplit.width.get) + n else n
val uint = convert(ULit(unsigned, fplit.width), ctx, info)
val lit = bp.asInstanceOf[KnownBinaryPoint].value
fir.DoPrim(firrtl.PrimOps.AsFixedPoint, Seq(uint), Seq(lit), fir.UnknownType)
case intervalLit @ IntervalLit(n, w, bp) =>
val unsigned = if (n < 0) (BigInt(1) << intervalLit.width.get) + n else n
val uint = convert(ULit(unsigned, intervalLit.width), ctx, info)
val lit = bp.asInstanceOf[KnownBinaryPoint].value
fir.DoPrim(firrtl.PrimOps.AsInterval, Seq(uint), Seq(n, n, lit), fir.UnknownType)
case lit: ILit =>
throwException(s"Internal Error! Unexpected ILit: $lit")
}
/** Convert Commands that map 1:1 to Statements */
def convertSimpleCommand(cmd: Command, ctx: Component): Option[fir.Statement] = cmd match {
case e: DefPrim[_] =>
val consts = e.args.collect { case ILit(i) => i }
val args = e.args.flatMap {
case _: ILit => None
case other => Some(convert(other, ctx, e.sourceInfo))
}
val expr = e.op.name match {
case "mux" =>
assert(args.size == 3, s"Mux with unexpected args: $args")
fir.Mux(args(0), args(1), args(2), fir.UnknownType)
case _ =>
fir.DoPrim(convert(e.op), args, consts, fir.UnknownType)
}
Some(fir.DefNode(convert(e.sourceInfo), e.name, expr))
case e @ DefWire(info, id) =>
Some(fir.DefWire(convert(info), e.name, extractType(id, info)))
case e @ DefReg(info, id, clock) =>
Some(
fir.DefRegister(
convert(info),
e.name,
extractType(id, info),
convert(clock, ctx, info),
firrtl.Utils.zero,
convert(getRef(id, info), ctx, info)
)
)
case e @ DefRegInit(info, id, clock, reset, init) =>
Some(
fir.DefRegister(
convert(info),
e.name,
extractType(id, info),
convert(clock, ctx, info),
convert(reset, ctx, info),
convert(init, ctx, info)
)
)
case e @ DefMemory(info, id, t, size) =>
Some(firrtl.CDefMemory(convert(info), e.name, extractType(t, info), size, false))
case e @ DefSeqMemory(info, id, t, size, ruw) =>
Some(firrtl.CDefMemory(convert(info), e.name, extractType(t, info), size, true, ruw))
case e: DefMemPort[_] =>
val info = e.sourceInfo
Some(
firrtl.CDefMPort(
convert(e.sourceInfo),
e.name,
fir.UnknownType,
e.source.fullName(ctx),
Seq(convert(e.index, ctx, info), convert(e.clock, ctx, info)),
convert(e.dir)
)
)
case Connect(info, loc, exp) =>
Some(fir.Connect(convert(info), convert(loc, ctx, info), convert(exp, ctx, info)))
case BulkConnect(info, loc, exp) =>
Some(fir.PartialConnect(convert(info), convert(loc, ctx, info), convert(exp, ctx, info)))
case Attach(info, locs) =>
Some(fir.Attach(convert(info), locs.map(l => convert(l, ctx, info))))
case DefInvalid(info, arg) =>
Some(fir.IsInvalid(convert(info), convert(arg, ctx, info)))
case e @ DefInstance(info, id, _) =>
Some(fir.DefInstance(convert(info), e.name, id.name))
case e @ Stop(_, info, clock, ret) =>
Some(fir.Stop(convert(info), ret, convert(clock, ctx, info), firrtl.Utils.one, e.name))
case e @ Printf(_, info, clock, pable) =>
val (fmt, args) = unpack(pable, ctx)
Some(
fir.Print(
convert(info),
fir.StringLit(fmt),
args.map(a => convert(a, ctx, info)),
convert(clock, ctx, info),
firrtl.Utils.one,
e.name
)
)
case e @ Verification(_, op, info, clk, pred, msg) =>
val firOp = op match {
case Formal.Assert => fir.Formal.Assert
case Formal.Assume => fir.Formal.Assume
case Formal.Cover => fir.Formal.Cover
}
Some(
fir.Verification(
firOp,
convert(info),
convert(clk, ctx, info),
convert(pred, ctx, info),
firrtl.Utils.one,
fir.StringLit(msg),
e.name
)
)
case _ => None
}
/** Internal datastructure to help translate Chisel's flat Command structure to FIRRTL's AST
*
* In particular, when scoping is translated from flat with begin end to a nested datastructure
*
* @param when Current when Statement, holds info, condition, and consequence as they are
* available
* @param outer Already converted Statements that precede the current when block in the scope in
* which the when is defined (ie. 1 level up from the scope inside the when)
* @param alt Indicates if currently processing commands in the alternate (else) of the when scope
*/
// TODO we should probably have a different structure in the IR to close elses
private case class WhenFrame(when: fir.Conditionally, outer: VectorBuilder[fir.Statement], alt: Boolean)
/** Convert Chisel IR Commands into FIRRTL Statements
*
* @note ctx is needed because references to ports translate differently when referenced within
* the module in which they are defined vs. parent modules
* @param cmds Chisel IR Commands to convert
* @param ctx Component (Module) context within which we are translating
* @return FIRRTL Statement that is equivalent to the input cmds
*/
def convert(cmds: Seq[Command], ctx: Component): fir.Statement = {
var stmts = new VectorBuilder[fir.Statement]()
var scope: List[WhenFrame] = Nil
var cmdsIt = cmds.iterator.buffered
// Extra var because sometimes we want to push a Command to the head of cmdsIt
// This is more efficient than changing the iterator
var nextCmd: Command = null
while (nextCmd != null || cmdsIt.hasNext) {
val cmd = if (nextCmd != null) {
val _nextCmd = nextCmd
nextCmd = null
_nextCmd
} else {
cmdsIt.next()
}
convertSimpleCommand(cmd, ctx) match {
// Most Commands map 1:1
case Some(stmt) =>
stmts += stmt
// When scoping logic does not map 1:1 and requires pushing/popping WhenFrames
// Please see WhenFrame for more details
case None =>
cmd match {
case WhenBegin(info, pred) =>
val when = fir.Conditionally(convert(info), convert(pred, ctx, info), fir.EmptyStmt, fir.EmptyStmt)
val frame = WhenFrame(when, stmts, false)
stmts = new VectorBuilder[fir.Statement]
scope = frame :: scope
case WhenEnd(info, depth, _) =>
val frame = scope.head
val when =
if (frame.alt) frame.when.copy(alt = fir.Block(stmts.result()))
else frame.when.copy(conseq = fir.Block(stmts.result()))
// Check if this when has an else
cmdsIt.headOption match {
case Some(AltBegin(_)) =>
assert(!frame.alt, "Internal Error! Unexpected when structure!") // Only 1 else per when
scope = frame.copy(when = when, alt = true) :: scope.tail
cmdsIt.next() // Consume the AltBegin
stmts = new VectorBuilder[fir.Statement]
case _ => // Not followed by otherwise
// If depth > 0 then we need to close multiple When scopes so we add a new WhenEnd
// If we're nested we need to add more WhenEnds to ensure each When scope gets
// properly closed
if (depth > 0) {
nextCmd = WhenEnd(info, depth - 1, false)
}
stmts = frame.outer
stmts += when
scope = scope.tail
}
case OtherwiseEnd(info, depth) =>
val frame = scope.head
val when = frame.when.copy(alt = fir.Block(stmts.result()))
// TODO For some reason depth == 1 indicates the last closing otherwise whereas
// depth == 0 indicates last closing when
if (depth > 1) {
nextCmd = OtherwiseEnd(info, depth - 1)
}
stmts = frame.outer
stmts += when
scope = scope.tail
}
}
}
assert(scope.isEmpty)
fir.Block(stmts.result())
}
def convert(width: Width): fir.Width = width match {
case UnknownWidth() => fir.UnknownWidth
case KnownWidth(value) => fir.IntWidth(value)
}
def convert(bp: BinaryPoint): fir.Width = bp match {
case UnknownBinaryPoint => fir.UnknownWidth
case KnownBinaryPoint(value) => fir.IntWidth(value)
}
private def firrtlUserDirOf(d: Data): SpecifiedDirection = d match {
case d: Vec[_] =>
SpecifiedDirection.fromParent(d.specifiedDirection, firrtlUserDirOf(d.sample_element))
case d => d.specifiedDirection
}
def extractType(data: Data, info: SourceInfo): fir.Type = extractType(data, false, info)
def extractType(data: Data, clearDir: Boolean, info: SourceInfo): fir.Type = data match {
case _: Clock => fir.ClockType
case _: AsyncReset => fir.AsyncResetType
case _: ResetType => fir.ResetType
case d: EnumType => fir.UIntType(convert(d.width))
case d: UInt => fir.UIntType(convert(d.width))
case d: SInt => fir.SIntType(convert(d.width))
case d: FixedPoint => fir.FixedType(convert(d.width), convert(d.binaryPoint))
case d: Interval => fir.IntervalType(d.range.lowerBound, d.range.upperBound, d.range.firrtlBinaryPoint)
case d: Analog => fir.AnalogType(convert(d.width))
case d: Vec[_] =>
val childClearDir = clearDir ||
d.specifiedDirection == SpecifiedDirection.Input || d.specifiedDirection == SpecifiedDirection.Output
fir.VectorType(extractType(d.sample_element, childClearDir, info), d.length)
case d: Record => {
val childClearDir = clearDir ||
d.specifiedDirection == SpecifiedDirection.Input || d.specifiedDirection == SpecifiedDirection.Output
def eltField(elt: Data): fir.Field = (childClearDir, firrtlUserDirOf(elt)) match {
case (true, _) => fir.Field(getRef(elt, info).name, fir.Default, extractType(elt, true, info))
case (false, SpecifiedDirection.Unspecified | SpecifiedDirection.Output) =>
fir.Field(getRef(elt, info).name, fir.Default, extractType(elt, false, info))
case (false, SpecifiedDirection.Flip | SpecifiedDirection.Input) =>
fir.Field(getRef(elt, info).name, fir.Flip, extractType(elt, false, info))
}
if (!d._isOpaqueType)
fir.BundleType(d.elements.toIndexedSeq.reverse.map { case (_, e) => eltField(e) })
else
extractType(d.elements.head._2, childClearDir, info)
}
}
def convert(name: String, param: Param): fir.Param = param match {
case IntParam(value) => fir.IntParam(name, value)
case DoubleParam(value) => fir.DoubleParam(name, value)
case StringParam(value) => fir.StringParam(name, fir.StringLit(value))
case RawParam(value) => fir.RawStringParam(name, value)
}
def convert(port: Port, topDir: SpecifiedDirection = SpecifiedDirection.Unspecified): fir.Port = {
val resolvedDir = SpecifiedDirection.fromParent(topDir, port.dir)
val dir = resolvedDir match {
case SpecifiedDirection.Unspecified | SpecifiedDirection.Output => fir.Output
case SpecifiedDirection.Flip | SpecifiedDirection.Input => fir.Input
}
val clearDir = resolvedDir match {
case SpecifiedDirection.Input | SpecifiedDirection.Output => true
case SpecifiedDirection.Unspecified | SpecifiedDirection.Flip => false
}
val info = UnlocatableSourceInfo // Unfortunately there is no source locator for ports ATM
val tpe = extractType(port.id, clearDir, info)
fir.Port(fir.NoInfo, getRef(port.id, info).name, dir, tpe)
}
def convert(component: Component): fir.DefModule = component match {
case ctx @ DefModule(_, name, ports, cmds) =>
fir.Module(fir.NoInfo, name, ports.map(p => convert(p)), convert(cmds, ctx))
case ctx @ DefBlackBox(id, name, ports, topDir, params) =>
fir.ExtModule(
fir.NoInfo,
name,
ports.map(p => convert(p, topDir)),
id.desiredName,
params.map { case (name, p) => convert(name, p) }.toSeq
)
}
def convert(circuit: Circuit): fir.Circuit =
fir.Circuit(fir.NoInfo, circuit.components.map(convert), circuit.name)
// TODO Unclear if this should just be the default
def convertLazily(circuit: Circuit): fir.Circuit = {
val lazyModules = LazyList() ++ circuit.components
fir.Circuit(fir.NoInfo, lazyModules.map(convert), circuit.name)
}
}
|