From b39bc25ff76762d93300975e533ec3bd4f119d1c Mon Sep 17 00:00:00 2001 From: Richard Lin Date: Tue, 22 Jan 2019 15:06:08 -0800 Subject: Define Data .toString (#985) toString on Data subtypes will now print the type and optionally binding information including literals and IO names as feasible.--- .../src/main/scala/chisel3/core/StrongEnum.scala | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala') diff --git a/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala b/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala index 889e9b11..8feb0541 100644 --- a/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala +++ b/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala @@ -33,6 +33,19 @@ import EnumAnnotations._ abstract class EnumType(private val factory: EnumFactory, selfAnnotating: Boolean = false) extends Element { + override def toString: String = { + val bindingString = litOption match { + case Some(value) => factory.nameOfValue(value) match { + case Some(name) => s"($value=$name)" + case None => s"($value=(invalid))" + } + case _ => bindingToString + } + // Use getSimpleName instead of enumTypeName because for debugging purposes the fully qualified name isn't + // necessary (compared to for the Enum annotation), and it's more consistent with Bundle printing. + s"${factory.getClass.getSimpleName.init}$bindingString" + } + override def cloneType: this.type = factory().asInstanceOf[this.type] private[core] def compop(sourceInfo: SourceInfo, op: PrimOp, other: EnumType): Bool = { @@ -151,6 +164,10 @@ abstract class EnumFactory { def all: Seq[Type] = enumInstances + private[chisel3] def nameOfValue(id: BigInt): Option[String] = { + enum_records.find(_.inst.litValue() == id).map(_.name) + } + protected def Value: Type = macro EnumMacros.ValImpl protected def Value(id: UInt): Type = macro EnumMacros.ValCustomImpl -- cgit v1.2.3