Example usage for org.objectweb.asm Opcodes F2D

List of usage examples for org.objectweb.asm Opcodes F2D

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes F2D.

Prototype

int F2D

To view the source code for org.objectweb.asm Opcodes F2D.

Click Source Link

Usage

From source file:org.elasticsearch.painless.node.SSource.java

License:Apache License

@Override
void write(MethodWriter writer, Globals globals) {
    if (reserved.usesScore()) {
        // if the _score value is used, we do this once:
        // final double _score = scorer.score();
        Variable scorer = mainMethod.getVariable(null, Locals.SCORER);
        Variable score = mainMethod.getVariable(null, Locals.SCORE);

        writer.visitVarInsn(Opcodes.ALOAD, scorer.getSlot());
        writer.invokeVirtual(WriterConstants.SCORER_TYPE, WriterConstants.SCORER_SCORE);
        writer.visitInsn(Opcodes.F2D);
        writer.visitVarInsn(Opcodes.DSTORE, score.getSlot());
    }//from   w w w.  j a  v a2 s .  c  om

    if (reserved.usesCtx()) {
        // if the _ctx value is used, we do this once:
        // final Map<String,Object> ctx = input.get("ctx");

        Variable input = mainMethod.getVariable(null, Locals.PARAMS);
        Variable ctx = mainMethod.getVariable(null, Locals.CTX);

        writer.visitVarInsn(Opcodes.ALOAD, input.getSlot());
        writer.push(Locals.CTX);
        writer.invokeInterface(MAP_TYPE, MAP_GET);
        writer.visitVarInsn(Opcodes.ASTORE, ctx.getSlot());
    }

    if (reserved.getMaxLoopCounter() > 0) {
        // if there is infinite loop protection, we do this once:
        // int #loop = settings.getMaxLoopCounter()

        Variable loop = mainMethod.getVariable(null, Locals.LOOP);

        writer.push(reserved.getMaxLoopCounter());
        writer.visitVarInsn(Opcodes.ISTORE, loop.getSlot());
    }

    for (AStatement statement : statements) {
        statement.write(writer, globals);
    }

    if (!methodEscape) {
        writer.visitInsn(Opcodes.ACONST_NULL);
        writer.returnValue();
    }
}

From source file:org.elasticsearch.plan.a.Caster.java

License:Apache License

void writeCast(final MethodVisitor visitor, final Cast cast) {
    final Type from = cast.from;
    final Type to = cast.to;

    if (from.equals(to)) {
        return;/*from  w ww.ja va 2s.c o m*/
    }

    if (from.metadata.numeric && to.metadata.numeric) {
        switch (from.metadata) {
        case BYTE:
            switch (to.metadata) {
            case SHORT:
                visitor.visitInsn(Opcodes.I2S);
                break;
            case CHAR:
                visitor.visitInsn(Opcodes.I2C);
                break;
            case LONG:
                visitor.visitInsn(Opcodes.I2L);
                break;
            case FLOAT:
                visitor.visitInsn(Opcodes.I2F);
                break;
            case DOUBLE:
                visitor.visitInsn(Opcodes.I2D);
                break;
            }
            break;
        case SHORT:
            switch (to.metadata) {
            case BYTE:
                visitor.visitInsn(Opcodes.I2B);
                break;
            case CHAR:
                visitor.visitInsn(Opcodes.I2C);
                break;
            case LONG:
                visitor.visitInsn(Opcodes.I2L);
                break;
            case FLOAT:
                visitor.visitInsn(Opcodes.I2F);
                break;
            case DOUBLE:
                visitor.visitInsn(Opcodes.I2D);
                break;
            }
            break;
        case CHAR:
            switch (to.metadata) {
            case BYTE:
                visitor.visitInsn(Opcodes.I2B);
                break;
            case SHORT:
                visitor.visitInsn(Opcodes.I2S);
                break;
            case LONG:
                visitor.visitInsn(Opcodes.I2L);
                break;
            case FLOAT:
                visitor.visitInsn(Opcodes.I2F);
                break;
            case DOUBLE:
                visitor.visitInsn(Opcodes.I2D);
                break;
            }
            break;
        case INT:
            switch (to.metadata) {
            case BYTE:
                visitor.visitInsn(Opcodes.I2B);
                break;
            case SHORT:
                visitor.visitInsn(Opcodes.I2S);
                break;
            case CHAR:
                visitor.visitInsn(Opcodes.I2C);
                break;
            case LONG:
                visitor.visitInsn(Opcodes.I2L);
                break;
            case FLOAT:
                visitor.visitInsn(Opcodes.I2F);
                break;
            case DOUBLE:
                visitor.visitInsn(Opcodes.I2D);
                break;
            }
            break;
        case LONG:
            switch (to.metadata) {
            case BYTE:
                visitor.visitInsn(Opcodes.L2I);
                visitor.visitInsn(Opcodes.I2B);
                break;
            case SHORT:
                visitor.visitInsn(Opcodes.L2I);
                visitor.visitInsn(Opcodes.I2S);
                break;
            case CHAR:
                visitor.visitInsn(Opcodes.L2I);
                visitor.visitInsn(Opcodes.I2C);
                break;
            case INT:
                visitor.visitInsn(Opcodes.L2I);
                break;
            case FLOAT:
                visitor.visitInsn(Opcodes.L2F);
                break;
            case DOUBLE:
                visitor.visitInsn(Opcodes.L2D);
                break;
            }
            break;
        case FLOAT:
            switch (to.metadata) {
            case BYTE:
                visitor.visitInsn(Opcodes.F2I);
                visitor.visitInsn(Opcodes.I2B);
                break;
            case SHORT:
                visitor.visitInsn(Opcodes.F2I);
                visitor.visitInsn(Opcodes.I2S);
                break;
            case CHAR:
                visitor.visitInsn(Opcodes.F2I);
                visitor.visitInsn(Opcodes.I2C);
                break;
            case INT:
                visitor.visitInsn(Opcodes.F2I);
                break;
            case LONG:
                visitor.visitInsn(Opcodes.F2L);
                break;
            case DOUBLE:
                visitor.visitInsn(Opcodes.F2D);
                break;
            }
            break;
        case DOUBLE:
            switch (to.metadata) {
            case BYTE:
                visitor.visitInsn(Opcodes.D2I);
                visitor.visitInsn(Opcodes.I2B);
                break;
            case SHORT:
                visitor.visitInsn(Opcodes.D2I);
                visitor.visitInsn(Opcodes.I2S);
                break;
            case CHAR:
                visitor.visitInsn(Opcodes.D2I);
                visitor.visitInsn(Opcodes.I2C);
                break;
            case INT:
                visitor.visitInsn(Opcodes.D2I);
                break;
            case LONG:
                visitor.visitInsn(Opcodes.D2L);
                break;
            case FLOAT:
                visitor.visitInsn(Opcodes.D2F);
                break;
            }
            break;
        }
    } else {
        try {
            from.clazz.asSubclass(to.clazz);
        } catch (ClassCastException exception) {
            visitor.visitTypeInsn(Opcodes.CHECKCAST, to.internal);
        }
    }
}

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 w w .j a  va  2 s .  c o  m*/
 *
 * @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.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 ww  w  .j  a v  a2  s .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  ww  .  j av a 2  s . c om
 *
 * @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/* w w  w  . j a va2s  . 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.lambdamatic.analyzer.ast.LambdaExpressionReader.java

License:Open Source License

/**
 * Reads the current {@link InsnNode} instruction and returns a {@link Statement} or {@code null}
 * if the instruction is not a full statement (in that case, the instruction is stored in the
 * given Expression {@link Stack}).//www  . jav  a  2s .  c om
 * 
 * @param insnNode the instruction to read
 * @param expressionStack the expression stack to put on or pop from.
 * @param localVariables the local variables
 * @return a {@link List} of {@link Statement} or empty list if no {@link Statement} was created
 *         after reading the current instruction.
 * @see <a href="https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings">Java bytcode
 *      instruction listings on Wikipedia</a>
 */
private List<Statement> readInstruction(final InsnCursor insnCursor, final Stack<Expression> expressionStack,
        final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) {
    final List<Statement> statements = new ArrayList<>();
    final AbstractInsnNode insnNode = insnCursor.getCurrent();
    switch (insnNode.getOpcode()) {
    // return a reference from a method
    case Opcodes.ARETURN:
        // return an integer from a method
    case Opcodes.IRETURN:
        statements.add(new ReturnStatement(expressionStack.pop()));
        break;
    // return void from method
    case Opcodes.RETURN:
        // wrap all pending expressions into ExpressionStatements
        while (!expressionStack.isEmpty()) {
            final Expression pendingExpression = expressionStack.pop();
            statements.add(new ExpressionStatement(pendingExpression));
        }
        break;
    // push a null reference onto the stack
    case Opcodes.ACONST_NULL:
        expressionStack.add(new NullLiteral());
        break;
    // load the int value 0 onto the stack
    case Opcodes.ICONST_0:
        // applies for byte, short, int and boolean
        expressionStack.add(new NumberLiteral(0));
        break;
    // load the int value 1 onto the stack
    case Opcodes.ICONST_1:
        // applies for byte, short, int and boolean
        expressionStack.add(new NumberLiteral(1));
        break;
    // load the int value 2 onto the stack
    case Opcodes.ICONST_2:
        expressionStack.add(new NumberLiteral(2));
        break;
    // load the int value 3 onto the stack
    case Opcodes.ICONST_3:
        expressionStack.add(new NumberLiteral(3));
        break;
    // load the int value 4 onto the stack
    case Opcodes.ICONST_4:
        expressionStack.add(new NumberLiteral(4));
        break;
    // load the int value 5 onto the stack
    case Opcodes.ICONST_5:
        expressionStack.add(new NumberLiteral(5));
        break;
    // push the long 0 onto the stack
    case Opcodes.LCONST_0:
        expressionStack.add(new NumberLiteral(0L));
        break;
    // push the long 1 onto the stack
    case Opcodes.LCONST_1:
        expressionStack.add(new NumberLiteral(1L));
        break;
    // push the 0.0f onto the stack
    case Opcodes.FCONST_0:
        expressionStack.add(new NumberLiteral(0f));
        break;
    // push the 1.0f onto the stack
    case Opcodes.FCONST_1:
        expressionStack.add(new NumberLiteral(1f));
        break;
    // push the 2.0f onto the stack
    case Opcodes.FCONST_2:
        expressionStack.add(new NumberLiteral(2f));
        break;
    // push the constant 0.0 onto the stack
    case Opcodes.DCONST_0:
        expressionStack.add(new NumberLiteral(0d));
        break;
    // push the constant 1.0 onto the stack
    case Opcodes.DCONST_1:
        expressionStack.add(new NumberLiteral(1d));
        break;
    // compare two longs values
    case Opcodes.LCMP:
        // compare two doubles
    case Opcodes.DCMPL:
        // compare two doubles
    case Opcodes.DCMPG:
        // compare two floats
    case Opcodes.FCMPL:
        // compare two floats
    case Opcodes.FCMPG:
        statements.addAll(
                readJumpInstruction(insnCursor.next(), expressionStack, capturedArguments, localVariables));
        break;
    // add 2 ints
    case Opcodes.IADD:
        expressionStack.add(readOperation(Operator.ADD, expressionStack));
        break;
    // int subtract
    case Opcodes.ISUB:
        expressionStack.add(readOperation(Operator.SUBTRACT, expressionStack));
        break;
    // multiply 2 integers
    case Opcodes.IMUL:
        expressionStack.add(readOperation(Operator.MULTIPLY, expressionStack));
        break;
    // divide 2 integers
    case Opcodes.IDIV:
        expressionStack.add(readOperation(Operator.DIVIDE, expressionStack));
        break;
    // negate int
    case Opcodes.INEG:
        expressionStack.add(inverseInteger(expressionStack));
        break;
    // discard the top value on the stack
    case Opcodes.POP:
        statements.add(new ExpressionStatement(expressionStack.pop()));
        break;
    // duplicate the value on top of the stack
    case Opcodes.DUP:
        expressionStack.push(expressionStack.peek());
        break;
    // insert a copy of the top value into the stack two values from the top.
    case Opcodes.DUP_X1:
        expressionStack.add(expressionStack.size() - 2, expressionStack.peek());
        break;
    // store into a reference in an array
    case Opcodes.AASTORE:
        readArrayStoreInstruction(insnNode, expressionStack);
        break;
    // converts Float to Double -> ignored.
    case Opcodes.F2D:
        break;
    default:
        throw new AnalyzeException(
                "Bytecode instruction with OpCode '" + insnNode.getOpcode() + "' is not supported.");
    }
    return statements;
}

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./*  ww  w .jav a2s  .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.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**
 * Gets the op-code to widen a value of a given type to a value of another type.
 * @param typeToWiden the type to widen.
 * @param valueType the type to which the typeToWiden should be widened.
 * @return int the widening op code, as defined in org.objectweb.asm.Opcodes. Opcodes.NOP us used for the no-op.     
 *///from w  w  w. ja  v  a  2s. c  o m
private static int getWideningOpCode(JavaTypeName typeToWiden, JavaTypeName valueType) {

    if (typeToWiden.equals(valueType)) {
        return Opcodes.NOP;
    }

    // Widen from int-type values -> float, long, double
    if (isInternalIntType(typeToWiden)) {

        switch (valueType.getTag()) {
        case JavaTypeName.BYTE_TAG:
        case JavaTypeName.SHORT_TAG:
        case JavaTypeName.CHAR_TAG:
        case JavaTypeName.INT_TAG:
            return Opcodes.NOP;

        case JavaTypeName.LONG_TAG:
            return Opcodes.I2L;

        case JavaTypeName.DOUBLE_TAG:
            return Opcodes.I2D;

        case JavaTypeName.FLOAT_TAG:
            return Opcodes.I2F;

        default:
            throw new IllegalArgumentException("Invalid widening conversion.");
        }

        // Widen from long -> float, double
    } else if (typeToWiden.equals(JavaTypeName.LONG)) {

        switch (valueType.getTag()) {

        case JavaTypeName.DOUBLE_TAG:
            return Opcodes.L2D;

        case JavaTypeName.FLOAT_TAG:
            return Opcodes.L2F;

        default:
            throw new IllegalArgumentException("Invalid widening conversion.");
        }

        // Widen from float -> double
    } else if (typeToWiden.equals(JavaTypeName.FLOAT)) {

        if (valueType.equals(JavaTypeName.DOUBLE)) {
            return Opcodes.F2D;
        }

        throw new IllegalArgumentException("Invalid widening conversion.");
    }

    //throw new IllegalArgumentException("Invalid widening conversion.");
    return Opcodes.NOP;
}

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");
    }//from   www. ja  va2s.  co 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");
    }
}