diff options
| author | Richard Lin | 2016-11-21 13:25:03 -0800 |
|---|---|---|
| committer | GitHub | 2016-11-21 13:25:03 -0800 |
| commit | cd6eb41275381a4399a8a88c886110d276bb805c (patch) | |
| tree | ee1a0d87a49903cd4d3a97fce200096452104615 /coreMacros/src | |
| parent | 822160cc8e76e70643fb56707bb39f6f7526b6fd (diff) | |
| parent | e8aea3f4153b58321784ac33734305207570ef75 (diff) | |
Merge pull request #369 from ucb-bar/rangemacro
Add ranging API
Diffstat (limited to 'coreMacros/src')
| -rw-r--r-- | coreMacros/src/main/scala/chisel3/internal/RangeTransform.scala | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/coreMacros/src/main/scala/chisel3/internal/RangeTransform.scala b/coreMacros/src/main/scala/chisel3/internal/RangeTransform.scala new file mode 100644 index 00000000..f431341d --- /dev/null +++ b/coreMacros/src/main/scala/chisel3/internal/RangeTransform.scala @@ -0,0 +1,102 @@ +// See LICENSE for license details. + +// Macro transforms that statically (at compile time) parse range specifiers and emit the raw +// (non-human-friendly) range constructor calls. + +package chisel3.internal + +import scala.language.experimental.macros +import scala.reflect.macros.blackbox.Context +import scala.reflect.macros.whitebox + +class RangeTransform(val c: Context) { + import c.universe._ + def apply(args: c.Tree*): c.Tree = { + val stringTrees = c.prefix.tree match { + case q"$_(scala.StringContext.apply(..$strings))" => strings + case _ => c.abort(c.enclosingPosition, s"Range macro unable to parse StringContext, got: ${showCode(c.prefix.tree)}") + } + val strings = stringTrees.map { tree => tree match { + case Literal(Constant(string: String)) => string + case _ => c.abort(c.enclosingPosition, s"Range macro unable to parse StringContext element, got: ${showRaw(tree)}") + } } + + var nextStringIndex: Int = 1 + var nextArgIndex: Int = 0 + var currString: String = strings(0) + + /** Mutably gets the next numeric value in the range specifier. + */ + def getNextValue(): c.Tree = { + currString = currString.dropWhile(_ == ' ') // allow whitespace + if (currString.isEmpty) { + if (nextArgIndex >= args.length) { + c.abort(c.enclosingPosition, s"Incomplete range specifier") + } + val nextArg = args(nextArgIndex) + nextArgIndex += 1 + + if (nextStringIndex >= strings.length) { + c.abort(c.enclosingPosition, s"Incomplete range specifier") + } + currString = strings(nextStringIndex) + nextStringIndex += 1 + + nextArg + } else { + val nextStringVal = currString.takeWhile(!Set('[', '(', ' ', ',', ')', ']').contains(_)) + currString = currString.substring(nextStringVal.length) + if (currString.isEmpty) { + c.abort(c.enclosingPosition, s"Incomplete range specifier") + } + c.parse(nextStringVal) + } + } + + // Currently, not allowed to have the end stops (inclusive / exclusive) be interpolated. + currString = currString.dropWhile(_ == ' ') + val startInclusive = currString(0) match { + case '[' => true + case '(' => false + case other => c.abort(c.enclosingPosition, s"Unknown start inclusive/exclusive specifier, got: '$other'") + } + currString = currString.substring(1) // eat the inclusive/exclusive specifier + val minArg = getNextValue() + currString = currString.dropWhile(_ == ' ') + if (currString(0) != ',') { + c.abort(c.enclosingPosition, s"Incomplete range specifier, expected ','") + } + currString = currString.substring(1) // eat the comma + val maxArg = getNextValue() + currString = currString.dropWhile(_ == ' ') + val endInclusive = currString(0) match { + case ']' => true + case ')' => false + case other => c.abort(c.enclosingPosition, s"Unknown end inclusive/exclusive specifier, got: '$other'") + } + currString = currString.substring(1) // eat the inclusive/exclusive specifier + currString = currString.dropWhile(_ == ' ') + + if (nextArgIndex < args.length) { + val unused = args.mkString("") + c.abort(c.enclosingPosition, s"Unused interpolated values in range specifier: '$unused'") + } + if (!currString.isEmpty || nextStringIndex < strings.length) { + val unused = currString + strings.slice(nextStringIndex, strings.length).mkString(", ") + c.abort(c.enclosingPosition, s"Unused characters in range specifier: '$unused'") + } + + val startBound = if (startInclusive) { + q"_root_.chisel3.internal.firrtl.Closed($minArg)" + } else { + q"_root_.chisel3.internal.firrtl.Open($minArg)" + } + val endBound = if (endInclusive) { + q"_root_.chisel3.internal.firrtl.Closed($maxArg)" + } else { + q"_root_.chisel3.internal.firrtl.Open($maxArg)" + } + + q"($startBound, $endBound)" + } +} |
