Example usage for org.objectweb.asm Opcodes CASTORE

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

Introduction

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

Prototype

int CASTORE

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

Click Source Link

Usage

From source file:bytecode.InstructionExporter.java

License:Apache License

/**
 * Outputs a write operation, choosing both the correct opcode family
 * (variable write, array store, field put, etc.) and type.
 *
 * @param instruction Write instruction.
 * @return            <code>null</code>
 *//* ww w. j av  a2  s.c o m*/
@Override
public Void visit(Write instruction) {
    // Variable Stores
    if (instruction.getState() instanceof Variable) {
        Variable v = (Variable) instruction.getState();

        switch (v.getType().getSort()) {
        case LONG:
            mv.visitVarInsn(Opcodes.LSTORE, v.getIndex());
            break;
        case FLOAT:
            mv.visitVarInsn(Opcodes.FSTORE, v.getIndex());
            break;
        case DOUBLE:
            mv.visitVarInsn(Opcodes.DSTORE, v.getIndex());
            break;
        case REF:
            mv.visitVarInsn(Opcodes.ASTORE, v.getIndex());
            break;
        default:
            mv.visitVarInsn(Opcodes.ISTORE, v.getIndex());
            break;
        }
        // Array Stores
    } else if (instruction.getState() instanceof ArrayElement) {
        switch (instruction.getState().getType().getSort()) {
        case INT:
            mv.visitInsn(Opcodes.IASTORE);
            break;
        case LONG:
            mv.visitInsn(Opcodes.LASTORE);
            break;
        case FLOAT:
            mv.visitInsn(Opcodes.FASTORE);
            break;
        case DOUBLE:
            mv.visitInsn(Opcodes.DASTORE);
            break;
        case REF:
            mv.visitInsn(Opcodes.AASTORE);
            break;
        case BYTE:
            mv.visitInsn(Opcodes.BASTORE);
            break;
        case BOOL:
            mv.visitInsn(Opcodes.BASTORE);
            break;
        case CHAR:
            mv.visitInsn(Opcodes.CASTORE);
            break;
        case SHORT:
            mv.visitInsn(Opcodes.SASTORE);
            break;
        }
        // Static Writes
    } else if (instruction.getState() instanceof Field) {
        Field f = (Field) instruction.getState();

        mv.visitFieldInsn(Opcodes.PUTSTATIC, f.getOwner().getName(), f.getName(), f.getType().getDescriptor());
        // Field Writes
    } else if (instruction.getState() instanceof InstanceField) {
        Field f = (Field) ((InstanceField) instruction.getState()).getField();

        mv.visitFieldInsn(Opcodes.PUTFIELD, f.getOwner().getName(), f.getName(), f.getType().getDescriptor());
    }

    return null;
}

From source file:bytecode.MethodImporter.java

License:Apache License

/**
 * Imports instructions with no immediate operands.
 *
 * @param opcode Opcode./*from  w  ww.  ja  va 2  s. c  om*/
 */
@Override
public void visitInsn(final int opcode) {
    Producer a, b, c, d;
    Type top;

    switch (opcode) {
    // Constants
    case Opcodes.ACONST_NULL:
        createConstant(null);
        break;
    case Opcodes.ICONST_M1:
        createConstant(new Integer(-1));
        break;
    case Opcodes.ICONST_0:
        createConstant(new Integer(0));
        break;
    case Opcodes.ICONST_1:
        createConstant(new Integer(1));
        break;
    case Opcodes.ICONST_2:
        createConstant(new Integer(2));
        break;
    case Opcodes.ICONST_3:
        createConstant(new Integer(3));
        break;
    case Opcodes.ICONST_4:
        createConstant(new Integer(4));
        break;
    case Opcodes.ICONST_5:
        createConstant(new Integer(5));
        break;
    case Opcodes.LCONST_0:
        createConstant(new Long(0));
        break;
    case Opcodes.LCONST_1:
        createConstant(new Long(1));
        break;
    case Opcodes.FCONST_0:
        createConstant(new Float(0.0f));
        break;
    case Opcodes.FCONST_1:
        createConstant(new Float(1.0f));
        break;
    case Opcodes.FCONST_2:
        createConstant(new Float(2.0f));
        break;
    case Opcodes.DCONST_0:
        createConstant(new Double(0.0f));
        break;
    case Opcodes.DCONST_1:
        createConstant(new Double(1.0f));
        break;

    // Binary Operations
    case Opcodes.IADD:
        createArithmetic(Arithmetic.Operator.ADD, Type.INT);
        break;
    case Opcodes.LADD:
        createArithmetic(Arithmetic.Operator.ADD, Type.LONG);
        break;
    case Opcodes.FADD:
        createArithmetic(Arithmetic.Operator.ADD, Type.FLOAT);
        break;
    case Opcodes.DADD:
        createArithmetic(Arithmetic.Operator.ADD, Type.DOUBLE);
        break;
    case Opcodes.ISUB:
        createArithmetic(Arithmetic.Operator.SUB, Type.INT);
        break;
    case Opcodes.LSUB:
        createArithmetic(Arithmetic.Operator.SUB, Type.LONG);
        break;
    case Opcodes.FSUB:
        createArithmetic(Arithmetic.Operator.SUB, Type.FLOAT);
        break;
    case Opcodes.DSUB:
        createArithmetic(Arithmetic.Operator.SUB, Type.DOUBLE);
        break;
    case Opcodes.IMUL:
        createArithmetic(Arithmetic.Operator.MUL, Type.INT);
        break;
    case Opcodes.LMUL:
        createArithmetic(Arithmetic.Operator.MUL, Type.LONG);
        break;
    case Opcodes.FMUL:
        createArithmetic(Arithmetic.Operator.MUL, Type.FLOAT);
        break;
    case Opcodes.DMUL:
        createArithmetic(Arithmetic.Operator.MUL, Type.DOUBLE);
        break;
    case Opcodes.IDIV:
        createArithmetic(Arithmetic.Operator.DIV, Type.INT);
        break;
    case Opcodes.LDIV:
        createArithmetic(Arithmetic.Operator.DIV, Type.LONG);
        break;
    case Opcodes.FDIV:
        createArithmetic(Arithmetic.Operator.DIV, Type.FLOAT);
        break;
    case Opcodes.DDIV:
        createArithmetic(Arithmetic.Operator.DIV, Type.DOUBLE);
        break;
    case Opcodes.IREM:
        createArithmetic(Arithmetic.Operator.REM, Type.INT);
        break;
    case Opcodes.LREM:
        createArithmetic(Arithmetic.Operator.REM, Type.LONG);
        break;
    case Opcodes.FREM:
        createArithmetic(Arithmetic.Operator.REM, Type.FLOAT);
        break;
    case Opcodes.DREM:
        createArithmetic(Arithmetic.Operator.REM, Type.DOUBLE);
        break;
    case Opcodes.IAND:
        createArithmetic(Arithmetic.Operator.AND, Type.INT);
        break;
    case Opcodes.LAND:
        createArithmetic(Arithmetic.Operator.AND, Type.LONG);
        break;
    case Opcodes.IOR:
        createArithmetic(Arithmetic.Operator.OR, Type.INT);
        break;
    case Opcodes.LOR:
        createArithmetic(Arithmetic.Operator.OR, Type.LONG);
        break;
    case Opcodes.IXOR:
        createArithmetic(Arithmetic.Operator.XOR, Type.INT);
        break;
    case Opcodes.LXOR:
        createArithmetic(Arithmetic.Operator.XOR, Type.LONG);
        break;
    case Opcodes.ISHL:
        createArithmetic(Arithmetic.Operator.SHL, Type.INT);
        break;
    case Opcodes.LSHL:
        createArithmetic(Arithmetic.Operator.SHL, Type.LONG);
        break;
    case Opcodes.ISHR:
        createArithmetic(Arithmetic.Operator.SHR, Type.INT);
        break;
    case Opcodes.LSHR:
        createArithmetic(Arithmetic.Operator.SHR, Type.LONG);
        break;
    case Opcodes.IUSHR:
        createArithmetic(Arithmetic.Operator.USHR, Type.INT);
        break;
    case Opcodes.LUSHR:
        createArithmetic(Arithmetic.Operator.USHR, Type.LONG);
        break;

    case Opcodes.LCMP:
        createCompare(false, Type.LONG);
        break;
    case Opcodes.FCMPL:
        createCompare(false, Type.FLOAT);
        break;
    case Opcodes.FCMPG:
        createCompare(true, Type.FLOAT);
        break;
    case Opcodes.DCMPL:
        createCompare(false, Type.DOUBLE);
        break;
    case Opcodes.DCMPG:
        createCompare(true, Type.DOUBLE);
        break;
    case Opcodes.INEG:
        createNegate();
        break;
    case Opcodes.LNEG:
        createNegate();
        break;
    case Opcodes.FNEG:
        createNegate();
        break;
    case Opcodes.DNEG:
        createNegate();
        break;
    case Opcodes.I2L:
        createConvert(Type.LONG);
        break;
    case Opcodes.I2F:
        createConvert(Type.FLOAT);
        break;
    case Opcodes.I2D:
        createConvert(Type.DOUBLE);
        break;
    case Opcodes.I2B:
        createConvert(Type.BYTE);
        break;
    case Opcodes.I2C:
        createConvert(Type.CHAR);
        break;
    case Opcodes.I2S:
        createConvert(Type.SHORT);
        break;
    case Opcodes.L2I:
        createConvert(Type.INT);
        break;
    case Opcodes.L2F:
        createConvert(Type.FLOAT);
        break;
    case Opcodes.L2D:
        createConvert(Type.DOUBLE);
        break;
    case Opcodes.F2I:
        createConvert(Type.INT);
        break;
    case Opcodes.F2L:
        createConvert(Type.LONG);
        break;
    case Opcodes.F2D:
        createConvert(Type.DOUBLE);
        break;
    case Opcodes.D2I:
        createConvert(Type.INT);
        break;
    case Opcodes.D2F:
        createConvert(Type.FLOAT);
        break;
    case Opcodes.D2L:
        createConvert(Type.LONG);
        break;
    case Opcodes.IALOAD:
        createArrayRead(Type.INT);
        break;
    case Opcodes.LALOAD:
        createArrayRead(Type.LONG);
        break;
    case Opcodes.FALOAD:
        createArrayRead(Type.FLOAT);
        break;
    case Opcodes.DALOAD:
        createArrayRead(Type.DOUBLE);
        break;
    case Opcodes.AALOAD:
        createArrayRead(Type.getFreshRef());
        break;
    case Opcodes.BALOAD:
        createArrayRead(Type.BYTE);
        break;
    case Opcodes.CALOAD:
        createArrayRead(Type.CHAR);
        break;
    case Opcodes.SALOAD:
        createArrayRead(Type.SHORT);
        break;
    case Opcodes.IASTORE:
        createArrayWrite(Type.INT);
        break;
    case Opcodes.LASTORE:
        createArrayWrite(Type.LONG);
        break;
    case Opcodes.FASTORE:
        createArrayWrite(Type.FLOAT);
        break;
    case Opcodes.DASTORE:
        createArrayWrite(Type.DOUBLE);
        break;
    case Opcodes.AASTORE:
        createArrayWrite(Type.getFreshRef());
        break;
    case Opcodes.BASTORE:
        createArrayWrite(Type.BYTE);
        break;
    case Opcodes.CASTORE:
        createArrayWrite(Type.CHAR);
        break;
    case Opcodes.SASTORE:
        createArrayWrite(Type.SHORT);
        break;
    case Opcodes.IRETURN:
        createReturn(Type.INT);
        break;
    case Opcodes.LRETURN:
        createReturn(Type.LONG);
        break;
    case Opcodes.FRETURN:
        createReturn(Type.FLOAT);
        break;
    case Opcodes.DRETURN:
        createReturn(Type.DOUBLE);
        break;
    case Opcodes.ARETURN:
        createReturn(Type.REF);
        break;
    case Opcodes.RETURN:
        createReturn(null);
        break;
    case Opcodes.ATHROW:
        createThrow();
        break;

    // Array Length
    case Opcodes.ARRAYLENGTH:
        ordered.add(stack.push(new ArrayLength(stack.pop())));
        break;

    // Swap
    case Opcodes.SWAP:
        a = stack.pop();
        b = stack.pop();

        stack.push(a);
        stack.push(b);

        ordered.add(new StackOperation(StackOperation.Sort.SWAP));

        break;

    // Duplicates
    case Opcodes.DUP:
        stack.push(stack.peek());
        ordered.add(new StackOperation(StackOperation.Sort.DUP));
        break;
    case Opcodes.DUP2:
        top = stack.peek().getType();

        // Type 2 Values
        if (top.getSize() == 2) {
            stack.push(stack.peek());
            // Type 1 Values
        } else {
            b = stack.pop();
            a = stack.pop();

            stack.push(a);
            stack.push(b);
            stack.push(a);
            stack.push(b);
        }

        ordered.add(new StackOperation(StackOperation.Sort.DUP2));

        break;
    case Opcodes.DUP_X1:
        b = stack.pop();
        a = stack.pop();

        stack.push(b);
        stack.push(a);
        stack.push(b);

        ordered.add(new StackOperation(StackOperation.Sort.DUP_X1));

        break;
    case Opcodes.DUP_X2:
        top = stack.peek().getType();

        // Type 2 Values
        if (top.getSize() == 2) {
            b = stack.pop();
            a = stack.pop();

            stack.push(b);
            stack.push(a);
            stack.push(b);
            // Type 1 Values
        } else {
            c = stack.pop();
            b = stack.pop();
            a = stack.pop();

            stack.push(c);
            stack.push(a);
            stack.push(b);
            stack.push(c);
        }

        ordered.add(new StackOperation(StackOperation.Sort.DUP_X2));

        break;

    // Pops
    case Opcodes.POP:
        stack.pop();
        ordered.add(new StackOperation(StackOperation.Sort.POP));
        break;
    case Opcodes.POP2:
        top = stack.peek().getType();

        // Type 2 Values
        if (top.getSize() == 2) {
            stack.pop();
            // Type 1 Values
        } else {
            stack.pop();
            stack.pop();
        }

        ordered.add(new StackOperation(StackOperation.Sort.POP2));

        break;

    // TODO: DUP2_X1, DUP2_X2, MONITORENTER, MONITOREXIT
    case Opcodes.MONITORENTER:
        throw new RuntimeException("visitInsn: MONITORENTER");
    case Opcodes.MONITOREXIT:
        throw new RuntimeException("visitInsn: MONITOREXIT");
    case Opcodes.DUP2_X1:
        throw new RuntimeException("visitInsn: DUP2_X1");
    case Opcodes.DUP2_X2:
        throw new RuntimeException("visitInsn: DUP2_X2");
    default:
        throw new RuntimeException("visitInsn: " + opcode);
    }
}

From source file:com.codename1.tools.translator.BytecodeMethod.java

License:Open Source License

boolean optimize() {
    int instructionCount = instructions.size();

    // optimize away a method that only contains the void return instruction e.g. blank constructors etc.
    if (instructionCount < 6) {
        int realCount = instructionCount;
        Instruction actual = null;//from   w  w  w.  j  av a 2  s  .  c  o  m
        for (int iter = 0; iter < instructionCount; iter++) {
            Instruction current = instructions.get(iter);
            if (current instanceof LabelInstruction) {
                realCount--;
                continue;
            }
            if (current instanceof LineNumber) {
                realCount--;
                continue;
            }
            actual = current;
        }

        if (realCount == 1 && actual != null && actual.getOpcode() == Opcodes.RETURN) {
            return false;
        }
    }

    boolean astoreCalls = false;
    boolean hasInstructions = false;

    boolean hasTryCatch = false;
    for (int iter = 0; iter < instructionCount - 1; iter++) {
        Instruction current = instructions.get(iter);
        if (current instanceof TryCatch) {
            hasTryCatch = true;
        }
        current.setMethod(this);
        if (current.isOptimized()) {
            continue;
        }
        int currentOpcode = current.getOpcode();
        switch (currentOpcode) {
        case Opcodes.CHECKCAST: {
            // Remove the check cast for now as it gets in the way of other optimizations
            instructions.remove(iter);
            iter--;
            instructionCount--;
            break;
        }
        }
    }

    for (int iter = 0; iter < instructionCount - 1; iter++) {
        Instruction current = instructions.get(iter);
        if (current.isOptimized()) {
            // This instruction has already been optimized
            // we should skip it and proceed to the next one
            continue;
        }
        Instruction next = instructions.get(iter + 1);

        int currentOpcode = current.getOpcode();
        int nextOpcode = next.getOpcode();

        if (ArithmeticExpression.isArithmeticOp(current)) {
            int addedIndex = ArithmeticExpression.tryReduce(instructions, iter);
            if (addedIndex >= 0) {
                iter = addedIndex;
                instructionCount = instructions.size();
                continue;
            }
        }

        if (current instanceof Field) {
            int newIter = Field.tryReduce(instructions, iter);
            if (newIter >= 0) {
                iter = newIter;
                instructionCount = instructions.size();
                continue;
            }
        }

        switch (currentOpcode) {

        case Opcodes.ARRAYLENGTH: {
            if (!dependentClasses.contains("java_lang_NullPointerException")) {
                dependentClasses.add("java_lang_NullPointerException");
            }
            int newIter = ArrayLengthExpression.tryReduce(instructions, iter);
            if (newIter >= 0) {
                instructionCount = instructions.size();
                iter = newIter;
                continue;
            }
            break;
        }

        case Opcodes.DUP: {
            int newIter = DupExpression.tryReduce(instructions, iter);
            if (newIter >= 0) {
                iter = newIter;
                instructionCount = instructions.size();
                continue;
            }
            break;
        }

        case Opcodes.POP: {
            if (iter > 0) {
                Instruction prev = instructions.get(iter - 1);
                if (prev instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) prev;
                    if (inv.methodHasReturnValue()) {
                        inv.setNoReturn(true);
                        instructions.remove(iter);
                        iter--;
                        instructionCount--;
                        continue;
                    }
                }
            }
            break;
        }

        case Opcodes.ASTORE:
        case Opcodes.ISTORE:
        case Opcodes.DSTORE:
        case Opcodes.LSTORE:
        case Opcodes.FSTORE: {
            if (iter > 0 && current instanceof VarOp) {
                VarOp currentVarOp = (VarOp) current;
                Instruction prev = instructions.get(iter - 1);
                if (prev instanceof AssignableExpression) {
                    AssignableExpression expr = (AssignableExpression) prev;
                    StringBuilder sb = new StringBuilder();
                    if (currentVarOp.assignFrom(expr, sb)) {
                        instructions.remove(iter - 1);
                        instructions.remove(iter - 1);
                        instructions.add(iter - 1,
                                new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                        iter = iter - 1;
                        instructionCount = instructions.size();
                        continue;
                    }

                } else if (prev instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) prev;
                    StringBuilder sb = new StringBuilder();
                    if (currentVarOp.assignFrom(inv, sb)) {
                        instructions.remove(iter - 1);
                        instructions.remove(iter - 1);
                        instructions.add(iter - 1,
                                new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                        iter = iter - 1;
                        instructionCount = instructions.size();
                        continue;
                    }
                }
            }

            break;
        }

        case Opcodes.IRETURN:
        case Opcodes.FRETURN:
        case Opcodes.ARETURN:
        case Opcodes.LRETURN:
        case Opcodes.DRETURN: {
            if (iter > 0 && current instanceof BasicInstruction) {
                Instruction prev = instructions.get(iter - 1);
                if (prev instanceof AssignableExpression) {
                    AssignableExpression expr = (AssignableExpression) prev;
                    StringBuilder sb = new StringBuilder();
                    if (expr.assignTo(null, sb)) {
                        instructions.remove(iter - 1);
                        instructions.remove(iter - 1);
                        String exprString = sb.toString().trim();
                        String retVal = exprString;
                        sb.setLength(0);
                        if (!prev.isConstant()) {
                            sb.append("\n{\n    ");
                            switch (currentOpcode) {
                            case Opcodes.IRETURN:
                                sb.append("JAVA_INT");
                                break;
                            case Opcodes.FRETURN:
                                sb.append("JAVA_FLOAT");
                                break;
                            case Opcodes.ARETURN:
                                sb.append("JAVA_OBJECT");
                                break;
                            case Opcodes.LRETURN:
                                sb.append("JAVA_LONG");
                                break;
                            case Opcodes.DRETURN:
                                sb.append("JAVA_DOUBLE");
                                break;
                            }
                            sb.append(" ___returnValue=").append(exprString).append(";\n");
                            retVal = "___returnValue";
                        }
                        if (synchronizedMethod) {
                            if (staticMethod) {
                                sb.append("    monitorExit(threadStateData, (JAVA_OBJECT)&class__");
                                sb.append(getClsName());
                                sb.append(");\n");
                            } else {
                                sb.append("    monitorExit(threadStateData, __cn1ThisObject);\n");
                            }
                        }
                        if (hasTryCatch) {
                            sb.append(
                                    "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ")
                                    .append(retVal).append(";\n");
                        } else {
                            sb.append("    releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ")
                                    .append(retVal).append(";\n");
                        }
                        if (!prev.isConstant()) {
                            sb.append("}\n");
                        }

                        instructions.add(iter - 1,
                                new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                        iter--;
                        instructionCount = instructions.size();
                        continue;

                    }
                } else if (prev instanceof CustomInvoke) {

                    CustomInvoke expr = (CustomInvoke) prev;
                    String returnType = expr.getReturnValue();
                    if (returnType != null && !"JAVA_OBJECT".equals(returnType)) {
                        // We can't safely return a JAVA_OBJECT directly because it needs to be added 
                        // to the stack for the GC
                        StringBuilder sb = new StringBuilder();
                        if (expr.appendExpression(sb)) {
                            instructions.remove(iter - 1);
                            instructions.remove(iter - 1);
                            String exprString = sb.toString().trim();
                            String retVal = exprString;
                            sb.setLength(0);
                            if (!expr.isConstant()) {

                                sb.append("\n{\n    ");
                                switch (currentOpcode) {
                                case Opcodes.IRETURN:
                                    sb.append("JAVA_INT");
                                    break;
                                case Opcodes.FRETURN:
                                    sb.append("JAVA_FLOAT");
                                    break;
                                case Opcodes.ARETURN:
                                    sb.append("JAVA_OBJECT");
                                    break;
                                case Opcodes.LRETURN:
                                    sb.append("JAVA_LONG");
                                    break;
                                case Opcodes.DRETURN:
                                    sb.append("JAVA_DOUBLE");
                                    break;
                                }

                                sb.append(" ___returnValue=").append(exprString).append(";\n");
                                retVal = "___returnValue";
                            }
                            if (synchronizedMethod) {
                                if (staticMethod) {
                                    sb.append("    monitorExit(threadStateData, (JAVA_OBJECT)&class__");
                                    sb.append(getClsName());
                                    sb.append(");\n");
                                } else {
                                    sb.append("    monitorExit(threadStateData, __cn1ThisObject);\n");
                                }
                            }
                            if (hasTryCatch) {
                                sb.append(
                                        "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ")
                                        .append(retVal).append(";\n");
                            } else {
                                sb.append(
                                        "    releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ")
                                        .append(retVal).append(";\n");
                            }
                            if (!expr.isConstant()) {
                                sb.append("}\n");
                            }

                            instructions.add(iter - 1,
                                    new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                            iter--;
                            instructionCount = instructions.size();
                            continue;

                        }
                    }
                }
            }
            break;
        }

        case Opcodes.BASTORE:
        case Opcodes.SASTORE:
        case Opcodes.CASTORE:
        case Opcodes.AASTORE:
        case Opcodes.IASTORE:
        case Opcodes.DASTORE:
        case Opcodes.LASTORE:
        case Opcodes.FASTORE: {
            if (iter > 2 && current instanceof BasicInstruction) {
                StringBuilder devNull = new StringBuilder();
                String arrayLiteral = null;
                String indexLiteral = null;
                String valueLiteral = null;
                Instruction prev3 = instructions.get(iter - 3);
                if (prev3 instanceof AssignableExpression) {
                    if (((AssignableExpression) prev3).assignTo(null, devNull)) {
                        arrayLiteral = devNull.toString().trim();

                    }
                }
                devNull.setLength(0);
                Instruction prev2 = instructions.get(iter - 2);
                if (prev2 instanceof AssignableExpression) {
                    if (((AssignableExpression) prev2).assignTo(null, devNull)) {
                        indexLiteral = devNull.toString().trim();
                    }
                }
                devNull.setLength(0);
                Instruction prev1 = instructions.get(iter - 1);

                if (prev1 instanceof AssignableExpression) {
                    if (((AssignableExpression) prev1).assignTo(null, devNull)) {
                        valueLiteral = devNull.toString().trim();
                    }
                } else if (prev1 instanceof CustomInvoke) {
                    devNull.setLength(0);
                    if (((CustomInvoke) prev1).appendExpression(devNull)) {
                        valueLiteral = devNull.toString().trim();
                    }
                }

                if (arrayLiteral != null && indexLiteral != null && valueLiteral != null) {
                    String elementType = null;
                    switch (current.getOpcode()) {
                    case Opcodes.AASTORE:
                        elementType = "OBJECT";
                        break;
                    case Opcodes.IASTORE:
                        elementType = "INT";
                        break;
                    case Opcodes.DASTORE:
                        elementType = "DOUBLE";
                        break;

                    case Opcodes.LASTORE:
                        elementType = "LONG";
                        break;
                    case Opcodes.FASTORE:
                        elementType = "FLOAT";
                        break;
                    case Opcodes.CASTORE:
                        elementType = "CHAR";
                        break;
                    case Opcodes.BASTORE:
                        elementType = "BYTE";
                        break;
                    case Opcodes.SASTORE:
                        elementType = "SHORT";
                        break;

                    }
                    if (elementType == null) {
                        break;
                    }

                    instructions.remove(iter - 3);
                    instructions.remove(iter - 3);
                    instructions.remove(iter - 3);
                    instructions.remove(iter - 3);
                    String code = "    CN1_SET_ARRAY_ELEMENT_" + elementType + "(" + arrayLiteral + ", "
                            + indexLiteral + ", " + valueLiteral + ");\n";
                    instructions.add(iter - 3, new CustomIntruction(code, code, dependentClasses));
                    iter = iter - 3;
                    instructionCount = instructions.size();
                    continue;
                }
            }

            break;
        }

        case Opcodes.FALOAD:
        case Opcodes.BALOAD:
        case Opcodes.IALOAD:
        case Opcodes.LALOAD:
        case Opcodes.DALOAD:
        case Opcodes.AALOAD:
        case Opcodes.SALOAD:
        case Opcodes.CALOAD: {
            int newIter = ArrayLoadExpression.tryReduce(instructions, iter);
            if (newIter >= 0) {
                iter = newIter;
                instructionCount = instructions.size();
                continue;
            }
            break;
        }

        /* Try to optimize if statements that just use constants
           and local variables so that they don't need the intermediate
           push and pop from the stack.
        */
        case Opcodes.IF_ACMPEQ:
        case Opcodes.IF_ACMPNE:
        case Opcodes.IF_ICMPLE:
        case Opcodes.IF_ICMPLT:
        case Opcodes.IF_ICMPNE:
        case Opcodes.IF_ICMPGT:
        case Opcodes.IF_ICMPEQ:
        case Opcodes.IF_ICMPGE: {

            if (iter > 1) {
                Instruction leftArg = instructions.get(iter - 2);
                Instruction rightArg = instructions.get(iter - 1);

                String leftLiteral = null;
                String rightLiteral = null;

                if (leftArg instanceof AssignableExpression) {
                    StringBuilder sb = new StringBuilder();
                    if (((AssignableExpression) leftArg).assignTo(null, sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                } else if (leftArg instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) leftArg;
                    StringBuilder sb = new StringBuilder();
                    if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                }
                if (rightArg instanceof AssignableExpression) {
                    StringBuilder sb = new StringBuilder();
                    if (((AssignableExpression) rightArg).assignTo(null, sb)) {
                        rightLiteral = sb.toString().trim();
                    }
                } else if (rightArg instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) rightArg;
                    StringBuilder sb = new StringBuilder();
                    if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) {
                        rightLiteral = sb.toString().trim();
                    }
                }

                if (rightLiteral != null && leftLiteral != null) {
                    Jump jmp = (Jump) current;
                    instructions.remove(iter - 2);
                    instructions.remove(iter - 2);
                    instructions.remove(iter - 2);
                    //instructions.remove(iter-2);
                    iter -= 2;
                    //instructionCount -= 2;
                    StringBuilder sb = new StringBuilder();
                    String operator = null;
                    String opName = null;
                    switch (currentOpcode) {
                    case Opcodes.IF_ICMPLE:
                        operator = "<=";
                        opName = "IF_ICMPLE";
                        break;
                    case Opcodes.IF_ICMPLT:
                        operator = "<";
                        opName = "IF_IMPLT";
                        break;
                    case Opcodes.IF_ICMPNE:
                        operator = "!=";
                        opName = "IF_ICMPNE";
                        break;
                    case Opcodes.IF_ICMPGT:
                        operator = ">";
                        opName = "IF_ICMPGT";
                        break;
                    case Opcodes.IF_ICMPGE:
                        operator = ">=";
                        opName = "IF_ICMPGE";
                        break;
                    case Opcodes.IF_ICMPEQ:
                        operator = "==";
                        opName = "IF_ICMPEQ";
                        break;
                    case Opcodes.IF_ACMPEQ:
                        operator = "==";
                        opName = "IF_ACMPEQ";
                        break;
                    case Opcodes.IF_ACMPNE:
                        operator = "!=";
                        opName = "IF_ACMPNE";
                        break;
                    default:
                        throw new RuntimeException(
                                "Invalid operator during optimization of integer comparison");
                    }

                    sb.append("if (").append(leftLiteral).append(operator).append(rightLiteral).append(") /* ")
                            .append(opName).append(" CustomJump */ ");
                    CustomJump newJump = CustomJump.create(jmp, sb.toString());
                    //jmp.setCustomCompareCode(sb.toString());
                    newJump.setOptimized(true);
                    instructions.add(iter, newJump);
                    instructionCount = instructions.size();

                }

            }
            break;
        }
        case Opcodes.IFNONNULL:
        case Opcodes.IFNULL:

        case Opcodes.IFLE:
        case Opcodes.IFLT:
        case Opcodes.IFNE:
        case Opcodes.IFGT:
        case Opcodes.IFEQ:
        case Opcodes.IFGE: {
            String rightArg = "0";
            if (currentOpcode == Opcodes.IFNONNULL || currentOpcode == Opcodes.IFNULL) {
                rightArg = "JAVA_NULL";
            }
            if (iter > 0) {
                Instruction leftArg = instructions.get(iter - 1);

                String leftLiteral = null;

                if (leftArg instanceof AssignableExpression) {
                    StringBuilder sb = new StringBuilder();
                    if (((AssignableExpression) leftArg).assignTo(null, sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                } else if (leftArg instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) leftArg;
                    StringBuilder sb = new StringBuilder();
                    if (inv.appendExpression(sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                }

                if (leftLiteral != null) {
                    Jump jmp = (Jump) current;
                    instructions.remove(iter - 1);
                    instructions.remove(iter - 1);
                    //instructions.remove(iter-2);
                    iter -= 1;
                    //instructionCount -= 2;
                    StringBuilder sb = new StringBuilder();
                    String operator = null;
                    String opName = null;
                    switch (currentOpcode) {
                    case Opcodes.IFLE:
                        operator = "<=";
                        opName = "IFLE";
                        break;
                    case Opcodes.IFLT:
                        operator = "<";
                        opName = "IFLT";
                        break;
                    case Opcodes.IFNE:
                        operator = "!=";
                        opName = "IFNE";
                        break;
                    case Opcodes.IFGT:
                        operator = ">";
                        opName = "IFGT";
                        break;
                    case Opcodes.IFGE:
                        operator = ">=";
                        opName = "IFGE";
                        break;
                    case Opcodes.IFEQ:
                        operator = "==";
                        opName = "IFEQ";
                        break;
                    case Opcodes.IFNULL:
                        operator = "==";
                        opName = "IFNULL";
                        break;
                    case Opcodes.IFNONNULL:
                        operator = "!=";
                        opName = "IFNONNULL";
                        break;
                    default:
                        throw new RuntimeException(
                                "Invalid operator during optimization of integer comparison");
                    }

                    sb.append("if (").append(leftLiteral).append(operator).append(rightArg).append(") /* ")
                            .append(opName).append(" CustomJump */ ");
                    CustomJump newJump = CustomJump.create(jmp, sb.toString());
                    //jmp.setCustomCompareCode(sb.toString());
                    newJump.setOptimized(true);
                    instructions.add(iter, newJump);
                    instructionCount = instructions.size();

                }

            }
            break;
        }

        case Opcodes.INVOKEVIRTUAL:
        case Opcodes.INVOKESTATIC:
        case Opcodes.INVOKESPECIAL:
        case Opcodes.INVOKEINTERFACE: {
            if (current instanceof Invoke) {
                Invoke inv = (Invoke) current;
                List<ByteCodeMethodArg> invocationArgs = inv.getArgs();
                int numArgs = invocationArgs.size();

                //if (current.getOpcode() != Opcodes.INVOKESTATIC) {
                //    numArgs++;
                //}
                if (iter >= numArgs) {
                    String[] argLiterals = new String[numArgs];
                    StringBuilder devNull = new StringBuilder();
                    for (int i = 0; i < numArgs; i++) {
                        devNull.setLength(0);
                        Instruction instr = instructions.get(iter - numArgs + i);
                        if (instr instanceof AssignableExpression
                                && ((AssignableExpression) instr).assignTo(null, devNull)) {
                            argLiterals[i] = devNull.toString().trim();
                        } else if (instr instanceof CustomInvoke) {
                            CustomInvoke cinv = (CustomInvoke) instr;
                            devNull.setLength(0);
                            if (!"JAVA_OBJECT".equals(cinv.getReturnValue())
                                    && cinv.appendExpression(devNull)) {
                                // We can't add invocations that return objects directly
                                // because they need to be added to the stack for GC
                                argLiterals[i] = devNull.toString().trim();
                            }
                        } else if (instr instanceof ArithmeticExpression) {
                            argLiterals[i] = ((ArithmeticExpression) instr).getExpressionAsString().trim();
                        } else if (instr instanceof VarOp) {
                            VarOp var = (VarOp) instr;
                            switch (instr.getOpcode()) {
                            case Opcodes.ALOAD: {
                                if (!isStatic() && var.getIndex() == 0) {
                                    argLiterals[i] = "__cn1ThisObject";
                                } else {
                                    argLiterals[i] = "locals[" + var.getIndex() + "].data.o";
                                }
                                break;
                            }
                            case Opcodes.ILOAD: {
                                argLiterals[i] = "ilocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.ACONST_NULL: {
                                argLiterals[i] = "JAVA_NULL";
                                break;
                            }
                            case Opcodes.DLOAD: {
                                argLiterals[i] = "dlocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.FLOAD: {
                                argLiterals[i] = "flocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.LLOAD: {
                                argLiterals[i] = "llocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.ICONST_0: {
                                argLiterals[i] = "0";
                                break;
                            }
                            case Opcodes.ICONST_1: {
                                argLiterals[i] = "1";
                                break;
                            }
                            case Opcodes.ICONST_2: {
                                argLiterals[i] = "2";
                                break;
                            }
                            case Opcodes.ICONST_3: {
                                argLiterals[i] = "3";
                                break;
                            }
                            case Opcodes.ICONST_4: {
                                argLiterals[i] = "4";
                                break;
                            }
                            case Opcodes.ICONST_5: {
                                argLiterals[i] = "5";
                                break;
                            }
                            case Opcodes.ICONST_M1: {
                                argLiterals[i] = "-1";
                                break;
                            }
                            case Opcodes.LCONST_0: {
                                argLiterals[i] = "(JAVA_LONG)0";
                                break;
                            }
                            case Opcodes.LCONST_1: {
                                argLiterals[i] = "(JAVA_LONG)1";
                                break;
                            }
                            case Opcodes.BIPUSH:
                            case Opcodes.SIPUSH: {
                                argLiterals[i] = String.valueOf(var.getIndex());

                                break;
                            }
                            }
                        } else {
                            switch (instr.getOpcode()) {

                            case Opcodes.ACONST_NULL: {
                                argLiterals[i] = "JAVA_NULL";
                                break;
                            }

                            case Opcodes.ICONST_0: {
                                argLiterals[i] = "0";
                                break;
                            }
                            case Opcodes.ICONST_1: {
                                argLiterals[i] = "1";
                                break;
                            }
                            case Opcodes.ICONST_2: {
                                argLiterals[i] = "2";
                                break;
                            }
                            case Opcodes.ICONST_3: {
                                argLiterals[i] = "3";
                                break;
                            }
                            case Opcodes.ICONST_4: {
                                argLiterals[i] = "4";
                                break;
                            }
                            case Opcodes.ICONST_5: {
                                argLiterals[i] = "5";
                                break;
                            }
                            case Opcodes.ICONST_M1: {
                                argLiterals[i] = "-1";
                                break;
                            }
                            case Opcodes.LCONST_0: {
                                argLiterals[i] = "(JAVA_LONG)0";
                                break;
                            }
                            case Opcodes.LCONST_1: {
                                argLiterals[i] = "(JAVA_LONG)1";
                                break;
                            }
                            case Opcodes.BIPUSH: {
                                if (instr instanceof BasicInstruction) {
                                    argLiterals[i] = String.valueOf(((BasicInstruction) instr).getValue());
                                }
                                break;
                            }
                            case Opcodes.LDC: {
                                if (instr instanceof Ldc) {
                                    Ldc ldc = (Ldc) instr;
                                    argLiterals[i] = ldc.getValueAsString();

                                }
                                break;
                            }

                            }

                        }
                    }

                    // Check to make sure that we have all the args as literals.
                    boolean missingLiteral = false;
                    for (String lit : argLiterals) {
                        if (lit == null) {
                            missingLiteral = true;
                            break;
                        }
                    }

                    // We have all of the arguments as literals.  Let's
                    // add them to our invoke instruction.
                    if (!missingLiteral) {
                        CustomInvoke newInvoke = CustomInvoke.create(inv);
                        instructions.remove(iter);
                        instructions.add(iter, newInvoke);
                        int newIter = iter;
                        for (int i = 0; i < numArgs; i++) {
                            instructions.remove(iter - numArgs);
                            newIter--;
                            newInvoke.setLiteralArg(i, argLiterals[i]);
                        }
                        if (inv.getOpcode() != Opcodes.INVOKESTATIC) {
                            Instruction ldTarget = instructions.get(iter - numArgs - 1);
                            if (ldTarget instanceof AssignableExpression) {
                                StringBuilder targetExprStr = new StringBuilder();
                                if (((AssignableExpression) ldTarget).assignTo(null, targetExprStr)) {
                                    newInvoke.setTargetObjectLiteral(targetExprStr.toString().trim());
                                    instructions.remove(iter - numArgs - 1);
                                    newIter--;

                                }

                            } else if (ldTarget instanceof CustomInvoke) {
                                // WE Can't pass a custom invoke as the target directly
                                // because it the return value needs to be added to the 
                                // stack for the GC
                            } else {
                                switch (ldTarget.getOpcode()) {
                                case Opcodes.ALOAD: {
                                    VarOp v = (VarOp) ldTarget;
                                    if (isStatic() && v.getIndex() == 0) {
                                        newInvoke.setTargetObjectLiteral("__cn1ThisObject");
                                    } else {
                                        newInvoke.setTargetObjectLiteral("locals[" + v.getIndex() + "].data.o");
                                    }
                                    instructions.remove(iter - numArgs - 1);
                                    newIter--;
                                    break;
                                }
                                }
                            }
                        }

                        newInvoke.setOptimized(true);
                        //iter = 0;
                        instructionCount = instructions.size();
                        iter = newIter;

                    }
                }
            }
            break;
        }

        }
        astoreCalls = astoreCalls || currentOpcode == Opcodes.ASTORE || currentOpcode == Opcodes.ISTORE
                || currentOpcode == Opcodes.LSTORE || currentOpcode == Opcodes.DSTORE
                || currentOpcode == Opcodes.FSTORE;

        hasInstructions = hasInstructions | current.getOpcode() != -1;
    }
    return hasInstructions;
}

From source file:com.codename1.tools.translator.bytecodes.BasicInstruction.java

License:Open Source License

@Override
public void appendInstruction(StringBuilder b, List<Instruction> instructions) {
    switch (opcode) {
    case Opcodes.NOP:
        break;// w  w  w.ja v  a2  s .c  o  m

    case Opcodes.ACONST_NULL:
        b.append("    PUSH_POINTER(JAVA_NULL); /* ACONST_NULL */\n");
        break;

    case Opcodes.ICONST_M1:
        b.append("    PUSH_INT(-1); /* ICONST_M1 */\n");
        break;

    case Opcodes.ICONST_0:
        b.append("    PUSH_INT(0); /* ICONST_0 */\n");
        break;

    case Opcodes.ICONST_1:
        b.append("    PUSH_INT(1); /* ICONST_1 */\n");
        break;

    case Opcodes.ICONST_2:
        b.append("    PUSH_INT(2); /* ICONST_2 */\n");
        break;

    case Opcodes.ICONST_3:
        b.append("    PUSH_INT(3); /* ICONST_3 */\n");
        break;

    case Opcodes.ICONST_4:
        b.append("    PUSH_INT(4); /* ICONST_4 */\n");
        break;

    case Opcodes.ICONST_5:
        b.append("    PUSH_INT(5); /* ICONST_5 */\n");
        break;

    case Opcodes.LCONST_0:
        b.append("    PUSH_LONG(0); /* LCONST_0 */\n");
        break;

    case Opcodes.LCONST_1:
        b.append("    PUSH_LONG(1); /* LCONST_1 */\n");
        break;

    case Opcodes.FCONST_0:
        b.append("    PUSH_FLOAT(0); /* FCONST_0 */\n");
        break;

    case Opcodes.FCONST_1:
        b.append("    PUSH_FLOAT(1); /* FCONST_1 */\n");
        break;

    case Opcodes.FCONST_2:
        b.append("    PUSH_FLOAT(2); /* FCONST_2 */\n");
        break;

    case Opcodes.DCONST_0:
        b.append("    PUSH_DOUBLE(0); /* DCONST_0 */\n");
        break;

    case Opcodes.DCONST_1:
        b.append("    PUSH_DOUBLE(1); /* DCONST_1 */\n");
        break;

    case Opcodes.BALOAD:
        b.append("    { CHECK_ARRAY_ACCESS(2, SP[-1].data.i); /* BALOAD */ \n"
                + "    SP--; SP[-1].type = CN1_TYPE_INT; \n"
                + "    SP[-1].data.i = ((JAVA_ARRAY_BYTE*) (*(JAVA_ARRAY)SP[-1].data.o).data)[(*SP).data.i]; \n"
                + "    }\n");
        break;

    case Opcodes.CALOAD:
        b.append("    CHECK_ARRAY_ACCESS(2, SP[-1].data.i); /* CALOAD */\n"
                + "    SP--; SP[-1].type = CN1_TYPE_INT; \n"
                + "    SP[-1].data.i = ((JAVA_ARRAY_CHAR*) (*(JAVA_ARRAY)SP[-1].data.o).data)[(*SP).data.i];\n");
        break;

    case Opcodes.IALOAD:
        b.append("    CHECK_ARRAY_ACCESS(2, SP[-1].data.i); /* IALOAD */\n"
                + "    SP--; SP[-1].type = CN1_TYPE_INT; \n"
                + "    SP[-1].data.i = ((JAVA_ARRAY_INT*) (*(JAVA_ARRAY)SP[-1].data.o).data)[(*SP).data.i];\n");
        break;

    case Opcodes.SALOAD:
        b.append("    CHECK_ARRAY_ACCESS(2, SP[-1].data.i); \n" + "    SP--; SP[-1].type = CN1_TYPE_INT; \n"
                + "    SP[-1].data.i = ((JAVA_ARRAY_SHORT*) (*(JAVA_ARRAY)SP[-1].data.o).data)[(*SP).data.i]; /* SALOAD */\n");
        break;

    case Opcodes.LALOAD:
        b.append("    CHECK_ARRAY_ACCESS(2, SP[-1].data.i); /* LALOAD */\n"
                + "    SP--; SP[-1].type = CN1_TYPE_LONG; \n"
                + "    SP[-1].data.l = LONG_ARRAY_LOOKUP((JAVA_ARRAY)SP[-1].data.o, (*SP).data.i);\n");
        break;

    case Opcodes.FALOAD:
        b.append("    CHECK_ARRAY_ACCESS(2, SP[-1].data.i); /* FALOAD */\n"
                + "    SP--; SP[-1].type = CN1_TYPE_FLOAT; \n"
                + "    SP[-1].data.f = FLOAT_ARRAY_LOOKUP((JAVA_ARRAY)SP[-1].data.o, (*SP).data.i);\n");
        break;

    case Opcodes.DALOAD:
        b.append("    CHECK_ARRAY_ACCESS(2, SP[-1].data.i); /* DALOAD */\n"
                + "    SP--; SP[-1].type = CN1_TYPE_DOUBLE; \n"
                + "    SP[-1].data.d = DOUBLE_ARRAY_LOOKUP((JAVA_ARRAY)SP[-1].data.o, (*SP).data.i);\n");
        break;

    case Opcodes.AALOAD:
        b.append("    CHECK_ARRAY_ACCESS(2, SP[-1].data.i); /* AALOAD */\n"
                + "    SP--; SP[-1].type = CN1_TYPE_INVALID; \n"
                + "    SP[-1].data.o = ((JAVA_ARRAY_OBJECT*) (*(JAVA_ARRAY)SP[-1].data.o).data)[(*SP).data.i]; \n"
                + "    SP[-1].type = CN1_TYPE_OBJECT; \n");
        break;

    case Opcodes.BASTORE:
        b.append("    CHECK_ARRAY_ACCESS(3, SP[-2].data.i); /* BASTORE */\n"
                + "    ((JAVA_ARRAY_BYTE*) (*(JAVA_ARRAY)SP[-3].data.o).data)[SP[-2].data.i] = SP[-1].data.i; SP -= 3;\n");
        break;

    case Opcodes.CASTORE:
        b.append("    CHECK_ARRAY_ACCESS(3, SP[-2].data.i); /* CASTORE */\n"
                + "    ((JAVA_ARRAY_CHAR*) (*(JAVA_ARRAY)SP[-3].data.o).data)[SP[-2].data.i] = SP[-1].data.i; SP -= 3;\n\n");
        break;

    case Opcodes.SASTORE:
        b.append("    CHECK_ARRAY_ACCESS(3, SP[-2].data.i); /* SASTORE */\n"
                + "    ((JAVA_ARRAY_SHORT*) (*(JAVA_ARRAY)SP[-3].data.o).data)[SP[-2].data.i] = SP[-1].data.i; SP -= 3;\n");
        break;

    case Opcodes.IASTORE:
        b.append("    CHECK_ARRAY_ACCESS(3, SP[-2].data.i); /* IASTORE */\n"
                + "    ((JAVA_ARRAY_INT*) (*(JAVA_ARRAY)SP[-3].data.o).data)[SP[-2].data.i] = SP[-1].data.i; SP -= 3;\n");
        break;

    case Opcodes.LASTORE:
        b.append("    CHECK_ARRAY_ACCESS(3, SP[-2].data.i); /* LASTORE */\n"
                + "    LONG_ARRAY_LOOKUP((JAVA_ARRAY)SP[-3].data.o, SP[-2].data.i) = SP[-1].data.l; SP -= 3;\n");
        break;

    case Opcodes.FASTORE:
        b.append("    CHECK_ARRAY_ACCESS(3, SP[-2].data.i); /* FASTORE */\n"
                + "    FLOAT_ARRAY_LOOKUP((JAVA_ARRAY)SP[-3].data.o, SP[-2].data.i) = SP[-1].data.f; SP -= 3;\n");
        break;

    case Opcodes.DASTORE:
        b.append("    CHECK_ARRAY_ACCESS(3, SP[-2].data.i); /* DASTORE */\n"
                + "    DOUBLE_ARRAY_LOOKUP((JAVA_ARRAY)SP[-3].data.o, SP[-2].data.i) = SP[-1].data.d; SP -= 3;\n");
        break;

    case Opcodes.AASTORE:
        b.append("    CHECK_ARRAY_ACCESS(3, SP[-2].data.i); { /* BC_AASTORE */\n"
                + "    JAVA_OBJECT aastoreTmp = SP[-3].data.o; \n"
                + "    ((JAVA_ARRAY_OBJECT*) (*(JAVA_ARRAY)aastoreTmp).data)[SP[-2].data.i] = SP[-1].data.o; \n"
                + "    SP -= 3; }\n");
        break;

    case Opcodes.POP:
        b.append("    SP--; /* POP */\n");
        break;

    case Opcodes.POP2:
        b.append("    popMany(threadStateData, 2, &SP); /* POP2 */\n");
        break;

    /*case Opcodes.DUP:
        b.append("    PUSH_INT(PEEK_INT(1));\n");
        break;
            
    case Opcodes.DUP_X1:
        b.append("    DUP_X1();\n");
        break;
                
    case Opcodes.DUP_X2:
        b.append("    DUP_X2();\n");
        break;*/

    case Opcodes.DUP:
        b.append("    BC_DUP(); /* DUP */\n");
        break;

    case Opcodes.DUP2:
        b.append("    BC_DUP2(); /* DUP2 */\n");
        break;

    case Opcodes.DUP_X1:
        b.append("    BC_DUP2_X1(); /* DUP_X1 */\n");
        break;

    case Opcodes.DUP2_X1:
        b.append("    BC_DUP2_X1(); /* DUP2_X1 */\n");
        break;

    case Opcodes.DUP_X2:
        b.append("    BC_DUP2_X2(); /* DUP_X2 */\n");
        break;

    case Opcodes.DUP2_X2:
        b.append("    BC_DUP2_X2(); /* DUP2_X2 */\n");
        break;

    case Opcodes.SWAP:
        b.append("    swapStack(SP); /* SWAP */\n");
        break;

    case Opcodes.IADD:
        b.append("    SP--; SP[-1].data.i = SP[-1].data.i + (*SP).data.i; /* IADD */\n");
        break;

    case Opcodes.LADD:
        b.append("    SP--; SP[-1].data.l = SP[-1].data.l + (*SP).data.l; /* LADD */\n");
        break;

    case Opcodes.FADD:
        b.append("    SP--; SP[-1].data.f = SP[-1].data.f + (*SP).data.f; /* FADD */\n");
        break;

    case Opcodes.DADD:
        b.append("    SP--; SP[-1].data.d = SP[-1].data.d + (*SP).data.d; /* DADD */\n");
        break;

    case Opcodes.ISUB:
        b.append("    SP--; SP[-1].data.i = (SP[-1].data.i - (*SP).data.i); /* ISUB */\n");
        break;

    case Opcodes.LSUB:
        b.append("    SP--; SP[-1].data.l = (SP[-1].data.l - (*SP).data.l); /* LSUB */\n");
        break;

    case Opcodes.FSUB:
        b.append("    SP--; SP[-1].data.f = (SP[-1].data.f - (*SP).data.f); /* FSUB */\n");
        break;

    case Opcodes.DSUB:
        b.append("    SP--; SP[-1].data.d = (SP[-1].data.d - (*SP).data.d); /* DSUB */\n");
        break;

    case Opcodes.IMUL:
        b.append("    SP--; SP[-1].data.i = SP[-1].data.i * (*SP).data.i; /* IMUL */\n");
        break;

    case Opcodes.LMUL:
        b.append("    SP--; SP[-1].data.l = SP[-1].data.l * (*SP).data.l; /* LMUL */\n");
        break;

    case Opcodes.FMUL:
        b.append("    SP--; SP[-1].data.f = SP[-1].data.f * (*SP).data.f; /* FMUL */\n");
        break;

    case Opcodes.DMUL:
        b.append("    SP--; SP[-1].data.d = SP[-1].data.d * (*SP).data.d; /* DMUL */\n");
        break;

    case Opcodes.IDIV:
        b.append("    SP--; SP[-1].data.i = SP[-1].data.i / (*SP).data.i; /* IDIV */\n");
        break;

    case Opcodes.LDIV:
        b.append("    SP--; SP[-1].data.l = SP[-1].data.l / (*SP).data.l; /* LDIV */\n");
        break;

    case Opcodes.FDIV:
        b.append("    SP--; SP[-1].data.f = SP[-1].data.f / (*SP).data.f; /* FDIV */\n");
        break;

    case Opcodes.DDIV:
        b.append("    SP--; SP[-1].data.d = SP[-1].data.d / (*SP).data.d; /* DDIV */\n");
        break;

    case Opcodes.IREM:
        b.append("    SP--; SP[-1].data.i = SP[-1].data.i % (*SP).data.i; /* IREM */\n");
        break;

    case Opcodes.LREM:
        b.append("    SP--; SP[-1].data.l = SP[-1].data.l % (*SP).data.l; /* LREM */\n");
        break;

    case Opcodes.FREM:
        b.append("    SP--; SP[-1].data.f = fmod(SP[-1].data.f, (*SP).data.f); /* FREM */\n");
        break;

    case Opcodes.DREM:
        b.append("    SP--; SP[-1].data.d = fmod(SP[-1].data.d, (*SP).data.d); /* DREM */\n");
        break;

    case Opcodes.INEG:
        b.append("    SP[-1].data.i *= -1; /* INEG */\n");
        break;

    case Opcodes.LNEG:
        b.append("    SP[-1].data.l *= -1; /* LNEG */\n");
        break;

    case Opcodes.FNEG:
        b.append("    SP[-1].data.f *= -1; /* FNEG */\n");
        break;

    case Opcodes.DNEG:
        b.append("    SP[-1].data.d *= -1; /* DNEG */\n");
        break;

    case Opcodes.ISHL:
        b.append("    SP--; SP[-1].data.i = (SP[-1].data.i << (0x1f & (*SP).data.i)); /* ISHL */\n");
        break;

    case Opcodes.LSHL:
        b.append("    SP--; SP[-1].data.l = (SP[-1].data.l << (0x3f & (*SP).data.i)); /* LSHL */\n");
        break;

    case Opcodes.ISHR:
        b.append("    SP--; SP[-1].data.i = (SP[-1].data.i >> (0x1f & (*SP).data.i)); /* ISHR */\n");
        break;

    case Opcodes.LSHR:
        b.append("    SP--; SP[-1].data.l = (SP[-1].data.l >> (0x3f & (*SP).data.l)); /* LSHR */\n");
        break;

    case Opcodes.IUSHR:
        b.append(
                "    SP--; SP[-1].data.i = (((unsigned int)SP[-1].data.i) >> (0x1f & ((unsigned int)(*SP).data.i))); /* IUSHR */\n");
        break;

    case Opcodes.LUSHR:
        b.append(
                "    SP--; SP[-1].data.l = (((unsigned long long)SP[-1].data.l) >> (0x3f & ((unsigned long long)(*SP).data.i))); /* LUSHR */\n");
        break;

    case Opcodes.IAND:
        b.append("    SP--; SP[-1].data.i = SP[-1].data.i & (*SP).data.i; /* IAND */\n");
        break;

    case Opcodes.LAND:
        b.append("    SP--; SP[-1].data.l = SP[-1].data.l & (*SP).data.l; /* LAND */\n");
        break;

    case Opcodes.IOR:
        b.append("    SP--; SP[-1].data.i = SP[-1].data.i | (*SP).data.i; /* IOR */\n");
        break;

    case Opcodes.LOR:
        b.append("    SP--; SP[-1].data.l = SP[-1].data.l | (*SP).data.l; /* LOR */\n");
        break;

    case Opcodes.IXOR:
        b.append("    SP--; SP[-1].data.i = SP[-1].data.i ^ (*SP).data.i; /* IXOR */\n");
        break;

    case Opcodes.LXOR:
        b.append("    SP--; SP[-1].data.l = SP[-1].data.l ^ (*SP).data.l; /* LXOR */\n");
        break;

    case Opcodes.I2L:
        b.append("    SP[-1].data.l = SP[-1].data.i; /* I2L */\n");
        break;

    case Opcodes.I2F:
        b.append("    SP[-1].data.f = (JAVA_FLOAT)SP[-1].data.i; /* I2F */\n");
        break;

    case Opcodes.I2D:
        b.append("    SP[-1].data.d = SP[-1].data.i; /* I2D */;\n");
        break;

    case Opcodes.L2I:
        b.append("    SP[-1].data.i = (JAVA_INT)SP[-1].data.l; /* L2I */\n");
        break;

    case Opcodes.L2F:
        b.append("    SP[-1].data.f = (JAVA_FLOAT)SP[-1].data.l; /* L2F */\n");
        break;

    case Opcodes.L2D:
        b.append("    SP[-1].data.d = (JAVA_DOUBLE)SP[-1].data.l; /* L2D */\n");
        break;

    case Opcodes.F2I:
        b.append("    SP[-1].data.i = (JAVA_INT)SP[-1].data.f; /* F2I */\n");
        break;

    case Opcodes.F2L:
        b.append("    SP[-1].data.l = (JAVA_LONG)SP[-1].data.f; /* F2L */\n");
        break;

    case Opcodes.F2D:
        b.append("    SP[-1].data.d = SP[-1].data.f; /* F2D */\n");
        break;

    case Opcodes.D2I:
        b.append("    SP[-1].data.i = (JAVA_INT)SP[-1].data.d; /* D2I */\n");
        break;

    case Opcodes.D2L:
        b.append("    SP[-1].data.l = (JAVA_LONG)SP[-1].data.d; /* D2L */\n");
        break;

    case Opcodes.D2F:
        b.append("    SP[-1].data.f = (JAVA_FLOAT)SP[-1].data.d; /* D2F */\n");
        break;

    case Opcodes.I2B:
        b.append("    SP[-1].data.i = ((SP[-1].data.i << 24) >> 24); /* I2B */\n");
        break;

    case Opcodes.I2C:
        b.append("    SP[-1].data.i = (SP[-1].data.i & 0xffff); /* I2C */\n");
        break;

    case Opcodes.I2S:
        b.append("    SP[-1].data.i = ((SP[-1].data.i << 16) >> 16); /* I2S */\n");
        break;

    case Opcodes.LCMP:
        b.append("    BC_LCMP();\n");
        break;

    case Opcodes.FCMPG:
    case Opcodes.FCMPL:
        b.append("    BC_FCMPL();\n");
        break;

    case Opcodes.DCMPL:
    case Opcodes.DCMPG:
        b.append("    BC_DCMPL();\n");
        break;

    case Opcodes.IRETURN:
        appendSynchronized(b);

        if (TryCatch.isTryCatchInMethod()) {
            b.append(
                    "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return SP[-1].data.i;\n");
            //                    b.append(maxLocals);
            //                    b.append(", stack, locals, methodBlockOffset); \n    return SP[-1].data.i;\n");
        } else {
            b.append("    releaseForReturn(threadStateData, cn1LocalsBeginInThread); return SP[-1].data.i;\n");
            //                    b.append(maxLocals);
            //                    b.append(", stack, locals); \n    return SP[-1].data.i;\n");
        }
        break;

    case Opcodes.LRETURN:
        appendSynchronized(b);

        if (TryCatch.isTryCatchInMethod()) {
            b.append(
                    "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); \n    return POP_LONG();\n");
        } else {
            b.append(
                    "    releaseForReturn(threadStateData, cn1LocalsBeginInThread); \n    return POP_LONG();\n");
        }
        break;

    case Opcodes.FRETURN:
        appendSynchronized(b);

        if (TryCatch.isTryCatchInMethod()) {
            b.append(
                    "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); \n    return POP_FLOAT();\n");
        } else {
            b.append(
                    "    releaseForReturn(threadStateData, cn1LocalsBeginInThread); \n    return POP_FLOAT();\n");
        }
        break;

    case Opcodes.DRETURN:
        appendSynchronized(b);

        if (TryCatch.isTryCatchInMethod()) {
            b.append(
                    "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); \n    return POP_DOUBLE();\n");
        } else {
            b.append(
                    "    releaseForReturn(threadStateData, cn1LocalsBeginInThread); \n    return POP_DOUBLE();\n");
        }
        break;

    case Opcodes.ARETURN:
        appendSynchronized(b);

        if (TryCatch.isTryCatchInMethod()) {
            b.append(
                    "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); \n    return POP_OBJ();\n");
        } else {
            b.append(
                    "    releaseForReturn(threadStateData, cn1LocalsBeginInThread); \n    return POP_OBJ();\n");
        }
        break;

    case Opcodes.RETURN:
        appendSynchronized(b);

        if (!hasInstructions) {
            b.append("    return;\n");
            break;
        }
        if (TryCatch.isTryCatchInMethod()) {
            b.append(
                    "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); \n    return;\n");
        } else {
            b.append("    releaseForReturn(threadStateData, cn1LocalsBeginInThread); \n    return;\n");
        }
        break;

    case Opcodes.ARRAYLENGTH:
        b.append("    { /* ARRAYLENGTH */\n" + "        if(SP[-1].data.o == JAVA_NULL) { \n"
                + "            throwException(threadStateData, __NEW_INSTANCE_java_lang_NullPointerException(threadStateData)); \n"
                + "        }; \n" + "        SP[-1].type = CN1_TYPE_INT; \n"
                + "        SP[-1].data.i = (*((JAVA_ARRAY)SP[-1].data.o)).length; \n" + "    }\n");
        break;

    case Opcodes.ATHROW:
        //b.append("    NSLog(@\"Exception thrown %s %d %s %s\\n\", __FILE__, __LINE__, __PRETTY_FUNCTION__, __FUNCTION__);\n");
        b.append("    throwException(threadStateData, POP_OBJ());\n");
        break;

    case Opcodes.MONITORENTER:
        b.append("    monitorEnter(threadStateData, POP_OBJ());\n");
        break;

    case Opcodes.MONITOREXIT:
        b.append("    monitorExit(threadStateData, POP_OBJ());\n");
        break;

    // int instructions
    case Opcodes.SIPUSH:
    case Opcodes.BIPUSH:
        b.append("    PUSH_INT(");
        b.append(value);
        b.append(");\n");
        break;

    case Opcodes.NEWARRAY:
        switch (value) {
        case 4: // boolean
            b.append(
                    "    SP--; PUSH_OBJ(allocArray(threadStateData, (*SP).data.i, &class_array1__JAVA_BOOLEAN, sizeof(JAVA_ARRAY_BOOLEAN), 1));\n");
            break;
        case 5: // char
            b.append(
                    "    SP--; PUSH_OBJ(allocArray(threadStateData, (*SP).data.i, &class_array1__JAVA_CHAR, sizeof(JAVA_ARRAY_CHAR), 1));\n");
            break;
        case 6: // float
            b.append(
                    "    SP--; PUSH_OBJ(allocArray(threadStateData, (*SP).data.i, &class_array1__JAVA_FLOAT, sizeof(JAVA_ARRAY_FLOAT), 1));\n");
            break;
        case 7: // double
            b.append(
                    "    SP--; PUSH_OBJ(allocArray(threadStateData, (*SP).data.i, &class_array1__JAVA_DOUBLE, sizeof(JAVA_ARRAY_DOUBLE), 1));\n");
            break;
        case 8: // byte
            b.append(
                    "    SP--; PUSH_OBJ(allocArray(threadStateData, (*SP).data.i, &class_array1__JAVA_BYTE, sizeof(JAVA_ARRAY_BYTE), 1));\n");
            break;
        case 9: // short
            b.append(
                    "    SP--; PUSH_OBJ(allocArray(threadStateData, (*SP).data.i, &class_array1__JAVA_SHORT, sizeof(JAVA_ARRAY_SHORT), 1));\n");
            break;
        case 10: // int
            b.append(
                    "    SP--; PUSH_OBJ(allocArray(threadStateData, (*SP).data.i, &class_array1__JAVA_INT, sizeof(JAVA_ARRAY_INT), 1));\n");
            break;
        case 11: // long 
            b.append(
                    "    SP--; PUSH_OBJ(allocArray(threadStateData, (*SP).data.i, &class_array1__JAVA_LONG, sizeof(JAVA_ARRAY_LONG), 1));\n");
            break;
        }
        break;
    }
}

From source file:com.codename1.tools.translator.Util.java

License:Open Source License

public static char[] getStackInputTypes(Instruction instr) {
    char[] out = instr.getStackInputTypes();
    if (out != null) {
        return out;
    }/*from   w  w w. j  a v  a2 s .c om*/

    switch (instr.getOpcode()) {

    case Opcodes.NOP:
    case Opcodes.ACONST_NULL:
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
    case Opcodes.SIPUSH:
    case Opcodes.BIPUSH:
        return new char[0];
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.IALOAD:
    case Opcodes.SALOAD:
    case Opcodes.LALOAD:
    case Opcodes.FALOAD:
    case Opcodes.DALOAD:
    case Opcodes.AALOAD:
        return new char[] { 'i', 'o' };

    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
    case Opcodes.IASTORE:
        return new char[] { 'i', 'i', 'o' };
    case Opcodes.LASTORE:
        return new char[] { 'l', 'i', 'o' };
    case Opcodes.FASTORE:
        return new char[] { 'f', 'i', 'o' };
    case Opcodes.DASTORE:
        return new char[] { 'd', 'i', 'o' };
    case Opcodes.AASTORE:
        return new char[] { 'o', 'i', 'o' };

    case Opcodes.POP:
        return new char[] { '*' };

    case Opcodes.POP2:
        return new char[] { '*', '*' };

    case Opcodes.DUP:
        return new char[] { '0' };

    case Opcodes.DUP2:
    case Opcodes.DUP_X2:
    case Opcodes.DUP2_X2:
        return null; // DUP2 depends on the types on the stack so we don't statically know the input types

    case Opcodes.DUP_X1:
    case Opcodes.DUP2_X1:
        return new char[] { '0', '1' };
    case Opcodes.SWAP:
        return new char[] { '0', '1' };
    case Opcodes.IADD:
    case Opcodes.ISUB:
    case Opcodes.IMUL:
    case Opcodes.IDIV:
    case Opcodes.IREM:
    case Opcodes.ISHL:
    case Opcodes.ISHR:
    case Opcodes.IUSHR:
    case Opcodes.IAND:
    case Opcodes.IOR:
    case Opcodes.IXOR:
        return new char[] { 'i', 'i' };

    case Opcodes.LADD:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
    case Opcodes.LDIV:
    case Opcodes.LREM:
    case Opcodes.LSHL:
    case Opcodes.LSHR:
    case Opcodes.LAND:
    case Opcodes.LOR:
    case Opcodes.LXOR:
    case Opcodes.LCMP:
        return new char[] { 'l', 'l' };
    case Opcodes.FADD:
    case Opcodes.FSUB:
    case Opcodes.FMUL:
    case Opcodes.FDIV:
    case Opcodes.FREM:
    case Opcodes.FCMPG:
    case Opcodes.FCMPL:
        return new char[] { 'f', 'f' };
    case Opcodes.DADD:
    case Opcodes.DSUB:
    case Opcodes.DMUL:
    case Opcodes.DDIV:
    case Opcodes.DREM:
    case Opcodes.DCMPL:
    case Opcodes.DCMPG:
        return new char[] { 'd', 'd' };

    case Opcodes.INEG:
    case Opcodes.I2L:
    case Opcodes.I2F:
    case Opcodes.I2D:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
    case Opcodes.NEWARRAY:
        return new char[] { 'i' };
    case Opcodes.LNEG:
    case Opcodes.L2I:
    case Opcodes.L2F:
    case Opcodes.L2D:
        return new char[] { 'l' };

    case Opcodes.FNEG:
    case Opcodes.F2I:
    case Opcodes.F2L:
    case Opcodes.F2D:
        return new char[] { 'f' };
    case Opcodes.DNEG:
    case Opcodes.D2I:
    case Opcodes.D2L:
    case Opcodes.D2F:
        return new char[] { 'd' };
    case Opcodes.LUSHR:
        return new char[] { 'i', 'l' };

    case Opcodes.ARRAYLENGTH:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
    case Opcodes.ATHROW:
        return new char[] { 'o' };
    default:
        return null;

    }

}

From source file:com.codename1.tools.translator.Util.java

License:Open Source License

public static char[] getStackOutputTypes(Instruction instr) {
    char[] out = instr.getStackOutputTypes();
    if (out != null) {
        return out;
    }// w ww .j av a2 s.c  o m

    switch (instr.getOpcode()) {
    case Opcodes.NOP:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
    case Opcodes.IASTORE:
    case Opcodes.LASTORE:
    case Opcodes.FASTORE:
    case Opcodes.DASTORE:
    case Opcodes.AASTORE:
    case Opcodes.POP:
    case Opcodes.POP2:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
    case Opcodes.ATHROW:
        return new char[0];

    case Opcodes.ACONST_NULL:
    case Opcodes.AALOAD:
    case Opcodes.NEWARRAY:
        return new char[] { 'o' };

    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:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.IALOAD:
    case Opcodes.SALOAD:
    case Opcodes.IADD:
    case Opcodes.ISUB:
    case Opcodes.IMUL:
    case Opcodes.IDIV:
    case Opcodes.IREM:
    case Opcodes.INEG:
    case Opcodes.ISHL:
    case Opcodes.ISHR:
    case Opcodes.IUSHR:
    case Opcodes.IAND:
    case Opcodes.IOR:
    case Opcodes.IXOR:
    case Opcodes.F2I:
    case Opcodes.D2I:
    case Opcodes.L2I:
    case Opcodes.I2C:
    case Opcodes.I2S:
    case Opcodes.LCMP:
    case Opcodes.FCMPG:
    case Opcodes.FCMPL:
    case Opcodes.DCMPL:
    case Opcodes.DCMPG:
    case Opcodes.ARRAYLENGTH:
    case Opcodes.SIPUSH:
    case Opcodes.BIPUSH:
        return new char[] { 'i' };
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
    case Opcodes.LALOAD:
    case Opcodes.LADD:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
    case Opcodes.LDIV:
    case Opcodes.LREM:
    case Opcodes.LNEG:
    case Opcodes.LSHL:
    case Opcodes.LSHR:
    case Opcodes.LUSHR:
    case Opcodes.LAND:
    case Opcodes.LOR:
    case Opcodes.LXOR:
    case Opcodes.I2L:
    case Opcodes.F2L:
    case Opcodes.D2L:
        return new char[] { 'l' };
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
    case Opcodes.FALOAD:
    case Opcodes.FADD:
    case Opcodes.FSUB:
    case Opcodes.FMUL:
    case Opcodes.FDIV:
    case Opcodes.FREM:
    case Opcodes.FNEG:
    case Opcodes.I2F:
    case Opcodes.D2F:
    case Opcodes.L2F:
        return new char[] { 'f' };
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
    case Opcodes.DALOAD:
    case Opcodes.DADD:
    case Opcodes.DSUB:
    case Opcodes.DMUL:
    case Opcodes.DDIV:
    case Opcodes.DREM:
    case Opcodes.DNEG:
    case Opcodes.I2D:
    case Opcodes.F2D:
    case Opcodes.L2D:
        return new char[] { 'd' };
    case Opcodes.DUP:
        return new char[] { '0', '0' };

    case Opcodes.DUP2:
    case Opcodes.DUP_X2:
    case Opcodes.DUP2_X2:
        return null;

    case Opcodes.DUP_X1:
    case Opcodes.DUP2_X1:
        return new char[] { '0', '1', '0' };
    case Opcodes.SWAP:
        return new char[] { '1', '0' };

    default:
        return null;
    }

}

From source file:com.github.anba.es6draft.compiler.assembler.InstructionAssembler.java

License:Open Source License

public final void astore(Type type) {
    switch (type.getOpcode(Opcodes.IASTORE)) {
    case Opcodes.IASTORE:
        iastore();/*from   w  ww . ja v a 2  s.com*/
        return;
    case Opcodes.LASTORE:
        lastore();
        return;
    case Opcodes.FASTORE:
        fastore();
        return;
    case Opcodes.DASTORE:
        dastore();
        return;
    case Opcodes.AASTORE:
        aastore();
        return;
    case Opcodes.BASTORE:
        bastore();
        return;
    case Opcodes.CASTORE:
        castore();
        return;
    case Opcodes.SASTORE:
        sastore();
        return;
    default:
        throw new IllegalArgumentException();
    }
}

From source file:com.github.anba.es6draft.compiler.assembler.InstructionAssembler.java

License:Open Source License

public void castore() {
    methodVisitor.visitInsn(Opcodes.CASTORE);
    stack.castore();
}

From source file:com.google.devtools.build.android.desugar.BytecodeTypeInference.java

License:Open Source License

@Override
public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
    case Opcodes.INEG:
    case Opcodes.LNEG:
    case Opcodes.FNEG:
    case Opcodes.DNEG:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
    case Opcodes.RETURN:
        break;//from   w w w  .ja v  a 2  s.  c o m
    case Opcodes.ACONST_NULL:
        push(InferredType.NULL);
        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(InferredType.INT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        push(InferredType.FLOAT);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.IALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD:
        pop(2);
        push(InferredType.INT);
        break;
    case Opcodes.LALOAD:
    case Opcodes.D2L:
        pop(2);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.DALOAD:
    case Opcodes.L2D:
        pop(2);
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.AALOAD:
        InferredType arrayType = pop(2);
        InferredType elementType = arrayType.getElementTypeIfArrayOrThrow();
        push(elementType);
        break;
    case Opcodes.IASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
    case Opcodes.FASTORE:
    case Opcodes.AASTORE:
        pop(3);
        break;
    case Opcodes.LASTORE:
    case Opcodes.DASTORE:
        pop(4);
        break;
    case Opcodes.POP:
    case Opcodes.IRETURN:
    case Opcodes.FRETURN:
    case Opcodes.ARETURN:
    case Opcodes.ATHROW:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
        pop();
        break;
    case Opcodes.POP2:
    case Opcodes.LRETURN:
    case Opcodes.DRETURN:
        pop(2);
        break;
    case Opcodes.DUP:
        push(top());
        break;
    case Opcodes.DUP_X1: {
        InferredType top = pop();
        InferredType next = pop();
        push(top);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP_X2: {
        InferredType top = pop();
        InferredType next = pop();
        InferredType bottom = pop();
        push(top);
        push(bottom);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2: {
        InferredType top = pop();
        InferredType next = pop();
        push(next);
        push(top);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2_X1: {
        InferredType top = pop();
        InferredType next = pop();
        InferredType bottom = pop();
        push(next);
        push(top);
        push(bottom);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2_X2: {
        InferredType t1 = pop();
        InferredType t2 = pop();
        InferredType t3 = pop();
        InferredType t4 = pop();
        push(t2);
        push(t1);
        push(t4);
        push(t3);
        push(t2);
        push(t1);
        break;
    }
    case Opcodes.SWAP: {
        InferredType top = pop();
        InferredType next = pop();
        push(top);
        push(next);
        break;
    }
    case Opcodes.IADD:
    case Opcodes.ISUB:
    case Opcodes.IMUL:
    case Opcodes.IDIV:
    case Opcodes.IREM:
    case Opcodes.ISHL:
    case Opcodes.ISHR:
    case Opcodes.IUSHR:
    case Opcodes.IAND:
    case Opcodes.IOR:
    case Opcodes.IXOR:
    case Opcodes.L2I:
    case Opcodes.D2I:
    case Opcodes.FCMPL:
    case Opcodes.FCMPG:
        pop(2);
        push(InferredType.INT);
        break;

    case Opcodes.LADD:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
    case Opcodes.LDIV:
    case Opcodes.LREM:
    case Opcodes.LAND:
    case Opcodes.LOR:
    case Opcodes.LXOR:
        pop(4);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;

    case Opcodes.LSHL:
    case Opcodes.LSHR:
    case Opcodes.LUSHR:
        pop(3);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.I2L:
    case Opcodes.F2L:
        pop();
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.I2F:
        pop();
        push(InferredType.FLOAT);
        break;

    case Opcodes.LCMP:
    case Opcodes.DCMPG:
    case Opcodes.DCMPL:
        pop(4);
        push(InferredType.INT);
        break;

    case Opcodes.I2D:
    case Opcodes.F2D:
        pop();
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.F2I:
    case Opcodes.ARRAYLENGTH:
        pop();
        push(InferredType.INT);
        break;
    case Opcodes.FALOAD:
    case Opcodes.FADD:
    case Opcodes.FSUB:
    case Opcodes.FMUL:
    case Opcodes.FDIV:
    case Opcodes.FREM:
    case Opcodes.L2F:
    case Opcodes.D2F:
        pop(2);
        push(InferredType.FLOAT);
        break;

    case Opcodes.DADD:
    case Opcodes.DSUB:
    case Opcodes.DMUL:
    case Opcodes.DDIV:
    case Opcodes.DREM:
        pop(4);
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    default:
        throw new RuntimeException("Unhandled opcode " + opcode);
    }
    super.visitInsn(opcode);
}

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitInsn(final int opcode) {
    switch (opcode) {
    case Opcodes.ACONST_NULL:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Load(lineNumber, new Constant(null, JavaType.OBJECT)));
            }/*from  www  . j av a  2 s.  co  m*/
        });
        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:
        loadConstant(opcode - Opcodes.ICONST_M1 - 1, JavaType.INT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        loadConstant(opcode - Opcodes.LCONST_0, JavaType.LONG);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        loadConstant(opcode - Opcodes.FCONST_0, JavaType.FLOAT);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        loadConstant(opcode - Opcodes.DCONST_0, JavaType.DOUBLE);
        break;
    case Opcodes.IALOAD:
        recordArrayLoad(JavaType.INT);
        break;
    case Opcodes.LALOAD:
        recordArrayLoad(JavaType.LONG);
        break;
    case Opcodes.FALOAD:
        recordArrayLoad(JavaType.FLOAT);
        break;
    case Opcodes.DALOAD:
        recordArrayLoad(JavaType.DOUBLE);
        break;
    case Opcodes.AALOAD:
        recordArrayLoad(JavaType.OBJECT);
        break;
    case Opcodes.BALOAD:
        recordArrayLoad(JavaType.BYTE);
        break;
    case Opcodes.CALOAD:
        recordArrayLoad(JavaType.CHAR);
        break;
    case Opcodes.SALOAD:
        recordArrayLoad(JavaType.SHORT);
        break;

    case Opcodes.IASTORE:
        recordArrayStore(JavaType.INT);
        break;
    case Opcodes.LASTORE:
        recordArrayStore(JavaType.LONG);
        break;
    case Opcodes.FASTORE:
        recordArrayStore(JavaType.FLOAT);
        break;
    case Opcodes.DASTORE:
        recordArrayStore(JavaType.DOUBLE);
        break;
    case Opcodes.AASTORE:
        recordArrayStore(JavaType.OBJECT);
        break;
    case Opcodes.BASTORE:
        recordArrayStore(JavaType.BYTE);
        break;
    case Opcodes.CASTORE:
        recordArrayStore(JavaType.CHAR);
        break;
    case Opcodes.SASTORE:
        recordArrayStore(JavaType.SHORT);
        break;
    case Opcodes.POP:
    case Opcodes.POP2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Pop(lineNumber, opcode - Opcodes.POP + 1));
            }
        });
        break;
    case Opcodes.DUP:
    case Opcodes.DUP_X1:
    case Opcodes.DUP_X2:
        recorder.add(new Runnable() {
            public void run() {
                int offset = opcode - Opcodes.DUP;
                block.addOp(new Duplicate(lineNumber, offset));
            }
        });
        break;
    case Opcodes.DUP2:
    case Opcodes.DUP2_X1:
    case Opcodes.DUP2_X2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Duplicate2(lineNumber, opcode - Opcodes.DUP2));
            }
        });
        break;
    case Opcodes.SWAP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Swap(lineNumber));
            }
        });
        break;
    case Opcodes.IRETURN:
        _return(JavaType.INT);
        break;
    case Opcodes.FRETURN:
        _return(JavaType.FLOAT);
        break;
    case Opcodes.ARETURN:
        _return(JavaType.OBJECT);
        break;
    case Opcodes.LRETURN:
        _return(JavaType.LONG);
        break;
    case Opcodes.DRETURN:
        _return(JavaType.DOUBLE);
        break;
    case Opcodes.ATHROW:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Throw(lineNumber));
            }
        });
        break;
    case Opcodes.RETURN:
        _return(JavaType.VOID);
        break;
    case Opcodes.LCMP:
        operation("cmp", JavaType.LONG, JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.FCMPL:
        operation("cmpl", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.FCMPG:
        operation("cmpg", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.DCMPL:
        operation("cmpl", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.DCMPG:
        operation("cmpg", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.LSHL:
        operation("shl", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LSHR:
        operation("shr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LUSHR:
        operation("ushr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LADD:
        operation("add", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LSUB:
        operation("sub", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LDIV:
        operation("div", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LREM:
        operation("rem", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LAND:
        operation("and", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LOR:
        operation("or", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LXOR:
        operation("xor", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LMUL:
        operation("mul", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.FADD:
        operation("add", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FSUB:
        operation("sub", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FMUL:
        operation("mul", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FREM:
        operation("rem", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FDIV:
        operation("div", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.ISHL:
        operation("shl", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISHR:
        operation("shr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IUSHR:
        operation("ushr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IADD:
        operation("add", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISUB:
        operation("sub", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IMUL:
        operation("mul", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IDIV:
        operation("div", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IREM:
        operation("rem", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IAND:
        operation("and", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IOR:
        operation("or", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IXOR:
        operation("xor", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.DSUB:
        operation("sub", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DADD:
        operation("add", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DMUL:
        operation("mul", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DDIV:
        operation("div", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DREM:
        operation("rem", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.L2I:
        convert(JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.L2F:
        convert(JavaType.LONG, JavaType.FLOAT);
        break;
    case Opcodes.L2D:
        convert(JavaType.LONG, JavaType.DOUBLE);
        break;
    case Opcodes.LNEG:
        operation("neg", JavaType.LONG, null, JavaType.LONG);
        break;
    case Opcodes.F2I:
        convert(JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.F2L:
        convert(JavaType.FLOAT, JavaType.LONG);
        break;
    case Opcodes.FNEG:
        operation("neg", JavaType.FLOAT, null, JavaType.FLOAT);
        break;
    case Opcodes.F2D:
        convert(JavaType.FLOAT, JavaType.DOUBLE);
        break;
    case Opcodes.D2I:
        convert(JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.D2L:
        convert(JavaType.DOUBLE, JavaType.LONG);
        break;
    case Opcodes.D2F:
        convert(JavaType.DOUBLE, JavaType.FLOAT);
        break;
    case Opcodes.DNEG:
        operation("neg", JavaType.DOUBLE, null, JavaType.DOUBLE);
        break;
    case Opcodes.I2L:
        convert(JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.I2F:
        convert(JavaType.INT, JavaType.FLOAT);
        break;
    case Opcodes.I2D:
        convert(JavaType.INT, JavaType.DOUBLE);
        break;
    case Opcodes.I2B:
        convert(JavaType.INT, JavaType.BYTE);
        break;
    case Opcodes.I2C:
        convert(JavaType.INT, JavaType.CHAR);
        break;
    case Opcodes.I2S:
        convert(JavaType.INT, JavaType.SHORT);
        break;
    case Opcodes.INEG:
        operation("neg", JavaType.INT, null, JavaType.INT);
        break;
    case Opcodes.ARRAYLENGTH:
        operation("arraylength", JavaType.OBJECT.toArray(), null, JavaType.INT);
        break;
    case Opcodes.MONITORENTER:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorEnter(lineNumber));
            }
        });
        break;
    case Opcodes.MONITOREXIT:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorExit(lineNumber));
            }
        });
        break;
    case Opcodes.NOP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Transform(lineNumber, "NOP", null, null, null));
            }
        });
    }
}