43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 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.
|
|
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 org.graalvm.compiler.printer;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.graph.Edges.Type.Inputs;
|
|
26 |
import static org.graalvm.compiler.graph.Edges.Type.Successors;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.nio.ByteBuffer;
|
|
30 |
import java.nio.channels.WritableByteChannel;
|
|
31 |
import java.nio.charset.Charset;
|
|
32 |
import java.util.Arrays;
|
|
33 |
import java.util.HashMap;
|
|
34 |
import java.util.LinkedHashMap;
|
|
35 |
import java.util.LinkedList;
|
|
36 |
import java.util.List;
|
|
37 |
import java.util.Map;
|
|
38 |
import java.util.Map.Entry;
|
|
39 |
|
|
40 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
46509
|
41 |
import org.graalvm.compiler.bytecode.Bytecode;
|
43972
|
42 |
import org.graalvm.compiler.core.common.cfg.BlockMap;
|
|
43 |
import org.graalvm.compiler.debug.Debug;
|
|
44 |
import org.graalvm.compiler.debug.GraalDebugConfig.Options;
|
|
45 |
import org.graalvm.compiler.graph.CachedGraph;
|
|
46 |
import org.graalvm.compiler.graph.Edges;
|
|
47 |
import org.graalvm.compiler.graph.Graph;
|
|
48 |
import org.graalvm.compiler.graph.InputEdges;
|
|
49 |
import org.graalvm.compiler.graph.Node;
|
|
50 |
import org.graalvm.compiler.graph.NodeClass;
|
|
51 |
import org.graalvm.compiler.graph.NodeList;
|
|
52 |
import org.graalvm.compiler.graph.NodeMap;
|
|
53 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
54 |
import org.graalvm.compiler.nodes.AbstractEndNode;
|
|
55 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
56 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
57 |
import org.graalvm.compiler.nodes.ControlSinkNode;
|
|
58 |
import org.graalvm.compiler.nodes.ControlSplitNode;
|
|
59 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
60 |
import org.graalvm.compiler.nodes.PhiNode;
|
|
61 |
import org.graalvm.compiler.nodes.ProxyNode;
|
|
62 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
63 |
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
|
|
64 |
import org.graalvm.compiler.nodes.VirtualState;
|
|
65 |
import org.graalvm.compiler.nodes.cfg.Block;
|
|
66 |
import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
|
|
67 |
import org.graalvm.compiler.phases.schedule.SchedulePhase;
|
|
68 |
|
|
69 |
import jdk.vm.ci.meta.JavaType;
|
|
70 |
import jdk.vm.ci.meta.ResolvedJavaField;
|
|
71 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
72 |
import jdk.vm.ci.meta.Signature;
|
|
73 |
|
|
74 |
public class BinaryGraphPrinter implements GraphPrinter {
|
|
75 |
|
|
76 |
private static final int CONSTANT_POOL_MAX_SIZE = 8000;
|
|
77 |
|
|
78 |
private static final int BEGIN_GROUP = 0x00;
|
|
79 |
private static final int BEGIN_GRAPH = 0x01;
|
|
80 |
private static final int CLOSE_GROUP = 0x02;
|
|
81 |
|
|
82 |
private static final int POOL_NEW = 0x00;
|
|
83 |
private static final int POOL_STRING = 0x01;
|
|
84 |
private static final int POOL_ENUM = 0x02;
|
|
85 |
private static final int POOL_CLASS = 0x03;
|
|
86 |
private static final int POOL_METHOD = 0x04;
|
|
87 |
private static final int POOL_NULL = 0x05;
|
|
88 |
private static final int POOL_NODE_CLASS = 0x06;
|
|
89 |
private static final int POOL_FIELD = 0x07;
|
|
90 |
private static final int POOL_SIGNATURE = 0x08;
|
|
91 |
|
|
92 |
private static final int PROPERTY_POOL = 0x00;
|
|
93 |
private static final int PROPERTY_INT = 0x01;
|
|
94 |
private static final int PROPERTY_LONG = 0x02;
|
|
95 |
private static final int PROPERTY_DOUBLE = 0x03;
|
|
96 |
private static final int PROPERTY_FLOAT = 0x04;
|
|
97 |
private static final int PROPERTY_TRUE = 0x05;
|
|
98 |
private static final int PROPERTY_FALSE = 0x06;
|
|
99 |
private static final int PROPERTY_ARRAY = 0x07;
|
|
100 |
private static final int PROPERTY_SUBGRAPH = 0x08;
|
|
101 |
|
|
102 |
private static final int KLASS = 0x00;
|
|
103 |
private static final int ENUM_KLASS = 0x01;
|
|
104 |
|
|
105 |
static final int CURRENT_MAJOR_VERSION = 1;
|
|
106 |
static final int CURRENT_MINOR_VERSION = 0;
|
|
107 |
|
|
108 |
static final byte[] MAGIC_BYTES = {'B', 'I', 'G', 'V'};
|
|
109 |
|
|
110 |
private void writeVersion() throws IOException {
|
|
111 |
writeBytesRaw(MAGIC_BYTES);
|
|
112 |
writeByte(CURRENT_MAJOR_VERSION);
|
|
113 |
writeByte(CURRENT_MINOR_VERSION);
|
|
114 |
}
|
|
115 |
|
|
116 |
private static final class ConstantPool extends LinkedHashMap<Object, Character> {
|
|
117 |
|
|
118 |
private final LinkedList<Character> availableIds;
|
|
119 |
private char nextId;
|
|
120 |
private static final long serialVersionUID = -2676889957907285681L;
|
|
121 |
|
|
122 |
ConstantPool() {
|
|
123 |
super(50, 0.65f);
|
|
124 |
availableIds = new LinkedList<>();
|
|
125 |
}
|
|
126 |
|
|
127 |
@Override
|
|
128 |
protected boolean removeEldestEntry(java.util.Map.Entry<Object, Character> eldest) {
|
|
129 |
if (size() > CONSTANT_POOL_MAX_SIZE) {
|
|
130 |
availableIds.addFirst(eldest.getValue());
|
|
131 |
return true;
|
|
132 |
}
|
|
133 |
return false;
|
|
134 |
}
|
|
135 |
|
|
136 |
private Character nextAvailableId() {
|
|
137 |
if (!availableIds.isEmpty()) {
|
|
138 |
return availableIds.removeFirst();
|
|
139 |
}
|
|
140 |
return nextId++;
|
|
141 |
}
|
|
142 |
|
|
143 |
public char add(Object obj) {
|
|
144 |
Character id = nextAvailableId();
|
|
145 |
put(obj, id);
|
|
146 |
return id;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
private final ConstantPool constantPool;
|
|
151 |
private final ByteBuffer buffer;
|
|
152 |
private final WritableByteChannel channel;
|
46344
|
153 |
private SnippetReflectionProvider snippetReflection;
|
43972
|
154 |
|
|
155 |
private static final Charset utf8 = Charset.forName("UTF-8");
|
|
156 |
|
46344
|
157 |
public BinaryGraphPrinter(WritableByteChannel channel) throws IOException {
|
43972
|
158 |
constantPool = new ConstantPool();
|
|
159 |
buffer = ByteBuffer.allocateDirect(256 * 1024);
|
|
160 |
this.channel = channel;
|
|
161 |
writeVersion();
|
|
162 |
}
|
|
163 |
|
|
164 |
@Override
|
46344
|
165 |
public void setSnippetReflectionProvider(SnippetReflectionProvider snippetReflection) {
|
|
166 |
this.snippetReflection = snippetReflection;
|
|
167 |
}
|
|
168 |
|
|
169 |
@Override
|
43972
|
170 |
public SnippetReflectionProvider getSnippetReflectionProvider() {
|
|
171 |
return snippetReflection;
|
|
172 |
}
|
|
173 |
|
46509
|
174 |
@SuppressWarnings("all")
|
43972
|
175 |
@Override
|
46509
|
176 |
public void print(Graph graph, Map<Object, Object> properties, int id, String format, Object... args) throws IOException {
|
43972
|
177 |
writeByte(BEGIN_GRAPH);
|
46509
|
178 |
if (CURRENT_MAJOR_VERSION == 3) {
|
|
179 |
writeInt(id);
|
|
180 |
writeString(format);
|
|
181 |
writeInt(args.length);
|
|
182 |
for (Object a : args) {
|
|
183 |
writePropertyObject(a);
|
|
184 |
}
|
|
185 |
} else {
|
|
186 |
writePoolObject(String.format(format, args));
|
|
187 |
}
|
43972
|
188 |
writeGraph(graph, properties);
|
|
189 |
flush();
|
|
190 |
}
|
|
191 |
|
|
192 |
private void writeGraph(Graph graph, Map<Object, Object> properties) throws IOException {
|
|
193 |
ScheduleResult scheduleResult = null;
|
|
194 |
if (graph instanceof StructuredGraph) {
|
|
195 |
|
|
196 |
StructuredGraph structuredGraph = (StructuredGraph) graph;
|
|
197 |
scheduleResult = structuredGraph.getLastSchedule();
|
|
198 |
if (scheduleResult == null) {
|
|
199 |
|
|
200 |
// Also provide a schedule when an error occurs
|
46344
|
201 |
if (Options.PrintGraphWithSchedule.getValue(graph.getOptions()) || Debug.contextLookup(Throwable.class) != null) {
|
43972
|
202 |
try {
|
46344
|
203 |
SchedulePhase schedule = new SchedulePhase(graph.getOptions());
|
43972
|
204 |
schedule.apply(structuredGraph);
|
|
205 |
scheduleResult = structuredGraph.getLastSchedule();
|
|
206 |
} catch (Throwable t) {
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
}
|
|
211 |
}
|
|
212 |
ControlFlowGraph cfg = scheduleResult == null ? Debug.contextLookup(ControlFlowGraph.class) : scheduleResult.getCFG();
|
|
213 |
BlockMap<List<Node>> blockToNodes = scheduleResult == null ? null : scheduleResult.getBlockToNodesMap();
|
|
214 |
NodeMap<Block> nodeToBlocks = scheduleResult == null ? null : scheduleResult.getNodeToBlockMap();
|
|
215 |
List<Block> blocks = cfg == null ? null : Arrays.asList(cfg.getBlocks());
|
|
216 |
writeProperties(properties);
|
|
217 |
writeNodes(graph, nodeToBlocks, cfg);
|
|
218 |
writeBlocks(blocks, blockToNodes);
|
|
219 |
}
|
|
220 |
|
|
221 |
private void flush() throws IOException {
|
|
222 |
buffer.flip();
|
46344
|
223 |
/*
|
|
224 |
* Try not to let interrupted threads aborting the write. There's still a race here but an
|
|
225 |
* interrupt that's been pending for a long time shouldn't stop this writing.
|
|
226 |
*/
|
|
227 |
boolean interrupted = Thread.interrupted();
|
|
228 |
try {
|
|
229 |
channel.write(buffer);
|
|
230 |
} finally {
|
|
231 |
if (interrupted) {
|
|
232 |
Thread.currentThread().interrupt();
|
|
233 |
}
|
|
234 |
}
|
43972
|
235 |
buffer.compact();
|
|
236 |
}
|
|
237 |
|
|
238 |
private void ensureAvailable(int i) throws IOException {
|
|
239 |
assert buffer.capacity() >= i : "Can not make " + i + " bytes available, buffer is too small";
|
|
240 |
while (buffer.remaining() < i) {
|
|
241 |
flush();
|
|
242 |
}
|
|
243 |
}
|
|
244 |
|
|
245 |
private void writeByte(int b) throws IOException {
|
|
246 |
ensureAvailable(1);
|
|
247 |
buffer.put((byte) b);
|
|
248 |
}
|
|
249 |
|
|
250 |
private void writeInt(int b) throws IOException {
|
|
251 |
ensureAvailable(4);
|
|
252 |
buffer.putInt(b);
|
|
253 |
}
|
|
254 |
|
|
255 |
private void writeLong(long b) throws IOException {
|
|
256 |
ensureAvailable(8);
|
|
257 |
buffer.putLong(b);
|
|
258 |
}
|
|
259 |
|
|
260 |
private void writeDouble(double b) throws IOException {
|
|
261 |
ensureAvailable(8);
|
|
262 |
buffer.putDouble(b);
|
|
263 |
}
|
|
264 |
|
|
265 |
private void writeFloat(float b) throws IOException {
|
|
266 |
ensureAvailable(4);
|
|
267 |
buffer.putFloat(b);
|
|
268 |
}
|
|
269 |
|
|
270 |
private void writeShort(char b) throws IOException {
|
|
271 |
ensureAvailable(2);
|
|
272 |
buffer.putChar(b);
|
|
273 |
}
|
|
274 |
|
|
275 |
private void writeString(String str) throws IOException {
|
|
276 |
byte[] bytes = str.getBytes(utf8);
|
|
277 |
writeBytes(bytes);
|
|
278 |
}
|
|
279 |
|
|
280 |
private void writeBytes(byte[] b) throws IOException {
|
|
281 |
if (b == null) {
|
|
282 |
writeInt(-1);
|
|
283 |
} else {
|
|
284 |
writeInt(b.length);
|
|
285 |
writeBytesRaw(b);
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
|
289 |
private void writeBytesRaw(byte[] b) throws IOException {
|
|
290 |
int bytesWritten = 0;
|
|
291 |
while (bytesWritten < b.length) {
|
|
292 |
int toWrite = Math.min(b.length - bytesWritten, buffer.capacity());
|
|
293 |
ensureAvailable(toWrite);
|
|
294 |
buffer.put(b, bytesWritten, toWrite);
|
|
295 |
bytesWritten += toWrite;
|
|
296 |
}
|
|
297 |
}
|
|
298 |
|
|
299 |
private void writeInts(int[] b) throws IOException {
|
|
300 |
if (b == null) {
|
|
301 |
writeInt(-1);
|
|
302 |
} else {
|
|
303 |
writeInt(b.length);
|
|
304 |
int sizeInBytes = b.length * 4;
|
|
305 |
ensureAvailable(sizeInBytes);
|
|
306 |
buffer.asIntBuffer().put(b);
|
|
307 |
buffer.position(buffer.position() + sizeInBytes);
|
|
308 |
}
|
|
309 |
}
|
|
310 |
|
|
311 |
private void writeDoubles(double[] b) throws IOException {
|
|
312 |
if (b == null) {
|
|
313 |
writeInt(-1);
|
|
314 |
} else {
|
|
315 |
writeInt(b.length);
|
|
316 |
int sizeInBytes = b.length * 8;
|
|
317 |
ensureAvailable(sizeInBytes);
|
|
318 |
buffer.asDoubleBuffer().put(b);
|
|
319 |
buffer.position(buffer.position() + sizeInBytes);
|
|
320 |
}
|
|
321 |
}
|
|
322 |
|
|
323 |
private void writePoolObject(Object object) throws IOException {
|
|
324 |
if (object == null) {
|
|
325 |
writeByte(POOL_NULL);
|
|
326 |
return;
|
|
327 |
}
|
|
328 |
Character id = constantPool.get(object);
|
|
329 |
if (id == null) {
|
|
330 |
addPoolEntry(object);
|
|
331 |
} else {
|
|
332 |
if (object instanceof Enum<?>) {
|
|
333 |
writeByte(POOL_ENUM);
|
|
334 |
} else if (object instanceof Class<?> || object instanceof JavaType) {
|
|
335 |
writeByte(POOL_CLASS);
|
|
336 |
} else if (object instanceof NodeClass) {
|
|
337 |
writeByte(POOL_NODE_CLASS);
|
46509
|
338 |
} else if (object instanceof ResolvedJavaMethod || object instanceof Bytecode) {
|
43972
|
339 |
writeByte(POOL_METHOD);
|
|
340 |
} else if (object instanceof ResolvedJavaField) {
|
|
341 |
writeByte(POOL_FIELD);
|
|
342 |
} else if (object instanceof Signature) {
|
|
343 |
writeByte(POOL_SIGNATURE);
|
|
344 |
} else {
|
|
345 |
writeByte(POOL_STRING);
|
|
346 |
}
|
|
347 |
writeShort(id.charValue());
|
|
348 |
}
|
|
349 |
}
|
|
350 |
|
|
351 |
private static String getClassName(Class<?> klass) {
|
|
352 |
if (!klass.isArray()) {
|
|
353 |
return klass.getName();
|
|
354 |
}
|
|
355 |
return getClassName(klass.getComponentType()) + "[]";
|
|
356 |
}
|
|
357 |
|
46509
|
358 |
@SuppressWarnings("all")
|
43972
|
359 |
private void addPoolEntry(Object object) throws IOException {
|
|
360 |
char index = constantPool.add(object);
|
|
361 |
writeByte(POOL_NEW);
|
|
362 |
writeShort(index);
|
|
363 |
if (object instanceof Class<?>) {
|
|
364 |
Class<?> klass = (Class<?>) object;
|
|
365 |
writeByte(POOL_CLASS);
|
|
366 |
writeString(getClassName(klass));
|
|
367 |
if (klass.isEnum()) {
|
|
368 |
writeByte(ENUM_KLASS);
|
|
369 |
Object[] enumConstants = klass.getEnumConstants();
|
|
370 |
writeInt(enumConstants.length);
|
|
371 |
for (Object o : enumConstants) {
|
|
372 |
writePoolObject(((Enum<?>) o).name());
|
|
373 |
}
|
|
374 |
} else {
|
|
375 |
writeByte(KLASS);
|
|
376 |
}
|
|
377 |
} else if (object instanceof Enum<?>) {
|
|
378 |
writeByte(POOL_ENUM);
|
|
379 |
writePoolObject(object.getClass());
|
|
380 |
writeInt(((Enum<?>) object).ordinal());
|
|
381 |
} else if (object instanceof JavaType) {
|
|
382 |
JavaType type = (JavaType) object;
|
|
383 |
writeByte(POOL_CLASS);
|
|
384 |
writeString(type.toJavaName());
|
|
385 |
writeByte(KLASS);
|
|
386 |
} else if (object instanceof NodeClass) {
|
|
387 |
NodeClass<?> nodeClass = (NodeClass<?>) object;
|
|
388 |
writeByte(POOL_NODE_CLASS);
|
46509
|
389 |
if (CURRENT_MAJOR_VERSION == 3) {
|
|
390 |
writePoolObject(nodeClass.getJavaClass());
|
|
391 |
} else {
|
|
392 |
writeString(nodeClass.getJavaClass().getSimpleName());
|
|
393 |
}
|
43972
|
394 |
writeString(nodeClass.getNameTemplate());
|
|
395 |
writeEdgesInfo(nodeClass, Inputs);
|
|
396 |
writeEdgesInfo(nodeClass, Successors);
|
46509
|
397 |
} else if (object instanceof ResolvedJavaMethod || object instanceof Bytecode) {
|
43972
|
398 |
writeByte(POOL_METHOD);
|
46509
|
399 |
ResolvedJavaMethod method;
|
|
400 |
if (object instanceof Bytecode) {
|
|
401 |
method = ((Bytecode) object).getMethod();
|
|
402 |
} else {
|
|
403 |
method = ((ResolvedJavaMethod) object);
|
|
404 |
}
|
43972
|
405 |
writePoolObject(method.getDeclaringClass());
|
|
406 |
writePoolObject(method.getName());
|
|
407 |
writePoolObject(method.getSignature());
|
|
408 |
writeInt(method.getModifiers());
|
|
409 |
writeBytes(method.getCode());
|
|
410 |
} else if (object instanceof ResolvedJavaField) {
|
|
411 |
writeByte(POOL_FIELD);
|
|
412 |
ResolvedJavaField field = ((ResolvedJavaField) object);
|
|
413 |
writePoolObject(field.getDeclaringClass());
|
|
414 |
writePoolObject(field.getName());
|
|
415 |
writePoolObject(field.getType().getName());
|
|
416 |
writeInt(field.getModifiers());
|
|
417 |
} else if (object instanceof Signature) {
|
|
418 |
writeByte(POOL_SIGNATURE);
|
|
419 |
Signature signature = ((Signature) object);
|
|
420 |
int args = signature.getParameterCount(false);
|
|
421 |
writeShort((char) args);
|
|
422 |
for (int i = 0; i < args; i++) {
|
|
423 |
writePoolObject(signature.getParameterType(i, null).getName());
|
|
424 |
}
|
|
425 |
writePoolObject(signature.getReturnType(null).getName());
|
|
426 |
} else {
|
|
427 |
writeByte(POOL_STRING);
|
|
428 |
writeString(object.toString());
|
|
429 |
}
|
|
430 |
}
|
|
431 |
|
|
432 |
private void writeEdgesInfo(NodeClass<?> nodeClass, Edges.Type type) throws IOException {
|
|
433 |
Edges edges = nodeClass.getEdges(type);
|
|
434 |
writeShort((char) edges.getCount());
|
|
435 |
for (int i = 0; i < edges.getCount(); i++) {
|
|
436 |
writeByte(i < edges.getDirectCount() ? 0 : 1);
|
|
437 |
writePoolObject(edges.getName(i));
|
|
438 |
if (type == Inputs) {
|
|
439 |
writePoolObject(((InputEdges) edges).getInputType(i));
|
|
440 |
}
|
|
441 |
}
|
|
442 |
}
|
|
443 |
|
|
444 |
private void writePropertyObject(Object obj) throws IOException {
|
|
445 |
if (obj instanceof Integer) {
|
|
446 |
writeByte(PROPERTY_INT);
|
|
447 |
writeInt(((Integer) obj).intValue());
|
|
448 |
} else if (obj instanceof Long) {
|
|
449 |
writeByte(PROPERTY_LONG);
|
|
450 |
writeLong(((Long) obj).longValue());
|
|
451 |
} else if (obj instanceof Double) {
|
|
452 |
writeByte(PROPERTY_DOUBLE);
|
|
453 |
writeDouble(((Double) obj).doubleValue());
|
|
454 |
} else if (obj instanceof Float) {
|
|
455 |
writeByte(PROPERTY_FLOAT);
|
|
456 |
writeFloat(((Float) obj).floatValue());
|
|
457 |
} else if (obj instanceof Boolean) {
|
|
458 |
if (((Boolean) obj).booleanValue()) {
|
|
459 |
writeByte(PROPERTY_TRUE);
|
|
460 |
} else {
|
|
461 |
writeByte(PROPERTY_FALSE);
|
|
462 |
}
|
|
463 |
} else if (obj instanceof Graph) {
|
|
464 |
writeByte(PROPERTY_SUBGRAPH);
|
|
465 |
writeGraph((Graph) obj, null);
|
|
466 |
} else if (obj instanceof CachedGraph) {
|
|
467 |
writeByte(PROPERTY_SUBGRAPH);
|
|
468 |
writeGraph(((CachedGraph<?>) obj).getReadonlyCopy(), null);
|
|
469 |
} else if (obj != null && obj.getClass().isArray()) {
|
|
470 |
Class<?> componentType = obj.getClass().getComponentType();
|
|
471 |
if (componentType.isPrimitive()) {
|
|
472 |
if (componentType == Double.TYPE) {
|
|
473 |
writeByte(PROPERTY_ARRAY);
|
|
474 |
writeByte(PROPERTY_DOUBLE);
|
|
475 |
writeDoubles((double[]) obj);
|
|
476 |
} else if (componentType == Integer.TYPE) {
|
|
477 |
writeByte(PROPERTY_ARRAY);
|
|
478 |
writeByte(PROPERTY_INT);
|
|
479 |
writeInts((int[]) obj);
|
|
480 |
} else {
|
|
481 |
writeByte(PROPERTY_POOL);
|
|
482 |
writePoolObject(obj);
|
|
483 |
}
|
|
484 |
} else {
|
|
485 |
writeByte(PROPERTY_ARRAY);
|
|
486 |
writeByte(PROPERTY_POOL);
|
|
487 |
Object[] array = (Object[]) obj;
|
|
488 |
writeInt(array.length);
|
|
489 |
for (Object o : array) {
|
|
490 |
writePoolObject(o);
|
|
491 |
}
|
|
492 |
}
|
|
493 |
} else {
|
|
494 |
writeByte(PROPERTY_POOL);
|
|
495 |
writePoolObject(obj);
|
|
496 |
}
|
|
497 |
}
|
|
498 |
|
|
499 |
@SuppressWarnings("deprecation")
|
|
500 |
private static int getNodeId(Node node) {
|
|
501 |
return node.getId();
|
|
502 |
}
|
|
503 |
|
|
504 |
private Object getBlockForNode(Node node, NodeMap<Block> nodeToBlocks) {
|
|
505 |
if (nodeToBlocks.isNew(node)) {
|
|
506 |
return "NEW (not in schedule)";
|
|
507 |
} else {
|
|
508 |
Block block = nodeToBlocks.get(node);
|
|
509 |
if (block != null) {
|
|
510 |
return block.getId();
|
|
511 |
} else if (node instanceof PhiNode) {
|
|
512 |
return getBlockForNode(((PhiNode) node).merge(), nodeToBlocks);
|
|
513 |
}
|
|
514 |
}
|
|
515 |
return null;
|
|
516 |
}
|
|
517 |
|
|
518 |
private void writeNodes(Graph graph, NodeMap<Block> nodeToBlocks, ControlFlowGraph cfg) throws IOException {
|
|
519 |
Map<Object, Object> props = new HashMap<>();
|
|
520 |
|
|
521 |
writeInt(graph.getNodeCount());
|
|
522 |
|
|
523 |
for (Node node : graph.getNodes()) {
|
|
524 |
NodeClass<?> nodeClass = node.getNodeClass();
|
|
525 |
node.getDebugProperties(props);
|
46344
|
526 |
if (cfg != null && Options.PrintGraphProbabilities.getValue(graph.getOptions()) && node instanceof FixedNode) {
|
43972
|
527 |
try {
|
|
528 |
props.put("probability", cfg.blockFor(node).probability());
|
|
529 |
} catch (Throwable t) {
|
|
530 |
props.put("probability", 0.0);
|
|
531 |
props.put("probability-exception", t);
|
|
532 |
}
|
|
533 |
}
|
46393
|
534 |
|
|
535 |
try {
|
|
536 |
props.put("NodeCost-Size", node.estimatedNodeSize());
|
|
537 |
props.put("NodeCost-Cycles", node.estimatedNodeCycles());
|
|
538 |
} catch (Throwable t) {
|
|
539 |
props.put("node-cost-exception", t.getMessage());
|
|
540 |
}
|
|
541 |
|
43972
|
542 |
if (nodeToBlocks != null) {
|
|
543 |
Object block = getBlockForNode(node, nodeToBlocks);
|
|
544 |
if (block != null) {
|
|
545 |
props.put("node-to-block", block);
|
|
546 |
}
|
|
547 |
}
|
|
548 |
|
|
549 |
if (node instanceof ControlSinkNode) {
|
|
550 |
props.put("category", "controlSink");
|
|
551 |
} else if (node instanceof ControlSplitNode) {
|
|
552 |
props.put("category", "controlSplit");
|
|
553 |
} else if (node instanceof AbstractMergeNode) {
|
|
554 |
props.put("category", "merge");
|
|
555 |
} else if (node instanceof AbstractBeginNode) {
|
|
556 |
props.put("category", "begin");
|
|
557 |
} else if (node instanceof AbstractEndNode) {
|
|
558 |
props.put("category", "end");
|
|
559 |
} else if (node instanceof FixedNode) {
|
|
560 |
props.put("category", "fixed");
|
|
561 |
} else if (node instanceof VirtualState) {
|
|
562 |
props.put("category", "state");
|
|
563 |
} else if (node instanceof PhiNode) {
|
|
564 |
props.put("category", "phi");
|
|
565 |
} else if (node instanceof ProxyNode) {
|
|
566 |
props.put("category", "proxy");
|
|
567 |
} else {
|
|
568 |
if (node instanceof ConstantNode) {
|
|
569 |
ConstantNode cn = (ConstantNode) node;
|
|
570 |
updateStringPropertiesForConstant(props, cn);
|
|
571 |
}
|
|
572 |
props.put("category", "floating");
|
|
573 |
}
|
|
574 |
|
|
575 |
writeInt(getNodeId(node));
|
|
576 |
writePoolObject(nodeClass);
|
|
577 |
writeByte(node.predecessor() == null ? 0 : 1);
|
|
578 |
writeProperties(props);
|
|
579 |
writeEdges(node, Inputs);
|
|
580 |
writeEdges(node, Successors);
|
|
581 |
|
|
582 |
props.clear();
|
|
583 |
}
|
|
584 |
}
|
|
585 |
|
|
586 |
private void writeProperties(Map<Object, Object> props) throws IOException {
|
|
587 |
if (props == null) {
|
|
588 |
writeShort((char) 0);
|
|
589 |
return;
|
|
590 |
}
|
|
591 |
// properties
|
|
592 |
writeShort((char) props.size());
|
|
593 |
for (Entry<Object, Object> entry : props.entrySet()) {
|
|
594 |
String key = entry.getKey().toString();
|
|
595 |
writePoolObject(key);
|
|
596 |
writePropertyObject(entry.getValue());
|
|
597 |
}
|
|
598 |
}
|
|
599 |
|
|
600 |
private void writeEdges(Node node, Edges.Type type) throws IOException {
|
|
601 |
NodeClass<?> nodeClass = node.getNodeClass();
|
|
602 |
Edges edges = nodeClass.getEdges(type);
|
|
603 |
final long[] curOffsets = edges.getOffsets();
|
|
604 |
for (int i = 0; i < edges.getDirectCount(); i++) {
|
|
605 |
writeNodeRef(Edges.getNode(node, curOffsets, i));
|
|
606 |
}
|
|
607 |
for (int i = edges.getDirectCount(); i < edges.getCount(); i++) {
|
|
608 |
NodeList<Node> list = Edges.getNodeList(node, curOffsets, i);
|
|
609 |
if (list == null) {
|
|
610 |
writeShort((char) 0);
|
|
611 |
} else {
|
|
612 |
int listSize = list.count();
|
|
613 |
assert listSize == ((char) listSize);
|
|
614 |
writeShort((char) listSize);
|
|
615 |
for (Node edge : list) {
|
|
616 |
writeNodeRef(edge);
|
|
617 |
}
|
|
618 |
}
|
|
619 |
}
|
|
620 |
}
|
|
621 |
|
|
622 |
private void writeNodeRef(Node edge) throws IOException {
|
|
623 |
if (edge != null) {
|
|
624 |
writeInt(getNodeId(edge));
|
|
625 |
} else {
|
|
626 |
writeInt(-1);
|
|
627 |
}
|
|
628 |
}
|
|
629 |
|
|
630 |
private void writeBlocks(List<Block> blocks, BlockMap<List<Node>> blockToNodes) throws IOException {
|
|
631 |
if (blocks != null && blockToNodes != null) {
|
|
632 |
for (Block block : blocks) {
|
|
633 |
List<Node> nodes = blockToNodes.get(block);
|
|
634 |
if (nodes == null) {
|
|
635 |
writeInt(0);
|
|
636 |
return;
|
|
637 |
}
|
|
638 |
}
|
|
639 |
writeInt(blocks.size());
|
|
640 |
for (Block block : blocks) {
|
|
641 |
List<Node> nodes = blockToNodes.get(block);
|
|
642 |
List<Node> extraNodes = new LinkedList<>();
|
|
643 |
writeInt(block.getId());
|
|
644 |
for (Node node : nodes) {
|
|
645 |
if (node instanceof AbstractMergeNode) {
|
|
646 |
AbstractMergeNode merge = (AbstractMergeNode) node;
|
|
647 |
for (PhiNode phi : merge.phis()) {
|
|
648 |
if (!nodes.contains(phi)) {
|
|
649 |
extraNodes.add(phi);
|
|
650 |
}
|
|
651 |
}
|
|
652 |
}
|
|
653 |
}
|
|
654 |
writeInt(nodes.size() + extraNodes.size());
|
|
655 |
for (Node node : nodes) {
|
|
656 |
writeInt(getNodeId(node));
|
|
657 |
}
|
|
658 |
for (Node node : extraNodes) {
|
|
659 |
writeInt(getNodeId(node));
|
|
660 |
}
|
|
661 |
writeInt(block.getSuccessors().length);
|
|
662 |
for (Block sux : block.getSuccessors()) {
|
|
663 |
writeInt(sux.getId());
|
|
664 |
}
|
|
665 |
}
|
|
666 |
} else {
|
|
667 |
writeInt(0);
|
|
668 |
}
|
|
669 |
}
|
|
670 |
|
|
671 |
@Override
|
|
672 |
public void beginGroup(String name, String shortName, ResolvedJavaMethod method, int bci, Map<Object, Object> properties) throws IOException {
|
|
673 |
writeByte(BEGIN_GROUP);
|
|
674 |
writePoolObject(name);
|
|
675 |
writePoolObject(shortName);
|
|
676 |
writePoolObject(method);
|
|
677 |
writeInt(bci);
|
|
678 |
writeProperties(properties);
|
|
679 |
}
|
|
680 |
|
|
681 |
@Override
|
|
682 |
public void endGroup() throws IOException {
|
|
683 |
writeByte(CLOSE_GROUP);
|
|
684 |
}
|
|
685 |
|
|
686 |
@Override
|
|
687 |
public void close() {
|
|
688 |
try {
|
|
689 |
flush();
|
|
690 |
channel.close();
|
|
691 |
} catch (IOException ex) {
|
|
692 |
throw new Error(ex);
|
|
693 |
}
|
|
694 |
}
|
|
695 |
}
|