From 1a4e0dd65ba3e64268beca8f592bd58d98c434a4 Mon Sep 17 00:00:00 2001 From: Adam Izraelevitz Date: Mon, 2 Mar 2020 10:51:51 -0800 Subject: Cleanup aspects (#1359) * Clean up aspects * Refactored InjectingAspect with InjectorAspect * Made AspectLibrary work with objects * Cleaned up code * Apply suggestions from code review * Added tests, removed deprecated newInstance call * Backed out removal of newInstance as exceptions were different * Removed trailing commas --- src/main/scala/chisel3/aop/AspectLibrary.scala | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/scala/chisel3/aop/AspectLibrary.scala (limited to 'src/main/scala/chisel3/aop/AspectLibrary.scala') diff --git a/src/main/scala/chisel3/aop/AspectLibrary.scala b/src/main/scala/chisel3/aop/AspectLibrary.scala new file mode 100644 index 00000000..e141688e --- /dev/null +++ b/src/main/scala/chisel3/aop/AspectLibrary.scala @@ -0,0 +1,49 @@ +// See LICENSE for license details. + +package chisel3.aop + +import chisel3.RawModule +import firrtl.options.{OptionsException, RegisteredLibrary, ShellOption} + +/** Enables adding aspects to a design from the commandline, e.g. + * sbt> runMain chisel3.stage.ChiselMain --module --with-aspect + */ +final class AspectLibrary() extends RegisteredLibrary { + val name = "AspectLibrary" + + import scala.reflect.runtime.universe._ + + private def apply(aspectName: String): Aspect[RawModule] = { + try { + // If a regular class, instantiate, otherwise try as a singleton object + try { + val x = Class.forName(aspectName).asInstanceOf[Class[_ <: Aspect[RawModule]]] + x.newInstance() + } catch { + case e: InstantiationException => + val rm = runtimeMirror(getClass.getClassLoader) + val x = rm.staticModule(aspectName) + try { + rm.reflectModule(x).instance.asInstanceOf[Aspect[RawModule]] + } catch { + case _: Exception => throw e + } + } + } catch { + case e: ClassNotFoundException => + throw new OptionsException(s"Unable to locate aspect '$aspectName'! (Did you misspell it?)", e) + case e: InstantiationException => + throw new OptionsException( + s"Unable to create instance of aspect '$aspectName'! (Does this class take parameters?)", e) + } + } + + val options = Seq(new ShellOption[String]( + longOption = "with-aspect", + toAnnotationSeq = { + case aspectName: String => Seq(apply(aspectName)) + }, + helpText = "The name/class of an aspect to compile with (must be a class/object without arguments!)", + helpValueName = Some(".") + )) +} -- cgit v1.2.3