summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@gmail.com>2014-12-11 18:08:57 +0100
committerLukas Rytz <lukas.rytz@gmail.com>2015-01-16 23:18:12 +0100
commit7c1983d153a81ba43858a1bb5f0b59c5708bbfcf (patch)
tree1a3d4c7ddedb1be38ecf0f2e5f62545ddbd7955e
parentff9512812c6619d6ab5a1dde99c9569a3b27c813 (diff)
downloadscala-7c1983d153a81ba43858a1bb5f0b59c5708bbfcf.tar.gz
scala-7c1983d153a81ba43858a1bb5f0b59c5708bbfcf.tar.bz2
scala-7c1983d153a81ba43858a1bb5f0b59c5708bbfcf.zip
Cleanup asm-to-string debug helpers
Introduces methods for textifying classes, methods, InsnLists and individual AbstractInsnNodes.
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala70
-rw-r--r--test/files/run/icode-reader-dead-code.check8
2 files changed, 64 insertions, 14 deletions
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala b/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala
index 7269910af6..75aa0fc984 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala
@@ -5,10 +5,11 @@
package scala.tools.nsc.backend.jvm
-import scala.tools.asm.tree.{AbstractInsnNode, ClassNode, MethodNode}
-import java.io.PrintWriter
+import scala.tools.asm.tree.{InsnList, AbstractInsnNode, ClassNode, MethodNode}
+import java.io.{StringWriter, PrintWriter}
import scala.tools.asm.util.{TraceClassVisitor, TraceMethodVisitor, Textifier}
import scala.tools.asm.ClassReader
+import scala.collection.convert.decorateAsScala._
object AsmUtils {
@@ -36,19 +37,12 @@ object AsmUtils {
def traceMethod(mnode: MethodNode): Unit = {
println(s"Bytecode for method ${mnode.name}")
- val p = new Textifier
- val tracer = new TraceMethodVisitor(p)
- mnode.accept(tracer)
- val w = new PrintWriter(System.out)
- p.print(w)
- w.flush()
+ println(textify(mnode))
}
def traceClass(cnode: ClassNode): Unit = {
println(s"Bytecode for class ${cnode.name}")
- val w = new PrintWriter(System.out)
- cnode.accept(new TraceClassVisitor(w))
- w.flush()
+ println(textify(cnode))
}
def traceClass(bytes: Array[Byte]): Unit = traceClass(readClass(bytes))
@@ -59,8 +53,56 @@ object AsmUtils {
node
}
- def instructionString(instruction: AbstractInsnNode): String = instruction.getOpcode match {
- case -1 => instruction.toString
- case op => scala.tools.asm.util.Printer.OPCODES(op)
+ /**
+ * Returns a human-readable representation of the cnode ClassNode.
+ */
+ def textify(cnode: ClassNode): String = {
+ val trace = new TraceClassVisitor(new PrintWriter(new StringWriter))
+ cnode.accept(trace)
+ val sw = new StringWriter
+ val pw = new PrintWriter(sw)
+ trace.p.print(pw)
+ sw.toString
}
+
+ /**
+ * Returns a human-readable representation of the code in the mnode MethodNode.
+ */
+ def textify(mnode: MethodNode): String = {
+ val trace = new TraceClassVisitor(new PrintWriter(new StringWriter))
+ mnode.accept(trace)
+ val sw = new StringWriter
+ val pw = new PrintWriter(sw)
+ trace.p.print(pw)
+ sw.toString
+ }
+
+ /**
+ * Returns a human-readable representation of the given instruction.
+ */
+ def textify(insn: AbstractInsnNode): String = {
+ val trace = new TraceMethodVisitor(new Textifier)
+ insn.accept(trace)
+ val sw = new StringWriter
+ val pw = new PrintWriter(sw)
+ trace.p.print(pw)
+ sw.toString.trim
+ }
+
+ /**
+ * Returns a human-readable representation of the given instruction sequence.
+ */
+ def textify(insns: Iterator[AbstractInsnNode]): String = {
+ val trace = new TraceMethodVisitor(new Textifier)
+ insns.foreach(_.accept(trace))
+ val sw: StringWriter = new StringWriter
+ val pw: PrintWriter = new PrintWriter(sw)
+ trace.p.print(pw)
+ sw.toString.trim
+ }
+
+ /**
+ * Returns a human-readable representation of the given instruction sequence.
+ */
+ def textify(insns: InsnList): String = textify(insns.iterator().asScala)
}
diff --git a/test/files/run/icode-reader-dead-code.check b/test/files/run/icode-reader-dead-code.check
index d1739fed3b..c9de93283e 100644
--- a/test/files/run/icode-reader-dead-code.check
+++ b/test/files/run/icode-reader-dead-code.check
@@ -1,4 +1,7 @@
Bytecode for method f
+
+ // access flags 0x11
+ public final f()I
L0
LINENUMBER 4 L0
ICONST_1
@@ -7,7 +10,11 @@ Bytecode for method f
LOCALVARIABLE this Lp/A; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
+
Bytecode for method f
+
+ // access flags 0x11
+ public final f()I
L0
LINENUMBER 4 L0
ICONST_1
@@ -17,3 +24,4 @@ Bytecode for method f
LOCALVARIABLE this Lp/A; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
+