Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Tuple2 Lookupable (backport #2372) #2406

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import chisel3.internal.{throwException, AggregateViewBinding, Builder, ChildBin
*/
@implicitNotFound(
"@public is only legal within a class or trait marked @instantiable, and only on vals of type" +
" Data, BaseModule, IsInstantiable, IsLookupable, or Instance[_], or in an Iterable, Option, or Either"
" Data, BaseModule, IsInstantiable, IsLookupable, or Instance[_], or in an Iterable, Option, Either, or Tuple2"
)
trait Lookupable[-B] {
type C // Return type of the lookup
Expand Down Expand Up @@ -402,6 +402,28 @@ object Lookupable {
}
}
}

implicit def lookupTuple2[X, Y](
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions,
lookupableX: Lookupable[X],
lookupableY: Lookupable[Y]
) = new Lookupable[(X, Y)] {
type C = (lookupableX.C, lookupableY.C)
def definitionLookup[A](that: A => (X, Y), definition: Definition[A]): C = {
val ret = that(definition.proto)
(
lookupableX.definitionLookup[A](_ => ret._1, definition),
lookupableY.definitionLookup[A](_ => ret._2, definition)
)
}
def instanceLookup[A](that: A => (X, Y), instance: Instance[A]): C = {
import instance._
val ret = that(proto)
(lookupableX.instanceLookup[A](_ => ret._1, instance), lookupableY.instanceLookup[A](_ => ret._2, instance))
}
}

implicit def lookupIsInstantiable[B <: IsInstantiable](
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ class DefinitionSpec extends ChiselFunSpec with Utils {
annos should contain(MarkAnnotation("~Top|HasEither>x".rt, "xright"))
annos should contain(MarkAnnotation("~Top|HasEither>y".rt, "yleft"))
}
it("3.12: should work on tuple2") {
class Top() extends Module {
val i = Definition(new HasTuple2())
mark(i.xy._1, "x")
mark(i.xy._2, "y")
}
val (_, annos) = getFirrtlAndAnnos(new Top)
annos should contain(MarkAnnotation("~Top|HasTuple2>x".rt, "x"))
annos should contain(MarkAnnotation("~Top|HasTuple2>y".rt, "y"))
}
}
describe("4: toDefinition") {
it("4.0: should work on modules") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ object Examples {
@public val y: Either[Bool, UInt] = Left(Wire(Bool()).suggestName("y"))
}
@instantiable
class HasTuple2() extends Module {
val x = Wire(UInt(3.W))
val y = Wire(Bool())
@public val xy = (x, y)
}
@instantiable
class HasVec() extends Module {
@public val x = VecInit(1.U, 2.U, 3.U)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,18 @@ class InstanceSpec extends ChiselFunSpec with Utils {
annos should contain(MarkAnnotation("~Top|Top/i:HasEither>x".rt, "xright"))
annos should contain(MarkAnnotation("~Top|Top/i:HasEither>y".rt, "yleft"))
}
it("3.12: should properly support val modifiers") {
it("3.12: should work on tuple2") {
class Top() extends Module {
val i = Instance(Definition(new HasTuple2()))
mark(i.xy._1, "x")
mark(i.xy._2, "y")
}
val (_, annos) = getFirrtlAndAnnos(new Top)
annos should contain(MarkAnnotation("~Top|Top/i:HasTuple2>x".rt, "x"))
annos should contain(MarkAnnotation("~Top|Top/i:HasTuple2>y".rt, "y"))
}

it("3.13: should properly support val modifiers") {
class SupClass extends Module {
val value = 10
val overriddenVal = 10
Expand Down