comparison graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinter.java @ 2708:4272b7af2d17

merge
author Lukas Stadler <lukas.stadler@jku.at>
date Wed, 18 May 2011 18:40:58 +0200
parents b179be22a3db
children 39a9d62e4c60
comparison
equal deleted inserted replaced
2707:7ed72769d51a 2708:4272b7af2d17
1 /*
2 * Copyright (c) 2011, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.sun.c1x.debug;
24
25 import java.io.*;
26 import java.util.*;
27
28 import com.oracle.graal.graph.*;
29
30 /**
31 * Generates a representation of {@link Graph Graphs} that can be visualized and inspected with the <a
32 * href="http://kenai.com/projects/igv">Ideal Graph Visualizer</a>.
33 */
34 public class IdealGraphPrinter {
35
36 private static class Edge {
37 final int from;
38 final int to;
39 final int index;
40
41 Edge(int from, int to, int index) {
42 this.from = from;
43 this.to = to;
44 this.index = index;
45 }
46 }
47
48 private final HashSet<Class<?>> omittedClasses = new HashSet<Class<?>>();
49 private final PrintStream stream;
50
51 /**
52 * Creates a new {@link IdealGraphPrinter} that writes to the specified output stream.
53 */
54 public IdealGraphPrinter(OutputStream stream) {
55 this.stream = new PrintStream(stream);
56 }
57
58 /**
59 * Adds a node class that is omitted in the output.
60 */
61 public void addOmittedClass(Class<?> clazz) {
62 omittedClasses.add(clazz);
63 }
64
65 /**
66 * Flushes any buffered output.
67 */
68 public void flush() {
69 stream.flush();
70 }
71
72 /**
73 * Starts a new graph document.
74 */
75 public void begin() {
76 stream.println("<graphDocument>");
77 }
78
79 /**
80 * Starts a new group of graphs with the given name, short name and method byte code index (BCI) as properties.
81 */
82 public void beginGroup(String name, String shortName, int bci) {
83 stream.println("<group>");
84 stream.printf(" <properties><p name='name'>%s</p></properties>%n", escape(name));
85 stream.printf(" <method name='%s' shortName='%s' bci='%d'/>%n", escape(name), escape(shortName), bci);
86 }
87
88 /**
89 * Ends the current group.
90 */
91 public void endGroup() {
92 stream.println("</group>");
93 }
94
95 /**
96 * Finishes the graph document and flushes the output stream.
97 */
98 public void end() {
99 stream.println("</graphDocument>");
100 flush();
101 }
102
103 /**
104 * Prints an entire {@link Graph} with the specified title, optionally using short names for nodes.
105 */
106 public void print(Graph graph, String title, boolean shortNames) {
107 stream.printf(" <graph name='%s'>%n", escape(title));
108
109 stream.println(" <nodes>");
110 List<Edge> edges = printNodes(graph.getNodes(), shortNames);
111 stream.println(" </nodes>");
112
113 stream.println(" <edges>");
114 for (Edge edge : edges) {
115 printEdge(edge);
116 }
117 stream.println(" </edges>");
118
119 stream.println(" </graph>");
120 }
121
122 private List<Edge> printNodes(Collection<Node> nodes, boolean shortNames) {
123 ArrayList<Edge> edges = new ArrayList<Edge>();
124
125 for (Node node : nodes) {
126 if (node == Node.Null || omittedClasses.contains(node)) {
127 continue;
128 }
129
130 String name;
131 if (shortNames) {
132 name = node.shortName();
133 } else {
134 name = node.toString();
135 }
136
137 stream.printf(" <node id='%d'><properties>", node.id());
138 stream.printf("<p name='idx'>%d</p>", node.id());
139 stream.printf("<p name='name'>%s</p>", escape(name));
140 stream.println("</properties></node>");
141
142 int index = 0;
143 for (Node predecessor : node.predecessors()) {
144 if (predecessor != Node.Null && !omittedClasses.contains(predecessor.getClass())) {
145 edges.add(new Edge(predecessor.id(), node.id(), index));
146 }
147 index++;
148 }
149 for (Node input : node.inputs()) {
150 if (input != Node.Null && !omittedClasses.contains(input.getClass())) {
151 edges.add(new Edge(input.id(), node.id(), index));
152 }
153 index++;
154 }
155 }
156
157 return edges;
158 }
159
160 private void printEdge(Edge edge) {
161 stream.printf(" <edge from='%d' to='%d' index='%d'/>%n", edge.from, edge.to, edge.index);
162 }
163
164 private String escape(String s) {
165 s = s.replace("&", "&amp;");
166 s = s.replace("<", "&lt;");
167 s = s.replace(">", "&gt;");
168 s = s.replace("\"", "&quot;");
169 s = s.replace("'", "&apos;");
170 return s;
171 }
172 }