aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJack2016-05-18 12:02:52 -0700
committerjackkoenig2016-09-12 21:30:38 -0700
commit9edf656e11084958d9e90807a4740a57b83babfe (patch)
tree6bddf2376fe3e64668b5e59f350cfbbcf86dfaee /src
parentf7dd234f7c5a2dc03c42640db11b1d6509108643 (diff)
Legalize bit select. Run Legalize after PadWidths.
Bit selecting a literal resulted in invalid Verilog. Legalize now deals with this by replacing any bits select of UInt or SInt literals with a new literal composed of the selected bits. Legalize also is now run after PadWidths because that pass introduces this issue. Fixes #170
Diffstat (limited to 'src')
-rw-r--r--src/main/scala/firrtl/LoweringCompilers.scala2
-rw-r--r--src/main/scala/firrtl/passes/Passes.scala11
2 files changed, 12 insertions, 1 deletions
diff --git a/src/main/scala/firrtl/LoweringCompilers.scala b/src/main/scala/firrtl/LoweringCompilers.scala
index c8430d2b..4d40d9a8 100644
--- a/src/main/scala/firrtl/LoweringCompilers.scala
+++ b/src/main/scala/firrtl/LoweringCompilers.scala
@@ -124,7 +124,6 @@ class HighFirrtlToMiddleFirrtl () extends Transform with SimpleRun {
// TODO(izraelevitz): Create RenameMap from RemoveCHIRRTL
class MiddleFirrtlToLowFirrtl () extends Transform with SimpleRun {
val passSeq = Seq(
- passes.Legalize,
passes.LowerTypes,
passes.ResolveKinds,
passes.InferTypes,
@@ -146,6 +145,7 @@ class EmitVerilogFromLowFirrtl (val writer: Writer) extends Transform with Simpl
passes.ConstProp,
passes.PadWidths,
passes.ConstProp,
+ passes.Legalize,
passes.VerilogWrap,
passes.SplitExpressions,
passes.CommonSubexpressionElimination,
diff --git a/src/main/scala/firrtl/passes/Passes.scala b/src/main/scala/firrtl/passes/Passes.scala
index 0405701f..66437556 100644
--- a/src/main/scala/firrtl/passes/Passes.scala
+++ b/src/main/scala/firrtl/passes/Passes.scala
@@ -228,6 +228,16 @@ object Legalize extends Pass {
e
}
}
+ private def legalizeBits(expr: DoPrim): Expression = {
+ lazy val (hi, low) = (expr.consts(0), expr.consts(1))
+ lazy val mask = (BigInt(1) << (hi - low + 1).toInt) - 1
+ lazy val width = IntWidth(hi - low + 1)
+ expr.args.head match {
+ case UIntLiteral(value, _) => UIntLiteral((value >> low.toInt) & mask, width)
+ case SIntLiteral(value, _) => SIntLiteral((value >> low.toInt) & mask, width)
+ case _ => expr
+ }
+ }
private def legalizeConnect(c: Connect): Statement = {
val t = c.loc.tpe
val w = bitWidth(t)
@@ -246,6 +256,7 @@ object Legalize extends Pass {
def legalizeE(expr: Expression): Expression = expr map legalizeE match {
case prim: DoPrim => prim.op match {
case Shr => legalizeShiftRight(prim)
+ case Bits => legalizeBits(prim)
case _ => prim
}
case e => e // respect pre-order traversal