comparison graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/debug/CFGPrinterObserver.java @ 2874:d90bf514d647

Renamed packages.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 08 Jun 2011 08:59:54 +0200
parents graal/com.oracle.max.graal.compiler/src/com/sun/c1x/debug/CFGPrinterObserver.java@0341b6424579
children 224412c24426
comparison
equal deleted inserted replaced
2873:810e2d253e00 2874:d90bf514d647
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.oracle.max.graal.compiler.debug;
24
25 import java.io.*;
26
27 import com.oracle.max.graal.compiler.*;
28 import com.oracle.max.graal.compiler.observer.*;
29 import com.sun.cri.ri.*;
30
31 /**
32 * Observes compilation events and uses {@link CFGPrinter} to produce a control flow graph for the <a
33 * href="https://c1visualizer.dev.java.net/">C1 Visualizer</a>.
34 *
35 * @author Peter Hofer
36 */
37 public class CFGPrinterObserver implements CompilationObserver {
38
39 private C1XCompilation currentCompilation;
40 private CFGPrinter cfgPrinter;
41 private ByteArrayOutputStream buffer = null;
42 private final OutputStream stream;
43
44 public CFGPrinterObserver() {
45 this(CFGPrinter.cfgFileStream());
46 }
47
48 public CFGPrinterObserver(OutputStream stream) {
49 this.stream = stream;
50 }
51
52 @Override
53 public void compilationStarted(CompilationEvent event) {
54 // Supports only one compilation at the same time
55 assert currentCompilation == null;
56
57 currentCompilation = event.getCompilation();
58 if (buffer == null) {
59 buffer = new ByteArrayOutputStream();
60 }
61 cfgPrinter = new CFGPrinter(buffer, currentCompilation.target);
62 cfgPrinter.printCompilation(currentCompilation.method);
63 }
64
65 @Override
66 public void compilationEvent(CompilationEvent event) {
67 assert currentCompilation == event.getCompilation();
68
69 String label = event.getLabel();
70
71 if (event.getAllocator() != null && event.getIntervals() != null) {
72 cfgPrinter.printIntervals(event.getAllocator(), event.getIntervals(), label);
73 }
74
75 boolean cfgprinted = false;
76
77 if (event.getBlockMap() != null && event.getCodeSize() >= 0) {
78 cfgPrinter.printCFG(event.getMethod(), event.getBlockMap(), event.getCodeSize(), label, event.isHIRValid(), event.isLIRValid());
79 cfgprinted = true;
80 }
81
82 // TODO fix that when schedule is here (startBlock : Instruction->Block)
83 /*if (event.getStartBlock() != null) {
84 cfgPrinter.printCFG((BlockBegin) event.getStartBlock(), label, event.isHIRValid(), event.isLIRValid());
85 cfgprinted = true;
86 }*/
87
88 if (event.getTargetMethod() != null) {
89 if (cfgprinted) {
90 // Avoid duplicate "cfg" section
91 label = null;
92 }
93
94 RiRuntime runtime = event.getCompilation().runtime;
95 cfgPrinter.printMachineCode(runtime.disassemble(event.getTargetMethod()), label);
96 }
97 }
98
99 @Override
100 public void compilationFinished(CompilationEvent event) {
101 assert currentCompilation == event.getCompilation();
102
103 cfgPrinter.flush();
104
105 if (stream != null) {
106 synchronized (stream) {
107 try {
108 stream.write(buffer.toByteArray());
109 stream.flush();
110 } catch (IOException e) {
111 TTY.println("WARNING: Error writing CFGPrinter output for %s: %s", event.getMethod(), e);
112 }
113 }
114 }
115
116 buffer.reset();
117 cfgPrinter = null;
118 currentCompilation = null;
119 }
120 }