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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
/*
Copyright (c) 2014 - 2016 The Regents of the University of
California (Regents). All Rights Reserved. Redistribution and use in
source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
two paragraphs of disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
two paragraphs of disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the Regents nor the names of its contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION
TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.
*/
package firrtl
package ir
import Utils.indent
/** Intermediate Representation */
abstract class FirrtlNode {
def serialize: String
}
abstract class Info extends FirrtlNode {
// default implementation
def serialize: String = this.toString
}
case object NoInfo extends Info {
override def toString: String = ""
}
case class FileInfo(info: StringLit) extends Info {
override def toString: String = " @[" + info.serialize + "]"
}
trait HasName {
val name: String
}
trait HasInfo {
val info: Info
}
trait IsDeclaration extends HasName with HasInfo
case class StringLit(array: Array[Byte]) extends FirrtlNode {
def serialize: String = FIRRTLStringLitHandler.escape(this)
}
/** Primitive Operation
*
* See [[PrimOps]]
*/
abstract class PrimOp extends FirrtlNode {
def serialize: String = this.toString
}
abstract class Expression extends FirrtlNode {
def tpe: Type
def mapExpr(f: Expression => Expression): Expression
def mapType(f: Type => Type): Expression
def mapWidth(f: Width => Width): Expression
}
case class Reference(name: String, tpe: Type) extends Expression with HasName {
def serialize: String = name
def mapExpr(f: Expression => Expression): Expression = this
def mapType(f: Type => Type): Expression = this.copy(tpe = f(tpe))
def mapWidth(f: Width => Width): Expression = this
}
case class SubField(expr: Expression, name: String, tpe: Type) extends Expression with HasName {
def serialize: String = s"${expr.serialize}.$name"
def mapExpr(f: Expression => Expression): Expression = this.copy(expr = f(expr))
def mapType(f: Type => Type): Expression = this.copy(tpe = f(tpe))
def mapWidth(f: Width => Width): Expression = this
}
case class SubIndex(expr: Expression, value: Int, tpe: Type) extends Expression {
def serialize: String = s"${expr.serialize}[$value]"
def mapExpr(f: Expression => Expression): Expression = this.copy(expr = f(expr))
def mapType(f: Type => Type): Expression = this.copy(tpe = f(tpe))
def mapWidth(f: Width => Width): Expression = this
}
case class SubAccess(expr: Expression, index: Expression, tpe: Type) extends Expression {
def serialize: String = s"${expr.serialize}[${index.serialize}]"
def mapExpr(f: Expression => Expression): Expression =
this.copy(expr = f(expr), index = f(index))
def mapType(f: Type => Type): Expression = this.copy(tpe = f(tpe))
def mapWidth(f: Width => Width): Expression = this
}
case class Mux(cond: Expression, tval: Expression, fval: Expression, tpe: Type) extends Expression {
def serialize: String = s"mux(${cond.serialize}, ${tval.serialize}, ${fval.serialize})"
def mapExpr(f: Expression => Expression): Expression = Mux(f(cond), f(tval), f(fval), tpe)
def mapType(f: Type => Type): Expression = this.copy(tpe = f(tpe))
def mapWidth(f: Width => Width): Expression = this
}
case class ValidIf(cond: Expression, value: Expression, tpe: Type) extends Expression {
def serialize: String = s"validif(${cond.serialize}, ${value.serialize})"
def mapExpr(f: Expression => Expression): Expression = ValidIf(f(cond), f(value), tpe)
def mapType(f: Type => Type): Expression = this.copy(tpe = f(tpe))
def mapWidth(f: Width => Width): Expression = this
}
abstract class Literal extends Expression {
val value: BigInt
val width: Width
}
case class UIntLiteral(value: BigInt, width: Width) extends Literal {
def tpe = UIntType(width)
def serialize = s"UInt${width.serialize}(" + Utils.serialize(value) + ")"
def mapExpr(f: Expression => Expression): Expression = this
def mapType(f: Type => Type): Expression = this
def mapWidth(f: Width => Width): Expression = UIntLiteral(value, f(width))
}
case class SIntLiteral(value: BigInt, width: Width) extends Literal {
def tpe = SIntType(width)
def serialize = s"SInt${width.serialize}(" + Utils.serialize(value) + ")"
def mapExpr(f: Expression => Expression): Expression = this
def mapType(f: Type => Type): Expression = this
def mapWidth(f: Width => Width): Expression = SIntLiteral(value, f(width))
}
case class DoPrim(op: PrimOp, args: Seq[Expression], consts: Seq[BigInt], tpe: Type) extends Expression {
def serialize: String = op.serialize + "(" +
(args.map(_.serialize) ++ consts.map(_.toString)).mkString(", ") + ")"
def mapExpr(f: Expression => Expression): Expression = this.copy(args = args map f)
def mapType(f: Type => Type): Expression = this.copy(tpe = f(tpe))
def mapWidth(f: Width => Width): Expression = this
}
abstract class Statement extends FirrtlNode {
def mapStmt(f: Statement => Statement): Statement
def mapExpr(f: Expression => Expression): Statement
def mapType(f: Type => Type): Statement
def mapString(f: String => String): Statement
}
case class DefWire(info: Info, name: String, tpe: Type) extends Statement with IsDeclaration {
def serialize: String = s"wire $name : ${tpe.serialize}" + info.serialize
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = this
def mapType(f: Type => Type): Statement = DefWire(info, name, f(tpe))
def mapString(f: String => String): Statement = DefWire(info, f(name), tpe)
}
case class DefRegister(
info: Info,
name: String,
tpe: Type,
clock: Expression,
reset: Expression,
init: Expression) extends Statement with IsDeclaration {
def serialize: String =
s"reg $name : ${tpe.serialize}, ${clock.serialize} with :" +
indent("\n" + s"reset => (${reset.serialize}, ${init.serialize})" + info.serialize)
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement =
DefRegister(info, name, tpe, f(clock), f(reset), f(init))
def mapType(f: Type => Type): Statement = this.copy(tpe = f(tpe))
def mapString(f: String => String): Statement = this.copy(name = f(name))
}
case class DefInstance(info: Info, name: String, module: String) extends Statement with IsDeclaration {
def serialize: String = s"inst $name of $module" + info.serialize
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = this
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = DefInstance(info, f(name), module)
}
case class DefMemory(
info: Info,
name: String,
dataType: Type,
depth: Int,
writeLatency: Int,
readLatency: Int,
readers: Seq[String],
writers: Seq[String],
readwriters: Seq[String],
// TODO: handle read-under-write
readUnderWrite: Option[String] = None) extends Statement with IsDeclaration {
def serialize: String =
s"mem $name :" + info.serialize +
indent(
(Seq("\ndata-type => " + dataType.serialize,
"depth => " + depth,
"read-latency => " + readLatency,
"write-latency => " + writeLatency) ++
(readers map ("reader => " + _)) ++
(writers map ("writer => " + _)) ++
(readwriters map ("readwriter => " + _)) ++
Seq("read-under-write => undefined")) mkString "\n")
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = this
def mapType(f: Type => Type): Statement = this.copy(dataType = f(dataType))
def mapString(f: String => String): Statement = this.copy(name = f(name))
}
case class DefNode(info: Info, name: String, value: Expression) extends Statement with IsDeclaration {
def serialize: String = s"node $name = ${value.serialize}" + info.serialize
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = DefNode(info, name, f(value))
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = DefNode(info, f(name), value)
}
case class Conditionally(
info: Info,
pred: Expression,
conseq: Statement,
alt: Statement) extends Statement with HasInfo {
def serialize: String =
s"when ${pred.serialize} :" + info.serialize +
indent("\n" + conseq.serialize) +
(if (alt == EmptyStmt) ""
else "\nelse :" + indent("\n" + alt.serialize))
def mapStmt(f: Statement => Statement): Statement = Conditionally(info, pred, f(conseq), f(alt))
def mapExpr(f: Expression => Expression): Statement = Conditionally(info, f(pred), conseq, alt)
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this
}
case class Block(stmts: Seq[Statement]) extends Statement {
def serialize: String = stmts map (_.serialize) mkString "\n"
def mapStmt(f: Statement => Statement): Statement = Block(stmts map f)
def mapExpr(f: Expression => Expression): Statement = this
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this
}
case class PartialConnect(info: Info, loc: Expression, expr: Expression) extends Statement with HasInfo {
def serialize: String = s"${loc.serialize} <- ${expr.serialize}" + info.serialize
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = PartialConnect(info, f(loc), f(expr))
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this
}
case class Connect(info: Info, loc: Expression, expr: Expression) extends Statement with HasInfo {
def serialize: String = s"${loc.serialize} <= ${expr.serialize}" + info.serialize
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = Connect(info, f(loc), f(expr))
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this
}
case class IsInvalid(info: Info, expr: Expression) extends Statement with HasInfo {
def serialize: String = s"${expr.serialize} is invalid" + info.serialize
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = IsInvalid(info, f(expr))
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this
}
case class Attach(info: Info, source: Expression, exprs: Seq[Expression]) extends Statement with HasInfo {
def serialize: String = "attach " + source.serialize + " to (" + exprs.map(_.serialize).mkString(", ") + ")"
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = Attach(info, f(source), exprs map f)
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this
}
case class Stop(info: Info, ret: Int, clk: Expression, en: Expression) extends Statement with HasInfo {
def serialize: String = s"stop(${clk.serialize}, ${en.serialize}, $ret)" + info.serialize
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = Stop(info, ret, f(clk), f(en))
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this
}
case class Print(
info: Info,
string: StringLit,
args: Seq[Expression],
clk: Expression,
en: Expression) extends Statement with HasInfo {
def serialize: String = {
val strs = Seq(clk.serialize, en.serialize, ("\"" + string.serialize + "\"")) ++
(args map (_.serialize))
"printf(" + (strs mkString ", ") + ")" + info.serialize
}
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = Print(info, string, args map f, f(clk), f(en))
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this
}
case object EmptyStmt extends Statement {
def serialize: String = "skip"
def mapStmt(f: Statement => Statement): Statement = this
def mapExpr(f: Expression => Expression): Statement = this
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this
}
abstract class Width extends FirrtlNode {
def +(x: Width): Width = (this, x) match {
case (a: IntWidth, b: IntWidth) => IntWidth(a.width + b.width)
case _ => UnknownWidth
}
def -(x: Width): Width = (this, x) match {
case (a: IntWidth, b: IntWidth) => IntWidth(a.width - b.width)
case _ => UnknownWidth
}
def max(x: Width): Width = (this, x) match {
case (a: IntWidth, b: IntWidth) => IntWidth(a.width max b.width)
case _ => UnknownWidth
}
def min(x: Width): Width = (this, x) match {
case (a: IntWidth, b: IntWidth) => IntWidth(a.width min b.width)
case _ => UnknownWidth
}
}
/** Positive Integer Bit Width of a [[GroundType]] */
object IntWidth {
private val maxCached = 1024
private val cache = new Array[IntWidth](maxCached + 1)
def apply(width: BigInt): IntWidth = {
if (0 <= width && width <= maxCached) {
val i = width.toInt
var w = cache(i)
if (w eq null) {
w = new IntWidth(width)
cache(i) = w
}
w
} else new IntWidth(width)
}
// For pattern matching
def unapply(w: IntWidth): Option[BigInt] = Some(w.width)
}
class IntWidth(val width: BigInt) extends Width with Product {
def serialize: String = s"<$width>"
override def equals(that: Any) = that match {
case w: IntWidth => width == w.width
case _ => false
}
override def hashCode = width.toInt
override def productPrefix = "IntWidth"
override def toString = s"$productPrefix($width)"
def copy(width: BigInt = width) = IntWidth(width)
def canEqual(that: Any) = that.isInstanceOf[Width]
def productArity = 1
def productElement(int: Int) = int match {
case 0 => width
case _ => throw new IndexOutOfBoundsException
}
}
case object UnknownWidth extends Width {
def serialize: String = ""
}
/** Orientation of [[Field]] */
abstract class Orientation extends FirrtlNode
case object Default extends Orientation {
def serialize: String = ""
}
case object Flip extends Orientation {
def serialize: String = "flip "
}
/** Field of [[BundleType]] */
case class Field(name: String, flip: Orientation, tpe: Type) extends FirrtlNode with HasName {
def serialize: String = flip.serialize + name + " : " + tpe.serialize
}
abstract class Type extends FirrtlNode {
def mapType(f: Type => Type): Type
def mapWidth(f: Width => Width): Type
}
abstract class GroundType extends Type {
val width: Width
def mapType(f: Type => Type): Type = this
}
object GroundType {
def unapply(ground: GroundType): Option[Width] = Some(ground.width)
}
abstract class AggregateType extends Type {
def mapWidth(f: Width => Width): Type = this
}
case class UIntType(width: Width) extends GroundType {
def serialize: String = "UInt" + width.serialize
def mapWidth(f: Width => Width): Type = UIntType(f(width))
}
case class SIntType(width: Width) extends GroundType {
def serialize: String = "SInt" + width.serialize
def mapWidth(f: Width => Width): Type = SIntType(f(width))
}
case class BundleType(fields: Seq[Field]) extends AggregateType {
def serialize: String = "{ " + (fields map (_.serialize) mkString ", ") + "}"
def mapType(f: Type => Type): Type =
BundleType(fields map (x => x.copy(tpe = f(x.tpe))))
}
case class VectorType(tpe: Type, size: Int) extends AggregateType {
def serialize: String = tpe.serialize + s"[$size]"
def mapType(f: Type => Type): Type = this.copy(tpe = f(tpe))
}
case object ClockType extends GroundType {
val width = IntWidth(1)
def serialize: String = "Clock"
def mapWidth(f: Width => Width): Type = this
}
case class AnalogType(width: Width) extends GroundType {
def serialize: String = "Analog" + width.serialize
def mapWidth(f: Width => Width): Type = AnalogType(f(width))
}
case object UnknownType extends Type {
def serialize: String = "?"
def mapType(f: Type => Type): Type = this
def mapWidth(f: Width => Width): Type = this
}
/** [[Port]] Direction */
abstract class Direction extends FirrtlNode
case object Input extends Direction {
def serialize: String = "input"
}
case object Output extends Direction {
def serialize: String = "output"
}
/** [[DefModule]] Port */
case class Port(
info: Info,
name: String,
direction: Direction,
tpe: Type) extends FirrtlNode with IsDeclaration {
def serialize: String = s"${direction.serialize} $name : ${tpe.serialize}" + info.serialize
}
/** Base class for modules */
abstract class DefModule extends FirrtlNode with IsDeclaration {
val info : Info
val name : String
val ports : Seq[Port]
protected def serializeHeader(tpe: String): String =
s"$tpe $name :" + info.serialize +
indent(ports map ("\n" + _.serialize) mkString) + "\n"
def mapStmt(f: Statement => Statement): DefModule
def mapPort(f: Port => Port): DefModule
def mapString(f: String => String): DefModule
}
/** Internal Module
*
* An instantiable hardware block
*/
case class Module(info: Info, name: String, ports: Seq[Port], body: Statement) extends DefModule {
def serialize: String = serializeHeader("module") + indent("\n" + body.serialize)
def mapStmt(f: Statement => Statement): DefModule = this.copy(body = f(body))
def mapPort(f: Port => Port): DefModule = this.copy(ports = ports map f)
def mapString(f: String => String): DefModule = this.copy(name = f(name))
}
/** External Module
*
* Generally used for Verilog black boxes
*/
case class ExtModule(info: Info, name: String, ports: Seq[Port]) extends DefModule {
def serialize: String = serializeHeader("extmodule")
def mapStmt(f: Statement => Statement): DefModule = this
def mapPort(f: Port => Port): DefModule = this.copy(ports = ports map f)
def mapString(f: String => String): DefModule = this.copy(name = f(name))
}
case class Circuit(info: Info, modules: Seq[DefModule], main: String) extends FirrtlNode with HasInfo {
def serialize: String =
s"circuit $main :" + info.serialize +
(modules map ("\n" + _.serialize) map indent mkString "\n") + "\n"
}
|