Search in sources :

Example 1 with Declaration

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

the class ConstantArrayCompressor method extractNextDeclaration.

public Declaration extractNextDeclaration(StatementIntermediate statement) {
    List<AbstractIntermediate> successors = Graphs.successorListOf(igc.getGraph(), statement);
    if (successors.size() != 1) {
        return null;
    }
    if (!(successors.get(0) instanceof StatementIntermediate)) {
        return null;
    }
    StatementIntermediate si = (StatementIntermediate) successors.get(0);
    if (si.getExpression() instanceof Declaration) {
        return (Declaration) si.getExpression();
    }
    return null;
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) Declaration(org.candle.decompiler.intermediate.expression.Declaration)

Example 2 with Declaration

use of org.candle.decompiler.intermediate.expression.Declaration 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;
}
Also used : ArrayAccess(org.candle.decompiler.intermediate.expression.ArrayAccess) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable) Variable(org.candle.decompiler.intermediate.expression.Variable) Expression(org.candle.decompiler.intermediate.expression.Expression) Declaration(org.candle.decompiler.intermediate.expression.Declaration)

Example 3 with Declaration

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

the class IntermediateTryCatch method generateCatch.

private void generateCatch(TryIntermediate tryIntermediate, CodeExceptionGen ceg) {
    LOG.debug("CEG: " + ceg);
    // convert the node to catch blocks...
    AbstractIntermediate catchDeclaration = igc.getOrderedIntermediate().ceiling(new NullIntermediate(ceg.getHandlerPC()));
    LOG.debug("Catch Declaration:" + catchDeclaration);
    if (catchDeclaration instanceof StatementIntermediate) {
        StatementIntermediate declarationStatement = (StatementIntermediate) catchDeclaration;
        if (declarationStatement.getExpression() instanceof Declaration) {
            Declaration declaration = (Declaration) declarationStatement.getExpression();
            // now, we can convert this into a catch block.
            CatchIntermediate catchIntermediate = new CatchIntermediate(declarationStatement.getInstruction(), ceg, declaration.getVariable());
            igc.getGraph().addVertex(catchIntermediate);
            // redirect statement to catch.
            igc.redirectPredecessors(declarationStatement, catchIntermediate);
            igc.redirectSuccessors(declarationStatement, catchIntermediate);
            // now, we just need to remove the statement.
            igc.getGraph().removeVertex(declarationStatement);
            // populate the bounds..
            // add the link between try and catch.
            igc.getGraph().addEdge(tryIntermediate, catchIntermediate);
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) CatchIntermediate(org.candle.decompiler.intermediate.code.CatchIntermediate) Declaration(org.candle.decompiler.intermediate.expression.Declaration) NullIntermediate(org.candle.decompiler.intermediate.graph.context.NullIntermediate)

Example 4 with Declaration

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

the class MethodIntermediateVisitor method visitStoreInstruction.

public void visitStoreInstruction(StoreInstruction instruction) {
    Expression right = this.context.getExpressions().pop();
    Type type = instruction.getType(context.getMethodGen().getConstantPool());
    // first, check to see whether the variable currently has been declared.
    // this would be the case if we don't get null when looking up the local variable.
    int pc = context.getCurrentInstruction().getPosition();
    int lvtIndex = instruction.getIndex();
    IntermediateVariable iv = context.getVariableResolver().getLocalVariable(lvtIndex, pc);
    // if the variable is not null, it is declared.
    boolean declared = (iv != null);
    Variable variable = null;
    if (!declared) {
        // look it up from the next phase code.
        pc = this.context.getCurrentInstruction().getNext().getPosition();
        iv = context.getVariableResolver().getLocalVariable(lvtIndex, pc);
        if (iv == null) {
            // probably need to create a variable for enhanced loops...
            LOG.debug("Adding index: " + instruction.getIndex() + " as: " + type);
            // try and resolve the type for the variable from the right hand side.
            if (type == Type.OBJECT) {
                if (right instanceof TypedExpression) {
                    type = ((TypedExpression) right).getType();
                }
            }
            // generate variable name...
            iv = context.getVariableResolver().addLocalVariable(instruction.getIndex(), context.getCurrentInstruction(), type);
            variable = new GeneratedVariable(context.getCurrentInstruction(), iv.getType(), iv.getName());
        }
    }
    // create the variable.
    if (variable == null) {
        variable = new Variable(context.getCurrentInstruction(), iv.getType(), iv.getName());
    }
    // create the assignment.
    Assignment assignment = new Assignment(context.getCurrentInstruction(), variable, right);
    Expression left = null;
    if (declared) {
        left = assignment;
    } else {
        // if it is currently not declared... create the declaration.
        left = new Declaration(context.getCurrentInstruction(), variable, assignment);
    }
    StatementIntermediate cl = new StatementIntermediate(context.getCurrentInstruction(), left);
    context.pushIntermediateToInstruction(cl);
    LOG.debug("Stored.");
}
Also used : Assignment(org.candle.decompiler.intermediate.expression.Assignment) OperationType(org.candle.decompiler.intermediate.expression.OperationType) ArithmeticType(org.candle.decompiler.intermediate.expression.ArithmeticType) 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) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) Declaration(org.candle.decompiler.intermediate.expression.Declaration) TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression)

Example 5 with Declaration

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

the class WhileToForLoopIterator method visitWhileIntermediate.

@Override
public void visitWhileIntermediate(WhileIntermediate line) {
    if (line.getExpression() instanceof SingleConditional) {
        if (((SingleConditional) line.getExpression()).getExpression() instanceof MethodInvocation) {
            MethodInvocation mi = (MethodInvocation) ((SingleConditional) line.getExpression()).getExpression();
            String iteratorName = null;
            if (mi.getTarget() instanceof Variable) {
                iteratorName = ((Variable) mi.getTarget()).getName();
            } else {
                return;
            }
            if (StringUtils.equals("hasNext", mi.getMethodName())) {
                // probably an iterator.
                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;
                    for (AbstractIntermediate predecessor : predecessors) {
                        if (comparator.before(predecessor, line)) {
                            if (predecessor instanceof StatementIntermediate) {
                                declaration = (StatementIntermediate) predecessor;
                                break;
                            }
                        }
                    }
                    // check to see if the declaration is a temporary variable..
                    if (declaration == null) {
                        return;
                    }
                    // otherwise, let's see if the declaration is an iterator.
                    if (declaration.getExpression() instanceof Declaration) {
                        Declaration declarationExpression = (Declaration) declaration.getExpression();
                        Variable v = (Variable) declarationExpression.getAssignment().getLeftHandSide();
                        // check to see if the declaration is the same as the iterator's name.
                        if (StringUtils.equals(iteratorName, v.getName())) {
                            LOG.debug("Identified Likely Iterator: " + v.getName());
                            // get the ".next()" statement, which should be the first child.
                            AbstractIntermediate firstChild = igc.getTrueTarget(line);
                            // then check the right side to see if it is an invocation.. and the invocation has the method name "next"...
                            if (firstChild instanceof StatementIntermediate) {
                                StatementIntermediate nextStatement = (StatementIntermediate) firstChild;
                                if (nextStatement.getExpression() instanceof Declaration) {
                                    // the statement is indeed a declaration.
                                    Declaration nextDeclaration = (Declaration) nextStatement.getExpression();
                                    if (nextDeclaration.getAssignment().getRightHandSide() instanceof MethodInvocation) {
                                        MethodInvocation nextMethodInvocation = (MethodInvocation) nextDeclaration.getAssignment().getRightHandSide();
                                        if (StringUtils.equals("next", nextMethodInvocation.getMethodName())) {
                                            // check to see if the next method is on the candidate iterator.
                                            if (nextMethodInvocation.getTarget() instanceof Variable) {
                                                Variable nextMethodTarget = (Variable) nextMethodInvocation.getTarget();
                                                if (StringUtils.equals(iteratorName, nextMethodTarget.getName())) {
                                                    LOG.info("Definitely an enhanced for loop.");
                                                    if (declarationExpression.getAssignment().getRightHandSide() instanceof MethodInvocation) {
                                                        MethodInvocation iteratorInvocation = (MethodInvocation) declarationExpression.getAssignment().getRightHandSide();
                                                        if (StringUtils.equals("iterator", iteratorInvocation.getMethodName())) {
                                                            // now, we are pretty certain this is an enhanced for loop...  we can chop up the graph.
                                                            EnhancedForIntermediate enhancedFor = new EnhancedForIntermediate(line, nextDeclaration.getVariable(), iteratorInvocation.getTarget());
                                                            igc.getGraph().addVertex(enhancedFor);
                                                            igc.redirectSuccessors(line, enhancedFor);
                                                            igc.redirectPredecessors(line, enhancedFor);
                                                            igc.getGraph().removeVertex(line);
                                                            igc.redirectPredecessors(declaration, igc.getSingleSuccessor(declaration));
                                                            igc.getGraph().removeVertex(declaration);
                                                            igc.redirectPredecessors(firstChild, igc.getSingleSuccessor(firstChild));
                                                            igc.getGraph().removeVertex(firstChild);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) Variable(org.candle.decompiler.intermediate.expression.Variable) EnhancedForIntermediate(org.candle.decompiler.intermediate.code.loop.EnhancedForIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) SingleConditional(org.candle.decompiler.intermediate.expression.SingleConditional) MethodInvocation(org.candle.decompiler.intermediate.expression.MethodInvocation) Declaration(org.candle.decompiler.intermediate.expression.Declaration)

Aggregations

Declaration (org.candle.decompiler.intermediate.expression.Declaration)10 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)7 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)6 GeneratedVariable (org.candle.decompiler.intermediate.expression.GeneratedVariable)4 Variable (org.candle.decompiler.intermediate.expression.Variable)4 Expression (org.candle.decompiler.intermediate.expression.Expression)3 EnhancedForIntermediate (org.candle.decompiler.intermediate.code.loop.EnhancedForIntermediate)2 Assignment (org.candle.decompiler.intermediate.expression.Assignment)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ArrayType (org.apache.bcel.generic.ArrayType)1 IntermediateVariable (org.candle.decompiler.intermediate.IntermediateVariable)1 CatchIntermediate (org.candle.decompiler.intermediate.code.CatchIntermediate)1 ForIntermediate (org.candle.decompiler.intermediate.code.loop.ForIntermediate)1 ArithmeticType (org.candle.decompiler.intermediate.expression.ArithmeticType)1 ArrayAccess (org.candle.decompiler.intermediate.expression.ArrayAccess)1 ConstantArray (org.candle.decompiler.intermediate.expression.ConstantArray)1 Increment (org.candle.decompiler.intermediate.expression.Increment)1 MethodInvocation (org.candle.decompiler.intermediate.expression.MethodInvocation)1 NewConstantArrayInstance (org.candle.decompiler.intermediate.expression.NewConstantArrayInstance)1