Search in sources :

Example 41 with Expression

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

the class MethodIntermediateVisitor method visitINVOKEINTERFACE.

public void visitINVOKEINTERFACE(INVOKEINTERFACE instruction) {
    ConstantPoolGen cpg = context.getMethodGen().getConstantPool();
    // collect all parameters from the stack.
    Type[] types = instruction.getArgumentTypes(cpg);
    final List<Expression> parameters = new ArrayList<Expression>(types.length);
    for (int i = 0, j = types.length; i < j; i++) {
        Expression param = context.getExpressions().pop();
        LOG.debug("Parameter: " + param);
        parameters.add(param);
    }
    // collect the method name we are calling.
    String methodName = instruction.getMethodName(context.getMethodGen().getConstantPool());
    Expression left = context.getExpressions().pop();
    // create the expression..
    MethodInvocation methodInvocation = new MethodInvocation(context.getCurrentInstruction(), left, methodName, parameters);
    Type returned = instruction.getReturnType(context.getMethodGen().getConstantPool());
    if (returned == BasicType.VOID) {
        StatementIntermediate completeLine = new StatementIntermediate(context.getCurrentInstruction(), methodInvocation);
        context.pushIntermediateToInstruction(completeLine);
        LOG.debug("Pushed complete line: " + completeLine.toString());
    } else {
        context.getExpressions().push(methodInvocation);
        LOG.debug("Pushed expression: " + methodInvocation);
    }
}
Also used : OperationType(org.candle.decompiler.intermediate.expression.OperationType) ArithmeticType(org.candle.decompiler.intermediate.expression.ArithmeticType) TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) ArrayList(java.util.ArrayList) MethodInvocation(org.candle.decompiler.intermediate.expression.MethodInvocation)

Example 42 with Expression

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

the class MethodIntermediateVisitor method visitARRAYLENGTH.

// array length instruction
public void visitARRAYLENGTH(ARRAYLENGTH instruction) {
    Expression target = context.getExpressions().pop();
    ArrayLength arrayLength = new ArrayLength(context.getCurrentInstruction(), target);
    context.getExpressions().push(arrayLength);
}
Also used : TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) ArrayLength(org.candle.decompiler.intermediate.expression.ArrayLength)

Example 43 with Expression

use of org.candle.decompiler.intermediate.expression.Expression 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 44 with Expression

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

the class MethodIntermediateVisitor method visitMULTIANEWARRAY.

/**
 * Decompiles "new multi-dimentional array" operations.
 */
public void visitMULTIANEWARRAY(MULTIANEWARRAY instruction) {
    Type type = instruction.getType(context.getMethodGen().getConstantPool());
    LinkedList<Expression> counts = new LinkedList<Expression>();
    int provided = instruction.getDimensions();
    for (int i = 0; i < provided; i++) {
        counts.addFirst(context.getExpressions().pop());
    }
    ArrayCreation nai = new ArrayCreation(context.getCurrentInstruction(), type, counts);
    context.getExpressions().push(nai);
}
Also used : OperationType(org.candle.decompiler.intermediate.expression.OperationType) ArithmeticType(org.candle.decompiler.intermediate.expression.ArithmeticType) TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) ArrayCreation(org.candle.decompiler.intermediate.expression.ArrayCreation) LinkedList(java.util.LinkedList)

Example 45 with Expression

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

Aggregations

Expression (org.candle.decompiler.intermediate.expression.Expression)52 TypedExpression (org.candle.decompiler.intermediate.expression.TypedExpression)46 Resolved (org.candle.decompiler.intermediate.expression.Resolved)11 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)10 ArithmeticType (org.candle.decompiler.intermediate.expression.ArithmeticType)10 OperationType (org.candle.decompiler.intermediate.expression.OperationType)10 ArrayList (java.util.ArrayList)6 Assignment (org.candle.decompiler.intermediate.expression.Assignment)6 BooleanBranchIntermediate (org.candle.decompiler.intermediate.code.BooleanBranchIntermediate)5 Return (org.candle.decompiler.intermediate.expression.Return)5 ArrayAccess (org.candle.decompiler.intermediate.expression.ArrayAccess)4 GeneratedVariable (org.candle.decompiler.intermediate.expression.GeneratedVariable)4 MethodInvocation (org.candle.decompiler.intermediate.expression.MethodInvocation)4 MultiConditional (org.candle.decompiler.intermediate.expression.MultiConditional)4 Variable (org.candle.decompiler.intermediate.expression.Variable)4 IntermediateVariable (org.candle.decompiler.intermediate.IntermediateVariable)3 ArrayCreation (org.candle.decompiler.intermediate.expression.ArrayCreation)3 Declaration (org.candle.decompiler.intermediate.expression.Declaration)3 NewConstantArrayInstance (org.candle.decompiler.intermediate.expression.NewConstantArrayInstance)3 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)2