[Scala3] Implement ChiselEnum macro#5182
Conversation
| if !owner.isValDef then report.errorAndAbort("Invalid Value definition") | ||
|
|
||
| val name = owner.name.trim | ||
| if name.contains(" ") then report.errorAndAbort("Value cannot be called without assigning to an enum") |
There was a problem hiding this comment.
Technically this is wrong but I see it's the same behavior in the Scala 2 macro so I'll leave it. But the following should work:
//> using repository https://central.sonatype.com/repository/maven-snapshots
//> using scala 2.13.18
//> using dep org.chipsalliance::chisel:7.7.0+34-94b8570e-SNAPSHOT
//> using plugin org.chipsalliance:::chisel-plugin:7.7.0+34-94b8570e-SNAPSHOT
//> using options -unchecked -deprecation -language:reflectiveCalls -feature -Xcheckinit
//> using options -Xfatal-warnings -Ywarn-dead-code -Ywarn-unused -Ymacro-annotations
import chisel3._
// _root_ disambiguates from package chisel3.util.circt if user imports chisel3.util._
import _root_.circt.stage.ChiselStage
object MyEnum extends ChiselEnum {
val `initial state`, running = Value
}
class Foo extends Module {
val toggle = IO(Input(Bool()))
val out = IO(Output(MyEnum()))
val myReg = RegInit(MyEnum.`initial state`)
when (toggle) {
when (myReg === MyEnum.`initial state`) {
myReg := MyEnum.running
}.otherwise {
myReg := MyEnum.`initial state`
}
}
out := myReg
}
object Main extends App {
println(
ChiselStage.emitSystemVerilog(
gen = new Foo,
firtoolOpts = Array("-disable-all-randomization", "-strip-debug-info", "-default-layer-specialization=enable")
)
)
}
jackkoenig
left a comment
There was a problem hiding this comment.
I would like more explanation of how the compiler plugin changes fix things, but the macro implementation look good.
|
I managed to replicate the compiler crashes independent of ChiselEnum stuff The original failure has to do with conflicting overriding ValDefs and DefDefs in the same closure. I narrowed the failure to this ChiselEnumSpec test: which defines vals |
|
ideally the BundlePhase update should go in separately but it's small enough so imo let's just ship it here |
This actually feels big to me (in that it's subtle and requires special handling), I would appreciate it as its own PR (since I suspect there may be related issues in the future and having a previous example helps). |
Want the plugin fix separate, will approve once that's split out.
Move ChiselEnum spec file, failing because Switch macro is not implemented yet
91ee443 to
b148ff7
Compare
Move ChiselEnum spec file, failing because Switch macro not merged yet
Contributor Checklist
docs/src?Type of Improvement
Desired Merge Strategy
Release Notes
ChiselEnum now supported in Scala 3
Reviewer Checklist (only modified by reviewer)
3.6.x,5.x, or6.xdepending on impact, API modification or big change:7.0)?Enable auto-merge (squash)and clean up the commit message.Create a merge commit.