Search in sources :

Example 36 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 37 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 38 with AbstractIntermediate

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

the class WhileToForLoopIncrement method visitWhileIntermediate.

@Override
public void visitWhileIntermediate(WhileIntermediate line) {
    List<AbstractIntermediate> predecessors = Graphs.predecessorListOf(igc.getGraph(), line);
    // look at the predecessor lines;  validate there are only 2 predecessors.
    if (predecessors.size() == 2) {
        StatementIntermediate declaration = null;
        StatementIntermediate iteration = null;
        for (AbstractIntermediate predecessor : predecessors) {
            if (comparator.before(predecessor, line)) {
                if (predecessor instanceof StatementIntermediate) {
                    declaration = (StatementIntermediate) predecessor;
                    continue;
                } else {
                    // a for loop.
                    return;
                }
            }
            // if the
            if (comparator.after(predecessor, line)) {
                if (predecessor instanceof StatementIntermediate) {
                    iteration = (StatementIntermediate) predecessor;
                } else {
                    return;
                }
            }
        }
        // at this point, both should be set.
        if (declaration != null && iteration != null) {
            if (declaration.getExpression() instanceof Declaration) {
                Declaration declarationExpression = (Declaration) declaration.getExpression();
                if (iteration.getExpression() instanceof Increment) {
                    Increment incrementExpression = (Increment) iteration.getExpression();
                    if (incrementExpression.getVariable().getType().equals(declarationExpression.getVariable().getType())) {
                        // now check names.
                        if (incrementExpression.getVariable().getName().equals(declarationExpression.getVariable().getName())) {
                            // we can actually convert this to a for loop.
                            ForIntermediate forIntermediate = new ForIntermediate(line, declarationExpression, incrementExpression);
                            // forIntermediate.setTrueBranch(line.getTrueBranch());
                            // forIntermediate.setFalseBranch(line.getFalseBranch());
                            igc.getGraph().addVertex(forIntermediate);
                            igc.redirectSuccessors(line, forIntermediate);
                            igc.redirectPredecessors(iteration, forIntermediate);
                            igc.redirectPredecessors(declaration, forIntermediate);
                            // remove the while loop, increment, and declaration.
                            igc.getGraph().removeVertex(line);
                            igc.getGraph().removeVertex(declaration);
                            igc.getGraph().removeVertex(iteration);
                        }
                    }
                }
            }
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) Increment(org.candle.decompiler.intermediate.expression.Increment) Declaration(org.candle.decompiler.intermediate.expression.Declaration) ForIntermediate(org.candle.decompiler.intermediate.code.loop.ForIntermediate)

Example 39 with AbstractIntermediate

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

the class ConstantArrayCompressor method visitStatementIntermediate.

@Override
public void visitStatementIntermediate(StatementIntermediate line) {
    // first, look for the
    Assignment assignment = extractConstantArrayAssignment(line.getExpression());
    if (assignment == null) {
        return;
    }
    // at this point, we know both the statement is an assignment, and that the left assignment is to a constant array value.
    // find the one that is right before the array assignment.
    Declaration declaration = extractNextDeclaration(line);
    // if we didn't find the declaration, this must not be the constant array assignment proceeding the declaration.
    if (declaration == null) {
        return;
    }
    // check the right hand of the declaration...
    if (!(declaration.getAssignment().getRightHandSide() instanceof NewConstantArrayInstance)) {
        return;
    }
    NewConstantArrayInstance ncai = (NewConstantArrayInstance) declaration.getAssignment().getRightHandSide();
    Expression countExpression = ncai.getCount();
    AbstractIntermediate current = line;
    Map<Integer, Expression> values = new HashMap<Integer, Expression>();
    collectConstantAssignments(current, values);
    // create a new array...
    Integer count = toInteger(countExpression);
    List<Expression> expressions = new ArrayList<Expression>(count);
    for (int i = 0, j = count; i < j; i++) {
        Expression exp = null;
        if (values.containsKey(i)) {
            exp = values.get(i);
        } else {
            exp = new Resolved(ncai.getInstructionHandle(), Type.NULL, "null");
        }
        expressions.add(i, exp);
    }
    // ok, we have the stack... now we need to just create a new expression.
    // create the contant...
    ConstantArray constantArray = new ConstantArray(declaration.getAssignment().getRightHandSide().getInstructionHandle(), expressions);
    declaration.getAssignment().setRightHandSide(constantArray);
    // excellent.  we have reordered the statements into the appropriate ContantArray assignment.  Now, we need to remove the dead nodes and heal the graph.
    AbstractIntermediate next = Graphs.successorListOf(igc.getGraph(), line).get(0);
    // loop through the dead elements...
    // we know the number of dead items is equal to the number of values we found.
    healGraph(line, next, values.size());
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) ConstantArray(org.candle.decompiler.intermediate.expression.ConstantArray) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NewConstantArrayInstance(org.candle.decompiler.intermediate.expression.NewConstantArrayInstance) Assignment(org.candle.decompiler.intermediate.expression.Assignment) Expression(org.candle.decompiler.intermediate.expression.Expression) Resolved(org.candle.decompiler.intermediate.expression.Resolved) Declaration(org.candle.decompiler.intermediate.expression.Declaration)

Example 40 with AbstractIntermediate

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

the class ConstantArrayCompressor method collectConstantAssignments.

public void collectConstantAssignments(AbstractIntermediate current, Map<Integer, Expression> assignments) {
    StatementIntermediate si = (StatementIntermediate) current;
    // get the assignment...
    Assignment assignment = extractConstantArrayAssignment(si.getExpression());
    if (assignment == null) {
        return;
    }
    Expression right = assignment.getRightHandSide();
    ArrayAccess apr = (ArrayAccess) assignment.getLeftHandSide();
    assignments.put(toInteger(apr.getIndex()), right);
    List<AbstractIntermediate> predecessor = Graphs.predecessorListOf(igc.getGraph(), current);
    if (predecessor.size() != 1) {
        return;
    }
    if (!(predecessor.get(0) instanceof StatementIntermediate)) {
        return;
    }
    for (AbstractIntermediate a : predecessor) {
        collectConstantAssignments(a, assignments);
    }
}
Also used : Assignment(org.candle.decompiler.intermediate.expression.Assignment) AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) ArrayAccess(org.candle.decompiler.intermediate.expression.ArrayAccess) Expression(org.candle.decompiler.intermediate.expression.Expression) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate)

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