use of org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction in project kotlin by JetBrains.
the class CFGraphToDotFilePrinter method dumpNodes.
private static void dumpNodes(List<Instruction> instructions, PrintStream out, int[] count, Map<Instruction, String> nodeToName, Set<Instruction> remainedAfterPostProcessInstructions) {
for (Instruction node : instructions) {
String name = "n" + count[0]++;
nodeToName.put(node, name);
String text = node.toString();
int newline = text.indexOf("\n");
if (newline >= 0) {
text = text.substring(0, newline);
}
String shape = "box";
if (node instanceof ConditionalJumpInstruction || node instanceof UnconditionalJumpInstruction) {
shape = "diamond";
} else if (node instanceof NondeterministicJumpInstruction) {
shape = "Mdiamond";
} else if (node instanceof MagicInstruction && ((MagicInstruction) node).getKind() == MagicKind.UNSUPPORTED_ELEMENT) {
shape = "box, fillcolor=red, style=filled";
} else if (node instanceof LocalFunctionDeclarationInstruction) {
shape = "Mcircle";
} else if (node instanceof SubroutineEnterInstruction || node instanceof SubroutineExitInstruction) {
shape = "roundrect, style=rounded";
}
if (!remainedAfterPostProcessInstructions.contains(node)) {
shape += "box, fillcolor=grey, style=filled";
}
out.println(name + "[label=\"" + text + "\", shape=" + shape + "];");
}
}
use of org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction in project kotlin by JetBrains.
the class AbstractControlFlowTest method formatInstructionList.
private static String formatInstructionList(Collection<Instruction> instructions) {
StringBuilder sb = new StringBuilder();
sb.append('[');
for (Iterator<Instruction> iterator = instructions.iterator(); iterator.hasNext(); ) {
Instruction instruction = iterator.next();
String instructionText = instruction.toString();
String[] parts = instructionText.split("\n");
if (parts.length > 1) {
StringBuilder instructionSb = new StringBuilder();
for (String part : parts) {
instructionSb.append(part.trim()).append(' ');
}
if (instructionSb.toString().length() > 30) {
sb.append(instructionSb.substring(0, 28)).append("..)");
} else {
sb.append(instructionSb);
}
} else {
sb.append(instruction);
}
if (iterator.hasNext()) {
sb.append(", ");
}
}
sb.append(']');
return sb.toString();
}
use of org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction in project kotlin by JetBrains.
the class AbstractDataFlowTest method countDataColumnWidth.
private static int countDataColumnWidth(@NotNull String prefix, @NotNull List<Instruction> instructions, @NotNull Map<Instruction, Edges<InitControlFlowInfo>> data) {
int maxWidth = 0;
for (Instruction instruction : instructions) {
Edges<InitControlFlowInfo> edges = data.get(instruction);
if (edges == null)
continue;
int length = dumpEdgesData(prefix, edges).length();
if (maxWidth < length) {
maxWidth = length;
}
}
return maxWidth;
}
use of org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction in project kotlin by JetBrains.
the class AbstractPseudocodeTest method dumpInstructions.
protected void dumpInstructions(@NotNull PseudocodeImpl pseudocode, @NotNull StringBuilder out, @NotNull Function3<Instruction, /*next*/
Instruction, /*prev*/
Instruction, String> getInstructionData) {
List<Instruction> instructions = pseudocode.getInstructionsIncludingDeadCode();
Set<Instruction> remainedAfterPostProcessInstructions = Sets.newHashSet(pseudocode.getInstructions());
List<PseudocodeLabel> labels = pseudocode.getLabels();
int instructionColumnWidth = countInstructionColumnWidth(instructions);
for (int i = 0; i < instructions.size(); i++) {
Instruction instruction = instructions.get(i);
for (PseudocodeLabel label : labels) {
if (label.getTargetInstructionIndex() == i) {
out.append(label).append(":\n");
}
}
StringBuilder line = new StringBuilder();
// Only print NEXT and PREV if the values are non-trivial
Instruction next = i == instructions.size() - 1 ? null : instructions.get(i + 1);
Instruction prev = i == 0 ? null : instructions.get(i - 1);
String prefix = getIsDeadInstructionPrefix(instruction, remainedAfterPostProcessInstructions) + getDepthInstructionPrefix(instruction, prev);
line.append(formatInstruction(instruction, instructionColumnWidth, prefix));
line.append(getInstructionData.invoke(instruction, next, prev));
out.append(StringUtil.trimTrailing(line.toString()));
out.append("\n");
}
}
use of org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction in project kotlin by JetBrains.
the class CFGraphToDotFilePrinter method dumpDot.
public static void dumpDot(File file, Collection<Pseudocode> pseudocodes) throws FileNotFoundException {
File target = KotlinTestUtils.replaceExtension(file, "dot");
PrintStream out = new PrintStream(target);
out.println("digraph " + FileUtil.getNameWithoutExtension(file) + " {");
int[] count = new int[1];
Map<Instruction, String> nodeToName = new HashMap<Instruction, String>();
for (Pseudocode pseudocode : pseudocodes) {
dumpNodes(pseudocode.getInstructionsIncludingDeadCode(), out, count, nodeToName, Sets.newHashSet(pseudocode.getInstructions()));
}
int i = 0;
for (Pseudocode pseudocode : pseudocodes) {
String label;
KtElement correspondingElement = pseudocode.getCorrespondingElement();
if (correspondingElement instanceof KtNamedDeclaration) {
KtNamedDeclaration namedDeclaration = (KtNamedDeclaration) correspondingElement;
label = namedDeclaration.getName();
} else {
label = "anonymous_" + i;
}
out.println("subgraph cluster_" + i + " {\n" + "label=\"" + label + "\";\n" + "color=blue;\n");
dumpEdges(pseudocode.getInstructionsIncludingDeadCode(), out, count, nodeToName);
out.println("}");
i++;
}
out.println("}");
out.close();
}
Aggregations