aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/Utils.scala
blob: d767f027fb747b1e38e4dcb7cc1a9eefecf5de43 (plain)
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
// Utility functions for FIRRTL IR

/* TODO
 *  - Adopt style more similar to Chisel3 Emitter?
 *  - Find way to have generic map function instead of mapE and mapS under Stmt implicits
 */

package firrtl

import scala.collection.mutable.StringBuilder
import java.io.PrintWriter
import Primops._
//import scala.reflect.runtime.universe._

object Utils {

  // Is there a more elegant way to do this?
  private type FlagMap = Map[Symbol, Boolean]
  private val FlagMap = Map[Symbol, Boolean]().withDefaultValue(false)

  def debug(node: AST)(implicit flags: FlagMap): String = {
    if (!flags.isEmpty) {
      var str = ""
      if (flags('types)) {
        val tpe = node.getType
        if( tpe != UnknownType ) str += s"@<t:${tpe.wipeWidth.serialize}>"
      }
      str
    }
    else {
      ""
    }
  }

  implicit class BigIntUtils(bi: BigInt){
    def serialize(implicit flags: FlagMap = FlagMap): String = 
      "\"h" + bi.toString(16) + "\""
  }

  implicit class ASTUtils(ast: AST) {
    def getType(): Type = 
      ast match {
        case e: Exp => e.getType
        case s: Stmt => s.getType
        //case f: Field => f.getType
        case t: Type => t.getType
        case p: Port => p.getType
        case _ => UnknownType
      }
  }

  implicit class PrimopUtils(op: Primop) {
    def serialize(implicit flags: FlagMap = FlagMap): String = op.getString
  }

  implicit class ExpUtils(exp: Exp) {
    def serialize(implicit flags: FlagMap = FlagMap): String = {
      val ret = exp match {
        case v: UIntValue => s"UInt${v.width.serialize}(${v.value.serialize})"
        case v: SIntValue => s"SInt${v.width.serialize}(${v.value.serialize})"
        case r: Ref => r.name
        case s: Subfield => s"${s.exp.serialize}.${s.name}"
        case s: Index => s"${s.exp.serialize}[${s.value}]"
        case p: DoPrimop => 
          s"${p.op.serialize}(" + (p.args.map(_.serialize) ++ p.consts.map(_.toString)).mkString(", ") + ")"
      } 
      ret + debug(exp)
    }

    def map(f: Exp => Exp): Exp = 
      exp match {
        case s: Subfield => Subfield(f(s.exp), s.name, s.tpe)
        case i: Index => Index(f(i.exp), i.value, i.tpe)
        case p: DoPrimop => DoPrimop(p.op, p.args.map(f), p.consts, p.tpe)
        case e: Exp => e
      }

    def getType(): Type = {
      exp match {
        case v: UIntValue => UIntType(UnknownWidth)
        case v: SIntValue => SIntType(UnknownWidth)
        case r: Ref => r.tpe
        case s: Subfield => s.tpe
        case i: Index => i.tpe
        case p: DoPrimop => p.tpe
      }
    }
  }
  
  // AccessorDir
  implicit class AccessorDirUtils(dir: AccessorDir) {
    def serialize(implicit flags: FlagMap = FlagMap): String = 
      dir match {
        case Infer => "infer"
        case Read => "read"
        case Write => "write"
        case RdWr => "rdwr"
      } 
  }

  // Some Scala implicit magic to solve type erasure on Stmt map function overloading
  private trait StmtMagnet {
    def map(stmt: Stmt): Stmt
  }
  private object StmtMagnet {
    implicit def forStmt(f: Stmt => Stmt) = new StmtMagnet {
      override def map(stmt: Stmt): Stmt =
        stmt match {
          case w: When => When(w.info, w.pred, f(w.conseq), f(w.alt))
          case b: Block => Block(b.stmts.map(f))
          case s: Stmt => s
        }
    }
    implicit def forExp(f: Exp => Exp) = new StmtMagnet {
      override def map(stmt: Stmt): Stmt =
        stmt match {
          case r: DefReg => DefReg(r.info, r.name, r.tpe, f(r.clock), f(r.reset))
          case m: DefMemory => DefMemory(m.info, m.name, m.seq, m.tpe, f(m.clock))
          case i: DefInst => DefInst(i.info, i.name, f(i.module))
          case n: DefNode => DefNode(n.info, n.name, f(n.value))
          case a: DefAccessor => DefAccessor(a.info, a.name, a.dir, f(a.source), f(a.index))
          case o: OnReset => OnReset(o.info, f(o.lhs), f(o.rhs))
          case c: Connect => Connect(c.info, f(c.lhs), f(c.rhs))
          case b: BulkConnect => BulkConnect(b.info, f(b.lhs), f(b.rhs))
          case w: When => When(w.info, f(w.pred), w.conseq, w.alt)
          case a: Assert => Assert(a.info, f(a.pred))
          case s: Stmt => s 
        }
    }
  }

  implicit class StmtUtils(stmt: Stmt) {
    def serialize(implicit flags: FlagMap = FlagMap): String =
    {
      var ret = stmt match {
        case w: DefWire => s"wire ${w.name} : ${w.tpe.serialize}"
        case r: DefReg => s"reg ${r.name} : ${r.tpe.serialize}, ${r.clock.serialize}, ${r.reset.serialize}"
        case m: DefMemory => (if(m.seq) "smem" else "cmem") + 
          s" ${m.name} : ${m.tpe.serialize}, ${m.clock.serialize}"
        case i: DefInst => s"inst ${i.name} of ${i.module.serialize}"
        case n: DefNode => s"node ${n.name} = ${n.value.serialize}"
        case p: DefPoison => s"poison ${p.name} : ${p.tpe.serialize}"
        case a: DefAccessor => s"${a.dir.serialize} accessor ${a.name} = ${a.source.serialize}[${a.index.serialize}]"
        case c: Connect => s"${c.lhs.serialize} := ${c.rhs.serialize}"
        case o: OnReset => s"onreset ${o.lhs.serialize} := ${o.rhs.serialize}"
        case b: BulkConnect => s"${b.lhs.serialize} <> ${b.rhs.serialize}"
        case w: When => {
          var str = new StringBuilder(s"when ${w.pred.serialize} : ")
          withIndent { str ++= w.conseq.serialize }
          if( w.alt != EmptyStmt ) {
            str ++= newline + "else :"
            withIndent { str ++= w.alt.serialize }
          }
          str.result
        }
        //case b: Block => b.stmts.map(newline ++ _.serialize).mkString
        case b: Block => {
          val s = new StringBuilder
          b.stmts.foreach { s ++= newline ++ _.serialize }
          s.result + debug(b)
        } 
        case a: Assert => s"assert ${a.pred.serialize}"
        case EmptyStmt => "skip"
      } 
      ret + debug(stmt)
    }

    // Using implicit types to allow overloading of function type to map, see StmtMagnet above
    def map[T](f: T => T)(implicit magnet: (T => T) => StmtMagnet): Stmt = magnet(f).map(stmt)
    
    def getType(): Type =
      stmt match {
        case s: DefWire   => s.tpe
        case s: DefReg    => s.tpe
        case s: DefMemory => s.tpe
        case s: DefPoison => s.tpe
        case _ => UnknownType
      }
  }

  implicit class WidthUtils(w: Width) {
    def serialize(implicit flags: FlagMap = FlagMap): String = {
      val s = w match {
        case UnknownWidth => "" //"?"
        case w: IntWidth => s"<${w.width.toString}>"
      } 
      s + debug(w)
    }
  }

  implicit class FieldDirUtils(dir: FieldDir) {
    def serialize(implicit flags: FlagMap = FlagMap): String = {
      val s = dir match {
        case Reverse => "flip"
        case Default => ""
      } 
      s + debug(dir)
    }
    def flip(): FieldDir = {
      dir match {
        case Reverse => Default
        case Default => Reverse
      }
    }
        
    def toPortDir(): PortDir = {
      dir match {
        case Default => Output
        case Reverse => Input
      }
    }
  }

  implicit class FieldUtils(field: Field) {
    def serialize(implicit flags: FlagMap = FlagMap): String = 
      s"${field.dir.serialize} ${field.name} : ${field.tpe.serialize}" + debug(field)
    def flip(): Field = Field(field.name, field.dir.flip, field.tpe)

    def getType(): Type = field.tpe
    def toPort(): Port = Port(NoInfo, field.name, field.dir.toPortDir, field.tpe)
  }

  implicit class TypeUtils(t: Type) {
    def serialize(implicit flags: FlagMap = FlagMap): String = {
      val commas = ", " // for mkString in BundleType
        val s = t match {
          case ClockType => "Clock"
          //case UnknownType => "UnknownType"
          case UnknownType => "?"
          case t: UIntType => s"UInt${t.width.serialize}"
          case t: SIntType => s"SInt${t.width.serialize}"
          case t: BundleType => s"{${t.fields.map(_.serialize).mkString(commas)}}"
          case t: VectorType => s"${t.tpe.serialize}[${t.size}]"
        } 
        s + debug(t)
    }

    def getType(): Type = 
      t match {
        case v: VectorType => v.tpe
        case tpe: Type => UnknownType
      }

    def wipeWidth(): Type = 
      t match {
        case t: UIntType => UIntType(UnknownWidth)
        case t: SIntType => SIntType(UnknownWidth)
        case _ => t
      }
  }

  implicit class PortDirUtils(p: PortDir) {
    def serialize(implicit flags: FlagMap = FlagMap): String = {
      val s = p match {
        case Input => "input"
        case Output => "output"
      } 
      s + debug(p)
    }
    def toFieldDir(): FieldDir = {
      p match {
        case Input => Reverse
        case Output => Default
      }
    }
  }

  implicit class PortUtils(p: Port) {
    def serialize(implicit flags: FlagMap = FlagMap): String = 
      s"${p.dir.serialize} ${p.name} : ${p.tpe.serialize}" + debug(p)
    def getType(): Type = p.tpe
    def toField(): Field = Field(p.name, p.dir.toFieldDir, p.tpe)
  }

  implicit class ModuleUtils(m: Module) {
    def serialize(implicit flags: FlagMap = FlagMap): String = {
      var s = new StringBuilder(s"module ${m.name} : ")
      withIndent {
        s ++= m.ports.map(newline ++ _.serialize).mkString
        s ++= newline ++ m.stmt.serialize
      }
      s ++= debug(m)
      s.toString
    }
  }

  implicit class CircuitUtils(c: Circuit) {
    def serialize(implicit flags: FlagMap = FlagMap): String = {
      var s = new StringBuilder(s"circuit ${c.name} : ")
      withIndent { s ++= newline ++ c.modules.map(_.serialize).mkString(newline + newline) }
      s ++= newline ++ newline
      s ++= debug(c)
      s.toString
    }
  }

  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() }

}