diff options
| -rw-r--r-- | core/src/main/scala/chisel3/Bits.scala | 18 | ||||
| -rw-r--r-- | core/src/main/scala/chisel3/package.scala | 12 | ||||
| -rw-r--r-- | macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala | 28 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/SIntOps.scala | 15 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/UIntOps.scala | 15 |
5 files changed, 70 insertions, 18 deletions
diff --git a/core/src/main/scala/chisel3/Bits.scala b/core/src/main/scala/chisel3/Bits.scala index a135a8e5..c914e88c 100644 --- a/core/src/main/scala/chisel3/Bits.scala +++ b/core/src/main/scala/chisel3/Bits.scala @@ -8,7 +8,13 @@ import chisel3.experimental.{FixedPoint, Interval} import chisel3.internal._ import chisel3.internal.Builder.pushOp import chisel3.internal.firrtl._ -import chisel3.internal.sourceinfo.{SourceInfo, SourceInfoTransform, SourceInfoWhiteboxTransform, UIntTransform} +import chisel3.internal.sourceinfo.{ + IntLiteralApplyTransform, + SourceInfo, + SourceInfoTransform, + SourceInfoWhiteboxTransform, + UIntTransform +} import chisel3.internal.firrtl.PrimOp._ import _root_.firrtl.{ir => firrtlir} import _root_.firrtl.{constraint => firrtlconstraint} @@ -94,7 +100,7 @@ sealed abstract class Bits(private[chisel3] val width: Width) extends Element wi * @param x an index * @return the specified bit */ - final def apply(x: BigInt): Bool = macro SourceInfoTransform.xArg + final def apply(x: BigInt): Bool = macro IntLiteralApplyTransform.safeApply /** @group SourceInfoTransformMacro */ final def do_apply(x: BigInt)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = { @@ -121,12 +127,12 @@ sealed abstract class Bits(private[chisel3] val width: Width) extends Element wi * * @param x an index * @return the specified bit - * @note convenience method allowing direct use of [[scala.Int]] without implicits */ - final def apply(x: Int): Bool = macro SourceInfoTransform.xArg + final def apply(x: Int): Bool = macro IntLiteralApplyTransform.safeApply /** @group SourceInfoTransformMacro */ - final def do_apply(x: Int)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = apply(BigInt(x)) + final def do_apply(x: Int)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = + do_apply(BigInt(x)) /** Returns the specified bit on this wire as a [[Bool]], dynamically addressed. * @@ -193,7 +199,7 @@ sealed abstract class Bits(private[chisel3] val width: Width) extends Element wi /** @group SourceInfoTransformMacro */ final def do_apply(x: BigInt, y: BigInt)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): UInt = - apply(castToInt(x, "High index"), castToInt(y, "Low index")) + do_apply(castToInt(x, "High index"), castToInt(y, "Low index")) private[chisel3] def unop[T <: Data](sourceInfo: SourceInfo, dest: T, op: PrimOp): T = { requireIsHardware(this, "bits operated on") diff --git a/core/src/main/scala/chisel3/package.scala b/core/src/main/scala/chisel3/package.scala index 5521c51e..87024683 100644 --- a/core/src/main/scala/chisel3/package.scala +++ b/core/src/main/scala/chisel3/package.scala @@ -7,6 +7,8 @@ import scala.collection.mutable /** This package contains the main chisel3 API. */ package object chisel3 { + import internal.chiselRuntimeDeprecated + import internal.sourceinfo.DeprecatedSourceInfo import internal.firrtl.{Port, Width} import internal.Builder @@ -39,13 +41,11 @@ package object chisel3 { case bigint => Builder.error(s"Cannot convert $bigint to Bool, must be 0 or 1"); Bool.Lit(false) } - /** Int to UInt conversion, recommended style for constants. - */ - def U: UInt = UInt.Lit(bigint, Width()) + /** Int to UInt conversion, recommended style for constants. */ + def U: UInt = UInt.Lit(bigint, Width()) // scalastyle:ignore method.name - /** Int to SInt conversion, recommended style for constants. - */ - def S: SInt = SInt.Lit(bigint, Width()) + /** Int to SInt conversion, recommended style for constants. */ + def S: SInt = SInt.Lit(bigint, Width()) // scalastyle:ignore method.name /** Int to UInt conversion with specified width, recommended style for constants. */ diff --git a/macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala b/macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala index 3e310774..6c83da00 100644 --- a/macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala +++ b/macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala @@ -249,3 +249,31 @@ class SourceInfoWhiteboxTransform(val c: whitebox.Context) extends AutoSourceTra q"$thisObj.$doFuncTerm($that)($implicitSourceInfo, $implicitCompileOptions)" } } + +// Workaround for https://github.com/sbt/sbt/issues/3966 +object IntLiteralApplyTransform + +class IntLiteralApplyTransform(val c: Context) extends AutoSourceTransform { + import c.universe._ + + def safeApply(x: c.Tree): c.Tree = { + c.macroApplication match { + case q"$_.$clazz($lit).$func.apply($arg)" => + if ( + Set("U", "S").contains(func.toString) && + Set("fromStringToLiteral", "fromIntToLiteral", "fromLongToIteral", "fromBigIntToLiteral").contains( + clazz.toString + ) + ) { + val msg = + s"""Passing an Int to .$func is usually a mistake: It does *not* set the width but does a bit extract. + |Did you mean .$func($arg.W)? + |If you want to hide this message, assign .$func to a val first, then invoke .apply($arg) + |""".stripMargin + c.warning(c.enclosingPosition, msg) + } + case _ => // do nothing + } + q"$thisObj.$doFuncTerm($x)($implicitSourceInfo, $implicitCompileOptions)" + } +} diff --git a/src/test/scala/chiselTests/SIntOps.scala b/src/test/scala/chiselTests/SIntOps.scala index 55b4c915..580a6e34 100644 --- a/src/test/scala/chiselTests/SIntOps.scala +++ b/src/test/scala/chiselTests/SIntOps.scala @@ -85,9 +85,18 @@ class SIntOpsTester(c: SIntOps) extends Tester(c) { */ class SIntLitExtractTester extends BasicTester { - assert(-5.S(1) === true.B) - assert(-5.S(2) === false.B) - assert(-5.S(100) === true.B) + assert({ + val lit = -5.S + lit(1) === true.B + }) + assert({ + val lit = -5.S + lit(2) === false.B + }) + assert({ + val lit = -5.S + lit(100) === true.B + }) assert(-5.S(3, 0) === "b1011".U) assert(-5.S(9, 0) === "b1111111011".U) assert(-5.S(4.W)(1) === true.B) diff --git a/src/test/scala/chiselTests/UIntOps.scala b/src/test/scala/chiselTests/UIntOps.scala index 0010e9ac..6569c9e7 100644 --- a/src/test/scala/chiselTests/UIntOps.scala +++ b/src/test/scala/chiselTests/UIntOps.scala @@ -172,9 +172,18 @@ class MatchedRotateLeftAndRight(w: Int = 13) extends BasicTester { } class UIntLitExtractTester extends BasicTester { - assert("b101010".U(2) === false.B) - assert("b101010".U(3) === true.B) - assert("b101010".U(100) === false.B) + assert({ + val lit = "b101010".U + lit(2) === false.B + }) + assert({ + val lit = "b101010".U + lit(3) === true.B + }) + assert({ + val lit = "b101010".U + lit(100) === false.B + }) assert("b101010".U(3, 0) === "b1010".U) assert("b101010".U(9, 0) === "b0000101010".U) |
