Skip to content

Commit

Permalink
Eagerly throw error if certain annotations are used on non-hardware t…
Browse files Browse the repository at this point in the history
…ypes (#2700)
  • Loading branch information
zyedidia authored Aug 25, 2022
1 parent fb8ea2a commit a4227b3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/scala/chisel3/util/experimental/ForceNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package chisel3.util.experimental

import chisel3.experimental.{annotate, ChiselAnnotation, RunFirrtlTransform}
import chisel3.internal.Builder
import firrtl.Mappers._
import firrtl._
import firrtl.annotations._
Expand All @@ -24,6 +25,7 @@ object forceName {
* @param name Name to force to
*/
def apply[T <: chisel3.Element](signal: T, name: String): T = {
if (!signal.isSynthesizable) Builder.error(s"Using forceName '$name' on non-hardware value $signal")
annotate(new ChiselAnnotation with RunFirrtlTransform {
def toFirrtl = ForceNameAnnotation(signal.toTarget, name)
override def transformClass: Class[_ <: Transform] = classOf[ForceNamesTransform]
Expand All @@ -37,6 +39,7 @@ object forceName {
* @param signal Signal to name
*/
def apply[T <: chisel3.Element](signal: T): T = {
if (!signal.isSynthesizable) Builder.error(s"Using forceName on non-hardware value $signal")
annotate(new ChiselAnnotation with RunFirrtlTransform {
def toFirrtl = ForceNameAnnotation(signal.toTarget, signal.toTarget.ref)
override def transformClass: Class[_ <: Transform] = classOf[ForceNamesTransform]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ object decoder extends LazyLogging {
val (plaInput, plaOutput) =
pla(minimizedTable.table.toSeq, BitPat(minimizedTable.default.value.U(minimizedTable.default.getWidth.W)))

assert(plaOutput.isSynthesizable, s"Using DecodeTableAnnotation on non-hardware value $plaOutput")
annotate(new ChiselAnnotation {
override def toFirrtl: Annotation =
DecodeTableAnnotation(plaOutput.toTarget, truthTable.toString, minimizedTable.toString)
Expand Down
14 changes: 14 additions & 0 deletions src/test/scala/chiselTests/experimental/ForceNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,18 @@ class ForceNamesSpec extends ChiselFlatSpec {
)
}
}
"Force Name of non-hardware value" should "error" in {
class Example extends Module {
val tpe = UInt(8.W)
forceName(tpe, "foobar")

val in = IO(Input(tpe))
val out = IO(Output(tpe))
out := in
}

a[ChiselException] shouldBe thrownBy {
chisel3.stage.ChiselStage.elaborate(new Example)
}
}
}

0 comments on commit a4227b3

Please sign in to comment.