Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/DefaultASTPrinter.java @ 15279:0c6d8a08e31b
Truffle: Major cleanup and extension of the Truffle Instrumentation framework in com.oracle.truffle.api
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Sun, 20 Apr 2014 20:37:27 -0700 |
parents | |
children | 8c34e2cc4add |
comparison
equal
deleted
inserted
replaced
14862:0e713dba33bb | 15279:0c6d8a08e31b |
---|---|
1 /* | |
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.api.instrument.impl; | |
26 | |
27 import java.io.*; | |
28 import java.util.*; | |
29 | |
30 import com.oracle.truffle.api.*; | |
31 import com.oracle.truffle.api.instrument.*; | |
32 import com.oracle.truffle.api.nodes.*; | |
33 import com.oracle.truffle.api.nodes.NodeUtil.NodeClass; | |
34 import com.oracle.truffle.api.nodes.NodeUtil.NodeField; | |
35 import com.oracle.truffle.api.nodes.NodeUtil.NodeFieldKind; | |
36 | |
37 /** | |
38 * A language-agnostic for printing out various pieces of a Truffle AST. | |
39 */ | |
40 public class DefaultASTPrinter implements ASTPrinter { | |
41 | |
42 public DefaultASTPrinter() { | |
43 } | |
44 | |
45 public void printTree(PrintWriter p, Node node, int maxDepth, Node markNode) { | |
46 printTree(p, node, maxDepth, markNode, 1); | |
47 p.println(); | |
48 p.flush(); | |
49 } | |
50 | |
51 public String printTreeToString(Node node, int maxDepth, Node markNode) { | |
52 StringWriter out = new StringWriter(); | |
53 printTree(new PrintWriter(out), node, maxDepth, markNode); | |
54 return out.toString(); | |
55 } | |
56 | |
57 public String printTreeToString(Node node, int maxDepth) { | |
58 return printTreeToString(node, maxDepth, null); | |
59 } | |
60 | |
61 private static void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, int level) { | |
62 if (node == null) { | |
63 p.print("null"); | |
64 return; | |
65 } | |
66 | |
67 p.print(nodeName(node)); | |
68 | |
69 String sep = ""; | |
70 p.print("("); | |
71 | |
72 final SourceSection src = node.getSourceSection(); | |
73 if (src != null) { | |
74 if (!(src instanceof NullSourceSection)) { | |
75 p.print(src.getSource().getName() + ":" + src.getStartLine()); | |
76 } | |
77 } | |
78 if (node instanceof PhylumTagged) { | |
79 final PhylumTagged taggedNode = (PhylumTagged) node; | |
80 String prefix = ""; | |
81 for (PhylumTag tag : taggedNode.getPhylumTags()) { | |
82 p.print(prefix); | |
83 prefix = ","; | |
84 p.print(tag.toString()); | |
85 } | |
86 | |
87 } | |
88 | |
89 ArrayList<NodeField> childFields = new ArrayList<>(); | |
90 | |
91 for (NodeField field : NodeClass.get(node.getClass()).getFields()) { | |
92 if (field.getKind() == NodeFieldKind.CHILD || field.getKind() == NodeFieldKind.CHILDREN) { | |
93 childFields.add(field); | |
94 } else if (field.getKind() == NodeFieldKind.DATA) { | |
95 // p.print(sep); | |
96 // sep = ", "; | |
97 // | |
98 // final String fieldName = field.getName(); | |
99 // switch (fieldName) { | |
100 // | |
101 // } | |
102 // p.print(fieldName); | |
103 // p.print(" = "); | |
104 // p.print(field.loadValue(node)); | |
105 } | |
106 } | |
107 p.print(")"); | |
108 | |
109 if (level <= maxDepth) { | |
110 | |
111 if (childFields.size() != 0) { | |
112 p.print(" {"); | |
113 for (NodeField field : childFields) { | |
114 | |
115 Object value = field.loadValue(node); | |
116 if (value == null) { | |
117 printNewLine(p, level); | |
118 p.print(field.getName()); | |
119 p.print(" = null "); | |
120 } else if (field.getKind() == NodeFieldKind.CHILD) { | |
121 final Node valueNode = (Node) value; | |
122 printNewLine(p, level, valueNode == markNode); | |
123 p.print(field.getName()); | |
124 p.print(" = "); | |
125 printTree(p, valueNode, maxDepth, markNode, level + 1); | |
126 } else if (field.getKind() == NodeFieldKind.CHILDREN) { | |
127 printNewLine(p, level); | |
128 p.print(field.getName()); | |
129 Node[] children = (Node[]) value; | |
130 p.print(" = ["); | |
131 sep = ""; | |
132 for (Node child : children) { | |
133 p.print(sep); | |
134 sep = ", "; | |
135 printTree(p, child, maxDepth, markNode, level + 1); | |
136 } | |
137 p.print("]"); | |
138 } else { | |
139 printNewLine(p, level); | |
140 p.print(field.getName()); | |
141 } | |
142 } | |
143 printNewLine(p, level - 1); | |
144 p.print("}"); | |
145 } | |
146 } | |
147 } | |
148 | |
149 private static void printNewLine(PrintWriter p, int level, boolean mark) { | |
150 p.println(); | |
151 for (int i = 0; i < level; i++) { | |
152 if (mark && i == 0) { | |
153 p.print(" -->"); | |
154 } else { | |
155 p.print(" "); | |
156 } | |
157 } | |
158 } | |
159 | |
160 private static void printNewLine(PrintWriter p, int level) { | |
161 printNewLine(p, level, false); | |
162 } | |
163 | |
164 private static String nodeName(Node node) { | |
165 return node.getClass().getSimpleName(); | |
166 } | |
167 | |
168 } |