Search in sources :

Example 1 with Instruction

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 + "];");
    }
}
Also used : LocalFunctionDeclarationInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction) MagicInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction) SubroutineExitInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction) SubroutineEnterInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction) Instruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction) SubroutineSinkInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction) SubroutineExitInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction) SubroutineEnterInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction) MagicInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction) LocalFunctionDeclarationInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction)

Example 2 with Instruction

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();
}
Also used : Instruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction)

Example 3 with Instruction

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;
}
Also used : Instruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction)

Example 4 with Instruction

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");
    }
}
Also used : PseudocodeLabel(org.jetbrains.kotlin.cfg.pseudocode.PseudocodeLabel) Instruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction) LocalFunctionDeclarationInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction)

Example 5 with Instruction

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();
}
Also used : PrintStream(java.io.PrintStream) KtElement(org.jetbrains.kotlin.psi.KtElement) KtNamedDeclaration(org.jetbrains.kotlin.psi.KtNamedDeclaration) Pseudocode(org.jetbrains.kotlin.cfg.pseudocode.Pseudocode) Instruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction) SubroutineSinkInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction) SubroutineExitInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction) SubroutineEnterInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction) MagicInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction) LocalFunctionDeclarationInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction) File(java.io.File)

Aggregations

Instruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction)5 LocalFunctionDeclarationInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction)3 MagicInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction)2 SubroutineEnterInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction)2 SubroutineExitInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction)2 SubroutineSinkInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction)2 File (java.io.File)1 PrintStream (java.io.PrintStream)1 Pseudocode (org.jetbrains.kotlin.cfg.pseudocode.Pseudocode)1 PseudocodeLabel (org.jetbrains.kotlin.cfg.pseudocode.PseudocodeLabel)1 KtElement (org.jetbrains.kotlin.psi.KtElement)1 KtNamedDeclaration (org.jetbrains.kotlin.psi.KtNamedDeclaration)1