Example usage for org.objectweb.asm Opcodes INTEGER

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

Introduction

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

Prototype

Integer INTEGER

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

Click Source Link

Usage

From source file:jaspex.speculation.newspec.FlowFrame.java

License:Open Source License

/** Converte lista de tipos no formato do visitFrame em BasicValues **/
private UtilList<BasicValue> convertFromFrame(List<Object> values, boolean locals) {
    UtilList<BasicValue> outList = new UtilArrayList<BasicValue>();

    for (Object o : values) {
        if (o instanceof Integer) {
            Integer i = (Integer) o;
            if (i.equals(Opcodes.TOP)) {
                outList.add(BasicValue.UNINITIALIZED_VALUE);
            } else if (i.equals(Opcodes.INTEGER)) {
                outList.add(BasicValue.INT_VALUE);
            } else if (i.equals(Opcodes.FLOAT)) {
                outList.add(BasicValue.FLOAT_VALUE);
            } else if (i.equals(Opcodes.LONG)) {
                outList.add(BasicValue.LONG_VALUE);
                if (locals)
                    outList.add(BasicValue.UNINITIALIZED_VALUE);
            } else if (i.equals(Opcodes.DOUBLE)) {
                outList.add(BasicValue.DOUBLE_VALUE);
                if (locals)
                    outList.add(BasicValue.UNINITIALIZED_VALUE);
            } else if (i.equals(Opcodes.NULL)) {
                outList.add(BasicValue.REFERENCE_VALUE);
            } else if (i.equals(Opcodes.UNINITIALIZED_THIS)) {
                throw new AssertionError("FIXME");
            } else {
                throw new AssertionError();
            }//from w w w  . j  a v a 2 s.c  om
        } else if (o instanceof String) {
            String s = (String) o;
            outList.add(_interpreter.newValue(Type.getObjectType(s)));
        } else if (o instanceof Label) {
            throw new AssertionError("FIXME");
        } else {
            throw new AssertionError("FIXME");
        }
    }

    return outList;
}

From source file:jaspex.speculation.newspec.FlowFrame.java

License:Open Source License

/** Converte lista de BasicValues no formato usado no visitFrame **/
private static Object[] convertToFrame(UtilList<BasicValue> values, boolean locals) {
    UtilList<Object> outList = new UtilArrayList<Object>();
    int top = 0;/*from  w ww .  j  a  va2  s  .co  m*/

    for (int i = 0; i < values.size(); i++) {
        BasicValue v = values.get(i);

        if (v.equals(BasicValue.UNINITIALIZED_VALUE) && locals) {
            // Os locals so criados logo com o tamanho do MAXLOCALS, mas com
            // UNINITIALIZED_VALUE at serem usados
            // Mesmo assim, podem existir outros locals usados, e temos
            // que introduzir algo na lista para as posies no mudarem

            // Por vezes um UninitializedValue est no lugar de um top
            if (i > 0 && (values.get(i - 1).equals(BasicValue.LONG_VALUE)
                    || values.get(i - 1).equals(BasicValue.DOUBLE_VALUE))) {
                top++;
                continue;
            }
            outList.add("jaspex/HACK/UninitializedValue");
            continue;
        }
        if (v instanceof MergedUninitializedValue) {
            // Normalmente no devia ser deixado um MergedUninitializedValue
            // na stack/locals, mas tal pode acontecer quando no faz diferena
            // nenhuma no bloco (por exemplo porque vai fazer return)
            outList.add("jaspex/HACK/MergedUninitializedValue");
            continue;
        }
        if (v.getType() == null || v.equals(BasicValue.RETURNADDRESS_VALUE)) {
            throw new AssertionError("FIXME");
        }
        Type type = v.getType();
        Object convertedType;
        switch (type.getSort()) {
        case Type.BOOLEAN:
        case Type.BYTE:
        case Type.CHAR:
        case Type.SHORT:
        case Type.INT:
            convertedType = Opcodes.INTEGER;
            break;
        case Type.FLOAT:
            convertedType = Opcodes.FLOAT;
            break;
        case Type.LONG:
            convertedType = Opcodes.LONG;
            break;
        case Type.DOUBLE:
            convertedType = Opcodes.DOUBLE;
            break;
        case Type.ARRAY:
        case Type.OBJECT:
            convertedType = type.getInternalName();
            break;
        default:
            throw new AssertionError();
        }
        outList.add(convertedType);
    }

    assert ((outList.size() + top) == values.size());
    return outList.toArray();
}

From source file:javaone2015.con7442.indyprotector.BootstrapMethodGenerator.java

License:Apache License

/**
 * Generate bootstrap method/*from  ww  w  .  j  a va  2s.  c  o m*/
 */
public void insertMethod(ClassVisitor target) {
    MethodVisitor mv = target.visitMethod(ACC_PRIVATE + ACC_STATIC, BSM_NAME, BSM_SIG, null, null);
    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitInsn(ACONST_NULL);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 4);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;",
            false);
    mv.visitVarInsn(ASTORE, 8);
    mv.visitLdcInsn(Type.getType(targetClassName));
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;", false);
    mv.visitVarInsn(ASTORE, 9);
    mv.visitVarInsn(ALOAD, 6);
    mv.visitVarInsn(ALOAD, 9);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "fromMethodDescriptorString",
            "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/invoke/MethodType;", false);
    mv.visitVarInsn(ASTORE, 10);
    mv.visitVarInsn(ILOAD, 3);
    Label l3 = new Label();
    Label l4 = new Label();
    Label l5 = new Label();
    mv.visitTableSwitchInsn(182, 185, l4, new Label[] { l3, l4, l5, l3 });
    mv.visitLabel(l5);
    mv.visitFrame(Opcodes.F_FULL, 11,
            new Object[] { "java/lang/invoke/MethodHandles$Lookup", "java/lang/String",
                    "java/lang/invoke/MethodType", Opcodes.INTEGER, "java/lang/String", "java/lang/String",
                    "java/lang/String", "java/lang/invoke/MethodHandle", "java/lang/Class",
                    "java/lang/ClassLoader", "java/lang/invoke/MethodType" },
            0, new Object[] {});
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 8);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ALOAD, 10);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findStatic",
            "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;",
            false);
    mv.visitVarInsn(ASTORE, 7);
    Label l6 = new Label();
    mv.visitJumpInsn(GOTO, l6);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 8);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ALOAD, 10);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findVirtual",
            "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;",
            false);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitJumpInsn(GOTO, l6);
    mv.visitLabel(l4);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/BootstrapMethodError");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/BootstrapMethodError", "<init>", "()V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 7);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "asType",
            "(Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitLabel(l1);
    Label l7 = new Label();
    mv.visitJumpInsn(GOTO, l7);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_FULL, 8,
            new Object[] { "java/lang/invoke/MethodHandles$Lookup", "java/lang/String",
                    "java/lang/invoke/MethodType", Opcodes.INTEGER, "java/lang/String", "java/lang/String",
                    "java/lang/String", "java/lang/invoke/MethodHandle" },
            1, new Object[] { "java/lang/Exception" });
    mv.visitVarInsn(ASTORE, 8);
    mv.visitTypeInsn(NEW, "java/lang/BootstrapMethodError");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/BootstrapMethodError", "<init>", "()V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l7);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/invoke/ConstantCallSite");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 7);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/invoke/ConstantCallSite", "<init>",
            "(Ljava/lang/invoke/MethodHandle;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 11);
    mv.visitEnd();

}

From source file:net.sandius.rembulan.compiler.gen.asm.BytecodeEmitVisitor.java

License:Apache License

@Override
public void visit(TabRawAppendMulti node) {
    /*/* ww w.  j a  va  2 s .c  om*/
     In Java terms, we're translating this into the following loop:
            
       Table tab;
       ReturnBuffer rbuf = context.getReturnBuffer();
       int i = 0;
       context.registerTicks(rbuf.size());  // only when we care about ticks spent
       while (i < rbuf.size()) {
    tab.rawset(OFFSET + i, rbuf.get(i));
    i++;
       }
    */

    LabelNode begin = new LabelNode();
    LabelNode end = new LabelNode();
    LabelNode top = new LabelNode();

    int lv_idx_tab = nextLocalVariableIndex();
    int lv_idx_stack = nextLocalVariableIndex() + 1;
    int lv_idx_i = nextLocalVariableIndex() + 2;

    locals.add(new LocalVariableNode("tab", Type.getDescriptor(Table.class), null, begin, end, lv_idx_tab));
    locals.add(new LocalVariableNode("rbuf", Type.getDescriptor(ReturnBuffer.class), null, begin, end,
            lv_idx_stack));
    locals.add(new LocalVariableNode("i", Type.INT_TYPE.getDescriptor(), null, begin, end, lv_idx_i));

    il.add(begin);

    il.add(new VarInsnNode(ALOAD, slot(node.obj())));
    il.add(new TypeInsnNode(CHECKCAST, Type.getInternalName(Table.class)));
    il.add(new VarInsnNode(ASTORE, lv_idx_tab));

    il.add(loadExecutionContext());
    il.add(loadReturnBuffer());
    il.add(new VarInsnNode(ASTORE, lv_idx_stack));

    il.add(ASMUtils.loadInt(0));
    il.add(new VarInsnNode(ISTORE, lv_idx_i));

    // context.registerTicks(rbuf.size());
    if (countingTicks()) {
        il.add(loadExecutionContext());
        il.add(new VarInsnNode(ALOAD, lv_idx_stack));
        il.add(ReturnBufferMethods.size());
        il.add(ExecutionContextMethods.registerTicks());
    }

    il.add(top);
    il.add(new FrameNode(F_APPEND, 3, new Object[] { Type.getInternalName(Table.class),
            Type.getInternalName(ReturnBuffer.class), Opcodes.INTEGER }, 0, null));

    il.add(new VarInsnNode(ILOAD, lv_idx_i));
    il.add(new VarInsnNode(ALOAD, lv_idx_stack));
    il.add(ReturnBufferMethods.size());
    il.add(new JumpInsnNode(IF_ICMPGE, end));

    il.add(new VarInsnNode(ALOAD, lv_idx_tab));

    // OFFSET + i
    il.add(ASMUtils.loadLong(node.firstIdx()));
    il.add(new VarInsnNode(ILOAD, lv_idx_i));
    il.add(new InsnNode(I2L));
    il.add(new InsnNode(LADD));

    // stack.get(i)
    il.add(new VarInsnNode(ALOAD, lv_idx_stack));
    il.add(new VarInsnNode(ILOAD, lv_idx_i));
    il.add(ReturnBufferMethods.get());

    // tab.rawset(offset + i, stack.get(i))
    il.add(TableMethods.rawset_int());

    // increment i
    il.add(new IincInsnNode(lv_idx_i, 1));

    il.add(new JumpInsnNode(GOTO, top));

    il.add(end);
    il.add(new FrameNode(F_CHOP, 3, null, 0, null));
}

From source file:net.sandius.rembulan.compiler.gen.asm.InvokeMethod.java

License:Apache License

public MethodNode methodNode() {
    MethodNode node = new MethodNode(ACC_PUBLIC, "invoke", context.invokeMethodType().getDescriptor(), null,
            runMethod.throwsExceptions());

    InsnList il = node.instructions;/*from   www .  ja v  a  2s.co  m*/
    List<LocalVariableNode> locals = node.localVariables;

    LabelNode begin = new LabelNode();
    LabelNode end = new LabelNode();

    int invokeKind = context.kind();

    il.add(begin);

    // a (slotIdx -> paramIdx) map
    int[] slotParamMap = new int[context.slots.numSlots()];
    Arrays.fill(slotParamMap, -1);

    for (int paramIdx = 0; paramIdx < context.fn.params().size(); paramIdx++) {
        int slotIdx = context.slots.slotOf(context.fn.params().get(paramIdx));
        assert (slotParamMap[slotIdx] == -1);
        slotParamMap[slotIdx] = paramIdx;
    }

    if (invokeKind > 0) {
        il.add(new VarInsnNode(ALOAD, 0)); // this
        il.add(new VarInsnNode(ALOAD, 1)); // context
        il.add(ASMUtils.loadInt(0)); // resumption point

        // we have (invokeKind - 1) standalone parameters, mapping them onto numSlots

        for (int paramIdx : slotParamMap) {
            if (paramIdx < 0) {
                // slot unused
                il.add(new InsnNode(ACONST_NULL));
            } else {
                // used by the parameter #paramIdx
                Var param = context.fn.params().get(paramIdx);
                boolean reified = context.types.isReified(param);

                if (reified) {
                    il.add(new TypeInsnNode(NEW, Type.getInternalName(Variable.class)));
                    il.add(new InsnNode(DUP));
                }

                il.add(new VarInsnNode(ALOAD, 2 + paramIdx));

                if (reified) {
                    il.add(VariableMethods.constructor());
                }
            }
        }
    } else {
        // variable number of parameters, encoded in an array at position 2

        int lv_varargsSize = 3;
        int lv_varargs = 4;

        int numParams = context.numOfParameters();

        if (context.isVararg()) {

            LabelNode l_v_begin = new LabelNode();
            LabelNode l_v_nonempty = new LabelNode();
            LabelNode l_v_empty = new LabelNode();
            LabelNode l_v_done = new LabelNode();

            il.add(new VarInsnNode(ALOAD, 2));
            il.add(new InsnNode(ARRAYLENGTH));

            if (numParams > 0) {
                il.add(ASMUtils.loadInt(context.numOfParameters()));
                il.add(new InsnNode(ISUB));
            }
            il.add(new VarInsnNode(ISTORE, lv_varargsSize));

            il.add(l_v_begin);

            il.add(new VarInsnNode(ILOAD, lv_varargsSize));
            il.add(new JumpInsnNode(IFLE, l_v_empty));

            // nonempty varargs

            // varargs = new Object[varargsSize];
            il.add(new VarInsnNode(ILOAD, lv_varargsSize));
            il.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Object.class)));
            il.add(new VarInsnNode(ASTORE, lv_varargs));

            il.add(l_v_nonempty);

            // call System.arraycopy(src, srcPos, dest, destPos, len)
            il.add(new VarInsnNode(ALOAD, 2)); // src
            il.add(ASMUtils.loadInt(numParams)); // srcPos
            il.add(new VarInsnNode(ALOAD, lv_varargs)); // dest
            il.add(ASMUtils.loadInt(0)); // destPos
            il.add(new VarInsnNode(ILOAD, lv_varargsSize)); // len
            il.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(System.class), "arraycopy",
                    Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class), Type.INT_TYPE,
                            Type.getType(Object.class), Type.INT_TYPE, Type.INT_TYPE),
                    false));

            il.add(new JumpInsnNode(GOTO, l_v_done));

            // empty varargs
            il.add(l_v_empty);
            il.add(new FrameNode(F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null));

            // varargs = new Object[0];
            il.add(ASMUtils.loadInt(0));
            il.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Object.class)));
            il.add(new VarInsnNode(ASTORE, lv_varargs));

            il.add(l_v_done);
            il.add(new FrameNode(F_APPEND, 1,
                    new Object[] { ASMUtils.arrayTypeFor(Object.class).getInternalName() }, 0, null));

            locals.add(new LocalVariableNode("sz", Type.INT_TYPE.getDescriptor(), null, l_v_begin, end,
                    lv_varargsSize));
            locals.add(new LocalVariableNode("varargs", ASMUtils.arrayTypeFor(Object.class).getDescriptor(),
                    null, l_v_nonempty, l_v_empty, lv_varargs));
            locals.add(new LocalVariableNode("varargs", ASMUtils.arrayTypeFor(Object.class).getDescriptor(),
                    null, l_v_done, end, lv_varargs));
        }

        // load #numOfParameters, mapping them onto #numOfRegisters

        int lv_param_offset = context.isVararg() ? lv_varargs + 1 : lv_varargsSize;

        if (numParams > 0) {
            // initialise parameter slot variables to null

            for (int i = 0; i < numParams; i++) {
                LabelNode l = new LabelNode();
                int lv = lv_param_offset + i;
                il.add(new InsnNode(ACONST_NULL));
                il.add(new VarInsnNode(ASTORE, lv));
                il.add(l);
                il.add(new FrameNode(F_APPEND, 1, new Object[] { Type.getInternalName(Object.class) }, 0,
                        null));
                locals.add(
                        new LocalVariableNode("arg_" + i, Type.getDescriptor(Object.class), null, l, end, lv));
            }

            // insert switch for filling parameter slots

            LabelNode[] l_s_table = new LabelNode[numParams + 1];
            for (int i = 0; i < numParams + 1; i++) {
                l_s_table[i] = new LabelNode();
            }

            il.add(new VarInsnNode(ALOAD, 2));
            il.add(new InsnNode(ARRAYLENGTH));
            il.add(new TableSwitchInsnNode(0, numParams, l_s_table[numParams], l_s_table));

            for (int i = numParams; i >= 0; i--) {
                // length of args is at least i; may assign into param (i - 1)
                int paramIdx = i - 1;

                il.add(l_s_table[i]);
                il.add(new FrameNode(F_SAME, 0, null, 0, null));

                if (paramIdx >= 0) {
                    // assign into param #paramIdx
                    il.add(new VarInsnNode(ALOAD, 2));
                    il.add(ASMUtils.loadInt(paramIdx));
                    il.add(new InsnNode(AALOAD));
                    il.add(new VarInsnNode(ASTORE, lv_param_offset + paramIdx));
                }
            }
        }

        // now assemble the run() method invocation, filling in nulls for non-parameter slots

        il.add(new VarInsnNode(ALOAD, 0)); // this
        il.add(new VarInsnNode(ALOAD, 1)); // context
        il.add(ASMUtils.loadInt(0)); // resumption point
        if (context.isVararg()) {
            il.add(new VarInsnNode(ALOAD, lv_varargs));
        }
        for (int paramIdx : slotParamMap) {
            if (paramIdx < 0) {
                // slot not used by a parameter
                il.add(new InsnNode(ACONST_NULL));
            } else {
                // slot is parameter #paramIdx
                Var param = context.fn.params().get(paramIdx);
                boolean reified = context.types.isReified(param);

                if (reified) {
                    il.add(new TypeInsnNode(NEW, Type.getInternalName(Variable.class)));
                    il.add(new InsnNode(DUP));
                }

                il.add(new VarInsnNode(ALOAD, lv_param_offset + paramIdx));

                if (reified) {
                    il.add(VariableMethods.constructor());
                }
            }
        }
    }

    il.add(runMethod.methodInvokeInsn());

    il.add(new InsnNode(RETURN));
    il.add(end);

    locals.add(new LocalVariableNode("this", context.thisClassType().getDescriptor(), null, begin, end, 0));
    locals.add(
            new LocalVariableNode("context", Type.getDescriptor(ExecutionContext.class), null, begin, end, 1));
    if (invokeKind > 0) {
        for (int i = 0; i < invokeKind; i++) {
            locals.add(new LocalVariableNode("arg_" + i, Type.getDescriptor(Object.class), null, begin, end,
                    2 + i));
        }
        // TODO: maxLocals, maxStack
    } else {
        locals.add(new LocalVariableNode("args", ASMUtils.arrayTypeFor(Object.class).getDescriptor(), null,
                begin, end, 2));
        // TODO: maxLocals, maxStack
    }

    return node;
}

From source file:net.sandius.rembulan.compiler.gen.asm.RunMethod.java

License:Apache License

private MethodNode emitSegmentedRunMethod(int numOfSegments) {
    MethodNode node = new MethodNode(ACC_PRIVATE, methodName(), methodType().getDescriptor(), null,
            throwsExceptions());/*from w  w w.  j a  v a 2s . co m*/

    InsnList il = node.instructions;

    int lvOffset = slotOffset() + numOfRegisters();

    int lv_rpp = lvOffset + 0;
    int lv_methodIdx = lvOffset + 1;
    int lv_jmpIdx = lvOffset + 2;
    int lv_stateIdx = lvOffset + 3;
    int lv_savedState = lvOffset + 4;

    LabelNode l_top = new LabelNode();
    LabelNode l_ret = new LabelNode();
    LabelNode l_end = new LabelNode();

    LabelNode l_rpp = new LabelNode();
    LabelNode l_methodIdx = new LabelNode();
    LabelNode l_jmpIdx = new LabelNode();
    LabelNode l_stateIdx = new LabelNode();
    LabelNode l_savedState = new LabelNode();

    il.add(l_top);
    il.add(new FrameNode(F_SAME, 0, null, 0, null));

    // rpp = rp & ((1 << ST_SHIFT_SEGMENT) - 1)
    il.add(new VarInsnNode(ILOAD, LV_RESUME));
    il.add(ASMUtils.loadInt((1 << ST_SHIFT_SEGMENT) - 1));
    il.add(new InsnNode(IAND));
    il.add(new VarInsnNode(ISTORE, lv_rpp));
    il.add(l_rpp);
    il.add(new FrameNode(F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null));

    // methodIdx = rp >>> ST_SHIFT_SEGMENT
    il.add(new VarInsnNode(ILOAD, LV_RESUME));
    il.add(ASMUtils.loadInt(ST_SHIFT_SEGMENT));
    il.add(new InsnNode(IUSHR));
    il.add(new VarInsnNode(ISTORE, lv_methodIdx));
    il.add(l_methodIdx);
    il.add(new FrameNode(F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null));

    // jmpIdx = rpp >>> ST_SHIFT_LABELIDX
    il.add(new VarInsnNode(ILOAD, lv_rpp));
    il.add(ASMUtils.loadInt(ST_SHIFT_LABELIDX));
    il.add(new InsnNode(IUSHR));
    il.add(new VarInsnNode(ISTORE, lv_jmpIdx));
    il.add(l_jmpIdx);
    il.add(new FrameNode(F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null));

    // stateIdx = (rp & ((1 << ST_SHIFT_LABELIDX) - 1)) - jmpIdx
    il.add(new VarInsnNode(ILOAD, LV_RESUME));
    il.add(ASMUtils.loadInt((1 << ST_SHIFT_LABELIDX) - 1));
    il.add(new InsnNode(IAND));
    il.add(new VarInsnNode(ILOAD, lv_jmpIdx));
    il.add(new InsnNode(ISUB));
    il.add(new VarInsnNode(ISTORE, lv_stateIdx));
    il.add(l_stateIdx);
    il.add(new FrameNode(F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null));

    // savedState = null
    il.add(new InsnNode(ACONST_NULL));
    il.add(new VarInsnNode(ASTORE, lv_savedState));
    il.add(l_savedState);
    il.add(new FrameNode(F_APPEND, 1, new Object[] { context.savedStateClassType().getInternalName() }, 0,
            null));

    // switch on methodIdx

    LabelNode l_after = new LabelNode();

    LabelNode l_error = new LabelNode();
    LabelNode[] l_invokes = new LabelNode[numOfSegments];
    for (int i = 0; i < numOfSegments; i++) {
        l_invokes[i] = new LabelNode();
    }

    il.add(new VarInsnNode(ILOAD, lv_methodIdx));
    il.add(new TableSwitchInsnNode(0, numOfSegments - 1, l_error, l_invokes));

    for (int i = 0; i < numOfSegments; i++) {
        il.add(l_invokes[i]);
        il.add(new FrameNode(F_SAME, 0, null, 0, null));
        // push arguments to stack
        il.add(new VarInsnNode(ALOAD, 0));
        il.add(new VarInsnNode(ALOAD, LV_CONTEXT));
        il.add(new VarInsnNode(ILOAD, lv_stateIdx)); // pass stateIdx to the sub-method
        if (context.isVararg()) {
            il.add(new VarInsnNode(ALOAD, LV_VARARGS));
        }
        for (int j = 0; j < numOfRegisters(); j++) {
            il.add(new VarInsnNode(ALOAD, slotOffset() + j));
        }

        il.add(new MethodInsnNode(INVOKESPECIAL, context.thisClassType().getInternalName(), subRunMethodName(i),
                subMethodType().getDescriptor(), false));

        il.add(new VarInsnNode(ASTORE, lv_savedState));
        il.add(new JumpInsnNode(GOTO, l_after));
    }

    // error state
    il.add(errorState(l_error));

    il.add(l_after);
    il.add(new FrameNode(F_SAME, 0, null, 0, null));

    il.add(new VarInsnNode(ALOAD, lv_savedState));
    il.add(new JumpInsnNode(IFNULL, l_ret)); // savedState == null ?

    // continuing: savedState != null

    // FIXME: taken from ResumeMethod -- beware of code duplication!

    il.add(new VarInsnNode(ALOAD, lv_savedState)); // saved state
    il.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(DefaultSavedState.class), "resumptionPoint",
            Type.getMethodDescriptor(Type.INT_TYPE), false)); // resumption point
    il.add(new VarInsnNode(ISTORE, LV_RESUME));

    // registers
    if (context.isVararg() || numOfRegisters() > 0) {
        il.add(new VarInsnNode(ALOAD, lv_savedState));
        il.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(DefaultSavedState.class), "registers",
                Type.getMethodDescriptor(ASMUtils.arrayTypeFor(Object.class)), false));

        int numRegs = numOfRegisters() + (context.isVararg() ? 1 : 0);

        for (int i = 0; i < numRegs; i++) {
            if (i + 1 < numRegs) {
                il.add(new InsnNode(DUP));
            }
            il.add(ASMUtils.loadInt(i));
            il.add(new InsnNode(AALOAD));
            if (i == 0 && context.isVararg()) {
                il.add(new TypeInsnNode(CHECKCAST, ASMUtils.arrayTypeFor(Object.class).getInternalName()));
            }
            il.add(new VarInsnNode(ASTORE, LV_VARARGS + i));
        }
    }

    // loop back to the beginning
    il.add(new JumpInsnNode(GOTO, l_top));

    // got a null, that's the end
    il.add(l_ret);
    il.add(new FrameNode(F_SAME, 0, null, 0, null));
    il.add(new InsnNode(RETURN));

    il.add(l_end);

    // add local variables
    node.localVariables.addAll(baseLocals(l_top, l_end));
    node.localVariables
            .add(new LocalVariableNode("rpp", Type.INT_TYPE.getDescriptor(), null, l_rpp, l_ret, lv_rpp));
    node.localVariables.add(new LocalVariableNode("methodIdx", Type.INT_TYPE.getDescriptor(), null, l_methodIdx,
            l_ret, lv_methodIdx));
    node.localVariables.add(
            new LocalVariableNode("jmpIdx", Type.INT_TYPE.getDescriptor(), null, l_jmpIdx, l_ret, lv_jmpIdx));
    node.localVariables.add(new LocalVariableNode("stateIdx", Type.INT_TYPE.getDescriptor(), null, l_stateIdx,
            l_ret, lv_stateIdx));
    node.localVariables.add(new LocalVariableNode("savedState", context.savedStateClassType().getDescriptor(),
            null, l_savedState, l_ret, lv_savedState));

    return node;
}

From source file:net.sourceforge.cobertura.instrument.pass3.TestUnitCodeProvider.java

License:GNU General Public License

/**
 * Generates:// w w w .j a va2s . c om
 *   public static void __cobertura_init()
 *   {
 *     if (__cobertura_counters == null)
 *       {
 *         __cobertura_counters = new TestUnitInformationHolder[class.length];
 *       }
 *       TouchCollector.registerClass("mypackage/HelloWorld");
 *   }
 */
public void generateCINITmethod(MethodVisitor mv, String className, int counters_cnt) {
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.IFNONNULL, l1);
    mv.visitLdcInsn(counters_cnt);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(TestUnitInformationHolder.class));
    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);

    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 1);
    Label l11 = new Label();
    mv.visitLabel(l11);
    Label l2 = new Label();
    mv.visitJumpInsn(GOTO, l2);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLineNumber(24, l3);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null);
    mv.visitFieldInsn(GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitTypeInsn(NEW, "net/sourceforge/cobertura/coveragedata/TestUnitInformationHolder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "net/sourceforge/cobertura/coveragedata/TestUnitInformationHolder",
            "<init>", "()V");
    mv.visitInsn(AASTORE);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitLineNumber(23, l4);
    mv.visitIincInsn(1, 1);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitFieldInsn(GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitInsn(ARRAYLENGTH);
    mv.visitJumpInsn(IF_ICMPLT, l3);

    mv.visitLabel(l1);

    generateRegisterClass(mv, className);
}

From source file:org.actorsguildframework.internal.codegenerator.GenerationUtils.java

License:Apache License

/**
 * Returns the descriptor type for the given class to put it into a frame descriptor
 * for MethodVisitor.visitFrame()./*www.j  ava  2 s .  c  o  m*/
 * Does not support uninitialized types.
 * @param clazz the class of the type. May be null for the null descriptor.
 * @return the descriptor
 */
public static Object getFrameType(Class<?> clazz) {
    if (clazz == null)
        return Opcodes.NULL;
    if (!clazz.isPrimitive())
        return Type.getInternalName(clazz);
    else if (clazz.equals(Integer.TYPE) || clazz.equals(Character.TYPE) || clazz.equals(Short.TYPE)
            || clazz.equals(Byte.TYPE) || clazz.equals(Boolean.TYPE))
        return Opcodes.INTEGER;
    else if (clazz.equals(Long.TYPE))
        return Opcodes.LONG;
    else if (clazz.equals(Double.TYPE))
        return Opcodes.DOUBLE;
    else if (clazz.equals(Float.TYPE))
        return Opcodes.FLOAT;
    else
        throw new RuntimeException("Oops, forgot the primitive " + clazz + "?");
}

From source file:org.classdump.luna.compiler.gen.asm.BytecodeEmitVisitor.java

License:Apache License

@Override
public void visit(TabRawAppendMulti node) {
    /*/*from w  w w  .j  av  a  2 s  . c  o m*/
       In Java terms, we're translating this into the following loop:
            
         Table tab;
         ReturnBuffer rbuf = context.getReturnBuffer();
         int i = 0;
         context.registerTicks(rbuf.size());  // only when we care about ticks spent
         while (i < rbuf.size()) {
    tab.rawset(OFFSET + i, rbuf.get(i));
    i++;
         }
      */

    LabelNode begin = new LabelNode();
    LabelNode end = new LabelNode();
    LabelNode top = new LabelNode();

    int lv_idx_tab = nextLocalVariableIndex();
    int lv_idx_stack = nextLocalVariableIndex() + 1;
    int lv_idx_i = nextLocalVariableIndex() + 2;

    locals.add(new LocalVariableNode("tab", Type.getDescriptor(Table.class), null, begin, end, lv_idx_tab));
    locals.add(new LocalVariableNode("rbuf", Type.getDescriptor(ReturnBuffer.class), null, begin, end,
            lv_idx_stack));
    locals.add(new LocalVariableNode("i", Type.INT_TYPE.getDescriptor(), null, begin, end, lv_idx_i));

    il.add(begin);

    il.add(new VarInsnNode(ALOAD, slot(node.obj())));
    il.add(new TypeInsnNode(CHECKCAST, Type.getInternalName(Table.class)));
    il.add(new VarInsnNode(ASTORE, lv_idx_tab));

    il.add(loadExecutionContext());
    il.add(loadReturnBuffer());
    il.add(new VarInsnNode(ASTORE, lv_idx_stack));

    il.add(ASMUtils.loadInt(0));
    il.add(new VarInsnNode(ISTORE, lv_idx_i));

    // context.registerTicks(rbuf.size());
    if (countingTicks()) {
        il.add(loadExecutionContext());
        il.add(new VarInsnNode(ALOAD, lv_idx_stack));
        il.add(ReturnBufferMethods.size());
        il.add(ExecutionContextMethods.registerTicks());
    }

    il.add(top);
    il.add(new FrameNode(F_APPEND, 3, new Object[] { Type.getInternalName(Table.class),
            Type.getInternalName(ReturnBuffer.class), Opcodes.INTEGER }, 0, null));

    il.add(new VarInsnNode(ILOAD, lv_idx_i));
    il.add(new VarInsnNode(ALOAD, lv_idx_stack));
    il.add(ReturnBufferMethods.size());
    il.add(new JumpInsnNode(IF_ICMPGE, end));

    il.add(new VarInsnNode(ALOAD, lv_idx_tab));

    // OFFSET + i
    il.add(ASMUtils.loadLong(node.firstIdx()));
    il.add(new VarInsnNode(ILOAD, lv_idx_i));
    il.add(new InsnNode(I2L));
    il.add(new InsnNode(LADD));

    // stack.get(i)
    il.add(new VarInsnNode(ALOAD, lv_idx_stack));
    il.add(new VarInsnNode(ILOAD, lv_idx_i));
    il.add(ReturnBufferMethods.get());

    // tab.rawset(offset + i, stack.get(i))
    il.add(TableMethods.rawset_int());

    // increment i
    il.add(new IincInsnNode(lv_idx_i, 1));

    il.add(new JumpInsnNode(GOTO, top));

    il.add(end);
    il.add(new FrameNode(F_CHOP, 3, null, 0, null));
}

From source file:org.copperengine.core.instrument.StackInfo.java

License:Apache License

static Type deferLocalDesc(Object object) {
    if (object instanceof String)
        return Type.getObjectType((String) object);
    // TODO: analyze opcode at pos label
    if (object instanceof Label)
        return Type.getType(Object.class);
    int intObject = (Integer) object;
    if (intObject == Opcodes.TOP)
        return null;
    if (intObject == Opcodes.INTEGER)
        return Type.INT_TYPE;
    if (intObject == Opcodes.FLOAT)
        return Type.FLOAT_TYPE;
    if (intObject == Opcodes.LONG)
        return Type.LONG_TYPE;
    if (intObject == Opcodes.DOUBLE)
        return Type.getType(double.class);
    if (intObject == Opcodes.LONG)
        return Type.getType(long.class);
    if (intObject == Opcodes.NULL)
        return Type.getType(Object.class);
    // TODO: defer from containing class
    if (intObject == Opcodes.UNINITIALIZED_THIS)
        return Type.getType(Object.class);
    throw new BuildStackFrameException("Couldnt defer desc for " + object);
}