List of usage examples for org.objectweb.asm Opcodes L2I
int L2I
To view the source code for org.objectweb.asm Opcodes L2I.
Click Source Link
From source file:org.evosuite.instrumentation.mutation.ReplaceVariable.java
License:Open Source License
/** * Generates the instructions to cast a numerical value from one type to * another./*from w ww .ja v a 2s . c om*/ * * @param from * the type of the top stack value * @param to * the type into which this value must be cast. * @return a {@link org.objectweb.asm.tree.InsnList} object. */ public static InsnList cast(final Type from, final Type to) { InsnList list = new InsnList(); if (from != to) { if (from == Type.DOUBLE_TYPE) { if (to == Type.FLOAT_TYPE) { list.add(new InsnNode(Opcodes.D2F)); } else if (to == Type.LONG_TYPE) { list.add(new InsnNode(Opcodes.D2L)); } else { list.add(new InsnNode(Opcodes.D2I)); list.add(cast(Type.INT_TYPE, to)); } } else if (from == Type.FLOAT_TYPE) { if (to == Type.DOUBLE_TYPE) { list.add(new InsnNode(Opcodes.F2D)); } else if (to == Type.LONG_TYPE) { list.add(new InsnNode(Opcodes.F2L)); } else { list.add(new InsnNode(Opcodes.F2I)); list.add(cast(Type.INT_TYPE, to)); } } else if (from == Type.LONG_TYPE) { if (to == Type.DOUBLE_TYPE) { list.add(new InsnNode(Opcodes.L2D)); } else if (to == Type.FLOAT_TYPE) { list.add(new InsnNode(Opcodes.L2F)); } else { list.add(new InsnNode(Opcodes.L2I)); list.add(cast(Type.INT_TYPE, to)); } } else { if (to == Type.BYTE_TYPE) { list.add(new InsnNode(Opcodes.I2B)); } else if (to == Type.CHAR_TYPE) { list.add(new InsnNode(Opcodes.I2C)); } else if (to == Type.DOUBLE_TYPE) { list.add(new InsnNode(Opcodes.I2D)); } else if (to == Type.FLOAT_TYPE) { list.add(new InsnNode(Opcodes.I2F)); } else if (to == Type.LONG_TYPE) { list.add(new InsnNode(Opcodes.I2L)); } else if (to == Type.SHORT_TYPE) { list.add(new InsnNode(Opcodes.I2S)); } } } return list; }
From source file:org.evosuite.instrumentation.ReturnValueAdapter.java
License:Open Source License
private void callLogPrototype(String traceMethod, PDType type) { if (type != PDType.LONG && type != PDType.DOUBLE) { this.visitInsn(Opcodes.DUP); if (type == PDType.FLOAT) { this.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "floatToRawIntBits", "(F)I", false); }//w w w . j av a 2s.c o m } else { this.visitInsn(Opcodes.DUP2); if (type == PDType.DOUBLE) { this.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "doubleToRawLongBits", "(D)J", false); } this.visitInsn(Opcodes.DUP2); this.visitIntInsn(Opcodes.BIPUSH, 32); this.visitInsn(Opcodes.LSHR); this.visitInsn(Opcodes.LXOR); this.visitInsn(Opcodes.L2I); } this.visitLdcInsn(className); this.visitLdcInsn(fullMethodName); this.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class), "returnValue", "(ILjava/lang/String;Ljava/lang/String;)V", false); }
From source file:org.jacoco.core.internal.instr.FrameTracker.java
License:Open Source License
@Override public void visitInsn(final int opcode) { final Object t1, t2, t3, t4; switch (opcode) { case Opcodes.NOP: case Opcodes.RETURN: break;//from w w w.j av a 2s. c o m case Opcodes.ARETURN: case Opcodes.ATHROW: case Opcodes.FRETURN: case Opcodes.IRETURN: case Opcodes.MONITORENTER: case Opcodes.MONITOREXIT: case Opcodes.POP: pop(1); break; case Opcodes.DRETURN: case Opcodes.LRETURN: case Opcodes.POP2: pop(2); break; case Opcodes.AASTORE: case Opcodes.BASTORE: case Opcodes.CASTORE: case Opcodes.FASTORE: case Opcodes.IASTORE: case Opcodes.SASTORE: pop(3); break; case Opcodes.LASTORE: case Opcodes.DASTORE: pop(4); break; case Opcodes.ICONST_M1: case Opcodes.ICONST_0: case Opcodes.ICONST_1: case Opcodes.ICONST_2: case Opcodes.ICONST_3: case Opcodes.ICONST_4: case Opcodes.ICONST_5: push(Opcodes.INTEGER); break; case Opcodes.ARRAYLENGTH: case Opcodes.F2I: case Opcodes.I2B: case Opcodes.I2C: case Opcodes.I2S: case Opcodes.INEG: pop(1); push(Opcodes.INTEGER); break; case Opcodes.BALOAD: case Opcodes.CALOAD: case Opcodes.D2I: case Opcodes.FCMPG: case Opcodes.FCMPL: case Opcodes.IADD: case Opcodes.IALOAD: case Opcodes.IAND: case Opcodes.IDIV: case Opcodes.IMUL: case Opcodes.IOR: case Opcodes.IREM: case Opcodes.ISHL: case Opcodes.ISHR: case Opcodes.ISUB: case Opcodes.IUSHR: case Opcodes.IXOR: case Opcodes.L2I: case Opcodes.SALOAD: pop(2); push(Opcodes.INTEGER); break; case Opcodes.DCMPG: case Opcodes.DCMPL: case Opcodes.LCMP: pop(4); push(Opcodes.INTEGER); break; case Opcodes.FCONST_0: case Opcodes.FCONST_1: case Opcodes.FCONST_2: push(Opcodes.FLOAT); break; case Opcodes.FNEG: case Opcodes.I2F: pop(1); push(Opcodes.FLOAT); break; case Opcodes.D2F: case Opcodes.FADD: case Opcodes.FALOAD: case Opcodes.FDIV: case Opcodes.FMUL: case Opcodes.FREM: case Opcodes.FSUB: case Opcodes.L2F: pop(2); push(Opcodes.FLOAT); break; case Opcodes.LCONST_0: case Opcodes.LCONST_1: push(Opcodes.LONG); push(Opcodes.TOP); break; case Opcodes.F2L: case Opcodes.I2L: pop(1); push(Opcodes.LONG); push(Opcodes.TOP); break; case Opcodes.D2L: case Opcodes.LALOAD: case Opcodes.LNEG: pop(2); push(Opcodes.LONG); push(Opcodes.TOP); break; case Opcodes.LSHL: case Opcodes.LSHR: case Opcodes.LUSHR: pop(3); push(Opcodes.LONG); push(Opcodes.TOP); break; case Opcodes.LADD: case Opcodes.LAND: case Opcodes.LDIV: case Opcodes.LMUL: case Opcodes.LOR: case Opcodes.LREM: case Opcodes.LSUB: case Opcodes.LXOR: pop(4); push(Opcodes.LONG); push(Opcodes.TOP); break; case Opcodes.DCONST_0: case Opcodes.DCONST_1: push(Opcodes.DOUBLE); push(Opcodes.TOP); break; case Opcodes.F2D: case Opcodes.I2D: pop(1); push(Opcodes.DOUBLE); push(Opcodes.TOP); break; case Opcodes.DALOAD: case Opcodes.DNEG: case Opcodes.L2D: pop(2); push(Opcodes.DOUBLE); push(Opcodes.TOP); break; case Opcodes.DADD: case Opcodes.DDIV: case Opcodes.DMUL: case Opcodes.DREM: case Opcodes.DSUB: pop(4); push(Opcodes.DOUBLE); push(Opcodes.TOP); break; case Opcodes.ACONST_NULL: push(Opcodes.NULL); break; case Opcodes.AALOAD: pop(1); t1 = pop(); push(Type.getType(((String) t1).substring(1))); break; case Opcodes.DUP: t1 = pop(); push(t1); push(t1); break; case Opcodes.DUP_X1: t1 = pop(); t2 = pop(); push(t1); push(t2); push(t1); break; case Opcodes.DUP_X2: t1 = pop(); t2 = pop(); t3 = pop(); push(t1); push(t3); push(t2); push(t1); break; case Opcodes.DUP2: t1 = pop(); t2 = pop(); push(t2); push(t1); push(t2); push(t1); break; case Opcodes.DUP2_X1: t1 = pop(); t2 = pop(); t3 = pop(); push(t2); push(t1); push(t3); push(t2); push(t1); break; case Opcodes.DUP2_X2: t1 = pop(); t2 = pop(); t3 = pop(); t4 = pop(); push(t2); push(t1); push(t4); push(t3); push(t2); push(t1); break; case Opcodes.SWAP: t1 = pop(); t2 = pop(); push(t1); push(t2); break; default: throw new IllegalArgumentException(); } mv.visitInsn(opcode); }
From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java
License:Open Source License
/** * Generates the instructions to cast a numerical value from one type to * another./*from w w w . jav a 2 s . co m*/ * * @param from the type of the top stack value * @param to the type into which this value must be cast. */ public void cast(final Type from, final Type to) { if (from != to) { if (from == Type.DOUBLE_TYPE) { if (to == Type.FLOAT_TYPE) { visitInsn(Opcodes.D2F); } else if (to == Type.LONG_TYPE) { visitInsn(Opcodes.D2L); } else { visitInsn(Opcodes.D2I); cast(Type.INT_TYPE, to); } } else if (from == Type.FLOAT_TYPE) { if (to == Type.DOUBLE_TYPE) { visitInsn(Opcodes.F2D); } else if (to == Type.LONG_TYPE) { visitInsn(Opcodes.F2L); } else { visitInsn(Opcodes.F2I); cast(Type.INT_TYPE, to); } } else if (from == Type.LONG_TYPE) { if (to == Type.DOUBLE_TYPE) { visitInsn(Opcodes.L2D); } else if (to == Type.FLOAT_TYPE) { visitInsn(Opcodes.L2F); } else { visitInsn(Opcodes.L2I); cast(Type.INT_TYPE, to); } } else { if (to == Type.BYTE_TYPE) { visitInsn(Opcodes.I2B); } else if (to == Type.CHAR_TYPE) { visitInsn(Opcodes.I2C); } else if (to == Type.DOUBLE_TYPE) { visitInsn(Opcodes.I2D); } else if (to == Type.FLOAT_TYPE) { visitInsn(Opcodes.I2F); } else if (to == Type.LONG_TYPE) { visitInsn(Opcodes.I2L); } else if (to == Type.SHORT_TYPE) { visitInsn(Opcodes.I2S); } } } }
From source file:org.jboss.byteman.rule.RuleElement.java
License:Open Source License
/** * compile code to convert a numeric or character primitive to a numeric or character primitive * @param fromType/* ww w. j a va 2 s . c o m*/ * @param toType * @param mv * @param compileContext * @throws CompileException */ protected void compilePrimitiveConversion(Type fromType, Type toType, MethodVisitor mv, CompileContext compileContext) throws CompileException { if (fromType == Type.B || fromType == Type.S || fromType == Type.I) { if (toType == Type.B) { mv.visitInsn(Opcodes.I2B); } else if (toType == Type.S) { mv.visitInsn(Opcodes.I2S); } else if (toType == Type.C) { mv.visitInsn(Opcodes.I2C); } else if (toType == Type.I) { // nothing to do } else if (toType == Type.J) { mv.visitInsn(Opcodes.I2L); compileContext.addStackCount(1); } else if (toType == Type.F) { mv.visitInsn(Opcodes.I2F); } else if (toType == Type.D) { mv.visitInsn(Opcodes.I2D); compileContext.addStackCount(1); } } else if (fromType == Type.C) { // convert to the relevant numeric size if (toType == Type.B) { mv.visitInsn(Opcodes.I2B); } else if (toType == Type.S) { mv.visitInsn(Opcodes.I2S); } else if (toType == Type.C) { // nothing to do } else if (toType == Type.I) { // nothing to do } else if (toType == Type.J) { mv.visitInsn(Opcodes.I2L); compileContext.addStackCount(1); } else if (toType == Type.F) { mv.visitInsn(Opcodes.I2F); } else if (toType == Type.D) { mv.visitInsn(Opcodes.I2D); compileContext.addStackCount(1); } } else if (fromType == Type.J) { if (toType == Type.B || toType == Type.S || toType == Type.I || toType == Type.C) { mv.visitInsn(Opcodes.L2I); compileContext.addStackCount(-1); } else if (toType == Type.J) { // nothing to do } else if (toType == Type.F) { mv.visitInsn(Opcodes.L2F); compileContext.addStackCount(-1); } else if (toType == Type.D) { mv.visitInsn(Opcodes.L2D); } } else if (fromType == Type.F) { if (toType == Type.B) { mv.visitInsn(Opcodes.F2I); mv.visitInsn(Opcodes.I2B); } else if (toType == Type.S) { mv.visitInsn(Opcodes.F2I); mv.visitInsn(Opcodes.I2S); } else if (toType == Type.C) { mv.visitInsn(Opcodes.F2I); mv.visitInsn(Opcodes.I2C); } else if (toType == Type.I) { mv.visitInsn(Opcodes.F2I); } else if (toType == Type.J) { mv.visitInsn(Opcodes.F2L); compileContext.addStackCount(1); } else if (toType == Type.F) { // nothing to do } else if (toType == Type.D) { mv.visitInsn(Opcodes.F2D); compileContext.addStackCount(1); } } else if (fromType == Type.D) { if (toType == Type.B) { mv.visitInsn(Opcodes.D2I); mv.visitInsn(Opcodes.I2B); compileContext.addStackCount(-1); } else if (toType == Type.S) { mv.visitInsn(Opcodes.D2I); mv.visitInsn(Opcodes.I2S); compileContext.addStackCount(-1); } else if (toType == Type.C) { mv.visitInsn(Opcodes.D2I); mv.visitInsn(Opcodes.I2C); compileContext.addStackCount(-1); } else if (toType == Type.I) { mv.visitInsn(Opcodes.D2I); compileContext.addStackCount(-1); } else if (toType == Type.J) { mv.visitInsn(Opcodes.D2L); } else if (toType == Type.F) { mv.visitInsn(Opcodes.D2F); compileContext.addStackCount(-1); } else if (toType == Type.D) { // nothing to do } } }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * Encodes the Java code for a given Java operator expression. * //from ww w .j ava2 s . co m * @param operatorExpr the java operator expression * @param context * @return JavaTypeName * @throws JavaGenerationException */ private static JavaTypeName encodeOperatorExpr(JavaExpression.OperatorExpression operatorExpr, GenerationContext context) throws JavaGenerationException { MethodVisitor mv = context.getMethodVisitor(); JavaOperator operator = operatorExpr.getJavaOperator(); String symbol = operator.getSymbol(); JavaTypeName valueType = operator.getValueType(); // Now carry out the operation according to its type. if (operator.isArithmeticOp()) { // Add the instructions to evaluate the first argument. JavaTypeName arg1Type = encodeExpr(operatorExpr.getArgument(0), context); if (operatorExpr instanceof OperatorExpression.Unary) { if (symbol.equals("-")) { // number negation mv.visitInsn(getNegateOpCode(arg1Type)); return arg1Type; } throw new JavaGenerationException("Unknown unary arithmetic operator " + symbol + "."); } // Add an instruction to widen the argument if necessary. int wideningOpCode = getWideningOpCode(arg1Type, valueType); if (wideningOpCode != Opcodes.NOP) { mv.visitInsn(wideningOpCode); } // Add the instructions to evaluate the second argument. JavaTypeName arg2Type = encodeExpr(operatorExpr.getArgument(1), context); // Add an instruction to widen the argument if necessary. wideningOpCode = getWideningOpCode(arg2Type, valueType); if (wideningOpCode != Opcodes.NOP) { mv.visitInsn(wideningOpCode); } // Evaluate. mv.visitInsn(getArithmeticBinaryOpCode(symbol, valueType)); return valueType; } if (operator.isBitOp()) { // Add the instructions to evaluate the first argument. JavaTypeName arg1Type = encodeExpr(operatorExpr.getArgument(0), context); // Add an instruction to widen the argument if necessary. int wideningOpCode = getWideningOpCode(arg1Type, valueType); if (wideningOpCode != Opcodes.NOP) { mv.visitInsn(wideningOpCode); } if (operatorExpr instanceof OperatorExpression.Unary) { if (symbol.equals("~")) { // number negation if (valueType == JavaTypeName.INT) { mv.visitInsn(Opcodes.ICONST_M1); mv.visitInsn(Opcodes.IXOR); return valueType; } else if (valueType == JavaTypeName.LONG) { encodePushLongValue(new Long(-1), context); mv.visitInsn(Opcodes.LXOR); return valueType; } } throw new JavaGenerationException("Unknown unary arithmetic operator " + symbol + "."); } // Add the instructions to evaluate the second argument. JavaTypeName arg2Type = encodeExpr(operatorExpr.getArgument(1), context); // If this is >>, >>>, or << we may need to narrow the second argument to an int if (symbol.equals(">>") || symbol.equals(">>>") || symbol.equals("<<")) { if (arg2Type == JavaTypeName.LONG) { mv.visitInsn(Opcodes.L2I); } } else { // Add an instruction to widen the argument if necessary. wideningOpCode = getWideningOpCode(arg2Type, valueType); if (wideningOpCode != Opcodes.NOP) { mv.visitInsn(wideningOpCode); } } // Evaluate. mv.visitInsn(getArithmeticBinaryOpCode(symbol, valueType)); return valueType; } if (operator.isLogicalOp() || operator.isRelationalOp()) { // Logical op: {"!", "&&", "||"} // Relational op: {">", ">=", "<", "<=", "==", "!="} Label trueContinuation = new Label(); Label falseContinuation = new Label(); encodeBooleanValuedOperatorHelper(operatorExpr, context, trueContinuation, falseContinuation); return encodeThenTrueElseFalse(trueContinuation, falseContinuation, context); } if (operator == JavaOperator.STRING_CONCATENATION) { // Create an uninitialized StringBuilder, duplicate the reference (so we can invoke the initializer). mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V"); // append the first arg. JavaTypeName firstArgType = encodeExpr(operatorExpr.getArgument(0), context); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", getAppendJVMDescriptor(firstArgType)); // Append the results of evaluation of the second arg. // Note that, conveniently, StringBuilder has an append() method for all the different types. JavaTypeName secondArgType = encodeExpr(operatorExpr.getArgument(1), context); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", getAppendJVMDescriptor(secondArgType)); // Call toString() on the result. mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;"); return JavaTypeName.STRING; } return encodeTernaryOperatorExpr((OperatorExpression.Ternary) operatorExpr, context); }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * Create the Java code for a given cast expression. This will cause the resulting casted object reference to * be pushed onto the operand stack.//from ww w .j ava 2s. co m * * @param castExpression the cast expression * @param context * @return JavaTypeName the type of the result on the operand stack. * @throws JavaGenerationException */ private static JavaTypeName encodeCastExpr(JavaExpression.CastExpression castExpression, GenerationContext context) throws JavaGenerationException { MethodVisitor mv = context.getMethodVisitor(); final JavaTypeName expressionToCastType = encodeExpr(castExpression.getExpressionToCast(), context); final JavaTypeName castType = castExpression.getCastType(); if (expressionToCastType.equals(castType)) { //no operation needed if the types are the same. return castType; } if (castType instanceof JavaTypeName.Reference) { //when the cast type is a object or array type, use the CHECKCAST instruction. This will fail bytecode verification if //the expressionToCast type is a primitive type mv.visitTypeInsn(Opcodes.CHECKCAST, castType.getJVMInternalName()); return castType; } //casting between primitive types. //There are 15 supported primitive conversions: I2L, I2F, I2D, L2I, L2F, L2D, F2I, F2L, F2D, D2I, D2L, D2F, I2B, I2C, I2S //Depending upon the expressionToCastType and castType, choose the appropriate instruction. final int conversionOpCode; switch (expressionToCastType.getTag()) { case JavaTypeName.VOID_TAG: case JavaTypeName.BOOLEAN_TAG: throw new JavaGenerationException("Unsupported primitive cast."); case JavaTypeName.BYTE_TAG: { switch (castType.getTag()) { case JavaTypeName.VOID_TAG: case JavaTypeName.BOOLEAN_TAG: throw new JavaGenerationException("Unsupported primitive cast."); case JavaTypeName.BYTE_TAG: //should be handled above as a no-op. throw new IllegalArgumentException(); case JavaTypeName.SHORT_TAG: conversionOpCode = Opcodes.I2S; break; case JavaTypeName.CHAR_TAG: conversionOpCode = Opcodes.I2C; break; case JavaTypeName.INT_TAG: //no-op return castType; case JavaTypeName.LONG_TAG: conversionOpCode = Opcodes.I2L; break; case JavaTypeName.DOUBLE_TAG: conversionOpCode = Opcodes.I2D; break; case JavaTypeName.FLOAT_TAG: conversionOpCode = Opcodes.I2F; break; case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: throw new JavaGenerationException("Cannot cast a primitive type to a reference type."); default: { throw new IllegalArgumentException(); } } break; } case JavaTypeName.SHORT_TAG: { switch (castType.getTag()) { case JavaTypeName.VOID_TAG: case JavaTypeName.BOOLEAN_TAG: throw new JavaGenerationException("Unsupported primitive cast."); case JavaTypeName.BYTE_TAG: conversionOpCode = Opcodes.I2B; break; case JavaTypeName.SHORT_TAG: //should be handled above as a no-op. throw new IllegalArgumentException(); case JavaTypeName.CHAR_TAG: conversionOpCode = Opcodes.I2C; break; case JavaTypeName.INT_TAG: //no-op return castType; case JavaTypeName.LONG_TAG: conversionOpCode = Opcodes.I2L; break; case JavaTypeName.DOUBLE_TAG: conversionOpCode = Opcodes.I2D; break; case JavaTypeName.FLOAT_TAG: conversionOpCode = Opcodes.I2F; break; case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: throw new JavaGenerationException("Cannot cast a primitive type to a reference type."); default: { throw new IllegalArgumentException(); } } break; } case JavaTypeName.CHAR_TAG: { switch (castType.getTag()) { case JavaTypeName.VOID_TAG: case JavaTypeName.BOOLEAN_TAG: throw new JavaGenerationException("Unsupported primitive cast."); case JavaTypeName.BYTE_TAG: conversionOpCode = Opcodes.I2B; break; case JavaTypeName.SHORT_TAG: conversionOpCode = Opcodes.I2S; break; case JavaTypeName.CHAR_TAG: //should be handled above as a no-op. throw new IllegalArgumentException(); case JavaTypeName.INT_TAG: //no-op return castType; case JavaTypeName.LONG_TAG: conversionOpCode = Opcodes.I2L; break; case JavaTypeName.DOUBLE_TAG: conversionOpCode = Opcodes.I2D; break; case JavaTypeName.FLOAT_TAG: conversionOpCode = Opcodes.I2F; break; case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: throw new JavaGenerationException("Cannot cast a primitive type to a reference type."); default: { throw new IllegalArgumentException(); } } break; } case JavaTypeName.INT_TAG: { switch (castType.getTag()) { case JavaTypeName.VOID_TAG: case JavaTypeName.BOOLEAN_TAG: throw new JavaGenerationException("Unsupported primitive cast."); case JavaTypeName.BYTE_TAG: conversionOpCode = Opcodes.I2B; break; case JavaTypeName.SHORT_TAG: conversionOpCode = Opcodes.I2S; break; case JavaTypeName.CHAR_TAG: conversionOpCode = Opcodes.I2C; break; case JavaTypeName.INT_TAG: //should be handled above as a no-op. throw new IllegalArgumentException(); case JavaTypeName.LONG_TAG: conversionOpCode = Opcodes.I2L; break; case JavaTypeName.DOUBLE_TAG: conversionOpCode = Opcodes.I2D; break; case JavaTypeName.FLOAT_TAG: conversionOpCode = Opcodes.I2F; break; case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: throw new JavaGenerationException("Cannot cast a primitive type to a reference type."); default: { throw new IllegalArgumentException(); } } break; } case JavaTypeName.LONG_TAG: { switch (castType.getTag()) { case JavaTypeName.VOID_TAG: case JavaTypeName.BOOLEAN_TAG: throw new JavaGenerationException("Unsupported primitive cast."); case JavaTypeName.BYTE_TAG: mv.visitInsn(Opcodes.L2I); conversionOpCode = Opcodes.I2B; break; case JavaTypeName.SHORT_TAG: mv.visitInsn(Opcodes.L2I); conversionOpCode = Opcodes.I2S; break; case JavaTypeName.CHAR_TAG: mv.visitInsn(Opcodes.L2I); conversionOpCode = Opcodes.I2C; break; case JavaTypeName.INT_TAG: conversionOpCode = Opcodes.L2I; break; case JavaTypeName.LONG_TAG: //should be handled above as a no-op. throw new IllegalArgumentException(); case JavaTypeName.DOUBLE_TAG: conversionOpCode = Opcodes.L2D; break; case JavaTypeName.FLOAT_TAG: conversionOpCode = Opcodes.L2F; break; case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: throw new JavaGenerationException("Cannot cast a primitive type to a reference type."); default: { throw new IllegalArgumentException(); } } break; } case JavaTypeName.DOUBLE_TAG: { switch (castType.getTag()) { case JavaTypeName.VOID_TAG: case JavaTypeName.BOOLEAN_TAG: throw new JavaGenerationException("Unsupported primitive cast."); case JavaTypeName.BYTE_TAG: mv.visitInsn(Opcodes.D2I); conversionOpCode = Opcodes.I2B; break; case JavaTypeName.SHORT_TAG: mv.visitInsn(Opcodes.D2I); conversionOpCode = Opcodes.I2S; break; case JavaTypeName.CHAR_TAG: mv.visitInsn(Opcodes.D2I); conversionOpCode = Opcodes.I2C; break; case JavaTypeName.INT_TAG: conversionOpCode = Opcodes.D2I; break; case JavaTypeName.LONG_TAG: conversionOpCode = Opcodes.D2L; break; case JavaTypeName.DOUBLE_TAG: //should be handled above as a no-op. throw new IllegalArgumentException(); case JavaTypeName.FLOAT_TAG: conversionOpCode = Opcodes.D2F; break; case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: throw new JavaGenerationException("Cannot cast a primitive type to a reference type."); default: { throw new IllegalArgumentException(); } } break; } case JavaTypeName.FLOAT_TAG: { switch (castType.getTag()) { case JavaTypeName.VOID_TAG: case JavaTypeName.BOOLEAN_TAG: throw new JavaGenerationException("Unsupported primitive cast."); case JavaTypeName.BYTE_TAG: mv.visitInsn(Opcodes.F2I); conversionOpCode = Opcodes.I2B; break; case JavaTypeName.SHORT_TAG: mv.visitInsn(Opcodes.F2I); conversionOpCode = Opcodes.I2S; break; case JavaTypeName.CHAR_TAG: mv.visitInsn(Opcodes.F2I); conversionOpCode = Opcodes.I2C; break; case JavaTypeName.INT_TAG: conversionOpCode = Opcodes.F2I; break; case JavaTypeName.LONG_TAG: conversionOpCode = Opcodes.F2L; break; case JavaTypeName.DOUBLE_TAG: conversionOpCode = Opcodes.F2D; break; case JavaTypeName.FLOAT_TAG: //should be handled above as a no-op. throw new IllegalArgumentException(); case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: throw new JavaGenerationException("Cannot cast a primitive type to a reference type."); default: { throw new IllegalArgumentException(); } } break; } case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: throw new JavaGenerationException("Cannot cast a reference type to a primitive type."); default: { throw new IllegalArgumentException(); } } mv.visitInsn(conversionOpCode); return castType; }
From source file:org.sonar.java.bytecode.se.BytecodeEGWalkerExecuteTest.java
License:Open Source License
@Test public void test_conversion() throws Exception { int[] toLongOrDouble = { Opcodes.I2D, Opcodes.I2L, Opcodes.F2D, Opcodes.F2L }; for (int opcode : toLongOrDouble) { SymbolicValue sv = new SymbolicValue(); ProgramState initialPs = ProgramState.EMPTY_STATE.stackValue(sv); ProgramState ps = execute(new Instruction(opcode), initialPs); assertThat(isDoubleOrLong(ps, sv)).isTrue(); assertThatThrownBy(() -> execute(new Instruction(opcode))) .hasMessage(Printer.OPCODES[opcode] + " needs value on stack"); }//w w w .j av a 2s.c o m int[] fromLongOrDouble = { Opcodes.D2F, Opcodes.D2I, Opcodes.L2F, Opcodes.L2I }; for (int opcode : fromLongOrDouble) { SymbolicValue sv = new SymbolicValue(); ProgramState initialPs = ProgramState.EMPTY_STATE.stackValue(sv); initialPs = setDoubleOrLong(initialPs, sv, true); ProgramState ps = execute(new Instruction(opcode), initialPs); assertThat(isDoubleOrLong(ps, sv)).isFalse(); assertThatThrownBy(() -> execute(new Instruction(opcode))) .hasMessage(Printer.OPCODES[opcode] + " needs value on stack"); } }
From source file:org.spongepowered.despector.emitter.bytecode.instruction.BytecodeCastEmitter.java
License:Open Source License
@Override public void emit(BytecodeEmitterContext ctx, Cast arg, TypeSignature type) { MethodVisitor mv = ctx.getMethodVisitor(); ctx.emitInstruction(arg.getValue(), null); TypeSignature to = arg.getType();/*w ww . jav a 2 s .c om*/ TypeSignature from = arg.getValue().inferType(); if (to == ClassTypeSignature.INT) { if (from == ClassTypeSignature.LONG) { mv.visitInsn(Opcodes.L2I); } else if (from == ClassTypeSignature.FLOAT) { mv.visitInsn(Opcodes.F2I); } else if (from == ClassTypeSignature.DOUBLE) { mv.visitInsn(Opcodes.D2I); } } else if (to == ClassTypeSignature.LONG) { if (from == ClassTypeSignature.INT) { mv.visitInsn(Opcodes.I2L); } else if (from == ClassTypeSignature.FLOAT) { mv.visitInsn(Opcodes.F2L); } else if (from == ClassTypeSignature.DOUBLE) { mv.visitInsn(Opcodes.D2L); } } else if (to == ClassTypeSignature.FLOAT) { if (from == ClassTypeSignature.LONG) { mv.visitInsn(Opcodes.L2F); } else if (from == ClassTypeSignature.INT) { mv.visitInsn(Opcodes.I2F); } else if (from == ClassTypeSignature.DOUBLE) { mv.visitInsn(Opcodes.D2F); } } else if (to == ClassTypeSignature.DOUBLE) { if (from == ClassTypeSignature.LONG) { mv.visitInsn(Opcodes.L2D); } else if (from == ClassTypeSignature.FLOAT) { mv.visitInsn(Opcodes.F2D); } else if (from == ClassTypeSignature.INT) { mv.visitInsn(Opcodes.I2D); } } mv.visitTypeInsn(Opcodes.CHECKCAST, to.getDescriptor()); }
From source file:serianalyzer.JVMImpl.java
License:Open Source License
/** * @param opcode/*from www . j a v a 2 s . com*/ * @param s */ static void handleJVMInsn(int opcode, JVMStackState s) { BaseType o1; BaseType o2; BaseType o3; List<BaseType> l1; List<BaseType> l2; switch (opcode) { case Opcodes.NOP: break; case Opcodes.ARRAYLENGTH: o1 = s.pop(); s.push(new BasicConstant(Type.INT_TYPE, 0, !(o1 != null && !o1.isTainted()))); break; case Opcodes.ACONST_NULL: s.push(new BasicConstant(Type.VOID_TYPE, "<null>")); //$NON-NLS-1$ break; case Opcodes.ICONST_M1: case Opcodes.ICONST_0: case Opcodes.ICONST_1: case Opcodes.ICONST_2: case Opcodes.ICONST_3: case Opcodes.ICONST_4: case Opcodes.ICONST_5: s.push(new BasicConstant(Type.INT_TYPE, opcode - 3)); break; case Opcodes.LCONST_0: case Opcodes.LCONST_1: s.push(new BasicConstant(Type.LONG_TYPE, opcode - 9L)); break; case Opcodes.FCONST_0: case Opcodes.FCONST_1: case Opcodes.FCONST_2: s.push(new BasicConstant(Type.FLOAT_TYPE, opcode - 11f)); break; case Opcodes.DCONST_0: case Opcodes.DCONST_1: s.push(new BasicConstant(Type.DOUBLE_TYPE, opcode - 14d)); break; case Opcodes.IALOAD: case Opcodes.LALOAD: case Opcodes.FALOAD: case Opcodes.DALOAD: case Opcodes.BALOAD: case Opcodes.CALOAD: case Opcodes.SALOAD: o1 = s.pop(); o2 = s.pop(); s.push(new BasicVariable(toType(opcode), "primitive array elem", //$NON-NLS-1$ (o1 == null || o1.isTainted()) | (o2 == null || o2.isTainted()))); break; case Opcodes.AALOAD: o1 = s.pop(); o2 = s.pop(); if (o1 != null && o2 instanceof SimpleType && ((SimpleType) o2).getType().toString().startsWith("[")) { //$NON-NLS-1$ Type atype = Type.getType(((SimpleType) o2).getType().toString().substring(1)); if (o2.getAlternativeTypes() != null && !o2.getAlternativeTypes().isEmpty()) { s.clear(); break; } s.push(new BasicVariable(atype, "array elem " + atype, o1.isTainted() | o2.isTainted())); //$NON-NLS-1$ } else { s.clear(); } break; case Opcodes.IASTORE: case Opcodes.LASTORE: case Opcodes.FASTORE: case Opcodes.DASTORE: case Opcodes.AASTORE: case Opcodes.BASTORE: case Opcodes.CASTORE: case Opcodes.SASTORE: s.pop(3); break; case Opcodes.POP2: s.pop(); case Opcodes.MONITORENTER: case Opcodes.MONITOREXIT: case Opcodes.POP: s.pop(); break; case Opcodes.DUP: if (!s.isEmpty()) { o1 = s.pop(); s.push(o1); s.push(o1); } break; case Opcodes.DUP_X1: o1 = s.pop(); o2 = s.pop(); s.push(o1); s.push(o2); s.push(o1); break; case Opcodes.DUP_X2: o1 = s.pop(); o2 = s.pop(); o3 = s.pop(); s.push(o1); s.push(o3); s.push(o2); s.push(o1); break; case Opcodes.DUP2: l1 = s.popWord(); if (l1.isEmpty()) { log.trace("DUP2 with unknown operand"); //$NON-NLS-1$ s.clear(); } else { s.pushWord(l1); s.pushWord(l1); } break; case Opcodes.DUP2_X1: l1 = s.popWord(); o1 = s.pop(); if (l1.isEmpty()) { log.trace("DUP2 with unknown operand"); //$NON-NLS-1$ s.clear(); } else { s.pushWord(l1); s.push(o1); s.pushWord(l1); } break; case Opcodes.DUP2_X2: l1 = s.popWord(); l2 = s.popWord(); if (l1.isEmpty() || l2.isEmpty()) { log.trace("DUP2 with unknown operand"); //$NON-NLS-1$ s.clear(); } else { s.pushWord(l1); s.pushWord(l2); s.pushWord(l1); } break; case Opcodes.SWAP: o1 = s.pop(); o2 = s.pop(); s.push(o1); s.push(o2); break; case Opcodes.IADD: case Opcodes.LADD: case Opcodes.FADD: case Opcodes.DADD: case Opcodes.ISUB: case Opcodes.LSUB: case Opcodes.FSUB: case Opcodes.DSUB: case Opcodes.IMUL: case Opcodes.LMUL: case Opcodes.FMUL: case Opcodes.DMUL: case Opcodes.IDIV: case Opcodes.LDIV: case Opcodes.FDIV: case Opcodes.DDIV: case Opcodes.IREM: case Opcodes.LREM: case Opcodes.FREM: case Opcodes.DREM: case Opcodes.IAND: case Opcodes.LAND: case Opcodes.IOR: case Opcodes.LOR: case Opcodes.IXOR: case Opcodes.LXOR: case Opcodes.LCMP: case Opcodes.FCMPL: case Opcodes.FCMPG: case Opcodes.DCMPL: case Opcodes.DCMPG: s.merge(2); break; case Opcodes.ISHL: case Opcodes.LSHL: case Opcodes.ISHR: case Opcodes.LSHR: case Opcodes.IUSHR: case Opcodes.LUSHR: s.pop(); // amount // ignore value break; case Opcodes.INEG: case Opcodes.F2I: case Opcodes.D2I: case Opcodes.L2I: s.push(cast(s.pop(), Type.INT_TYPE)); break; case Opcodes.LNEG: case Opcodes.I2L: case Opcodes.F2L: case Opcodes.D2L: s.push(cast(s.pop(), Type.LONG_TYPE)); break; case Opcodes.FNEG: case Opcodes.I2F: case Opcodes.L2F: case Opcodes.D2F: s.push(cast(s.pop(), Type.FLOAT_TYPE)); case Opcodes.DNEG: case Opcodes.I2D: case Opcodes.L2D: case Opcodes.F2D: s.push(cast(s.pop(), Type.DOUBLE_TYPE)); case Opcodes.I2B: s.push(cast(s.pop(), Type.BYTE_TYPE)); break; case Opcodes.I2C: s.push(cast(s.pop(), Type.CHAR_TYPE)); break; case Opcodes.I2S: s.push(cast(s.pop(), Type.SHORT_TYPE)); break; case Opcodes.ARETURN: s.clear(); break; case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: case Opcodes.RETURN: if (log.isTraceEnabled()) { log.trace("Found return " + s.pop()); //$NON-NLS-1$ } s.clear(); break; case Opcodes.ATHROW: Object thrw = s.pop(); log.trace("Found throw " + thrw); //$NON-NLS-1$ s.clear(); break; default: log.warn("Unsupported instruction code " + opcode); //$NON-NLS-1$ } }