Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/instrument/SLASTPrinter.java @ 18485:e3c95cbbb50c
Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Sun, 23 Nov 2014 16:07:23 -0800 |
parents | 618d92152d3c |
children | 5b7db8941fd7 |
comparison
equal
deleted
inserted
replaced
18484:e97e1f07a3d6 | 18485:e3c95cbbb50c |
---|---|
24 | 24 |
25 import java.io.*; | 25 import java.io.*; |
26 import java.util.*; | 26 import java.util.*; |
27 | 27 |
28 import com.oracle.truffle.api.instrument.*; | 28 import com.oracle.truffle.api.instrument.*; |
29 import com.oracle.truffle.api.instrument.impl.*; | |
29 import com.oracle.truffle.api.nodes.*; | 30 import com.oracle.truffle.api.nodes.*; |
30 import com.oracle.truffle.api.nodes.NodeUtil.NodeClass; | 31 import com.oracle.truffle.api.nodes.NodeUtil.NodeClass; |
31 import com.oracle.truffle.api.nodes.NodeUtil.NodeField; | 32 import com.oracle.truffle.api.nodes.NodeUtil.NodeField; |
32 import com.oracle.truffle.api.nodes.NodeUtil.NodeFieldKind; | 33 import com.oracle.truffle.api.nodes.NodeUtil.NodeFieldKind; |
33 import com.oracle.truffle.api.source.*; | |
34 | 34 |
35 /** | 35 /** |
36 * SLASTPrinter is used to print for SL's internal Truffle AST. This is used by | 36 * SLASTPrinter is used to print for SL's internal Truffle AST. This is used by |
37 * {@link SLDefaultVisualizer} to provide a means of displaying the internal Truffle AST | 37 * {@link SLDefaultVisualizer} to provide a means of displaying the internal Truffle AST |
38 */ | 38 */ |
39 public final class SLASTPrinter implements ASTPrinter { | 39 public final class SLASTPrinter extends DefaultASTPrinter { |
40 | |
40 public SLASTPrinter() { | 41 public SLASTPrinter() { |
41 } | 42 } |
42 | 43 |
43 public void printTree(PrintWriter p, Node node, int maxDepth, Node markNode) { | 44 @Override |
44 printTree(p, node, maxDepth, markNode, 1); | 45 protected void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, int level) { |
45 p.println(); | |
46 p.flush(); | |
47 } | |
48 | |
49 public String printTreeToString(Node node, int maxDepth, Node markNode) { | |
50 StringWriter out = new StringWriter(); | |
51 printTree(new PrintWriter(out), node, maxDepth, markNode); | |
52 return out.toString(); | |
53 } | |
54 | |
55 public String printTreeToString(Node node, int maxDepth) { | |
56 return printTreeToString(node, maxDepth, null); | |
57 } | |
58 | |
59 private static void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, int level) { | |
60 if (node == null) { | 46 if (node == null) { |
61 p.print("null"); | 47 p.print("null"); |
62 return; | 48 return; |
63 } | 49 } |
64 | 50 |
65 p.print(nodeName(node)); | 51 p.print(nodeName(node)); |
66 | 52 |
67 String sep = ""; | |
68 p.print("("); | 53 p.print("("); |
69 | 54 |
70 final SourceSection src = node.getSourceSection(); | 55 if (node instanceof InstrumentationNode) { |
71 if (src != null) { | 56 p.print(instrumentInfo((InstrumentationNode) node)); |
72 if (!(src instanceof NullSourceSection)) { | |
73 p.print(src.getSource().getName() + ":" + src.getStartLine()); | |
74 } else if (src instanceof NullSourceSection) { | |
75 final NullSourceSection nullSection = (NullSourceSection) src; | |
76 p.print(nullSection.getShortDescription()); | |
77 } | |
78 } | 57 } |
79 if (node instanceof SyntaxTagged) { | 58 |
80 final SyntaxTagged taggedNode = (SyntaxTagged) node; | 59 p.print(sourceInfo(node)); |
81 p.print("["); | 60 |
82 String prefix = ""; | 61 p.print(NodeUtil.printSyntaxTags(node)); |
83 for (SyntaxTag tag : taggedNode.getSyntaxTags()) { | |
84 p.print(prefix); | |
85 prefix = ","; | |
86 p.print(tag.toString()); | |
87 } | |
88 p.print("]"); | |
89 } | |
90 | 62 |
91 ArrayList<NodeField> childFields = new ArrayList<>(); | 63 ArrayList<NodeField> childFields = new ArrayList<>(); |
92 | 64 |
93 for (NodeField field : NodeClass.get(node.getClass()).getFields()) { | 65 for (NodeField field : NodeClass.get(node.getClass()).getFields()) { |
94 if (field.getKind() == NodeFieldKind.CHILD || field.getKind() == NodeFieldKind.CHILDREN) { | 66 if (field.getKind() == NodeFieldKind.CHILD || field.getKind() == NodeFieldKind.CHILDREN) { |
118 if (value == null) { | 90 if (value == null) { |
119 printNewLine(p, level); | 91 printNewLine(p, level); |
120 p.print(field.getName()); | 92 p.print(field.getName()); |
121 p.print(" = null "); | 93 p.print(" = null "); |
122 } else if (field.getKind() == NodeFieldKind.CHILD) { | 94 } else if (field.getKind() == NodeFieldKind.CHILD) { |
123 final Node valueNode = (Node) value; | 95 printChild(p, maxDepth, markNode, level, field, value); |
124 printNewLine(p, level, valueNode == markNode); | |
125 p.print(field.getName()); | |
126 p.print(" = "); | |
127 printTree(p, valueNode, maxDepth, markNode, level + 1); | |
128 } else if (field.getKind() == NodeFieldKind.CHILDREN) { | 96 } else if (field.getKind() == NodeFieldKind.CHILDREN) { |
129 printNewLine(p, level); | 97 printChildren(p, maxDepth, markNode, level, field, value); |
130 p.print(field.getName()); | |
131 Node[] children = (Node[]) value; | |
132 p.print(" = ["); | |
133 sep = ""; | |
134 for (Node child : children) { | |
135 p.print(sep); | |
136 sep = ", "; | |
137 printTree(p, child, maxDepth, markNode, level + 1); | |
138 } | |
139 p.print("]"); | |
140 } else { | 98 } else { |
141 printNewLine(p, level); | 99 printNewLine(p, level); |
142 p.print(field.getName()); | 100 p.print(field.getName()); |
143 } | 101 } |
144 } | 102 } |
146 p.print("}"); | 104 p.print("}"); |
147 } | 105 } |
148 } | 106 } |
149 } | 107 } |
150 | 108 |
151 private static void printNewLine(PrintWriter p, int level, boolean mark) { | |
152 p.println(); | |
153 for (int i = 0; i < level; i++) { | |
154 if (mark && i == 0) { | |
155 p.print(" -->"); | |
156 } else { | |
157 p.print(" "); | |
158 } | |
159 } | |
160 } | |
161 | |
162 private static void printNewLine(PrintWriter p, int level) { | |
163 printNewLine(p, level, false); | |
164 } | |
165 | |
166 private static String nodeName(Node node) { | |
167 return node.getClass().getSimpleName(); | |
168 } | |
169 } | 109 } |