Search in sources :

Example 6 with GeneratedVariable

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);
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) ArrayType(org.apache.bcel.generic.ArrayType) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable) EnhancedForIntermediate(org.candle.decompiler.intermediate.code.loop.EnhancedForIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) Declaration(org.candle.decompiler.intermediate.expression.Declaration)

Aggregations

GeneratedVariable (org.candle.decompiler.intermediate.expression.GeneratedVariable)6 Variable (org.candle.decompiler.intermediate.expression.Variable)5 Declaration (org.candle.decompiler.intermediate.expression.Declaration)4 IntermediateVariable (org.candle.decompiler.intermediate.IntermediateVariable)3 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)3 Expression (org.candle.decompiler.intermediate.expression.Expression)3 ArithmeticType (org.candle.decompiler.intermediate.expression.ArithmeticType)2 OperationType (org.candle.decompiler.intermediate.expression.OperationType)2 TypedExpression (org.candle.decompiler.intermediate.expression.TypedExpression)2 ArrayType (org.apache.bcel.generic.ArrayType)1 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)1 EnhancedForIntermediate (org.candle.decompiler.intermediate.code.loop.EnhancedForIntermediate)1 ArrayAccess (org.candle.decompiler.intermediate.expression.ArrayAccess)1 Assignment (org.candle.decompiler.intermediate.expression.Assignment)1 Increment (org.candle.decompiler.intermediate.expression.Increment)1