Mercurial > hg > graal-jvmci-8
comparison graal/Compiler/src/com/sun/c1x/debug/CFGPrinterObserver.java @ 2507:9ec15d6914ca
Pull over of compiler from maxine repository.
author | Thomas Wuerthinger <thomas@wuerthinger.net> |
---|---|
date | Wed, 27 Apr 2011 11:43:22 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2506:4a3bf8a5bf41 | 2507:9ec15d6914ca |
---|---|
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 | |
27 import com.sun.c1x.*; | |
28 import com.sun.c1x.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 | |
43 public CFGPrinterObserver() { | |
44 } | |
45 | |
46 @Override | |
47 public void compilationStarted(CompilationEvent event) { | |
48 // Supports only one compilation at the same time | |
49 assert currentCompilation == null; | |
50 | |
51 currentCompilation = event.getCompilation(); | |
52 if (buffer == null) { | |
53 buffer = new ByteArrayOutputStream(); | |
54 } | |
55 cfgPrinter = new CFGPrinter(buffer, currentCompilation.target); | |
56 cfgPrinter.printCompilation(currentCompilation.method); | |
57 } | |
58 | |
59 @Override | |
60 public void compilationEvent(CompilationEvent event) { | |
61 assert currentCompilation == event.getCompilation(); | |
62 | |
63 String label = event.getLabel(); | |
64 | |
65 if (event.getAllocator() != null && event.getIntervals() != null) { | |
66 cfgPrinter.printIntervals(event.getAllocator(), event.getIntervals(), label); | |
67 } | |
68 | |
69 boolean cfgprinted = false; | |
70 | |
71 if (event.getBlockMap() != null && event.getCodeSize() >= 0) { | |
72 cfgPrinter.printCFG(event.getMethod(), event.getBlockMap(), event.getCodeSize(), label, event.isHIRValid(), event.isLIRValid()); | |
73 cfgprinted = true; | |
74 } | |
75 | |
76 if (event.getStartBlock() != null) { | |
77 cfgPrinter.printCFG(event.getStartBlock(), label, event.isHIRValid(), event.isLIRValid()); | |
78 cfgprinted = true; | |
79 } | |
80 | |
81 if (event.getTargetMethod() != null) { | |
82 if (cfgprinted) { | |
83 // Avoid duplicate "cfg" section | |
84 label = null; | |
85 } | |
86 | |
87 RiRuntime runtime = event.getCompilation().runtime; | |
88 cfgPrinter.printMachineCode(runtime.disassemble(event.getTargetMethod()), label); | |
89 } | |
90 } | |
91 | |
92 @Override | |
93 public void compilationFinished(CompilationEvent event) { | |
94 assert currentCompilation == event.getCompilation(); | |
95 | |
96 cfgPrinter.flush(); | |
97 | |
98 OutputStream cfgFileStream = CFGPrinter.cfgFileStream(); | |
99 if (cfgFileStream != null) { | |
100 synchronized (cfgFileStream) { | |
101 try { | |
102 cfgFileStream.write(buffer.toByteArray()); | |
103 } catch (IOException e) { | |
104 TTY.println("WARNING: Error writing CFGPrinter output for %s to disk: %s", event.getMethod(), e); | |
105 } | |
106 } | |
107 } | |
108 | |
109 buffer.reset(); | |
110 cfgPrinter = null; | |
111 currentCompilation = null; | |
112 } | |
113 } |