use of org.candle.decompiler.intermediate.expression.GeneratedVariable in project candle-decompiler by bradsdavis.
the class ArrayForToEnhancedFor method firstMatchesGeneratedVariables.
private boolean firstMatchesGeneratedVariables(StatementIntermediate first, GeneratedVariable generatedArrayRef, GeneratedVariable generatedArrayIterator) {
Declaration childDeclaration = (Declaration) first.getExpression();
Expression right = childDeclaration.getAssignment().getRightHandSide();
if (right instanceof ArrayAccess) {
ArrayAccess apr = (ArrayAccess) right;
if (!(apr.getIndex() instanceof Variable)) {
return false;
}
if (!(apr.getArray() instanceof Variable)) {
return false;
}
//cast both to variable. check the variables match the name and type of the ones found above.
Variable arrayPosition = (Variable) apr.getArray();
Variable arrayRef = (Variable) apr.getArray();
if (!StringUtils.equals(arrayPosition.getName(), generatedArrayIterator.getName())) {
return false;
}
if (!StringUtils.equals(arrayRef.getName(), generatedArrayRef.getName())) {
return false;
}
return true;
}
return true;
}
use of org.candle.decompiler.intermediate.expression.GeneratedVariable in project candle-decompiler by bradsdavis.
the class MethodIntermediateVisitor method visitLoadInstruction.
public void visitLoadInstruction(LoadInstruction instruction) {
int index = instruction.getIndex();
if (index >= 0) {
IntermediateVariable localVar = context.getVariableResolver().getLocalVariable(index, context.getCurrentInstruction().getPosition());
if (localVar == null) {
LOG.debug("Did not find local variable: " + index + " for position: " + context.getCurrentInstruction().getPosition());
}
Variable variable = null;
if (localVar == null) {
//probably need to create a variable for enhanced loops...
Type type = instruction.getType(context.getMethodGen().getConstantPool());
localVar = context.getVariableResolver().addLocalVariable(index, context.getCurrentInstruction(), type);
variable = new GeneratedVariable(context.getCurrentInstruction(), localVar.getType(), localVar.getName());
} else {
variable = new Variable(context.getCurrentInstruction(), localVar.getType(), localVar.getName());
}
//Variable variable = new Variable(context.getCurrentInstruction(), localVar.getType(), localVar.getName());
context.getExpressions().push(variable);
LOG.debug("Loaded: " + variable);
} else {
LOG.warn("Did not load because index: " + index);
}
}
use of org.candle.decompiler.intermediate.expression.GeneratedVariable 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.GeneratedVariable in project candle-decompiler by bradsdavis.
the class ArrayForToEnhancedFor method visitForIntermediate.
@Override
public void visitForIntermediate(ForIntermediate line) {
//we need to look at the previous 2 statements and the first statement child.
AbstractIntermediate arrayLenthCandidate = getForExteriorPredecessor(line);
//check that the array length candidate's declared length variable is used in the for's condition.
AbstractIntermediate tempArrayCandidate = null;
AbstractIntermediate firstChild = igc.getTrueTarget(line);
if (arrayLenthCandidate != null) {
tempArrayCandidate = getSinglePredecessor(arrayLenthCandidate);
}
//if either of these are null, then this doesn't match.
if (arrayLenthCandidate == null || tempArrayCandidate == null) {
return;
}
GeneratedVariable generatedArrayLength = extractGeneratedVariableDeclaration(arrayLenthCandidate);
GeneratedVariable generatedArrayReference = extractGeneratedVariableDeclaration(tempArrayCandidate);
GeneratedVariable arrayIteratorValue = extractGeneratedVariableDeclaration(line.getInit());
if (generatedArrayLength == null || generatedArrayReference == null || arrayIteratorValue == null) {
return;
}
if (generatedArrayLength.getType() != Type.INT) {
if (!(generatedArrayReference.getType() instanceof ArrayType)) {
return;
}
if (arrayIteratorValue.getType() != Type.INT) {
return;
}
}
//great; at this point we know the pattern matches. check the next statement to see if the transformation is possible.
//format should be: 40 : GENERATED_ARRAY_REFERENCE_TYPE i = GENERATED_ARRAY_REFERENCE[ARRAY_ITERATOR_VALUE] |
StatementIntermediate childDeclarationStatement = ((StatementIntermediate) firstChild);
Declaration childDeclaration = (Declaration) childDeclarationStatement.getExpression();
if (firstMatchesGeneratedVariables(childDeclarationStatement, generatedArrayReference, arrayIteratorValue)) {
LOG.debug("Likely a enhanced for loop for array: " + generatedArrayLength + " , " + generatedArrayReference);
//we are good to go here. Now we just need to reorganize the graph. Start by creating the new enhanced for loop.
EnhancedForIntermediate efl = new EnhancedForIntermediate(line.getInstruction(), line.getConditionalIntermediate(), childDeclaration.getVariable(), extractExpressionFromGeneratedArrayAssignment(tempArrayCandidate));
//efl.setTrueBranch(line.getTrueBranch());
//efl.setFalseBranch(line.getFalseBranch());
//add the new node...
this.igc.getGraph().addVertex(efl);
//now, we just need to redirect.
igc.redirectPredecessors(tempArrayCandidate, efl);
igc.redirectPredecessors(line, efl);
igc.redirectSuccessors(line, efl);
AbstractIntermediate nextChild = getSingleSuccessor(firstChild);
igc.redirectPredecessors(firstChild, nextChild);
//remove line.
igc.getGraph().removeVertex(line);
igc.getGraph().removeVertex(tempArrayCandidate);
igc.getGraph().removeVertex(firstChild);
igc.getGraph().removeVertex(arrayLenthCandidate);
}
}
use of org.candle.decompiler.intermediate.expression.GeneratedVariable in project candle-decompiler by bradsdavis.
the class ArrayForToEnhancedFor method extractGeneratedVariableDeclaration.
private GeneratedVariable extractGeneratedVariableDeclaration(Expression expression) {
if (expression instanceof Declaration) {
Declaration dec = (Declaration) expression;
Variable var = dec.getVariable();
if (var instanceof GeneratedVariable) {
return (GeneratedVariable) var;
}
}
return null;
}
Aggregations