Search in sources :

Example 11 with IntInsnNode

use of org.objectweb.asm.tree.IntInsnNode in project enumerable by hraberg.

the class ExpressionInterpreter method unaryOperation.

public Value unaryOperation(final AbstractInsnNode insn, final Value value) throws AnalyzerException {
    ExpressionValue expressionValue = (ExpressionValue) value;
    switch(insn.getOpcode()) {
        case INEG:
            return new ExpressionValue(PRIMITIVE_INT, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
        case IINC:
            IincInsnNode node = (IincInsnNode) insn;
            NameExpr nameExpr = new NameExpr(getLocalVariable(node.var).name);
            if (node.incr == 1)
                iinc = new UnaryExpr(nameExpr, UnaryExpr.Operator.posIncrement);
            if (node.incr == -1)
                iinc = new UnaryExpr(nameExpr, UnaryExpr.Operator.posDecrement);
            if (node.incr > 1)
                iincAssign = new AssignExpr(nameExpr, new IntegerLiteralExpr(node.incr + ""), AssignExpr.Operator.plus);
            if (node.incr < -1)
                iincAssign = new AssignExpr(nameExpr, new IntegerLiteralExpr(-node.incr + ""), AssignExpr.Operator.minus);
            return value;
        case L2I:
        case F2I:
        case D2I:
            return new ExpressionValue(PRIMITIVE_INT, new CastExpr(PRIMITIVE_INT, expressionValue.expression));
        case I2B:
            return new ExpressionValue(PRIMITIVE_BYTE, new CastExpr(PRIMITIVE_BYTE, expressionValue.expression));
        case I2C:
            return new ExpressionValue(PRIMITIVE_CHAR, new CastExpr(PRIMITIVE_CHAR, expressionValue.expression));
        case I2S:
            return new ExpressionValue(PRIMITIVE_SHORT, new CastExpr(PRIMITIVE_SHORT, expressionValue.expression));
        case FNEG:
            return new ExpressionValue(PRIMITIVE_FLOAT, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
        case I2F:
        case L2F:
        case D2F:
            return new ExpressionValue(PRIMITIVE_FLOAT, new CastExpr(PRIMITIVE_FLOAT, expressionValue.expression));
        case LNEG:
            return new ExpressionValue(PRIMITIVE_LONG, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
        case I2L:
        case F2L:
        case D2L:
            return new ExpressionValue(PRIMITIVE_LONG, new CastExpr(PRIMITIVE_LONG, expressionValue.expression));
        case DNEG:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
        case I2D:
        case L2D:
        case F2D:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new CastExpr(PRIMITIVE_DOUBLE, expressionValue.expression));
        case IFEQ:
            if (conditional != null) {
                if (conditional.getCondition() instanceof BinaryExpr && cmpConditional) {
                    ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.notEquals);
                    cmpConditional = false;
                } else {
                    handleNestedConditional(expressionValue.expression);
                }
            } else {
                conditional = new ConditionalExpr(expressionValue.expression, null, null);
            }
            return null;
        case IFNE:
            if (conditional != null) {
                if (conditional.getCondition() instanceof BinaryExpr && cmpConditional) {
                    ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.equals);
                    cmpConditional = false;
                } else {
                    handleNestedConditional(new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.not));
                }
            } else {
                conditional = new ConditionalExpr(new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.not), null, null);
            }
            return null;
        case IFGT:
            ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.lessEquals);
            cmpConditional = false;
            return null;
        case IFLE:
            ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.greater);
            cmpConditional = false;
            return null;
        case IFLT:
            ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.greaterEquals);
            cmpConditional = false;
            return null;
        case IFGE:
            ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.less);
            cmpConditional = false;
            return null;
        case TABLESWITCH:
        case LOOKUPSWITCH:
            throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
        case IRETURN:
        case LRETURN:
        case FRETURN:
        case DRETURN:
        case ARETURN:
            return null;
        case PUTSTATIC:
            FieldInsnNode fieldNode = (FieldInsnNode) insn;
            ExpressionValue putField = (ExpressionValue) newValue(getType(fieldNode.desc));
            putField.expression = new AssignExpr(new FieldAccessExpr(new NameExpr(removeJavaLang(getObjectType(fieldNode.owner).getClassName())), fieldNode.name), expressionValue.expression, AssignExpr.Operator.assign);
            assign = putField;
            return null;
        case GETFIELD:
            fieldNode = (FieldInsnNode) insn;
            ExpressionValue getField = (ExpressionValue) newValue(Type.getType(fieldNode.desc));
            getField.expression = new FieldAccessExpr(expressionValue.expression, fieldNode.name);
            return getField;
        case NEWARRAY:
            PrimitiveType type;
            switch(((IntInsnNode) insn).operand) {
                case T_BOOLEAN:
                    type = PRIMITIVE_BOOLEAN;
                    break;
                case T_CHAR:
                    type = PRIMITIVE_CHAR;
                    break;
                case T_BYTE:
                    type = PRIMITIVE_BYTE;
                    break;
                case T_SHORT:
                    type = PRIMITIVE_SHORT;
                    break;
                case T_INT:
                    type = PRIMITIVE_INT;
                    break;
                case T_FLOAT:
                    type = PRIMITIVE_FLOAT;
                    break;
                case T_DOUBLE:
                    type = PRIMITIVE_DOUBLE;
                    break;
                case T_LONG:
                    type = PRIMITIVE_LONG;
                    break;
                default:
                    throw new AnalyzerException(insn, "Invalid array type");
            }
            ArrayList<Expression> dimensions = new ArrayList<Expression>();
            dimensions.add(expressionValue.expression);
            return new ExpressionValue(new ReferenceType(type, 1), new ArrayCreationExpr(type, dimensions, 0));
        case ANEWARRAY:
            ExpressionValue newArray = (ExpressionValue) newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
            dimensions = new ArrayList<Expression>();
            dimensions.add(expressionValue.expression);
            newArray.expression = new ArrayCreationExpr(newArray.type, dimensions, 0);
            return newArray;
        case ARRAYLENGTH:
            return new ExpressionValue(PRIMITIVE_INT, new FieldAccessExpr(expressionValue.expression, "length"));
        case ATHROW:
            throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
        case CHECKCAST:
            ExpressionValue cast = (ExpressionValue) newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
            cast.expression = new CastExpr(new ReferenceType(cast.type), expressionValue.expression);
            return cast;
        case INSTANCEOF:
            ExpressionValue instanceOf = (ExpressionValue) newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
            instanceOf.expression = new InstanceOfExpr(expressionValue.expression, new ReferenceType(instanceOf.type));
            return instanceOf;
        case MONITORENTER:
        case MONITOREXIT:
            throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
        case IFNULL:
            handleNestedConditional(new BinaryExpr(expressionValue.expression, new NullLiteralExpr(), BinaryExpr.Operator.notEquals));
            return null;
        case IFNONNULL:
            handleNestedConditional(new BinaryExpr(expressionValue.expression, new NullLiteralExpr(), BinaryExpr.Operator.equals));
            return null;
        default:
            throw new Error("Internal error.");
    }
}
Also used : AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) ArrayList(java.util.ArrayList) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) ReferenceType(japa.parser.ast.type.ReferenceType) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) PrimitiveType(japa.parser.ast.type.PrimitiveType)

Example 12 with IntInsnNode

use of org.objectweb.asm.tree.IntInsnNode in project evosuite by EvoSuite.

the class MethodNodeTransformer method transform.

/**
 * <p>transform</p>
 *
 * @param mn a {@link org.objectweb.asm.tree.MethodNode} object.
 */
public void transform(MethodNode mn) {
    setupLocals(mn);
    Set<AbstractInsnNode> originalNodes = new HashSet<AbstractInsnNode>();
    AbstractInsnNode node = mn.instructions.getFirst();
    while (node != mn.instructions.getLast()) {
        originalNodes.add(node);
        node = node.getNext();
    }
    // int currentIndex = 0;
    node = mn.instructions.getFirst();
    // while (currentIndex < mn.instructions.size()) {
    boolean finished = false;
    while (!finished) {
        // } else
        if (node instanceof MethodInsnNode) {
            node = transformMethodInsnNode(mn, (MethodInsnNode) node);
        } else if (node instanceof VarInsnNode) {
            node = transformVarInsnNode(mn, (VarInsnNode) node);
        } else if (node instanceof FieldInsnNode) {
            node = transformFieldInsnNode(mn, (FieldInsnNode) node);
        } else if (node instanceof InsnNode) {
            node = transformInsnNode(mn, (InsnNode) node);
        } else if (node instanceof TypeInsnNode) {
            node = transformTypeInsnNode(mn, (TypeInsnNode) node);
        } else if (node instanceof JumpInsnNode) {
            node = transformJumpInsnNode(mn, (JumpInsnNode) node);
        } else if (node instanceof LabelNode) {
            node = transformLabelNode(mn, (LabelNode) node);
        } else if (node instanceof IntInsnNode) {
            node = transformIntInsnNode(mn, (IntInsnNode) node);
        } else if (node instanceof MultiANewArrayInsnNode) {
            node = transformMultiANewArrayInsnNode(mn, (MultiANewArrayInsnNode) node);
        }
        if (node == mn.instructions.getLast()) {
            finished = true;
        } else {
            node = node.getNext();
        }
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) HashSet(java.util.HashSet)

Example 13 with IntInsnNode

use of org.objectweb.asm.tree.IntInsnNode in project evosuite by EvoSuite.

the class Instrumenter method addCaptureCall.

private InsnList addCaptureCall(final boolean isStatic, final String internalClassName, final String methodName, final String methodDesc, final Type[] argTypes) {
    // construction of
    // Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
    // call
    final InsnList il = new InsnList();
    il.add(new LdcInsnNode(this.captureId));
    // --- load receiver argument
    int varIndex;
    if (isStatic) {
        // static method invocation
        il.add(new LdcInsnNode(internalClassName));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(CaptureUtil.class), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
        varIndex = 0;
    } else {
        // non-static method call
        il.add(new VarInsnNode(Opcodes.ALOAD, 0));
        varIndex = 1;
    }
    // --- load method name argument
    il.add(new LdcInsnNode(methodName));
    // --- load method description argument
    il.add(new LdcInsnNode(methodDesc));
    // --- load methodParams arguments
    // load methodParams length
    // TODO ICONST_1 to ICONST_5 would be more efficient
    il.add(new IntInsnNode(Opcodes.BIPUSH, argTypes.length));
    // create array object
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    for (int i = 0; i < argTypes.length; i++) {
        il.add(new InsnNode(Opcodes.DUP));
        // TODO ICONST_1 to ICONST_5 would be more efficient
        il.add(new IntInsnNode(Opcodes.BIPUSH, i));
        // check for primitives
        this.loadAndConvertToObject(il, argTypes[i], varIndex++);
        il.add(new InsnNode(Opcodes.AASTORE));
        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            varIndex++;
        }
    }
    // --- construct Capture.capture() call
    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.Capturer.class), "capture", "(ILjava/lang/Object;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V"));
    return il;
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 14 with IntInsnNode

use of org.objectweb.asm.tree.IntInsnNode in project cassandra by apache.

the class MonitorMethodTransformer method writeTryCatchMonitorEnterExit.

void writeTryCatchMonitorEnterExit() {
    access |= Opcodes.ACC_SYNTHETIC;
    name = baseName;
    Label start = new Label();
    Label inmonitor = new Label();
    Label normal = new Label();
    // normal
    Label except = new Label();
    // normal return failed
    Label normalRetExcept = new Label();
    // exceptional return success
    Label exceptRetNormal = new Label();
    // exceptional return failed
    Label exceptRetExcept = new Label();
    Label end = new Label();
    reset(start, end);
    // add a local variable slot to save any exceptions into (at maxLocalParams position)
    ++maxLocals;
    // must load self or class onto stack, and return value (if any)
    maxStack = Math.max(maxLocalParams, returnCode == Opcodes.RETURN ? 2 : 3);
    tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(inmonitor), getLabelNode(normal), getLabelNode(except), null));
    tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(normal), getLabelNode(normalRetExcept), getLabelNode(normalRetExcept), null));
    tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(except), getLabelNode(exceptRetNormal), getLabelNode(exceptRetExcept), null));
    // preMonitorEnter
    // monitorenter
    instructions.add(getLabelNode(start));
    invokePreMonitorEnter();
    invokeMonitor(Opcodes.MONITORENTER);
    {
        // try1 { val = original();
        instructions.add(getLabelNode(inmonitor));
        int invokeCode = loadParamsAndReturnInvokeCode();
        instructions.add(new MethodInsnNode(invokeCode, className, baseName + "$unsync", desc));
        {
            // try2 { preMonitorExit(); monitorexit; return val; }
            instructions.add(getLabelNode(normal));
            invokePreMonitorExit();
            invokeMonitor(Opcodes.MONITOREXIT);
            // success
            instructions.add(new InsnNode(returnCode()));
            // }
            // catch2 { monitorexit; throw }
            instructions.add(getLabelNode(normalRetExcept));
            instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
            instructions.add(new IntInsnNode(Opcodes.ASTORE, maxLocalParams));
            pushRef();
            invokeMonitor(Opcodes.MONITOREXIT);
            instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
            instructions.add(new InsnNode(Opcodes.ATHROW));
        // }
        }
        // catch1 { try3 { preMonitorExit; monitorexit; throw
        instructions.add(getLabelNode(except));
        instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
        instructions.add(new IntInsnNode(Opcodes.ASTORE, maxLocalParams));
        invokePreMonitorExit();
        invokeMonitor(Opcodes.MONITOREXIT);
        instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
        instructions.add(getLabelNode(exceptRetNormal));
        instructions.add(new InsnNode(Opcodes.ATHROW));
        instructions.add(getLabelNode(exceptRetExcept));
        instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
        instructions.add(new IntInsnNode(Opcodes.ASTORE, maxLocalParams));
        pushRef();
        invokeMonitor(Opcodes.MONITOREXIT);
        instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
        instructions.add(new InsnNode(Opcodes.ATHROW));
    }
    instructions.add(getLabelNode(end));
    methodWriterSink.writeSyntheticMethod(MONITOR, this);
}
Also used : TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) FrameNode(org.objectweb.asm.tree.FrameNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) Label(org.objectweb.asm.Label) IntInsnNode(org.objectweb.asm.tree.IntInsnNode)

Example 15 with IntInsnNode

use of org.objectweb.asm.tree.IntInsnNode in project spring-loaded by spring-projects.

the class TypeDiffComputer method sameIntInsnNode.

private static boolean sameIntInsnNode(AbstractInsnNode o, AbstractInsnNode n) {
    IntInsnNode oi = (IntInsnNode) o;
    if (!(n instanceof IntInsnNode)) {
        return false;
    }
    IntInsnNode ni = (IntInsnNode) n;
    return oi.operand == ni.operand;
}
Also used : IntInsnNode(org.objectweb.asm.tree.IntInsnNode)

Aggregations

IntInsnNode (org.objectweb.asm.tree.IntInsnNode)15 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)7 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)7 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)7 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)7 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)6 InsnNode (org.objectweb.asm.tree.InsnNode)5 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)4 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)3 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)3 MultiANewArrayInsnNode (org.objectweb.asm.tree.MultiANewArrayInsnNode)3 JsonObject (com.google.gson.JsonObject)2 PrimitiveType (japa.parser.ast.type.PrimitiveType)2 ReferenceType (japa.parser.ast.type.ReferenceType)2 ArrayList (java.util.ArrayList)2 Label (org.objectweb.asm.Label)2 FrameNode (org.objectweb.asm.tree.FrameNode)2 LabelNode (org.objectweb.asm.tree.LabelNode)2 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)2 AnalyzerException (org.objectweb.asm.tree.analysis.AnalyzerException)2