Search in sources :

Example 1 with FrameNode

use of org.jetbrains.org.objectweb.asm.tree.FrameNode in project kotlin by JetBrains.

the class ControlFlowGraph method dotDescribe.

private static String dotDescribe(Node node) {
    AbstractInsnNode instruction = node.instruction;
    if (instruction instanceof LabelNode) {
        return "Label";
    } else if (instruction instanceof LineNumberNode) {
        LineNumberNode lineNode = (LineNumberNode) instruction;
        return "Line " + lineNode.line;
    } else if (instruction instanceof FrameNode) {
        return "Stack Frame";
    } else if (instruction instanceof MethodInsnNode) {
        MethodInsnNode method = (MethodInsnNode) instruction;
        String cls = method.owner.substring(method.owner.lastIndexOf('/') + 1);
        cls = cls.replace('$', '.');
        return "Call " + cls + "#" + method.name;
    } else if (instruction instanceof FieldInsnNode) {
        FieldInsnNode field = (FieldInsnNode) instruction;
        String cls = field.owner.substring(field.owner.lastIndexOf('/') + 1);
        cls = cls.replace('$', '.');
        return "Field " + cls + "#" + field.name;
    } else if (instruction instanceof TypeInsnNode && instruction.getOpcode() == Opcodes.NEW) {
        return "New " + ((TypeInsnNode) instruction).desc;
    }
    StringBuilder sb = new StringBuilder();
    String opcodeName = getOpcodeName(instruction.getOpcode());
    sb.append(opcodeName);
    if (instruction instanceof IntInsnNode) {
        IntInsnNode in = (IntInsnNode) instruction;
        sb.append(" ").append(Integer.toString(in.operand));
    } else if (instruction instanceof LdcInsnNode) {
        LdcInsnNode ldc = (LdcInsnNode) instruction;
        sb.append(" ");
        if (ldc.cst instanceof String) {
            sb.append("\\\"");
        }
        sb.append(ldc.cst);
        if (ldc.cst instanceof String) {
            sb.append("\\\"");
        }
    }
    return sb.toString();
}
Also used : LabelNode(org.jetbrains.org.objectweb.asm.tree.LabelNode) FrameNode(org.jetbrains.org.objectweb.asm.tree.FrameNode) LdcInsnNode(org.jetbrains.org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.jetbrains.org.objectweb.asm.tree.MethodInsnNode) FieldInsnNode(org.jetbrains.org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.jetbrains.org.objectweb.asm.tree.IntInsnNode) LineNumberNode(org.jetbrains.org.objectweb.asm.tree.LineNumberNode) TypeInsnNode(org.jetbrains.org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)

Example 2 with FrameNode

use of org.jetbrains.org.objectweb.asm.tree.FrameNode in project kotlin by JetBrains.

the class ControlFlowGraph method toDot.

/**
     * Generates dot output of the graph. This can be used with
     * graphwiz to visualize the graph. For example, if you
     * save the output as graph1.gv you can run
     * <pre>
     * $ dot -Tps graph1.gv -o graph1.ps
     * </pre>
     * to generate a postscript file, which you can then view
     * with "gv graph1.ps".
     *
     * (There are also some online web sites where you can
     * paste in dot graphs and see the visualization right
     * there in the browser.)
     *
     * @return a dot description of this control flow graph,
     *    useful for debugging
     */
@SuppressWarnings("unused")
public String toDot(@Nullable Set<Node> highlight) {
    StringBuilder sb = new StringBuilder();
    sb.append("digraph G {\n");
    AbstractInsnNode instruction = mMethod.instructions.getFirst();
    // Special start node
    sb.append("  start -> ").append(getId(mNodeMap.get(instruction))).append(";\n");
    sb.append("  start [shape=plaintext];\n");
    while (instruction != null) {
        Node node = mNodeMap.get(instruction);
        if (node != null) {
            if (node.successors != null) {
                for (Node to : node.successors) {
                    sb.append("  ").append(getId(node)).append(" -> ").append(getId(to));
                    if (node.instruction instanceof JumpInsnNode) {
                        sb.append(" [label=\"");
                        if (((JumpInsnNode) node.instruction).label == to.instruction) {
                            sb.append("yes");
                        } else {
                            sb.append("no");
                        }
                        sb.append("\"]");
                    }
                    sb.append(";\n");
                }
            }
            if (node.exceptions != null) {
                for (Node to : node.exceptions) {
                    sb.append(getId(node)).append(" -> ").append(getId(to));
                    sb.append(" [label=\"exception\"];\n");
                }
            }
        }
        instruction = instruction.getNext();
    }
    // Labels
    sb.append("\n");
    for (Node node : mNodeMap.values()) {
        instruction = node.instruction;
        sb.append("  ").append(getId(node)).append(" ");
        sb.append("[label=\"").append(dotDescribe(node)).append("\"");
        if (highlight != null && highlight.contains(node)) {
            sb.append(",shape=box,style=filled");
        } else if (instruction instanceof LineNumberNode || instruction instanceof LabelNode || instruction instanceof FrameNode) {
            sb.append(",shape=oval,style=dotted");
        } else {
            sb.append(",shape=box");
        }
        sb.append("];\n");
    }
    sb.append("}");
    return sb.toString();
}
Also used : LabelNode(org.jetbrains.org.objectweb.asm.tree.LabelNode) FrameNode(org.jetbrains.org.objectweb.asm.tree.FrameNode) FieldInsnNode(org.jetbrains.org.objectweb.asm.tree.FieldInsnNode) FrameNode(org.jetbrains.org.objectweb.asm.tree.FrameNode) JumpInsnNode(org.jetbrains.org.objectweb.asm.tree.JumpInsnNode) MethodInsnNode(org.jetbrains.org.objectweb.asm.tree.MethodInsnNode) TryCatchBlockNode(org.jetbrains.org.objectweb.asm.tree.TryCatchBlockNode) AbstractInsnNode(org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode) LineNumberNode(org.jetbrains.org.objectweb.asm.tree.LineNumberNode) TypeInsnNode(org.jetbrains.org.objectweb.asm.tree.TypeInsnNode) ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) IntInsnNode(org.jetbrains.org.objectweb.asm.tree.IntInsnNode) LabelNode(org.jetbrains.org.objectweb.asm.tree.LabelNode) LdcInsnNode(org.jetbrains.org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) JumpInsnNode(org.jetbrains.org.objectweb.asm.tree.JumpInsnNode) LineNumberNode(org.jetbrains.org.objectweb.asm.tree.LineNumberNode) AbstractInsnNode(org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)

Aggregations

AbstractInsnNode (org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)2 FieldInsnNode (org.jetbrains.org.objectweb.asm.tree.FieldInsnNode)2 FrameNode (org.jetbrains.org.objectweb.asm.tree.FrameNode)2 IntInsnNode (org.jetbrains.org.objectweb.asm.tree.IntInsnNode)2 LabelNode (org.jetbrains.org.objectweb.asm.tree.LabelNode)2 LdcInsnNode (org.jetbrains.org.objectweb.asm.tree.LdcInsnNode)2 LineNumberNode (org.jetbrains.org.objectweb.asm.tree.LineNumberNode)2 MethodInsnNode (org.jetbrains.org.objectweb.asm.tree.MethodInsnNode)2 TypeInsnNode (org.jetbrains.org.objectweb.asm.tree.TypeInsnNode)2 ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)1 JumpInsnNode (org.jetbrains.org.objectweb.asm.tree.JumpInsnNode)1 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)1 TryCatchBlockNode (org.jetbrains.org.objectweb.asm.tree.TryCatchBlockNode)1