summaryrefslogtreecommitdiff
path: root/src/main/scala/Chisel/internal
diff options
context:
space:
mode:
authorAndrew Waterman2016-02-08 15:09:35 -0800
committerAndrew Waterman2016-02-08 15:09:35 -0800
commit271bc0c5cf17d86203347e17c2082a495cd5d530 (patch)
treefb0fa6698919b7d22d2f062062859666dbc5442e /src/main/scala/Chisel/internal
parent62fa95acc5d3d301fe461c5844c29d0c75ca6a5d (diff)
Escape control characters in Printf string literals
If we end up generalizing Strings, this code should be moved elsewhere. Note FIRRTL doesn't handle this stuff right, so I'm not committing the companion test yet.
Diffstat (limited to 'src/main/scala/Chisel/internal')
-rw-r--r--src/main/scala/Chisel/internal/firrtl/IR.scala12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/main/scala/Chisel/internal/firrtl/IR.scala b/src/main/scala/Chisel/internal/firrtl/IR.scala
index 7bb273c0..6946652f 100644
--- a/src/main/scala/Chisel/internal/firrtl/IR.scala
+++ b/src/main/scala/Chisel/internal/firrtl/IR.scala
@@ -161,9 +161,19 @@ case class Connect(loc: Node, exp: Arg) extends Command
case class BulkConnect(loc1: Node, loc2: Node) extends Command
case class ConnectInit(loc: Node, exp: Arg) extends Command
case class Stop(clk: Arg, ret: Int) extends Command
-case class Printf(clk: Arg, format: String, ids: Seq[Arg]) extends Command
case class Component(id: Module, name: String, ports: Seq[Port], commands: Seq[Command]) extends Arg
case class Port(id: Data, dir: Direction)
+case class Printf(clk: Arg, formatIn: String, ids: Seq[Arg]) extends Command {
+ require(formatIn.forall(c => c.toInt > 0 && c.toInt < 128), "format strings must comprise non-null ASCII values")
+ def format = {
+ def escaped(x: Char) =
+ if (x == '"' || x == '\\' || x == '?') "\\" + x
+ else if (x == '\n') "\\n"
+ else if (x.toInt < 32) s"\\x${BigInt(x.toInt).toString(16)}"
+ else x
+ formatIn.map(escaped _).mkString
+ }
+}
case class Circuit(name: String, components: Seq[Component], refMap: RefMap) {
def emit: String = new Emitter(this).toString