annotate graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinterObserver.java @ 2691:4149feada801

GraphvizPrinterObserver: close output stream also in case of exception
author Peter Hofer <peter.hofer@jku.at>
date Wed, 18 May 2011 11:27:10 +0200
parents ed36daed4c43
children 79590d6b4a7c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2684
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
1 /*
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
4 *
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
7 * published by the Free Software Foundation.
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
8 *
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
13 * accompanied this code).
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
14 *
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
18 *
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
21 * questions.
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
22 */
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
23 package com.sun.c1x.debug;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
24
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
25 import java.io.*;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
26
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
27 import com.oracle.graal.graph.*;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
28 import com.sun.c1x.*;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
29 import com.sun.c1x.observer.*;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
30 import com.sun.c1x.value.*;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
31
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
32 /**
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
33 * Observes compilation events and uses {@link IdealGraphPrinter} to generate a graph representation that can be
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
34 * inspected with the <a href="http://kenai.com/projects/igv">Ideal Graph Visualizer</a>.
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
35 *
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
36 * @author Peter Hofer
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
37 */
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
38 public class IdealGraphPrinterObserver implements CompilationObserver {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
39
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
40 private IdealGraphPrinter printer;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
41 private OutputStream stream;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
42
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
43 @Override
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
44 public void compilationStarted(CompilationEvent event) {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
45 assert (stream == null && printer == null);
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
46
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
47 if (!TTY.isSuppressed()) {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
48 String name = event.getMethod().holder().name();
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
49 name = name.substring(1, name.length() - 1).replace('/', '.');
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
50 name = name + "." + event.getMethod().name();
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
51 String filename = name + ".igv.xml";
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
52 try {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
53 stream = new FileOutputStream(filename);
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
54 printer = new IdealGraphPrinter(stream);
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
55
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
56 if (C1XOptions.OmitDOTFrameStates) {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
57 printer.addOmittedClass(FrameState.class);
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
58 }
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
59
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
60 printer.begin(name, name, -1);
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
61 } catch (IOException e) {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
62 e.printStackTrace();
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
63 }
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
64 }
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
65 }
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
66
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
67 @Override
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
68 public void compilationEvent(CompilationEvent event) {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
69 if (printer != null && event.getStartBlock() != null) {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
70 Graph graph = event.getStartBlock().graph();
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
71 printer.print(graph, event.getLabel(), true);
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
72 }
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
73 }
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
74
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
75 @Override
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
76 public void compilationFinished(CompilationEvent event) {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
77 if (printer != null) {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
78 try {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
79 printer.end();
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
80 stream.close();
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
81 } catch (IOException e) {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
82 e.printStackTrace();
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
83 } finally {
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
84 printer = null;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
85 stream = null;
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
86 }
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
87 }
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
88 }
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
89
ed36daed4c43 Add new option PrintIdealGraphToFile and another printer/observer pair to generate a graph representation that can be visualized and inspected with the ideal graph visualizer
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
90 }