diff options
| author | chick | 2020-08-14 19:47:53 -0700 |
|---|---|---|
| committer | Jack Koenig | 2020-08-14 19:47:53 -0700 |
| commit | 6fc742bfaf5ee508a34189400a1a7dbffe3f1cac (patch) | |
| tree | 2ed103ee80b0fba613c88a66af854ae9952610ce /src/main/scala/firrtl/passes/Uniquify.scala | |
| parent | b516293f703c4de86397862fee1897aded2ae140 (diff) | |
All of src/ formatted with scalafmt
Diffstat (limited to 'src/main/scala/firrtl/passes/Uniquify.scala')
| -rw-r--r-- | src/main/scala/firrtl/passes/Uniquify.scala | 241 |
1 files changed, 136 insertions, 105 deletions
diff --git a/src/main/scala/firrtl/passes/Uniquify.scala b/src/main/scala/firrtl/passes/Uniquify.scala index b9cd32fa..10198b33 100644 --- a/src/main/scala/firrtl/passes/Uniquify.scala +++ b/src/main/scala/firrtl/passes/Uniquify.scala @@ -2,7 +2,6 @@ package firrtl.passes - import scala.annotation.tailrec import firrtl._ import firrtl.ir._ @@ -35,12 +34,11 @@ import MemPortUtils.memType object Uniquify extends Transform with DependencyAPIMigration { override def prerequisites = - Seq( Dependency(ResolveKinds), - Dependency(InferTypes) ) ++ firrtl.stage.Forms.WorkingIR + Seq(Dependency(ResolveKinds), Dependency(InferTypes)) ++ firrtl.stage.Forms.WorkingIR override def invalidates(a: Transform): Boolean = a match { case ResolveKinds | InferTypes => true - case _ => false + case _ => false } private case class UniquifyException(msg: String) extends FirrtlInternalException(msg) @@ -55,12 +53,13 @@ object Uniquify extends Transform with DependencyAPIMigration { */ @tailrec def findValidPrefix( - prefix: String, - elts: Seq[String], - namespace: collection.mutable.HashSet[String]): String = { - elts find (elt => namespace.contains(prefix + elt)) match { + prefix: String, + elts: Seq[String], + namespace: collection.mutable.HashSet[String] + ): String = { + elts.find(elt => namespace.contains(prefix + elt)) match { case Some(_) => findValidPrefix(prefix + "_", elts, namespace) - case None => prefix + case None => prefix } } @@ -70,16 +69,16 @@ object Uniquify extends Transform with DependencyAPIMigration { * => foo, foo bar, foo bar 0, foo bar 1, foo bar 0 a, foo bar 0 b, foo bar 1 a, foo bar 1 b, foo c * }}} */ - private [firrtl] def enumerateNames(tpe: Type): Seq[Seq[String]] = tpe match { + private[firrtl] def enumerateNames(tpe: Type): Seq[Seq[String]] = tpe match { case t: BundleType => - t.fields flatMap { f => - (enumerateNames(f.tpe) map (f.name +: _)) ++ Seq(Seq(f.name)) + t.fields.flatMap { f => + (enumerateNames(f.tpe).map(f.name +: _)) ++ Seq(Seq(f.name)) } case t: VectorType => - ((0 until t.size) map (i => Seq(i.toString))) ++ - ((0 until t.size) flatMap { i => - enumerateNames(t.tpe) map (i.toString +: _) - }) + ((0 until t.size).map(i => Seq(i.toString))) ++ + ((0 until t.size).flatMap { i => + enumerateNames(t.tpe).map(i.toString +: _) + }) case _ => Seq() } @@ -87,27 +86,38 @@ object Uniquify extends Transform with DependencyAPIMigration { def stmtToType(s: Statement)(implicit sinfo: Info, mname: String): BundleType = { // Recursive helper def recStmtToType(s: Statement): Seq[Field] = s match { - case sx: DefWire => Seq(Field(sx.name, Default, sx.tpe)) + case sx: DefWire => Seq(Field(sx.name, Default, sx.tpe)) case sx: DefRegister => Seq(Field(sx.name, Default, sx.tpe)) case sx: WDefInstance => Seq(Field(sx.name, Default, sx.tpe)) - case sx: DefMemory => sx.dataType match { - case (_: UIntType | _: SIntType | _: FixedType) => - Seq(Field(sx.name, Default, memType(sx))) - case tpe: BundleType => - val newFields = tpe.fields map ( f => - DefMemory(sx.info, f.name, f.tpe, sx.depth, sx.writeLatency, - sx.readLatency, sx.readers, sx.writers, sx.readwriters) - ) flatMap recStmtToType - Seq(Field(sx.name, Default, BundleType(newFields))) - case tpe: VectorType => - val newFields = (0 until tpe.size) map ( i => - sx.copy(name = i.toString, dataType = tpe.tpe) - ) flatMap recStmtToType - Seq(Field(sx.name, Default, BundleType(newFields))) - } - case sx: DefNode => Seq(Field(sx.name, Default, sx.value.tpe)) + case sx: DefMemory => + sx.dataType match { + case (_: UIntType | _: SIntType | _: FixedType) => + Seq(Field(sx.name, Default, memType(sx))) + case tpe: BundleType => + val newFields = tpe.fields + .map(f => + DefMemory( + sx.info, + f.name, + f.tpe, + sx.depth, + sx.writeLatency, + sx.readLatency, + sx.readers, + sx.writers, + sx.readwriters + ) + ) + .flatMap(recStmtToType) + Seq(Field(sx.name, Default, BundleType(newFields))) + case tpe: VectorType => + val newFields = + (0 until tpe.size).map(i => sx.copy(name = i.toString, dataType = tpe.tpe)).flatMap(recStmtToType) + Seq(Field(sx.name, Default, BundleType(newFields))) + } + case sx: DefNode => Seq(Field(sx.name, Default, sx.value.tpe)) case sx: Conditionally => recStmtToType(sx.conseq) ++ recStmtToType(sx.alt) - case sx: Block => (sx.stmts map recStmtToType).flatten + case sx: Block => (sx.stmts.map(recStmtToType)).flatten case sx => Seq() } BundleType(recStmtToType(s)) @@ -116,40 +126,44 @@ object Uniquify extends Transform with DependencyAPIMigration { // Accepts a Type and an initial namespace // Returns new Type with uniquified names private def uniquifyNames( - t: BundleType, - namespace: collection.mutable.HashSet[String]) - (implicit sinfo: Info, mname: String): BundleType = { + t: BundleType, + namespace: collection.mutable.HashSet[String] + )( + implicit sinfo: Info, + mname: String + ): BundleType = { def recUniquifyNames(t: Type, namespace: collection.mutable.HashSet[String]): (Type, Seq[String]) = t match { case tx: BundleType => // First add everything - val newFieldsAndElts = tx.fields map { f => + val newFieldsAndElts = tx.fields.map { f => val newName = findValidPrefix(f.name, Seq(""), namespace) namespace += newName Field(newName, f.flip, f.tpe) - } map { f => f.tpe match { - case _: GroundType => (f, Seq[String](f.name)) - case _ => - val (tpe, eltsx) = recUniquifyNames(f.tpe, collection.mutable.HashSet()) - // Need leading _ for findValidPrefix, it doesn't add _ for checks - val eltsNames: Seq[String] = eltsx map (e => "_" + e) - val prefix = findValidPrefix(f.name, eltsNames, namespace) - // We added f.name in previous map, delete if we change it - if (prefix != f.name) { - namespace -= f.name - namespace += prefix - } - val newElts: Seq[String] = eltsx map (e => LowerTypes.loweredName(prefix +: Seq(e))) - namespace ++= newElts - (Field(prefix, f.flip, tpe), prefix +: newElts) + }.map { f => + f.tpe match { + case _: GroundType => (f, Seq[String](f.name)) + case _ => + val (tpe, eltsx) = recUniquifyNames(f.tpe, collection.mutable.HashSet()) + // Need leading _ for findValidPrefix, it doesn't add _ for checks + val eltsNames: Seq[String] = eltsx.map(e => "_" + e) + val prefix = findValidPrefix(f.name, eltsNames, namespace) + // We added f.name in previous map, delete if we change it + if (prefix != f.name) { + namespace -= f.name + namespace += prefix + } + val newElts: Seq[String] = eltsx.map(e => LowerTypes.loweredName(prefix +: Seq(e))) + namespace ++= newElts + (Field(prefix, f.flip, tpe), prefix +: newElts) } } val (newFields, elts) = newFieldsAndElts.unzip (BundleType(newFields), elts.flatten) case tx: VectorType => val (tpe, elts) = recUniquifyNames(tx.tpe, namespace) - val newElts = ((0 until tx.size) map (i => i.toString)) ++ - ((0 until tx.size) flatMap { i => - elts map (e => LowerTypes.loweredName(Seq(i.toString, e))) + val newElts = ((0 until tx.size).map(i => i.toString)) ++ + ((0 until tx.size).flatMap { i => + elts.map(e => LowerTypes.loweredName(Seq(i.toString, e))) }) (VectorType(tpe, tx.size), newElts) case tx => (tx, Nil) @@ -164,19 +178,26 @@ object Uniquify extends Transform with DependencyAPIMigration { // Creates a mapping from flattened references to members of $from -> // flattened references to members of $to private def createNameMapping( - from: Type, - to: Type) - (implicit sinfo: Info, mname: String): Map[String, NameMapNode] = { + from: Type, + to: Type + )( + implicit sinfo: Info, + mname: String + ): Map[String, NameMapNode] = { (from, to) match { case (fromx: BundleType, tox: BundleType) => - (fromx.fields zip tox.fields flatMap { case (f, t) => - val eltsMap = createNameMapping(f.tpe, t.tpe) - if ((f.name != t.name) || eltsMap.nonEmpty) { - Map(f.name -> NameMapNode(t.name, eltsMap)) - } else { - Map[String, NameMapNode]() - } - }).toMap + (fromx.fields + .zip(tox.fields) + .flatMap { + case (f, t) => + val eltsMap = createNameMapping(f.tpe, t.tpe) + if ((f.name != t.name) || eltsMap.nonEmpty) { + Map(f.name -> NameMapNode(t.name, eltsMap)) + } else { + Map[String, NameMapNode]() + } + }) + .toMap case (fromx: VectorType, tox: VectorType) => createNameMapping(fromx.tpe, tox.tpe) case (fromx, tox) => @@ -187,18 +208,19 @@ object Uniquify extends Transform with DependencyAPIMigration { // Maps names in expression to new uniquified names private def uniquifyNamesExp( - exp: Expression, - map: Map[String, NameMapNode]) - (implicit sinfo: Info, mname: String): Expression = { + exp: Expression, + map: Map[String, NameMapNode] + )( + implicit sinfo: Info, + mname: String + ): Expression = { // Recursive Helper - def rec(exp: Expression, m: Map[String, NameMapNode]): - (Expression, Map[String, NameMapNode]) = exp match { + def rec(exp: Expression, m: Map[String, NameMapNode]): (Expression, Map[String, NameMapNode]) = exp match { case e: WRef => if (m.contains(e.name)) { val node = m(e.name) (WRef(node.name, e.tpe, e.kind, e.flow), node.elts) - } - else (e, Map()) + } else (e, Map()) case e: WSubField => val (subExp, subMap) = rec(e.expr, m) val (retName, retMap) = @@ -218,18 +240,21 @@ object Uniquify extends Transform with DependencyAPIMigration { (WSubAccess(subExp, index, e.tpe, e.flow), subMap) case (_: UIntLiteral | _: SIntLiteral) => (exp, m) case (_: Mux | _: ValidIf | _: DoPrim) => - (exp map ((e: Expression) => uniquifyNamesExp(e, map)), m) + (exp.map((e: Expression) => uniquifyNamesExp(e, map)), m) } rec(exp, map)._1 } // Uses map to recursively rename fields of tpe private def uniquifyNamesType( - tpe: Type, - map: Map[String, NameMapNode]) - (implicit sinfo: Info, mname: String): Type = tpe match { + tpe: Type, + map: Map[String, NameMapNode] + )( + implicit sinfo: Info, + mname: String + ): Type = tpe match { case t: BundleType => - val newFields = t.fields map { f => + val newFields = t.fields.map { f => if (map.contains(f.name)) { val node = map(f.name) Field(node.name, f.flip, uniquifyNamesType(f.tpe, node.elts)) @@ -244,8 +269,11 @@ object Uniquify extends Transform with DependencyAPIMigration { } // Everything wrapped in run so that it's thread safe - @deprecated("The functionality of Uniquify is now part of LowerTypes." + - "Please file an issue with firrtl if you use Uniquify outside of the context of LowerTypes.", "Firrtl 1.4") + @deprecated( + "The functionality of Uniquify is now part of LowerTypes." + + "Please file an issue with firrtl if you use Uniquify outside of the context of LowerTypes.", + "Firrtl 1.4" + ) def execute(state: CircuitState): CircuitState = { val c = state.circuit val renames = RenameMap() @@ -263,22 +291,22 @@ object Uniquify extends Transform with DependencyAPIMigration { val nameMap = collection.mutable.HashMap[String, NameMapNode]() def uniquifyExp(e: Expression): Expression = e match { - case (_: WRef | _: WSubField | _: WSubIndex | _: WSubAccess ) => + case (_: WRef | _: WSubField | _: WSubIndex | _: WSubAccess) => uniquifyNamesExp(e, nameMap.toMap) - case e: Mux => e map uniquifyExp - case e: ValidIf => e map uniquifyExp + case e: Mux => e.map(uniquifyExp) + case e: ValidIf => e.map(uniquifyExp) case (_: UIntLiteral | _: SIntLiteral) => e - case e: DoPrim => e map uniquifyExp + case e: DoPrim => e.map(uniquifyExp) } def uniquifyStmt(s: Statement): Statement = { - s map uniquifyStmt map uniquifyExp match { + s.map(uniquifyStmt).map(uniquifyExp) match { case sx: DefWire => sinfo = sx.info if (nameMap.contains(sx.name)) { val node = nameMap(sx.name) val newType = uniquifyNamesType(sx.tpe, node.elts) - (Utils.create_exps(sx.name, sx.tpe) zip Utils.create_exps(node.name, newType)) foreach { + (Utils.create_exps(sx.name, sx.tpe).zip(Utils.create_exps(node.name, newType))).foreach { case (from, to) => renames.rename(from.serialize, to.serialize) } DefWire(sx.info, node.name, newType) @@ -290,7 +318,7 @@ object Uniquify extends Transform with DependencyAPIMigration { if (nameMap.contains(sx.name)) { val node = nameMap(sx.name) val newType = uniquifyNamesType(sx.tpe, node.elts) - (Utils.create_exps(sx.name, sx.tpe) zip Utils.create_exps(node.name, newType)) foreach { + (Utils.create_exps(sx.name, sx.tpe).zip(Utils.create_exps(node.name, newType))).foreach { case (from, to) => renames.rename(from.serialize, to.serialize) } DefRegister(sx.info, node.name, newType, sx.clock, sx.reset, sx.init) @@ -302,7 +330,7 @@ object Uniquify extends Transform with DependencyAPIMigration { if (nameMap.contains(sx.name)) { val node = nameMap(sx.name) val newType = portTypeMap(m.name) - (Utils.create_exps(sx.name, sx.tpe) zip Utils.create_exps(node.name, newType)) foreach { + (Utils.create_exps(sx.name, sx.tpe).zip(Utils.create_exps(node.name, newType))).foreach { case (from, to) => renames.rename(from.serialize, to.serialize) } WDefInstance(sx.info, node.name, sx.module, newType) @@ -317,7 +345,7 @@ object Uniquify extends Transform with DependencyAPIMigration { val mem = sx.copy(name = node.name, dataType = dataType) // Create new mapping to handle references to memory data fields val uniqueMemMap = createNameMapping(memType(sx), memType(mem)) - (Utils.create_exps(sx.name, memType(sx)) zip Utils.create_exps(node.name, memType(mem))) foreach { + (Utils.create_exps(sx.name, memType(sx)).zip(Utils.create_exps(node.name, memType(mem)))).foreach { case (from, to) => renames.rename(from.serialize, to.serialize) } nameMap(sx.name) = NameMapNode(node.name, node.elts ++ uniqueMemMap) @@ -329,9 +357,12 @@ object Uniquify extends Transform with DependencyAPIMigration { sinfo = sx.info if (nameMap.contains(sx.name)) { val node = nameMap(sx.name) - (Utils.create_exps(sx.name, s.asInstanceOf[DefNode].value.tpe) zip Utils.create_exps(node.name, sx.value.tpe)) foreach { - case (from, to) => renames.rename(from.serialize, to.serialize) - } + (Utils + .create_exps(sx.name, s.asInstanceOf[DefNode].value.tpe) + .zip(Utils.create_exps(node.name, sx.value.tpe))) + .foreach { + case (from, to) => renames.rename(from.serialize, to.serialize) + } DefNode(sx.info, node.name, sx.value) } else { sx @@ -354,19 +385,18 @@ object Uniquify extends Transform with DependencyAPIMigration { mname = m.name m match { case m: ExtModule => m - case m: Module => + case m: Module => // Adds port names to namespace and namemap nameMap ++= portNameMap(m.name) - namespace ++= create_exps("", portTypeMap(m.name)) map - LowerTypes.loweredName map (_.tail) - m.copy(body = uniquifyBody(m.body) ) + namespace ++= create_exps("", portTypeMap(m.name)).map(LowerTypes.loweredName).map(_.tail) + m.copy(body = uniquifyBody(m.body)) } } def uniquifyPorts(renames: RenameMap)(m: DefModule): DefModule = { renames.setModule(m.name) def uniquifyPorts(ports: Seq[Port]): Seq[Port] = { - val portsType = BundleType(ports map { + val portsType = BundleType(ports.map { case Port(_, name, dir, tpe) => Field(name, to_flip(dir), tpe) }) val uniquePortsType = uniquifyNames(portsType, collection.mutable.HashSet()) @@ -374,11 +404,12 @@ object Uniquify extends Transform with DependencyAPIMigration { portNameMap += (m.name -> localMap) portTypeMap += (m.name -> uniquePortsType) - ports zip uniquePortsType.fields map { case (p, f) => - (Utils.create_exps(p.name, p.tpe) zip Utils.create_exps(f.name, f.tpe)) foreach { - case (from, to) => renames.rename(from.serialize, to.serialize) - } - Port(p.info, f.name, p.direction, f.tpe) + ports.zip(uniquePortsType.fields).map { + case (p, f) => + (Utils.create_exps(p.name, p.tpe).zip(Utils.create_exps(f.name, f.tpe))).foreach { + case (from, to) => renames.rename(from.serialize, to.serialize) + } + Port(p.info, f.name, p.direction, f.tpe) } } @@ -386,12 +417,12 @@ object Uniquify extends Transform with DependencyAPIMigration { mname = m.name m match { case m: ExtModule => m.copy(ports = uniquifyPorts(m.ports)) - case m: Module => m.copy(ports = uniquifyPorts(m.ports)) + case m: Module => m.copy(ports = uniquifyPorts(m.ports)) } } sinfo = c.info - val result = Circuit(c.info, c.modules map uniquifyPorts(renames) map uniquifyModule(renames), c.main) + val result = Circuit(c.info, c.modules.map(uniquifyPorts(renames)).map(uniquifyModule(renames)), c.main) state.copy(circuit = result, renames = Some(renames)) } } |
