Search in sources :

Example 1 with BooleanBranchIntermediate

use of org.candle.decompiler.intermediate.code.BooleanBranchIntermediate 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) GoToIntermediate(org.candle.decompiler.intermediate.code.GoToIntermediate) TreeSet(java.util.TreeSet) 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 BooleanBranchIntermediate

use of org.candle.decompiler.intermediate.code.BooleanBranchIntermediate 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 3 with BooleanBranchIntermediate

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

the class MethodIntermediateVisitor method processComparator.

protected void processComparator() {
    Expression left = context.getExpressions().pop();
    Expression right = context.getExpressions().pop();
    MultiConditional conditional = new MultiConditional(context.getCurrentInstruction(), left, right, OperationType.EQ);
    BooleanBranchIntermediate line = new BooleanBranchIntermediate(this.context.getCurrentInstruction(), conditional);
    context.pushIntermediateToInstruction(line);
}
Also used : TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) BooleanBranchIntermediate(org.candle.decompiler.intermediate.code.BooleanBranchIntermediate) MultiConditional(org.candle.decompiler.intermediate.expression.MultiConditional)

Example 4 with BooleanBranchIntermediate

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

the class MethodIntermediateVisitor method visitIFNULL.

@Override
public void visitIFNULL(IFNULL instruction) {
    Expression left = context.getExpressions().pop();
    Expression right = new NullLiteral(context.getCurrentInstruction());
    MultiConditional conditional = new MultiConditional(context.getCurrentInstruction(), left, right, OperationType.EQ);
    BooleanBranchIntermediate line = new BooleanBranchIntermediate(context.getCurrentInstruction(), conditional);
    context.pushIntermediateToInstruction(line);
}
Also used : TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) BooleanBranchIntermediate(org.candle.decompiler.intermediate.code.BooleanBranchIntermediate) NullLiteral(org.candle.decompiler.intermediate.expression.NullLiteral) MultiConditional(org.candle.decompiler.intermediate.expression.MultiConditional)

Example 5 with BooleanBranchIntermediate

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

the class MethodIntermediateVisitor method visitIFEQ.

@Override
public void visitIFEQ(IFEQ instruction) {
    Expression left = context.getExpressions().pop();
    SingleConditional conditional = new SingleConditional(context.getCurrentInstruction(), left);
    BooleanBranchIntermediate line = new BooleanBranchIntermediate(context.getCurrentInstruction(), conditional);
    context.pushIntermediateToInstruction(line);
}
Also used : TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) SingleConditional(org.candle.decompiler.intermediate.expression.SingleConditional) BooleanBranchIntermediate(org.candle.decompiler.intermediate.code.BooleanBranchIntermediate)

Aggregations

BooleanBranchIntermediate (org.candle.decompiler.intermediate.code.BooleanBranchIntermediate)9 Expression (org.candle.decompiler.intermediate.expression.Expression)5 TypedExpression (org.candle.decompiler.intermediate.expression.TypedExpression)5 MultiConditional (org.candle.decompiler.intermediate.expression.MultiConditional)4 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)3 GoToIntermediate (org.candle.decompiler.intermediate.code.GoToIntermediate)2 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)2 ElseIfIntermediate (org.candle.decompiler.intermediate.code.conditional.ElseIfIntermediate)2 IfIntermediate (org.candle.decompiler.intermediate.code.conditional.IfIntermediate)2 NullLiteral (org.candle.decompiler.intermediate.expression.NullLiteral)2 SingleConditional (org.candle.decompiler.intermediate.expression.SingleConditional)2 TreeSet (java.util.TreeSet)1 InstructionHandle (org.apache.bcel.generic.InstructionHandle)1 IntermediateComparator (org.candle.decompiler.intermediate.code.IntermediateComparator)1