Skip to content

Commit 7c1983d

Browse files
committed
Cleanup asm-to-string debug helpers
Introduces methods for textifying classes, methods, InsnLists and individual AbstractInsnNodes.
1 parent ff95128 commit 7c1983d

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
package scala.tools.nsc.backend.jvm
77

8-
import scala.tools.asm.tree.{AbstractInsnNode, ClassNode, MethodNode}
9-
import java.io.PrintWriter
8+
import scala.tools.asm.tree.{InsnList, AbstractInsnNode, ClassNode, MethodNode}
9+
import java.io.{StringWriter, PrintWriter}
1010
import scala.tools.asm.util.{TraceClassVisitor, TraceMethodVisitor, Textifier}
1111
import scala.tools.asm.ClassReader
12+
import scala.collection.convert.decorateAsScala._
1213

1314
object AsmUtils {
1415

@@ -36,19 +37,12 @@ object AsmUtils {
3637

3738
def traceMethod(mnode: MethodNode): Unit = {
3839
println(s"Bytecode for method ${mnode.name}")
39-
val p = new Textifier
40-
val tracer = new TraceMethodVisitor(p)
41-
mnode.accept(tracer)
42-
val w = new PrintWriter(System.out)
43-
p.print(w)
44-
w.flush()
40+
println(textify(mnode))
4541
}
4642

4743
def traceClass(cnode: ClassNode): Unit = {
4844
println(s"Bytecode for class ${cnode.name}")
49-
val w = new PrintWriter(System.out)
50-
cnode.accept(new TraceClassVisitor(w))
51-
w.flush()
45+
println(textify(cnode))
5246
}
5347

5448
def traceClass(bytes: Array[Byte]): Unit = traceClass(readClass(bytes))
@@ -59,8 +53,56 @@ object AsmUtils {
5953
node
6054
}
6155

62-
def instructionString(instruction: AbstractInsnNode): String = instruction.getOpcode match {
63-
case -1 => instruction.toString
64-
case op => scala.tools.asm.util.Printer.OPCODES(op)
56+
/**
57+
* Returns a human-readable representation of the cnode ClassNode.
58+
*/
59+
def textify(cnode: ClassNode): String = {
60+
val trace = new TraceClassVisitor(new PrintWriter(new StringWriter))
61+
cnode.accept(trace)
62+
val sw = new StringWriter
63+
val pw = new PrintWriter(sw)
64+
trace.p.print(pw)
65+
sw.toString
6566
}
67+
68+
/**
69+
* Returns a human-readable representation of the code in the mnode MethodNode.
70+
*/
71+
def textify(mnode: MethodNode): String = {
72+
val trace = new TraceClassVisitor(new PrintWriter(new StringWriter))
73+
mnode.accept(trace)
74+
val sw = new StringWriter
75+
val pw = new PrintWriter(sw)
76+
trace.p.print(pw)
77+
sw.toString
78+
}
79+
80+
/**
81+
* Returns a human-readable representation of the given instruction.
82+
*/
83+
def textify(insn: AbstractInsnNode): String = {
84+
val trace = new TraceMethodVisitor(new Textifier)
85+
insn.accept(trace)
86+
val sw = new StringWriter
87+
val pw = new PrintWriter(sw)
88+
trace.p.print(pw)
89+
sw.toString.trim
90+
}
91+
92+
/**
93+
* Returns a human-readable representation of the given instruction sequence.
94+
*/
95+
def textify(insns: Iterator[AbstractInsnNode]): String = {
96+
val trace = new TraceMethodVisitor(new Textifier)
97+
insns.foreach(_.accept(trace))
98+
val sw: StringWriter = new StringWriter
99+
val pw: PrintWriter = new PrintWriter(sw)
100+
trace.p.print(pw)
101+
sw.toString.trim
102+
}
103+
104+
/**
105+
* Returns a human-readable representation of the given instruction sequence.
106+
*/
107+
def textify(insns: InsnList): String = textify(insns.iterator().asScala)
66108
}

test/files/run/icode-reader-dead-code.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Bytecode for method f
2+
3+
// access flags 0x11
4+
public final f()I
25
L0
36
LINENUMBER 4 L0
47
ICONST_1
@@ -7,7 +10,11 @@ Bytecode for method f
710
LOCALVARIABLE this Lp/A; L0 L1 0
811
MAXSTACK = 1
912
MAXLOCALS = 1
13+
1014
Bytecode for method f
15+
16+
// access flags 0x11
17+
public final f()I
1118
L0
1219
LINENUMBER 4 L0
1320
ICONST_1
@@ -17,3 +24,4 @@ Bytecode for method f
1724
LOCALVARIABLE this Lp/A; L0 L1 0
1825
MAXSTACK = 1
1926
MAXLOCALS = 1
27+

0 commit comments

Comments
 (0)