Search in sources :

Example 1 with Increment

use of org.candle.decompiler.intermediate.expression.Increment in project candle-decompiler by bradsdavis.

the class MethodIntermediateVisitor method visitIINC.

public void visitIINC(IINC instruction) {
    // increment variable.
    int index = instruction.getIndex();
    IntermediateVariable iv = context.getVariableResolver().getLocalVariable(index, context.getCurrentInstruction().getPosition());
    Variable variable = null;
    if (iv == null) {
        // generate IV.
        iv = context.getVariableResolver().addLocalVariable(index, context.getCurrentInstruction(), instruction.getType(context.getMethodGen().getConstantPool()));
        variable = new GeneratedVariable(context.getCurrentInstruction(), iv.getType(), iv.getName());
    } else {
        variable = new Variable(context.getCurrentInstruction(), iv.getType(), iv.getName());
    }
    // now, how much does it increment by?
    int incrementBy = instruction.getIncrement();
    StringBuilder incrementerBuilder = new StringBuilder(iv.getName());
    if (incrementBy == 1) {
        incrementerBuilder.append("++");
    } else if (incrementBy == -1) {
        incrementerBuilder.append("--");
    } else if (incrementBy < 1) {
        incrementerBuilder.append(" -= ").append((-1 * incrementBy));
    } else {
        incrementerBuilder.append(" += ").append(incrementBy);
    }
    Expression exp = new Increment(context.getCurrentInstruction(), variable, Type.INT, incrementerBuilder.toString());
    context.pushIntermediateToInstruction(new StatementIntermediate(context.getCurrentInstruction(), exp));
}
Also used : IntermediateVariable(org.candle.decompiler.intermediate.IntermediateVariable) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable) Variable(org.candle.decompiler.intermediate.expression.Variable) IntermediateVariable(org.candle.decompiler.intermediate.IntermediateVariable) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable) TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) Increment(org.candle.decompiler.intermediate.expression.Increment) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate)

Example 2 with Increment

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

Aggregations

StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)2 Increment (org.candle.decompiler.intermediate.expression.Increment)2 IntermediateVariable (org.candle.decompiler.intermediate.IntermediateVariable)1 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)1 ForIntermediate (org.candle.decompiler.intermediate.code.loop.ForIntermediate)1 Declaration (org.candle.decompiler.intermediate.expression.Declaration)1 Expression (org.candle.decompiler.intermediate.expression.Expression)1 GeneratedVariable (org.candle.decompiler.intermediate.expression.GeneratedVariable)1 TypedExpression (org.candle.decompiler.intermediate.expression.TypedExpression)1 Variable (org.candle.decompiler.intermediate.expression.Variable)1