Example usage for org.objectweb.asm Opcodes I2B

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

Introduction

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

Prototype

int I2B

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

Click Source Link

Usage

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  ww  w .  ja va2 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  w  w  w.  j a v a  2 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 ww  w.  j  a  v 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.expression.ArithmeticExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    int currentStack = compileContext.getStackCount();
    int expectedStack = 0;
    Expression operand0 = getOperand(0);
    Expression operand1 = getOperand(1);
    Type type0 = operand0.getType();
    Type type1 = operand1.getType();
    // compile lhs -- it adds 1 or 2 to the stack height
    operand0.compile(mv, compileContext);
    // do any required type conversion
    compileTypeConversion(type0, type, mv, compileContext);
    // compile rhs -- it adds 1 or 2 to the stack height
    operand1.compile(mv, compileContext);
    // do any required type conversion
    compileTypeConversion(type1, type, mv, compileContext);

    try {// w  w w. java 2s .c  o  m
        // n.b. be careful with characters here
        if (type == type.B || type == type.S || type == type.C || type == type.I) {
            // TODO we should probably only respect the byte, short and char types for + and -
            // TODO also need to decide how to handle divide by zero

            expectedStack = 1;

            switch (oper) {
            case MUL:
                mv.visitInsn(Opcodes.IMUL);
                break;
            case DIV:
                mv.visitInsn(Opcodes.IDIV);
                break;
            case PLUS:
                mv.visitInsn(Opcodes.IADD);
                break;
            case MINUS:
                mv.visitInsn(Opcodes.ISUB);
                break;
            case MOD:
                mv.visitInsn(Opcodes.IREM);
                break;
            default:
                // should never happen
                throw new CompileException("ArithmeticExpression.compile : unexpected operator " + oper);
            }
            // now coerce back to appropriate type
            if (type == type.B) {
                mv.visitInsn(Opcodes.I2B);
            } else if (type == type.S) {
                mv.visitInsn(Opcodes.I2S);
            } else if (type == type.C) {
                mv.visitInsn(Opcodes.I2C);
            } // else if (type == type.I) { do nothing }
              // ok, we popped two bytes but added one
            compileContext.addStackCount(-1);
        } else if (type == type.J) {

            expectedStack = 2;

            switch (oper) {
            case MUL:
                mv.visitInsn(Opcodes.LMUL);
                break;
            case DIV:
                mv.visitInsn(Opcodes.LDIV);
                break;
            case PLUS:
                mv.visitInsn(Opcodes.LADD);
                break;
            case MINUS:
                mv.visitInsn(Opcodes.LSUB);
                break;
            case MOD:
                mv.visitInsn(Opcodes.LREM);
                break;
            default:
                // should never happen
                throw new CompileException("ArithmeticExpression.compile : unexpected operator " + oper);
            }
            // ok, we popped four bytes but added two
            compileContext.addStackCount(-2);
        } else if (type == type.F) {

            expectedStack = 1;

            switch (oper) {
            case MUL:
                mv.visitInsn(Opcodes.FMUL);
                break;
            case DIV:
                mv.visitInsn(Opcodes.FDIV);
                break;
            case PLUS:
                mv.visitInsn(Opcodes.FADD);
                break;
            case MINUS:
                mv.visitInsn(Opcodes.FSUB);
                break;
            case MOD:
                mv.visitInsn(Opcodes.FREM);
                break;
            default:
                // should never happen
                throw new CompileException("ArithmeticExpression.compile : unexpected operator " + oper);
            }
            // ok, we popped two bytes but added one
            compileContext.addStackCount(-1);
        } else if (type == type.D) {

            expectedStack = 2;

            switch (oper) {
            case MUL:
                mv.visitInsn(Opcodes.DMUL);
                break;
            case DIV:
                mv.visitInsn(Opcodes.DDIV);
                break;
            case PLUS:
                mv.visitInsn(Opcodes.DADD);
                break;
            case MINUS:
                mv.visitInsn(Opcodes.DSUB);
                break;
            case MOD:
                mv.visitInsn(Opcodes.DREM);
                break;
            default:
                // should never happen
                throw new CompileException("ArithmeticExpression.compile : unexpected operator " + oper);
            }
            // ok, we popped four bytes but added two
            compileContext.addStackCount(-2);
        } else {
            throw new CompileException(
                    "ArithmeticExpression.compile : unexpected result type " + type.getName());
        }
    } catch (CompileException e) {
        throw e;
    } catch (Exception e) {
        throw new CompileException("ArithmeticExpression.compile : unexpected exception for operation " + token
                + getPos() + " in rule " + rule.getName(), e);
    }

    // check stack heights
    if (compileContext.getStackCount() != currentStack + expectedStack) {
        throw new CompileException("ArithmeticExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + (currentStack + expectedStack));
    }
}

From source file:org.jboss.byteman.rule.expression.BitExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    int currentStack = compileContext.getStackCount();
    int expected = 0;
    Expression oper0 = getOperand(0);
    Expression oper1 = getOperand(1);
    // compile the operands and make sure the result is our target type
    oper0.compile(mv, compileContext);/*from  w w  w  .  j ava  2s.  c om*/
    compileTypeConversion(oper0.getType(), type, mv, compileContext);
    oper1.compile(mv, compileContext);
    compileTypeConversion(oper1.getType(), type, mv, compileContext);

    if (type == Type.B || type == Type.S || type == Type.I) {
        switch (oper) {
        case BOR:
            mv.visitInsn(Opcodes.IOR);
            break;
        case BAND:
            mv.visitInsn(Opcodes.IAND);
            break;
        case BXOR:
            mv.visitInsn(Opcodes.IXOR);
            break;
        }
        if (type == Type.B) {
            mv.visitInsn(Opcodes.I2B);
        } else if (type == Type.S) {
            mv.visitInsn(Opcodes.I2S);
        } else if (type == Type.C) {
            mv.visitInsn(Opcodes.I2C);
        }
        // ok, we popped two words but added one
        compileContext.addStackCount(-2);
        expected = 1;
    } else if (type == Type.J) {
        switch (oper) {
        case BOR:
            mv.visitInsn(Opcodes.LOR);
            break;
        case BAND:
            mv.visitInsn(Opcodes.LAND);
            break;
        case BXOR:
            mv.visitInsn(Opcodes.LXOR);
            break;
        }
        // ok, we popped four words but added two
        compileContext.addStackCount(-2);
        expected = 2;
    }
    // we have either a 1 words or a 2 words result
    // check that the stack height is what we expect

    compileContext.addStackCount(expected);

    if (compileContext.getStackCount() != currentStack + expected) {
        throw new CompileException("BitExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + currentStack + expected);
    }
}

From source file:org.jboss.byteman.rule.expression.PlusExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    Expression oper0 = getOperand(0);
    Expression oper1 = getOperand(1);

    int currentStack = compileContext.getStackCount();
    int expected = 0;

    // compile and type convert each operand -- adds 2 or 4 depending upon type
    oper0.compile(mv, compileContext);/* w w w  .  jav  a  2 s  . c o m*/
    compileTypeConversion(oper0.getType(), type, mv, compileContext);
    oper1.compile(mv, compileContext);
    compileTypeConversion(oper1.getType(), type, mv, compileContext);

    if (type == Type.STRING) {
        // ok, we could optimize this for the case where the left or right operand is a String plus expression
        // by employing a StringBuffer but for now we will just evaluate the left and right operand and
        // then call concat to join them
        // add two strings leaving one string
        expected = 1;
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "concat",
                "(Ljava/lang/String;)Ljava/lang/String;");
        compileContext.addStackCount(-1);
    } else if (type == Type.B) {
        // add two bytes leaving one byte
        expected = 1;
        mv.visitInsn(Opcodes.IADD);
        mv.visitInsn(Opcodes.I2B);
        compileContext.addStackCount(-1);
    } else if (type == Type.S) {
        // add two shorts leaving one short
        expected = 1;
        mv.visitInsn(Opcodes.IADD);
        mv.visitInsn(Opcodes.I2S);
        compileContext.addStackCount(-1);
    } else if (type == Type.C) {
        // add two chars leaving one char
        expected = 1;
        mv.visitInsn(Opcodes.IADD);
        mv.visitInsn(Opcodes.I2C);
        compileContext.addStackCount(-1);
    } else if (type == Type.I) {
        // add two ints leaving one int
        expected = 1;
        mv.visitInsn(Opcodes.IADD);
        compileContext.addStackCount(-1);
    } else if (type == Type.J) {
        // add two longs leaving one long
        expected = 2;
        mv.visitInsn(Opcodes.LADD);
        compileContext.addStackCount(-2);
    } else if (type == Type.F) {
        // add two floats leaving one float
        expected = 1;
        mv.visitInsn(Opcodes.FADD);
        compileContext.addStackCount(-1);
    } else if (type == Type.D) {
        // add two doubles leaving one double
        expected = 2;
        mv.visitInsn(Opcodes.DADD);
        compileContext.addStackCount(-2);
    }

    if (compileContext.getStackCount() != currentStack + expected) {
        throw new CompileException("PlusExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + (currentStack + expected));
    }
}

From source file:org.jboss.byteman.rule.expression.ShiftExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    int currentStack = compileContext.getStackCount();
    int expected = 0;
    Expression oper0 = getOperand(0);
    Expression oper1 = getOperand(1);
    // compile the operands and make sure the first result is our target type and the second is int
    oper0.compile(mv, compileContext);/*from w w w .j ava 2s  .  c om*/
    compileTypeConversion(oper0.getType(), type, mv, compileContext);
    oper1.compile(mv, compileContext);
    compileTypeConversion(oper1.getType(), Type.I, mv, compileContext);

    if (type == Type.B || type == Type.S || type == Type.I) {
        switch (oper) {
        case LSH:
            mv.visitInsn(Opcodes.ISHL);
            break;
        case RSH:
            if (type == Type.C) {
                mv.visitInsn(Opcodes.IUSHR);
            } else {
                mv.visitInsn(Opcodes.ISHR);
            }
            break;
        case URSH:
            mv.visitInsn(Opcodes.IUSHR);
            break;
        }
        if (type == Type.B) {
            mv.visitInsn(Opcodes.I2B);
        } else if (type == Type.S) {
            mv.visitInsn(Opcodes.I2S);
        } else if (type == Type.C) {
            mv.visitInsn(Opcodes.I2C);
        }
        // ok, we popped two words but added one
        compileContext.addStackCount(-1);
        expected = 1;
    } else if (type == Type.J) {
        switch (oper) {
        case LSH:
            mv.visitInsn(Opcodes.LSHL);
            break;
        case RSH:
            mv.visitInsn(Opcodes.LSHR);
            break;
        case URSH:
            mv.visitInsn(Opcodes.LUSHR);
            break;
        }
        // ok, we popped three words but added two
        compileContext.addStackCount(-1);
        expected = 2;
    }
    // we have either a 1 words or a 2 words result
    // check that the stack height is what we expect

    if (compileContext.getStackCount() != currentStack + expected) {
        throw new CompileException("ShiftExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + currentStack + expected);
    }
}

From source file:org.jboss.byteman.rule.expression.TwiddleExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    // compile the operand and then bit twiddle it
    Expression oper = getOperand(0);
    Type operType = oper.getType();

    int currentStack = compileContext.getStackCount();
    int expected = 0;

    oper.compile(mv, compileContext);/*from w  ww  .j a  v  a2 s  .  co m*/
    compileContext.addStackCount((operType.getNBytes() > 4 ? 2 : 1));
    compileTypeConversion(operType, type, mv, compileContext);
    if (type == Type.B) {
        expected = 1;
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IXOR);
        mv.visitInsn(Opcodes.I2B);
    } else if (type == Type.S) {
        expected = 1;
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IXOR);
        mv.visitInsn(Opcodes.I2S);
    } else if (type == Type.C) {
        expected = 1;
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IXOR);
        mv.visitInsn(Opcodes.I2C);
    } else if (type == Type.I) {
        expected = 1;
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IXOR);
    } else if (type == Type.J) {
        expected = 2;
        mv.visitInsn(Opcodes.LCONST_1);
        mv.visitInsn(Opcodes.LXOR);
    }

    // check the stack height is what we expect
    if (compileContext.getStackCount() != currentStack + expected) {
        throw new CompileException("MinusExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + currentStack + expected);
    }
}

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//from  w ww .  ja v  a 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

/**
 * Create the Java code for a given cast expression. This will cause the resulting casted object reference to
 * be pushed onto the operand stack.//  w w  w  .  ja v  a  2s.c  o  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;
}