Search in sources :

Example 6 with AbstractIntermediate

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

the class RetractDuplicateFinally method processTry.

protected void processTry(TryIntermediate tryIntermediate, FinallyIntermediate finallyIntermediate, Set<Integer> offsets) {
    // ok, now let's handle the try...
    InstructionHandle end = tryIntermediate.getBlockRange().getEnd();
    // next should be GOTO.
    AbstractIntermediate tryEndNode = igc.findNextNode(end);
    AbstractIntermediate gotoIntermediate = null;
    // check to see if this is loop...
    if (tryEndNode instanceof StatementIntermediate) {
        LOG.debug("Position: " + tryEndNode.getInstruction().getPosition() + " Value: " + tryEndNode);
        gotoIntermediate = igc.getSingleSuccessor(tryEndNode);
    } else if (tryEndNode instanceof BooleanBranchIntermediate) {
        BooleanBranchIntermediate bbi = (BooleanBranchIntermediate) tryEndNode;
        // find higher target...
        AbstractIntermediate trueTarget = igc.getTrueTarget(bbi);
        AbstractIntermediate falseTarget = igc.getFalseTarget(bbi);
        int trueTargetPosition = trueTarget.getInstruction().getPosition();
        int falseTargetPosition = falseTarget.getInstruction().getPosition();
        gotoIntermediate = (trueTargetPosition > falseTargetPosition) ? trueTarget : falseTarget;
    }
    // validate it is a GOTO.
    if (!(gotoIntermediate instanceof GoToIntermediate)) {
        LOG.warn("Expected GOTO.  But this isn't: " + gotoIntermediate);
    } else {
        AbstractIntermediate tryFinallyFirst = igc.getSingleSuccessor(gotoIntermediate);
        eliminateNode(tryFinallyFirst, offsets);
        // now, eliminate the GOTO.
        igc.getGraph().removeVertex(gotoIntermediate);
        igc.getGraph().addEdge(tryIntermediate, finallyIntermediate);
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) GoToIntermediate(org.candle.decompiler.intermediate.code.GoToIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) BooleanBranchIntermediate(org.candle.decompiler.intermediate.code.BooleanBranchIntermediate) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 7 with AbstractIntermediate

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

the class RetractDuplicateFinally method matchTryBlock.

protected TryIntermediate matchTryBlock(InstructionHandle min, InstructionHandle max) {
    LinkedList<TryIntermediate> matches = new LinkedList<TryIntermediate>();
    // find the try block...
    for (AbstractIntermediate ai : igc.getGraph().vertexSet()) {
        if (ai instanceof TryIntermediate) {
            TryIntermediate tryIntermediate = ((TryIntermediate) ai);
            LOG.debug("Finally: " + tryIntermediate + " , " + tryIntermediate.getInstruction().getPosition() + " , " + tryIntermediate.getBlockRange().getStart());
            if (tryIntermediate.getBlockRange().getStart().getPosition() == min.getPosition()) {
                // only add where max > try's max range...
                if (tryIntermediate.getBlockRange().getEnd().getPosition() < max.getPosition()) {
                    matches.add(tryIntermediate);
                }
            }
        }
    }
    // return the smaller range...
    if (matches.size() > 0) {
        Collections.sort(matches, new Comparator<TryIntermediate>() {

            @Override
            public int compare(TryIntermediate t1, TryIntermediate t2) {
                if (t1.getBlockRange().getEnd().getPosition() > t2.getBlockRange().getEnd().getPosition()) {
                    return -1;
                }
                return 0;
            }
        });
        LOG.debug("Match: " + matches.peekFirst() + " Range: " + matches.peekFirst().getBlockRange());
        return matches.getFirst();
    }
    return null;
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) TryIntermediate(org.candle.decompiler.intermediate.code.TryIntermediate) LinkedList(java.util.LinkedList)

Example 8 with AbstractIntermediate

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

the class RetractDuplicateFinally method processCatch.

protected void processCatch(CatchIntermediate catchIntermediate, FinallyIntermediate finallyIntermediate, Set<Integer> offsets) {
    for (CodeExceptionGen ceg : finallyIntermediate.getCodeExceptions()) {
        int position = ceg.getEndPC().getPosition();
        if (catchIntermediate.getBlockRange().containsNumber(position)) {
            LOG.debug("Relevant: " + position);
            // we found the relevant position, now we need to find the next...
            AbstractIntermediate lastNode = igc.findNextNode(ceg.getEndPC());
            AbstractIntermediate finallyStart = igc.getSingleSuccessor(lastNode);
            LOG.debug("Finally start: " + finallyStart);
            // start eliminating nodes from this point.
            eliminateNode(finallyStart, offsets);
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

Example 9 with AbstractIntermediate

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

the class FinallyRangeVisitor method visitFinallyIntermediate.

@Override
public void visitFinallyIntermediate(FinallyIntermediate line) {
    NonGotoIterator iter = new NonGotoIterator(igc.getGraph(), line);
    // walk until we find a THROW.
    AbstractIntermediate throwsStatement = null;
    while (iter.hasNext()) {
        AbstractIntermediate i = iter.next();
        if (i instanceof StatementIntermediate) {
            StatementIntermediate s = (StatementIntermediate) i;
            if (s.getExpression() instanceof Throw) {
                throwsStatement = s;
                break;
            }
        }
    }
    if (throwsStatement != null) {
        line.getBlockRange().setEnd(throwsStatement.getInstruction());
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) Throw(org.candle.decompiler.intermediate.expression.Throw) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate)

Example 10 with AbstractIntermediate

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

the class ConstantArrayCompressor method extractNextDeclaration.

public Declaration extractNextDeclaration(StatementIntermediate statement) {
    List<AbstractIntermediate> successors = Graphs.successorListOf(igc.getGraph(), statement);
    if (successors.size() != 1) {
        return null;
    }
    if (!(successors.get(0) instanceof StatementIntermediate)) {
        return null;
    }
    StatementIntermediate si = (StatementIntermediate) successors.get(0);
    if (si.getExpression() instanceof Declaration) {
        return (Declaration) si.getExpression();
    }
    return null;
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) Declaration(org.candle.decompiler.intermediate.expression.Declaration)

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