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));
}
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);
}
}
}
}
}
}
}
Aggregations