Search in sources :

Example 1 with ElseIfIntermediate

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

the class Else method visitAbstractIntermediate.

@Override
public void visitAbstractIntermediate(AbstractIntermediate line) {
    //for all lines...
    List<AbstractIntermediate> predecessors = Graphs.predecessorListOf(igc.getGraph(), line);
    if (predecessors.size() == 0) {
        return;
    }
    TreeSet<GoToIntermediate> gotoIntermediates = new TreeSet<GoToIntermediate>(new IntermediateComparator());
    for (AbstractIntermediate predecessor : predecessors) {
        if (predecessor instanceof GoToIntermediate) {
            gotoIntermediates.add((GoToIntermediate) predecessor);
        }
    }
    if (gotoIntermediates.size() == 0) {
        return;
    }
    //now, the largest should be...
    GoToIntermediate maxGotoForBranch = gotoIntermediates.pollLast();
    if (maxGotoForBranch.getInstruction().getPosition() > line.getInstruction().getPosition()) {
        return;
    }
    //find the element directly after this one...
    SortedSet<AbstractIntermediate> elseBranchElements = igc.getOrderedIntermediate().subSet(maxGotoForBranch, false, line, false);
    AbstractIntermediate ai = igc.getSinglePredecessor(elseBranchElements.first());
    if (!(ai instanceof IfIntermediate || ai instanceof ElseIfIntermediate)) {
        return;
    }
    //get the first element... 
    if (elseBranchElements.size() > 0) {
        AbstractIntermediate firstElseBlockElement = elseBranchElements.first();
        if (firstElseBlockElement instanceof StatementIntermediate) {
            //we should add the ELSE right away...
            addElseBlock(firstElseBlockElement, maxGotoForBranch);
            return;
        }
        if (firstElseBlockElement instanceof BooleanBranchIntermediate) {
            //only add ELSE if the child of conditional doesn't go to the target.
            BooleanBranchIntermediate ci = (BooleanBranchIntermediate) firstElseBlockElement;
            if (igc.getFalseTarget(ci) == line || igc.getTrueTarget(ci) == line) {
                //do nothing.
                return;
            }
            //else if this is an ElseIf, probably should be an IF.
            if (firstElseBlockElement instanceof ElseIfIntermediate) {
                IfIntermediate ifIntermediate = new IfIntermediate(firstElseBlockElement.getInstruction(), ((BooleanBranchIntermediate) firstElseBlockElement).getExpression());
                igc.getGraph().addVertex(ifIntermediate);
                igc.redirectPredecessors(firstElseBlockElement, ifIntermediate);
                igc.redirectSuccessors(firstElseBlockElement, ifIntermediate);
                igc.getGraph().removeVertex(firstElseBlockElement);
                //add the else between this conditional.
                addElseBlock(ifIntermediate, maxGotoForBranch);
            }
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) TreeSet(java.util.TreeSet) GoToIntermediate(org.candle.decompiler.intermediate.code.GoToIntermediate) IfIntermediate(org.candle.decompiler.intermediate.code.conditional.IfIntermediate) ElseIfIntermediate(org.candle.decompiler.intermediate.code.conditional.ElseIfIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) IntermediateComparator(org.candle.decompiler.intermediate.code.IntermediateComparator) BooleanBranchIntermediate(org.candle.decompiler.intermediate.code.BooleanBranchIntermediate) ElseIfIntermediate(org.candle.decompiler.intermediate.code.conditional.ElseIfIntermediate)

Example 2 with ElseIfIntermediate

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

the class ElseIf method visitIfIntermediate.

@Override
public void visitIfIntermediate(IfIntermediate line) {
    //check to see if the predecessor is an if block.
    List<AbstractIntermediate> predecessors = Graphs.predecessorListOf(igc.getGraph(), line);
    if (predecessors.size() != 1) {
        return;
    }
    //otherwise, see if it is another IF.
    if (predecessors.get(0) instanceof BooleanBranchIntermediate) {
        //check to see whether it is on the ELSE side.
        BooleanBranchIntermediate parent = (BooleanBranchIntermediate) predecessors.get(0);
        LOG.debug(parent.getClass());
        if (!(parent instanceof IfIntermediate)) {
            return;
        }
        if (igc.getFalseTarget(parent) == line) {
            //then this could be an IF block.
            ElseIfIntermediate eii = new ElseIfIntermediate(line.getInstruction(), line.getExpression());
            igc.getGraph().addVertex(eii);
            igc.redirectPredecessors(line, eii);
            igc.redirectSuccessors(line, eii);
            igc.getGraph().removeVertex(line);
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) IfIntermediate(org.candle.decompiler.intermediate.code.conditional.IfIntermediate) ElseIfIntermediate(org.candle.decompiler.intermediate.code.conditional.ElseIfIntermediate) BooleanBranchIntermediate(org.candle.decompiler.intermediate.code.BooleanBranchIntermediate) ElseIfIntermediate(org.candle.decompiler.intermediate.code.conditional.ElseIfIntermediate)

Aggregations

AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)2 BooleanBranchIntermediate (org.candle.decompiler.intermediate.code.BooleanBranchIntermediate)2 ElseIfIntermediate (org.candle.decompiler.intermediate.code.conditional.ElseIfIntermediate)2 IfIntermediate (org.candle.decompiler.intermediate.code.conditional.IfIntermediate)2 TreeSet (java.util.TreeSet)1 GoToIntermediate (org.candle.decompiler.intermediate.code.GoToIntermediate)1 IntermediateComparator (org.candle.decompiler.intermediate.code.IntermediateComparator)1 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)1