From 271bc0c5cf17d86203347e17c2082a495cd5d530 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Mon, 8 Feb 2016 15:09:35 -0800 Subject: 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. --- src/main/scala/Chisel/internal/firrtl/IR.scala | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src') 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 -- cgit v1.2.3