Search in sources :

Example 11 with IntermediateEdge

use of org.candle.decompiler.intermediate.graph.edge.IntermediateEdge in project candle-decompiler by bradsdavis.

the class ClassIntermediateVisitor method writeGraph.

private void writeGraph(String name, InstructionGraphContext igc) {
    LOG.debug("Instruction Graph ======");
    File a = new File("/Users/bradsdavis/Projects/workspace/clzTest/" + name);
    Writer x;
    try {
        x = new FileWriter(a);
        DOTExporter<InstructionHandle, IntermediateEdge> f = new DOTExporter<InstructionHandle, IntermediateEdge>(new IntegerNameProvider<InstructionHandle>(), new InstructionLabelProvider(), new IntermediateEdgeProvider(), null, new InstructionEdgeAttributeProvider());
        f.export(x, igc.getGraph());
    } catch (IOException e) {
        e.printStackTrace();
    }
    LOG.debug("End Instruction Graph ======");
}
Also used : DOTExporter(org.jgrapht.ext.DOTExporter) InstructionLabelProvider(org.candle.decompiler.instruction.graph.vertex.InstructionLabelProvider) IntermediateEdgeProvider(org.candle.decompiler.intermediate.graph.edge.IntermediateEdgeProvider) FileWriter(java.io.FileWriter) InstructionEdgeAttributeProvider(org.candle.decompiler.instruction.graph.edge.InstructionEdgeAttributeProvider) IOException(java.io.IOException) File(java.io.File) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionGraphWriter(org.candle.decompiler.instruction.graph.enhancer.InstructionGraphWriter) Writer(java.io.Writer) IntermediateGraphWriter(org.candle.decompiler.intermediate.graph.enhancer.IntermediateGraphWriter) FileWriter(java.io.FileWriter) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 12 with IntermediateEdge

use of org.candle.decompiler.intermediate.graph.edge.IntermediateEdge in project candle-decompiler by bradsdavis.

the class Try method visitAbstractIntermediate.

@Override
public void visitAbstractIntermediate(AbstractIntermediate line) {
    // such poor performance...
    final Set<IntermediateEdge> edges = new HashSet<IntermediateEdge>(igc.getIncomingEdges(line));
    for (IntermediateEdge ie : edges) {
        if (ie.getType() == EdgeType.EXCEPTION) {
            System.out.println("EXCEEEEEPTIONS!");
            TryIntermediate ti = new TryIntermediate(line.getInstruction());
            igc.getGraph().addVertex(ti);
            igc.redirectPredecessors(line, ti);
        }
    }
}
Also used : TryIntermediate(org.candle.decompiler.intermediate.code.TryIntermediate) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) HashSet(java.util.HashSet)

Example 13 with IntermediateEdge

use of org.candle.decompiler.intermediate.graph.edge.IntermediateEdge in project candle-decompiler by bradsdavis.

the class CatchUpperRangeVisitor method visitCatchIntermediate.

@Override
public void visitCatchIntermediate(CatchIntermediate line) {
    // first, check if the line has an end already..
    if (line.getBlockRange().getEnd() != null) {
        return;
    }
    // processLastCatch(line);
    BreadthFirstIterator<AbstractIntermediate, IntermediateEdge> bfi = new BreadthFirstIterator<AbstractIntermediate, IntermediateEdge>(igc.getGraph(), line);
    AbstractIntermediate lastStatement = null;
    while (bfi.hasNext()) {
        AbstractIntermediate next = bfi.next();
        if (next instanceof GoToIntermediate) {
            // this would be a possible GOTO... find previous.
            LOG.debug("Catch GOGO: " + next + " goto:" + next.getInstruction().getPosition());
            lastStatement = igc.getSinglePredecessor(next);
            break;
        }
        if (next instanceof StatementIntermediate) {
            // determine what type of statement...
            if (((StatementIntermediate) next).getExpression() instanceof Throw) {
                lastStatement = next;
                break;
            }
            if (((StatementIntermediate) next).getExpression() instanceof Return) {
                lastStatement = next;
                break;
            }
        }
    }
    if (lastStatement != null) {
        line.getBlockRange().setEnd(lastStatement.getInstruction());
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) BreadthFirstIterator(org.jgrapht.traverse.BreadthFirstIterator) Return(org.candle.decompiler.intermediate.expression.Return) GoToIntermediate(org.candle.decompiler.intermediate.code.GoToIntermediate) Throw(org.candle.decompiler.intermediate.expression.Throw) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)

Example 14 with IntermediateEdge

use of org.candle.decompiler.intermediate.graph.edge.IntermediateEdge in project candle-decompiler by bradsdavis.

the class GraphUtil method redirectSuccessors.

public void redirectSuccessors(T source, T target) {
    List<T> successors = Graphs.successorListOf(this.graph, source);
    // remove edges between predecessor and source.
    for (T s : successors) {
        IntermediateEdge existing = graph.getEdge(source, s);
        redirectStart(existing, target);
    }
}
Also used : IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)

Example 15 with IntermediateEdge

use of org.candle.decompiler.intermediate.graph.edge.IntermediateEdge in project candle-decompiler by bradsdavis.

the class ConditionToWhileLoop method visitBooleanBranchIntermediate.

@Override
public void visitBooleanBranchIntermediate(BooleanBranchIntermediate line) {
    List<AbstractIntermediate> predecessors = Graphs.predecessorListOf(igc.getGraph(), line);
    CycleDetector<AbstractIntermediate, IntermediateEdge> cycleDetector = new CycleDetector<AbstractIntermediate, IntermediateEdge>(igc.getGraph());
    if (!cycleDetector.detectCyclesContainingVertex(line)) {
        return;
    }
    // first, determine if the condition has two incoming lines.
    if (predecessors.size() >= 2) {
        // check to see that 1 predecessor is a GOTO.
        TreeSet<GoToIntermediate> incomingGotoNonNested = new TreeSet<GoToIntermediate>(new IntermediateComparator());
        TreeSet<GoToIntermediate> incomingGotoNested = new TreeSet<GoToIntermediate>(new IntermediateComparator());
        GoToIntermediate nestedLine = null;
        AbstractIntermediate otherLine = null;
        // classify.
        for (AbstractIntermediate predecessor : predecessors) {
            // check to see if 1 is GOTO.
            if (predecessor instanceof GoToIntermediate) {
                if (isNested(line, predecessor)) {
                    incomingGotoNested.add((GoToIntermediate) predecessor);
                } else {
                    incomingGotoNonNested.add((GoToIntermediate) predecessor);
                }
                continue;
            } else {
                otherLine = predecessor;
            }
        }
        // if there are more than one GOTO statements that are not-nested, return.
        if (incomingGotoNonNested.size() > 1) {
            return;
        }
        nestedLine = getCandidateGoto(incomingGotoNonNested, incomingGotoNested);
        // stop if both conditions aren't met.
        if (nestedLine == null || otherLine == null) {
            return;
        }
        // check to validate that the GOTO instruction is less than the other incoming...
        if (comparator.before(otherLine, line)) {
            // take the lower condition...
            BranchHandle refHandle = null;
            if (comparator.before(nestedLine, line)) {
                refHandle = (BranchHandle) nestedLine.getInstruction();
            } else {
                refHandle = (BranchHandle) line.getInstruction();
            }
            WhileIntermediate whileIntermediate = new WhileIntermediate(refHandle, line);
            // add this to the graph.
            this.igc.getGraph().addVertex(whileIntermediate);
            // get the incoming from the goto...
            igc.redirectPredecessors(nestedLine, whileIntermediate);
            igc.redirectSuccessors(line, whileIntermediate);
            // now, create line from other to while.
            this.igc.getGraph().addEdge(otherLine, whileIntermediate);
            // now, remove the GOTO and Conditional Vertex from graph.
            igc.getGraph().removeVertex(nestedLine);
            igc.getGraph().removeVertex(line);
            // now, the other GOTO lines coming in should all be CONTINUE statements...
            for (GoToIntermediate gotoIntermediate : incomingGotoNested) {
                Continue continueExpression = new Continue(gotoIntermediate.getInstruction());
                StatementIntermediate continueIntermediate = new StatementIntermediate(gotoIntermediate.getInstruction(), continueExpression);
                // add the node...
                igc.getGraph().addVertex(continueIntermediate);
                igc.redirectPredecessors(gotoIntermediate, continueIntermediate);
                // remove vertex.
                igc.getGraph().removeVertex(gotoIntermediate);
                // now, add line to the loop.
                igc.getGraph().addEdge(continueIntermediate, whileIntermediate);
            }
            updateEdges(whileIntermediate);
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) WhileIntermediate(org.candle.decompiler.intermediate.code.loop.WhileIntermediate) GoToIntermediate(org.candle.decompiler.intermediate.code.GoToIntermediate) BranchHandle(org.apache.bcel.generic.BranchHandle) IntermediateComparator(org.candle.decompiler.intermediate.code.IntermediateComparator) Continue(org.candle.decompiler.intermediate.expression.Continue) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) CycleDetector(org.jgrapht.alg.CycleDetector) TreeSet(java.util.TreeSet) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate)

Aggregations

IntermediateEdge (org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)19 InstructionHandle (org.apache.bcel.generic.InstructionHandle)11 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)6 File (java.io.File)3 FileWriter (java.io.FileWriter)3 IOException (java.io.IOException)3 Writer (java.io.Writer)3 TreeSet (java.util.TreeSet)3 BranchHandle (org.apache.bcel.generic.BranchHandle)3 InstructionEdgeAttributeProvider (org.candle.decompiler.instruction.graph.edge.InstructionEdgeAttributeProvider)3 IntermediateEdgeProvider (org.candle.decompiler.intermediate.graph.edge.IntermediateEdgeProvider)3 DOTExporter (org.jgrapht.ext.DOTExporter)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 InstructionLabelProvider (org.candle.decompiler.instruction.graph.vertex.InstructionLabelProvider)2 GoToIntermediate (org.candle.decompiler.intermediate.code.GoToIntermediate)2 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)2 ListenableDirectedGraph (org.jgrapht.graph.ListenableDirectedGraph)2 BreadthFirstIterator (org.jgrapht.traverse.BreadthFirstIterator)2 CodeExceptionGen (org.apache.bcel.generic.CodeExceptionGen)1