use of org.graalvm.compiler.bytecode.BytecodeDisassembler in project graal by oracle.
the class BytecodeParser method toString.
@Override
public String toString() {
Formatter fmt = new Formatter();
BytecodeParser bp = this;
String indent = "";
while (bp != null) {
if (bp != this) {
fmt.format("%n%s", indent);
}
fmt.format("%s [bci: %d, intrinsic: %s]", bp.code.asStackTraceElement(bp.bci()), bp.bci(), bp.parsingIntrinsic());
fmt.format("%n%s", new BytecodeDisassembler().disassemble(bp.code, bp.bci(), bp.bci() + 10));
bp = bp.parent;
indent += " ";
}
return fmt.toString();
}
use of org.graalvm.compiler.bytecode.BytecodeDisassembler in project graal by oracle.
the class GraalTutorial method testGetBytecodes.
/*
* Example for the Graal API: access the Graal API metadata object for a method.
*/
@Test
public void testGetBytecodes() throws NoSuchMethodException {
Method reflectionMethod = String.class.getDeclaredMethod("hashCode");
ResolvedJavaMethod method = metaAccess.lookupJavaMethod(reflectionMethod);
/*
* ResolvedJavaMethod provides all information that you want about a method, for example,
* the bytecodes.
*/
byte[] bytecodes = method.getCode();
/*
* BytecodeDisassembler shows you how to iterate bytecodes, how to access type information,
* and more.
*/
String disassembly = new BytecodeDisassembler().disassemble(method);
/*
* We don't want test cases to print any output, so we check the validity of the output
* instead.
*/
Pattern disassemblyLineRE = Pattern.compile(" *\\d+: [a-z][\\w_]+");
for (String line : disassembly.split("\\n")) {
Assert.assertTrue(line, disassemblyLineRE.matcher(line).find());
}
Assert.assertTrue(bytecodes.length > 0);
}
use of org.graalvm.compiler.bytecode.BytecodeDisassembler in project graal by oracle.
the class GraalOSRTestBase method getBackedgeBCI.
/**
* Returns the target BCI of the first bytecode backedge. This is where HotSpot triggers
* on-stack-replacement in case the backedge counter overflows.
*/
private static int getBackedgeBCI(DebugContext debug, ResolvedJavaMethod method) {
Bytecode code = new ResolvedJavaMethodBytecode(method);
BytecodeStream stream = new BytecodeStream(code.getCode());
OptionValues options = debug.getOptions();
BciBlockMapping bciBlockMapping = BciBlockMapping.create(stream, code, options, debug);
for (BciBlock block : bciBlockMapping.getBlocks()) {
if (block.startBci != -1) {
int bci = block.startBci;
for (BciBlock succ : block.getSuccessors()) {
if (succ.startBci != -1) {
int succBci = succ.startBci;
if (succBci < bci) {
// back edge
return succBci;
}
}
}
}
}
TTY.println("Cannot find loop back edge with bytecode loops at:%s", Arrays.toString(bciBlockMapping.getLoopHeaders()));
TTY.println(new BytecodeDisassembler().disassemble(code));
return -1;
}
use of org.graalvm.compiler.bytecode.BytecodeDisassembler in project graal by oracle.
the class CFGPrinter method printCFG.
/**
* Prints the specified list of blocks.
*
* @param label A label describing the compilation phase that produced the control flow graph.
* @param blocks The list of blocks to be printed.
*/
public void printCFG(String label, AbstractBlockBase<?>[] blocks, boolean printNodes) {
if (lir == null) {
latestScheduling = new NodeMap<>(cfg.getNodeToBlock());
for (AbstractBlockBase<?> abstractBlock : blocks) {
if (abstractBlock == null) {
continue;
}
Block block = (Block) abstractBlock;
Node cur = block.getBeginNode();
while (true) {
assert inFixedSchedule(cur) && latestScheduling.get(cur) == block;
scheduleInputs(cur, block);
if (cur == block.getEndNode()) {
break;
}
assert cur.successors().count() == 1;
cur = cur.successors().first();
}
}
}
begin("cfg");
out.print("name \"").print(label).println('"');
for (AbstractBlockBase<?> block : blocks) {
printBlock(block, printNodes);
}
end("cfg");
// As a workaround we dump the bytecode after every cfg.
if (method != null) {
printBytecodes(new BytecodeDisassembler(false).disassemble(method));
}
latestScheduling = null;
}
use of org.graalvm.compiler.bytecode.BytecodeDisassembler in project graal by oracle.
the class CFGPrinterObserver method dumpSandboxed.
public void dumpSandboxed(DebugContext debug, Object object, String message) {
OptionValues options = debug.getOptions();
boolean dumpFrontend = PrintCFG.getValue(options);
if (!dumpFrontend && isFrontendObject(object)) {
return;
}
if (cfgPrinter == null) {
try {
Path dumpFile = debug.getDumpPath(".cfg", false);
cfgFile = dumpFile.toFile();
OutputStream out = new BufferedOutputStream(new FileOutputStream(cfgFile));
cfgPrinter = new CFGPrinter(out);
} catch (IOException e) {
throw (GraalError) new GraalError("Could not open %s", cfgFile == null ? "[null]" : cfgFile.getAbsolutePath()).initCause(e);
}
}
if (!checkMethodScope(debug)) {
return;
}
try {
if (curMethod instanceof ResolvedJavaMethod) {
cfgPrinter.method = (ResolvedJavaMethod) curMethod;
}
if (object instanceof LIR) {
cfgPrinter.lir = (LIR) object;
} else {
cfgPrinter.lir = debug.contextLookup(LIR.class);
}
cfgPrinter.nodeLirGenerator = debug.contextLookup(NodeLIRBuilder.class);
cfgPrinter.livenessInfo = debug.contextLookup(GlobalLivenessInfo.class);
cfgPrinter.res = debug.contextLookup(LIRGenerationResult.class);
if (cfgPrinter.nodeLirGenerator != null) {
cfgPrinter.target = cfgPrinter.nodeLirGenerator.getLIRGeneratorTool().target();
}
if (cfgPrinter.lir != null && cfgPrinter.lir.getControlFlowGraph() instanceof ControlFlowGraph) {
cfgPrinter.cfg = (ControlFlowGraph) cfgPrinter.lir.getControlFlowGraph();
}
CodeCacheProvider codeCache = debug.contextLookup(CodeCacheProvider.class);
if (codeCache != null) {
cfgPrinter.target = codeCache.getTarget();
}
if (object instanceof BciBlockMapping) {
BciBlockMapping blockMap = (BciBlockMapping) object;
cfgPrinter.printCFG(message, blockMap);
if (blockMap.code.getCode() != null) {
cfgPrinter.printBytecodes(new BytecodeDisassembler(false).disassemble(blockMap.code));
}
} else if (object instanceof LIR) {
// Currently no node printing for lir
cfgPrinter.printCFG(message, cfgPrinter.lir.codeEmittingOrder(), false);
lastLIR = (LIR) object;
if (delayedIntervals != null) {
cfgPrinter.printIntervals(message, delayedIntervals);
delayedIntervals = null;
}
} else if (object instanceof ScheduleResult) {
cfgPrinter.printSchedule(message, (ScheduleResult) object);
} else if (object instanceof StructuredGraph) {
if (cfgPrinter.cfg == null) {
StructuredGraph graph = (StructuredGraph) object;
cfgPrinter.cfg = ControlFlowGraph.compute(graph, true, true, true, false);
cfgPrinter.printCFG(message, cfgPrinter.cfg.getBlocks(), true);
} else {
cfgPrinter.printCFG(message, cfgPrinter.cfg.getBlocks(), true);
}
} else if (object instanceof CompilationResult) {
final CompilationResult compResult = (CompilationResult) object;
cfgPrinter.printMachineCode(disassemble(codeCache, compResult, null), message);
} else if (object instanceof InstalledCode) {
CompilationResult compResult = debug.contextLookup(CompilationResult.class);
if (compResult != null) {
cfgPrinter.printMachineCode(disassemble(codeCache, compResult, (InstalledCode) object), message);
}
} else if (object instanceof IntervalDumper) {
if (lastLIR == cfgPrinter.lir) {
cfgPrinter.printIntervals(message, (IntervalDumper) object);
} else {
if (delayedIntervals != null) {
debug.log("Some delayed intervals were dropped (%s)", delayedIntervals);
}
delayedIntervals = (IntervalDumper) object;
}
} else if (object instanceof AbstractBlockBase<?>[]) {
cfgPrinter.printCFG(message, (AbstractBlockBase<?>[]) object, false);
} else if (object instanceof Trace) {
cfgPrinter.printCFG(message, ((Trace) object).getBlocks(), false);
} else if (object instanceof TraceBuilderResult) {
cfgPrinter.printTraces(message, (TraceBuilderResult) object);
}
} finally {
cfgPrinter.target = null;
cfgPrinter.lir = null;
cfgPrinter.res = null;
cfgPrinter.nodeLirGenerator = null;
cfgPrinter.livenessInfo = null;
cfgPrinter.cfg = null;
cfgPrinter.flush();
}
}
Aggregations