Search in sources :

Example 46 with AbstractIntermediate

use of org.candle.decompiler.intermediate.code.AbstractIntermediate in project candle-decompiler by bradsdavis.

the class IfLowerRangeVisitor method visitIfIntermediate.

@Override
public void visitIfIntermediate(IfIntermediate line) {
    AbstractIntermediate l = igc.getTrueTarget(line);
    InstructionHandle lower = l.getInstruction();
    line.getBlockRange().setStart(lower);
    // upper range...
    AbstractIntermediate u = igc.getFalseTarget(line);
    NullIntermediate nullIntermediate = new NullIntermediate(u.getInstruction().getPrev());
    AbstractIntermediate ai = igc.getOrderedIntermediate().floor(nullIntermediate);
    if (ai != null) {
        line.getBlockRange().setEnd(ai.getInstruction());
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) InstructionHandle(org.apache.bcel.generic.InstructionHandle) NullIntermediate(org.candle.decompiler.intermediate.graph.context.NullIntermediate)

Example 47 with AbstractIntermediate

use of org.candle.decompiler.intermediate.code.AbstractIntermediate in project candle-decompiler by bradsdavis.

the class WhileRangeVisitor method visitWhileIntermediate.

@Override
public void visitWhileIntermediate(WhileIntermediate line) {
    AbstractIntermediate falseTarget = igc.getFalseTarget(line);
    AbstractIntermediate trueTarget = igc.getTrueTarget(line);
    line.getBlockRange().setStart(trueTarget.getInstruction());
    line.getBlockRange().setEnd(falseTarget.getInstruction().getPrev());
    super.visitWhileIntermediate(line);
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate)

Example 48 with AbstractIntermediate

use of org.candle.decompiler.intermediate.code.AbstractIntermediate 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)

Example 49 with AbstractIntermediate

use of org.candle.decompiler.intermediate.code.AbstractIntermediate in project candle-decompiler by bradsdavis.

the class RemoveCaseToCaseEdge method visitCaseIntermediate.

@Override
public void visitCaseIntermediate(CaseIntermediate line) {
    // find successor of Case...
    AbstractIntermediate ai = igc.getSingleSuccessor(line);
    // find the previous node...
    Set<AbstractIntermediate> predecessors = new HashSet<AbstractIntermediate>(Graphs.predecessorListOf(igc.getGraph(), ai));
    predecessors.remove(line);
    // remove line.
    for (AbstractIntermediate predecessor : predecessors) {
        // remove line...
        igc.getGraph().removeEdge(predecessor, ai);
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) HashSet(java.util.HashSet)

Example 50 with AbstractIntermediate

use of org.candle.decompiler.intermediate.code.AbstractIntermediate in project candle-decompiler by bradsdavis.

the class IntermediateGraphTransformer method getIntermediateGraphContext.

public IntermediateGraphContext getIntermediateGraphContext() {
    Map<InstructionHandle, AbstractIntermediate> nodeMapping = new HashMap<InstructionHandle, AbstractIntermediate>();
    ListenableDirectedGraph<AbstractIntermediate, IntermediateEdge> intermediateGraph = new ListenableDirectedGraph<AbstractIntermediate, IntermediateEdge>(IntermediateEdge.class);
    // walk the instruction graph and generate the intermediate graph.  start by walking and adding all vertices.  Then, add the connections between the vertices.
    for (InstructionHandle ih : igc.getGraph().vertexSet()) {
        if (igc.hasIntermediate(ih)) {
            AbstractIntermediate vertex = igc.getIntermediateFromInstruction(ih);
            intermediateGraph.addVertex(vertex);
            nodeMapping.put(ih, vertex);
        }
    }
    // now, add the links.
    for (InstructionHandle ih : igc.getGraph().vertexSet()) {
        AbstractIntermediate nodeIntermediate = nodeMapping.get(ih);
        if (nodeIntermediate == null) {
            LOG.warn("This shouldn't be...");
            continue;
        }
        List<InstructionHandle> predecessors = igc.getPredecessors(ih);
        List<InstructionHandle> successors = igc.getSuccessors(ih);
        for (InstructionHandle predecessor : predecessors) {
            // find it's AbstractIntermediate.
            AbstractIntermediate predIntermediate = nodeMapping.get(predecessor);
            if (predIntermediate == null) {
                // then something is wrong.
                LOG.warn("This shouldn't be...");
                continue;
            }
            IntermediateEdge insEdge = igc.getGraph().getEdge(predecessor, ih);
            // add an edge to the intermediate graph.
            if (intermediateGraph.containsEdge(predIntermediate, nodeIntermediate)) {
                continue;
            }
            intermediateGraph.addEdge(predIntermediate, nodeIntermediate, (IntermediateEdge) insEdge.clone());
        }
        for (InstructionHandle successor : successors) {
            // find it's AbstractIntermediate.
            AbstractIntermediate successorIntermediate = nodeMapping.get(successor);
            if (successorIntermediate == null) {
                LOG.warn("This shouldn't be...");
                continue;
            }
            if (intermediateGraph.containsEdge(nodeIntermediate, successorIntermediate)) {
                continue;
            }
            IntermediateEdge insEdge = igc.getGraph().getEdge(ih, successor);
            // add an edge to the intermediate graph.
            intermediateGraph.addEdge(nodeIntermediate, successorIntermediate, (IntermediateEdge) insEdge.clone());
        }
    }
    return new IntermediateGraphContext(intermediateGraph);
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) HashMap(java.util.HashMap) IntermediateGraphContext(org.candle.decompiler.intermediate.graph.context.IntermediateGraphContext) ListenableDirectedGraph(org.jgrapht.graph.ListenableDirectedGraph) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Aggregations

AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)50 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)11 InstructionHandle (org.apache.bcel.generic.InstructionHandle)9 TreeSet (java.util.TreeSet)8 GoToIntermediate (org.candle.decompiler.intermediate.code.GoToIntermediate)8 HashSet (java.util.HashSet)6 CatchIntermediate (org.candle.decompiler.intermediate.code.CatchIntermediate)6 Declaration (org.candle.decompiler.intermediate.expression.Declaration)6 IntermediateEdge (org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)6 TryIntermediate (org.candle.decompiler.intermediate.code.TryIntermediate)5 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 IntermediateComparator (org.candle.decompiler.intermediate.code.IntermediateComparator)4 BooleanBranchIntermediate (org.candle.decompiler.intermediate.code.BooleanBranchIntermediate)3 FinallyIntermediate (org.candle.decompiler.intermediate.code.FinallyIntermediate)3 ArrayList (java.util.ArrayList)2 BranchHandle (org.apache.bcel.generic.BranchHandle)2 CodeExceptionGen (org.apache.bcel.generic.CodeExceptionGen)2 ElseIfBlock (org.candle.decompiler.ast.conditional.ElseIfBlock)2 CaseIntermediate (org.candle.decompiler.intermediate.code.CaseIntermediate)2