Skip to content

Commit

Permalink
Add support for String Property type and literal. (chipsalliance#3490)
Browse files Browse the repository at this point in the history
This includes support for Property[String] and literals.
  • Loading branch information
mikeurbach authored Aug 16, 2023
1 parent 39d4107 commit 7a60422
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/scala/chisel3/internal/firrtl/Converter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private[chisel3] object Converter {
case PropertyLit(lit: Int) => fir.IntegerPropertyLiteral(lit)
case PropertyLit(lit: Long) => fir.IntegerPropertyLiteral(lit)
case PropertyLit(lit: BigInt) => fir.IntegerPropertyLiteral(lit)
case PropertyLit(lit: String) => fir.StringPropertyLiteral(lit)
case e @ ProbeExpr(probe) =>
fir.ProbeExpr(convert(probe, ctx, info))
case e @ RWProbeExpr(probe) =>
Expand Down Expand Up @@ -382,6 +383,7 @@ private[chisel3] object Converter {
case t: Property[_] =>
t.getPropertyType match {
case IntegerPropertyType => fir.IntegerPropertyType
case StringPropertyType => fir.StringPropertyType
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/chisel3/internal/firrtl/IR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ object MemPortDirection {

private[chisel3] sealed trait PropertyType
private[chisel3] case object IntegerPropertyType extends PropertyType
private[chisel3] case object StringPropertyType extends PropertyType

@deprecated(deprecatedPublicAPIMsg, "Chisel 3.6")
abstract class Command {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/chisel3/properties/Property.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ private[chisel3] object PropertyType {
implicit val bigIntPropertyTypeInstance = new PropertyType[BigInt] {
override def getPropertyType: ir.PropertyType = ir.IntegerPropertyType
}

implicit val stringPropertyTypeInstance = new PropertyType[String] {
override def getPropertyType: ir.PropertyType = ir.StringPropertyType
}
}

/** Property is the base type for all properties.
Expand Down
1 change: 1 addition & 0 deletions docs/src/explanations/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The following are legal `Property` types:
* `Property[Int]`
* `Property[Long]`
* `Property[BigInt]`
* `Property[String]`

## Using Properties

Expand Down
7 changes: 7 additions & 0 deletions firrtl/src/main/scala/firrtl/ir/IR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ case class IntegerPropertyLiteral(value: BigInt) extends Literal with UseSeriali
val width = UnknownWidth
}

case class StringPropertyLiteral(value: String) extends Expression with UseSerializer {
def tpe = StringPropertyType
val width = UnknownWidth
}

case class DoPrim(op: PrimOp, args: Seq[Expression], consts: Seq[BigInt], tpe: Type)
extends Expression
with UseSerializer
Expand Down Expand Up @@ -510,6 +515,8 @@ case class AnalogType(width: Width) extends GroundType with UseSerializer

case object IntegerPropertyType extends Type with UseSerializer

case object StringPropertyType extends Type with UseSerializer

case object UnknownType extends Type with UseSerializer

/** [[Port]] Direction */
Expand Down
3 changes: 3 additions & 0 deletions firrtl/src/main/scala/firrtl/ir/Serializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ object Serializer {
b ++= "SInt"; s(width); b ++= "(0h"; b ++= value.toString(16); b ++= ")"
case IntegerPropertyLiteral(value) =>
b ++= "Integer("; b ++= value.toString(10); b ++= ")"
case StringPropertyLiteral(value) =>
b ++= "String(\""; b ++= value; b ++= "\")"
case ProbeExpr(expr, _) => b ++= "probe("; s(expr); b += ')'
case RWProbeExpr(expr, _) => b ++= "rwprobe("; s(expr); b += ')'
case ProbeRead(expr, _) => b ++= "read("; s(expr); b += ')'
Expand Down Expand Up @@ -362,6 +364,7 @@ object Serializer {
case AsyncResetType => b ++= "AsyncReset"
case AnalogType(width) => b ++= "Analog"; s(width)
case IntegerPropertyType => b ++= "Integer"
case StringPropertyType => b ++= "String"
case UnknownType => b += '?'
case other => b ++= other.serialize // Handle user-defined nodes
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/scala/chiselTests/properties/PropertySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ class PropertySpec extends ChiselFlatSpec with MatchesAndOmits {
)()
}

it should "support String as a Property type" in {
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
val stringProp = IO(Input(Property[String]()))
})

matchesAndOmits(chirrtl)(
"input stringProp : String"
)()
}

it should "support String as a Property literal" in {
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
val propOut = IO(Output(Property[String]()))
propOut := Property("fubar")
})

matchesAndOmits(chirrtl)(
"propassign propOut, String(\"fubar\")"
)()
}

it should "support connecting Property types of the same type" in {
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
val propIn = IO(Input(Property[Int]()))
Expand Down

0 comments on commit 7a60422

Please sign in to comment.