diff options
| author | Andrew Waterman | 2015-08-08 16:34:35 -0700 |
|---|---|---|
| committer | Andrew Waterman | 2015-08-09 21:31:03 -0700 |
| commit | 1c9f3936172683a464e59a6e5b935381b7cbffe7 (patch) | |
| tree | 328c6053968ad3ae7586cefbd4ada633a97d55ee /src | |
| parent | c22364ee6495e8796b2db5ae2549f7164f0aee1d (diff) | |
Regularize literal handling
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/Chisel/Core.scala | 24 | ||||
| -rw-r--r-- | src/main/scala/Chisel/FP.scala | 36 |
2 files changed, 25 insertions, 35 deletions
diff --git a/src/main/scala/Chisel/Core.scala b/src/main/scala/Chisel/Core.scala index 79e2c145..c348b3a4 100644 --- a/src/main/scala/Chisel/Core.scala +++ b/src/main/scala/Chisel/Core.scala @@ -238,8 +238,6 @@ abstract class Definition extends Command { def id: Id def name = Builder.globalRefMap.getRefForId(id).name } -case class DefUInt(id: Id, value: BigInt, width: Int) extends Definition -case class DefSInt(id: Id, value: BigInt, width: Int) extends Definition case class DefFlo(id: Id, value: Float) extends Definition case class DefDbl(id: Id, value: Double) extends Definition case class DefPrim[T <: Data](id: T, op: PrimOp, args: Arg*) extends Definition @@ -316,7 +314,7 @@ abstract class Data(dirArg: Direction) extends Id { pushCommand(BulkConnect(this.lref, that.lref)) private[Chisel] def collectElts = { } private[Chisel] def lref: Alias = Alias(this) - private[Chisel] def ref: Arg = if (isLit) litArg() else lref + private[Chisel] def ref: Arg = if (isLit) litArg.get else lref private[Chisel] def debugName = _mod.debugName + "." + Builder.globalRefMap.getRefForId(this).debugName private[Chisel] def cloneTypeWidth(width: Width): this.type @@ -324,9 +322,9 @@ abstract class Data(dirArg: Direction) extends Id { def <> (that: Data): Unit = this badConnect that def cloneType: this.type def name = Builder.globalRefMap.getRefForId(this).name - def litArg(): LitArg = None.get - def litValue(): BigInt = None.get - def isLit(): Boolean = false + def litArg(): Option[LitArg] = None + def litValue(): BigInt = litArg.get.num + def isLit(): Boolean = litArg.isDefined def floLitValue: Float = intBitsToFloat(litValue().toInt) def dblLitValue: Double = longBitsToDouble(litValue().toLong) @@ -367,9 +365,10 @@ object Reg { private[Chisel] def makeType[T <: Data](t: T = null, next: T = null, init: T = null): T = { if (t ne null) t.cloneType else if (next ne null) next.cloneTypeWidth(Width()) - else if (init ne null) { - if (init.isLit && init.litArg.forcedWidth) init.cloneType - else init.cloneTypeWidth(Width()) + else if (init ne null) init.litArg match { + // For e.g. Reg(init=UInt(0, k)), fix the Reg's width to k + case Some(lit) if lit.forcedWidth => init.cloneType + case _ => init.cloneTypeWidth(Width()) } else throwException("cannot infer type") } @@ -585,10 +584,7 @@ sealed class Clock(dirArg: Direction) extends Element(dirArg, Width(1)) { } } -sealed abstract class Bits(dirArg: Direction, width: Width, lit: Option[LitArg]) extends Element(dirArg, width) { - override def litArg(): LitArg = lit.get - override def isLit(): Boolean = lit.isDefined - override def litValue(): BigInt = lit.get.num +sealed abstract class Bits(dirArg: Direction, width: Width, override val litArg: Option[LitArg]) extends Element(dirArg, width) { def fromInt(x: BigInt): this.type = makeLit(x) def makeLit(value: BigInt): this.type def cloneType: this.type = cloneTypeWidth(width) @@ -1100,8 +1096,6 @@ class Emitter(circuit: Circuit) { case e: ClockType => s"Clock" } private def emit(e: Command): String = e match { - case e: DefUInt => s"node ${e.name} = UInt<${e.width}>(${e.value})" - case e: DefSInt => s"node ${e.name} = SInt<${e.width}>(${e.value})" case e: DefFlo => s"node ${e.name} = Flo(${e.value})" case e: DefDbl => s"node ${e.name} = Dbl(${e.value})" case e: DefPrim[_] => s"node ${e.name} = ${emit(e.op)}(${join(e.args.map(x => emit(x)), ", ")})" diff --git a/src/main/scala/Chisel/FP.scala b/src/main/scala/Chisel/FP.scala index 2c50795d..3d346dcc 100644 --- a/src/main/scala/Chisel/FP.scala +++ b/src/main/scala/Chisel/FP.scala @@ -35,16 +35,18 @@ import ChiselError._ /// FLO +case class FloLit(num: Float) extends Arg { + def name = s"Flo(${num.toString})" +} + +case class DblLit(num: Double) extends Arg { + def name = s"Dbl(${num.toString})" +} + object Flo { - def apply(x: Float): Flo = floLit(x) + def apply(x: Float): Flo = new Flo(NO_DIR, Some(FloLit(x))) def apply(x: Double): Flo = Flo(x.toFloat) - def floLit(value: Float): Flo = { - val b = new Flo(NO_DIR, Some(value)) - pushCommand(DefFlo(b, value)) - b - } - def apply(dir: Direction = null): Flo = - new Flo(dir) + def apply(dir: Direction = null): Flo = new Flo(dir) } object FloPrimOp { @@ -88,9 +90,9 @@ sealed abstract class FloBase[T <: Data](dir: Direction, width: Width) extends E def toUInt = toBits } -class Flo(dir: Direction = NO_DIR, val value:Option[Float] = None) extends FloBase[Flo](dir, Width(32)) with Num[Flo] { +class Flo(dir: Direction = NO_DIR, val value:Option[FloLit] = None) extends FloBase[Flo](dir, Width(32)) with Num[Flo] { type T = Flo; - override def floLitValue: Float = value.get + override def floLitValue: Float = value.get.num def cloneTypeWidth(width: Width): this.type = cloneType override def fromBits(n: Bits): this.type = pushOp(DefPrim(cloneType, BitsToFlo, this.ref)).asInstanceOf[this.type] @@ -134,14 +136,8 @@ import java.lang.Double.doubleToLongBits object Dbl { def apply(x: Float): Dbl = Dbl(x.toDouble); - def apply(x: Double): Dbl = dblLit(x) - def dblLit(value: Double): Dbl = { - val b = new Dbl(NO_DIR, Some(value)) - pushCommand(DefDbl(b, value)) - b - } - def apply(dir: Direction = NO_DIR): Dbl = - new Dbl(dir) + def apply(x: Double): Dbl = new Dbl(NO_DIR, Some(DblLit(x))) + def apply(dir: Direction = NO_DIR): Dbl = new Dbl(dir) } object DblPrimOp { @@ -174,9 +170,9 @@ object DblPrimOp { } import DblPrimOp._ -class Dbl(dir: Direction, val value: Option[Double] = None) extends FloBase[Dbl](dir, Width(64)) with Num[Dbl] { +class Dbl(dir: Direction, val value: Option[DblLit] = None) extends FloBase[Dbl](dir, Width(64)) with Num[Dbl] { type T = Dbl; - override def dblLitValue: Double = value.get + override def dblLitValue: Double = value.get.num def cloneTypeWidth(width: Width): this.type = cloneType override def fromBits(n: Bits): this.type = pushOp(DefPrim(cloneType, BitsToDbl, this.ref)).asInstanceOf[this.type] |
