diff options
| author | Jack Koenig | 2020-06-10 12:47:44 -0700 |
|---|---|---|
| committer | GitHub | 2020-06-10 19:47:44 +0000 |
| commit | 1e497ce39fd161c681ee64c0a4a882859eb8850b (patch) | |
| tree | a0d02dce28ca6c15d1b372be109cd71dd424d2d8 /src | |
| parent | a7fe69ba40823215ed52ccb1895bd33e25eaed60 (diff) | |
Build ArrayBuffers in Block.mapStmt (#1669)
* Build ArrayBuffers in Block.mapStmt
* Have empty Block serialize as "skip"
The FIRRTL parser requires at least one indented line in each module.
Sometimes tests emit and parse modules with no contents; this ensures
there's always at least a "skip" in empty modules.
Also fix tests that expected certain skips
* Use var List as stack in Block.mapStmt impl
This replaces Iterator concatenation. In Scala 2.11, RHS recursion on
Iterators is not stack safe. This seems to have been fixed in 2.12 by
Scala PR 5033.
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Diffstat (limited to 'src')
4 files changed, 25 insertions, 23 deletions
diff --git a/src/main/scala/firrtl/ir/IR.scala b/src/main/scala/firrtl/ir/IR.scala index af3709cd..a47a4cea 100644 --- a/src/main/scala/firrtl/ir/IR.scala +++ b/src/main/scala/firrtl/ir/IR.scala @@ -421,8 +421,29 @@ object Block { } 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 serialize: String = { + val res = stmts.view.map(_.serialize).mkString("\n") + if (res.nonEmpty) res else EmptyStmt.serialize + } + def mapStmt(f: Statement => Statement): Statement = { + val res = new scala.collection.mutable.ArrayBuffer[Statement]() + var its = stmts.iterator :: Nil + while (its.nonEmpty) { + val it = its.head + if (it.hasNext) { + it.next() match { + case EmptyStmt => // flatten out + case b: Block => + its = b.stmts.iterator :: its + case other => + res.append(f(other)) + } + } else { + its = its.tail + } + } + Block(res) + } def mapExpr(f: Expression => Expression): Statement = this def mapType(f: Type => Type): Statement = this def mapString(f: String => String): Statement = this diff --git a/src/test/scala/firrtlTests/annotationTests/EliminateTargetPathsSpec.scala b/src/test/scala/firrtlTests/annotationTests/EliminateTargetPathsSpec.scala index e1cadf32..73f36cf0 100644 --- a/src/test/scala/firrtlTests/annotationTests/EliminateTargetPathsSpec.scala +++ b/src/test/scala/firrtlTests/annotationTests/EliminateTargetPathsSpec.scala @@ -386,7 +386,6 @@ class EliminateTargetPathsSpec extends FirrtlPropSpec with FirrtlMatchers { """|circuit Foo: | module Bar: | node x = UInt<1>(0) - | skip | module Foo: | inst bar of Bar | inst baz of Bar""".stripMargin @@ -394,10 +393,8 @@ class EliminateTargetPathsSpec extends FirrtlPropSpec with FirrtlMatchers { """|circuit Foo: | module Bar___Foo_bar: | node x = UInt<1>(0) - | skip | module Bar: | node x = UInt<1>(0) - | skip | module Foo: | inst bar of Bar___Foo_bar | inst baz of Bar""".stripMargin @@ -427,7 +424,6 @@ class EliminateTargetPathsSpec extends FirrtlPropSpec with FirrtlMatchers { | module Bar: | node foo = UInt<1>(0) | inst baz of Baz - | skip | module Foo: | node foo = UInt<1>(0) | inst bar of Bar diff --git a/src/test/scala/firrtlTests/annotationTests/MorphismSpec.scala b/src/test/scala/firrtlTests/annotationTests/MorphismSpec.scala index 985779ab..cc486c99 100644 --- a/src/test/scala/firrtlTests/annotationTests/MorphismSpec.scala +++ b/src/test/scala/firrtlTests/annotationTests/MorphismSpec.scala @@ -220,16 +220,12 @@ class MorphismSpec extends FlatSpec with Matchers { """|circuit Top: | module Foo: | node a = UInt<1>(0) - | skip | module Bop: | node a = UInt<1>(0) - | skip | module Fub: | node a = UInt<1>(0) - | skip | module Bar: | node a = UInt<1>(0) - | skip | module Baz: | input x: UInt<1> | inst foo of Foo @@ -246,7 +242,6 @@ class MorphismSpec extends FlatSpec with Matchers { """|circuit Top: | module Foo: | node a = UInt<1>(0) - | skip | module Baz: | input x: UInt<1> | inst foo of Foo @@ -358,10 +353,8 @@ class MorphismSpec extends FlatSpec with Matchers { """|circuit Top: | module Foo: | node a = UInt<1>(0) - | skip | module Bar: | node a = UInt<1>(0) - | skip | module Baz: | input x: UInt<1> | inst foo of Foo @@ -374,22 +367,16 @@ class MorphismSpec extends FlatSpec with Matchers { """|circuit Top : | module Foo___Top_baz_bar : | node a = UInt<1>("h0") - | skip | module Foo___Top_qux_foox : | node a = UInt<1>("h0") - | skip | module Foo___Top_qux_bar : | node a = UInt<1>("h0") - | skip | module Foo___Top_baz_foox : | node a = UInt<1>("h0") - | skip | module Foo___Top_baz_foo : | node a = UInt<1>("h0") - | skip | module Foo___Top_qux_foo : | node a = UInt<1>("h0") - | skip | module Baz___Top_baz : | input x : UInt<1> | inst foo of Foo___Top_baz_foo @@ -486,10 +473,8 @@ class MorphismSpec extends FlatSpec with Matchers { """|circuit Top: | module Foo: | node a = UInt<1>(0) - | skip | module Bar: | node a = UInt<1>(0) - | skip | module Baz: | input x: UInt<1> | inst foo of Foo @@ -502,7 +487,6 @@ class MorphismSpec extends FlatSpec with Matchers { """|circuit Top : | module Foo : | node a = UInt<1>("h0") - | skip | module Baz : | input x : UInt<1> | inst foo of Foo diff --git a/src/test/scala/firrtlTests/transforms/TopWiringTest.scala b/src/test/scala/firrtlTests/transforms/TopWiringTest.scala index e26b0445..0ac12ef8 100644 --- a/src/test/scala/firrtlTests/transforms/TopWiringTest.scala +++ b/src/test/scala/firrtlTests/transforms/TopWiringTest.scala @@ -618,7 +618,8 @@ class TopWiringTests extends MiddleTransformSpec with TopWiringTestsCommon { "--info-mode", "ignore" ) firrtl.Driver.execute(args) match { - case FirrtlExecutionSuccess(_, emitted) => parse(emitted) should be (parse(input)) + case FirrtlExecutionSuccess(_, emitted) => + parse(emitted).serialize should be (parse(input).serialize) case _ => fail } } |
