-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make '--module' support arguments (#4358)
Dramatically increase the power of the '--module' argument to 'circt.stage.ChiselMain'. This adds support for constructing modules with parameters using reflection as demonstrated elsewhere [[1]]. This is long missing support that I intended to add when both the original reflective construction was added [[2]] and turned on [[3]]. At the time, I didn't know how to do reflective construction of arbitrary modules and left that feature unfinished for the next 5 years. This is done to move 'ChiselMain' more in the direction of being a command line utility for running Chisel generators. This is related to the discussions of a hypothetical 'chisel-cli' that has been discussed on This commit changes the API such that any class constructed this way must take a trailing '()'. Anything without this will be interpreted as a string. This is a change to how '--module' used to work where the argument was assumed to be a class. [1]: https://github.com/seldridge/reflective-builder [2]: a423db5 [3]: 4eff16b Signed-off-by: Schuyler Eldridge <[email protected]>
- Loading branch information
Showing
3 changed files
with
213 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package circtTests.stage | ||
|
||
import chisel3._ | ||
import circt.stage.ChiselMain | ||
import java.io.File | ||
import org.scalatest.funspec.AnyFunSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import scala.io.Source | ||
|
||
object ChiselMainSpec { | ||
|
||
class ParameterlessModule extends RawModule { | ||
val a = IO(Input(Bool())) | ||
val b = IO(Output(Bool())) | ||
|
||
b :<= a | ||
} | ||
|
||
class BooleanModule(param: Boolean) extends RawModule { | ||
override final def desiredName = s"${this.getClass().getSimpleName()}_$param" | ||
} | ||
|
||
class IntegerModule(param: Int) extends RawModule { | ||
override final def desiredName = s"${this.getClass().getSimpleName()}_$param" | ||
} | ||
|
||
class DoubleModule(param: Double) extends RawModule { | ||
override final def desiredName = s"${this.getClass().getSimpleName()}_$param" | ||
} | ||
|
||
object Enum { | ||
sealed trait Type | ||
class A extends Type { | ||
override def toString = "A" | ||
} | ||
class B extends Type { | ||
override def toString = "B" | ||
} | ||
} | ||
class ObjectModule(param: Enum.Type) extends RawModule { | ||
override final def desiredName = s"${this.getClass().getSimpleName()}_$param" | ||
} | ||
|
||
class StringModule(param: String) extends RawModule { | ||
override final def desiredName = s"${this.getClass().getSimpleName()}_$param" | ||
} | ||
|
||
class MultipleParameters(bool: Boolean, int: Int) extends RawModule { | ||
override final def desiredName = s"${this.getClass().getSimpleName()}_${bool}_$int" | ||
} | ||
|
||
} | ||
|
||
class ChiselMainSpec extends AnyFunSpec with Matchers with chiselTests.Utils { | ||
import ChiselMainSpec._ | ||
|
||
val testDir = new File("test_run_dir/ChiselMainSpec") | ||
case class Test(module: String, filename: String) { | ||
def test() = { | ||
val outFile = new File(testDir, filename) | ||
outFile.delete() | ||
outFile shouldNot exist | ||
|
||
info(module) | ||
ChiselMain.main( | ||
Array( | ||
"--module", | ||
module, | ||
"--target", | ||
"chirrtl", | ||
"--target-dir", | ||
testDir.toString | ||
) | ||
) | ||
|
||
outFile should exist | ||
} | ||
} | ||
|
||
describe("ChiselMain") { | ||
|
||
describe("support for modules without parameters") { | ||
|
||
it("should elaborate a parameterless module") { | ||
|
||
Test( | ||
"circtTests.stage.ChiselMainSpec$ParameterlessModule()", | ||
"ParameterlessModule.fir" | ||
).test() | ||
|
||
} | ||
|
||
} | ||
|
||
describe("support for modules with parameters") { | ||
|
||
it("should elaborate a module with a Boolean parameter") { | ||
|
||
Test( | ||
"circtTests.stage.ChiselMainSpec$BooleanModule(true)", | ||
"BooleanModule_true.fir" | ||
).test() | ||
|
||
Test( | ||
"circtTests.stage.ChiselMainSpec$BooleanModule(false)", | ||
"BooleanModule_false.fir" | ||
).test() | ||
|
||
} | ||
|
||
it("should elaborate a module with an Integer parameter") { | ||
|
||
Test( | ||
"circtTests.stage.ChiselMainSpec$IntegerModule(42)", | ||
"IntegerModule_42.fir" | ||
).test() | ||
|
||
} | ||
|
||
it("should elaborate a module with a Double parameter") { | ||
|
||
Test( | ||
"circtTests.stage.ChiselMainSpec$DoubleModule(3.141592654)", | ||
"DoubleModule_3141592654.fir" | ||
).test() | ||
|
||
} | ||
|
||
it("should elaborate a module with an object parameter") { | ||
|
||
Test( | ||
"circtTests.stage.ChiselMainSpec$ObjectModule(circtTests.stage.ChiselMainSpec$Enum$A())", | ||
"ObjectModule_A.fir" | ||
).test() | ||
|
||
Test( | ||
"circtTests.stage.ChiselMainSpec$ObjectModule(circtTests.stage.ChiselMainSpec$Enum$B())", | ||
"ObjectModule_B.fir" | ||
).test() | ||
|
||
} | ||
|
||
it("should elaborate a module with a string parameter") { | ||
|
||
Test( | ||
"""circtTests.stage.ChiselMainSpec$StringModule("hello")""", | ||
"StringModule_hello.fir" | ||
).test() | ||
|
||
} | ||
|
||
it("should elaborate a module that takes multiple parameters") { | ||
|
||
Test( | ||
"circtTests.stage.ChiselMainSpec$MultipleParameters(true,42)", | ||
"MultipleParameters_true_42.fir" | ||
).test() | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |