Example usage for org.objectweb.asm Opcodes IRETURN

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

Introduction

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

Prototype

int IRETURN

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

Click Source Link

Usage

From source file:com.nginious.http.xsp.XspCompiler.java

License:Apache License

/**
 * Creates subclass of {@link XspService} for the XSP file represented by the specified document
 * tree node structure.//from w  ww  .j av  a 2s  .c  om
 * 
 * @param document the document tree node structure
 * @param srcFilePath the XSP file source path
 * @return a descriptor for the generated subclass
 * @throws XspException if unable to create subclass
 */
private ClassDescriptor compileService(DocumentPart document, String srcFilePath) throws XspException {
    ClassWriter writer = new ClassWriter(0);

    // Create class
    String packageName = document.getMetaContent("package");
    String intServiceClazzName = createIntServiceClassName(packageName, srcFilePath);
    String serviceClazzName = createServiceClassName(packageName, srcFilePath);
    writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intServiceClazzName, "Lcom/nginious/http/xsp/XspService;",
            "com/nginious/http/xsp/XspService", null);

    // Create constructor
    createConstructor(writer, "com/nginious/http/xsp/XspService");

    // Create xsp service method
    MethodVisitor visitor = createXspMethod(writer);

    Label tryLabel = new Label();
    Label startCatchLabel = new Label();
    Label endCatchLabel = new Label();

    // Start try block
    visitor.visitTryCatchBlock(tryLabel, startCatchLabel, endCatchLabel, "java/lang/Throwable");

    visitor.visitLabel(tryLabel);

    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/expr/HttpRequestVariables");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/expr/HttpRequestVariables", "<init>",
            "(Lcom/nginious/http/HttpRequest;)V");
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "setVariables",
            "(Lcom/nginious/http/xsp/expr/Variables;)V");

    document.compile(intServiceClazzName, writer, visitor);

    visitor.visitLabel(startCatchLabel);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "removeVariables",
            "()V");

    visitor.visitLdcInsn(true);
    visitor.visitInsn(Opcodes.IRETURN);

    // Start finally block
    visitor.visitLabel(endCatchLabel);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "removeVariables",
            "()V");
    visitor.visitInsn(Opcodes.ATHROW);

    visitor.visitMaxs(12, 12);
    visitor.visitEnd();

    document.compileMethod(intServiceClazzName, writer);

    writer.visitEnd();
    byte[] clazzBytes = writer.toByteArray();
    return new ClassDescriptor(serviceClazzName, clazzBytes);
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Generates instructions that returns a dummy value. Return values are as follows:
 * <ul>/*from  www . jav  a2s .c  om*/
 * <li>void -&gt; no value</li>
 * <li>boolean -&gt; false</li>
 * <li>byte/short/char/int -&gt; 0</li>
 * <li>long -&gt; 0L</li>
 * <li>float -&gt; 0.0f</li>
 * <li>double -&gt; 0.0</li>
 * <li>Object -&gt; null</li>
 * </ul>
 *
 * @param returnType return type of the method this generated bytecode is for
 * @return instructions to return a dummy value
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code returnType}'s sort is of {@link Type#METHOD}
 */
public static InsnList returnDummy(Type returnType) {
    Validate.notNull(returnType);
    Validate.isTrue(returnType.getSort() != Type.METHOD);

    InsnList ret = new InsnList();

    switch (returnType.getSort()) {
    case Type.VOID:
        ret.add(new InsnNode(Opcodes.RETURN));
        break;
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.SHORT:
    case Type.CHAR:
    case Type.INT:
        ret.add(new InsnNode(Opcodes.ICONST_0));
        ret.add(new InsnNode(Opcodes.IRETURN));
        break;
    case Type.LONG:
        ret.add(new InsnNode(Opcodes.LCONST_0));
        ret.add(new InsnNode(Opcodes.LRETURN));
        break;
    case Type.FLOAT:
        ret.add(new InsnNode(Opcodes.FCONST_0));
        ret.add(new InsnNode(Opcodes.FRETURN));
        break;
    case Type.DOUBLE:
        ret.add(new InsnNode(Opcodes.DCONST_0));
        ret.add(new InsnNode(Opcodes.DRETURN));
        break;
    case Type.OBJECT:
    case Type.ARRAY:
        ret.add(new InsnNode(Opcodes.ACONST_NULL));
        ret.add(new InsnNode(Opcodes.ARETURN));
        break;
    default:
        throw new IllegalStateException();
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Generates instructions that returns a value.
 *
 * @param returnType return type of the method this generated bytecode is for
 * @param returnValueInsnList instructions that produce the return value (should leave it on the top of the stack)
 * @return instructions to return a value
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code returnType}'s sort is of {@link Type#METHOD}
 *///from   ww  w . jav  a  2 s  .  c om
public static InsnList returnValue(Type returnType, InsnList returnValueInsnList) {
    Validate.notNull(returnType);
    Validate.isTrue(returnType.getSort() != Type.METHOD);

    InsnList ret = new InsnList();

    ret.add(returnValueInsnList);

    switch (returnType.getSort()) {
    case Type.VOID:
        ret.add(new InsnNode(Opcodes.RETURN));
        break;
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.SHORT:
    case Type.CHAR:
    case Type.INT:
        ret.add(new InsnNode(Opcodes.IRETURN));
        break;
    case Type.LONG:
        ret.add(new InsnNode(Opcodes.LRETURN));
        break;
    case Type.FLOAT:
        ret.add(new InsnNode(Opcodes.FRETURN));
        break;
    case Type.DOUBLE:
        ret.add(new InsnNode(Opcodes.DRETURN));
        break;
    case Type.OBJECT:
    case Type.ARRAY:
        ret.add(new InsnNode(Opcodes.ARETURN));
        break;
    default:
        throw new IllegalStateException();
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.ContinuationGenerators.java

License:Open Source License

/**
 * Generates instructions that returns a dummy value. Return values are as follows:
 * <ul>//from  ww w .  jav a  2s.  c o  m
 * <li>void -&gt; no value</li>
 * <li>boolean -&gt; false</li>
 * <li>byte/short/char/int -&gt; 0</li>
 * <li>long -&gt; 0L</li>
 * <li>float -&gt; 0.0f</li>
 * <li>double -&gt; 0.0</li>
 * <li>Object -&gt; null</li>
 * </ul>
 *
 * @param returnType return type of the method this generated bytecode is for
 * @return instructions to return a dummy value
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code returnType}'s sort is of {@link Type#METHOD}
 */
private static InsnList returnDummy(Type returnType) {
    Validate.notNull(returnType);
    Validate.isTrue(returnType.getSort() != Type.METHOD);

    InsnList ret = new InsnList();

    switch (returnType.getSort()) {
    case Type.VOID:
        ret.add(new InsnNode(Opcodes.RETURN));
        break;
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.SHORT:
    case Type.CHAR:
    case Type.INT:
        ret.add(new InsnNode(Opcodes.ICONST_0));
        ret.add(new InsnNode(Opcodes.IRETURN));
        break;
    case Type.LONG:
        ret.add(new InsnNode(Opcodes.LCONST_0));
        ret.add(new InsnNode(Opcodes.LRETURN));
        break;
    case Type.FLOAT:
        ret.add(new InsnNode(Opcodes.FCONST_0));
        ret.add(new InsnNode(Opcodes.FRETURN));
        break;
    case Type.DOUBLE:
        ret.add(new InsnNode(Opcodes.DCONST_0));
        ret.add(new InsnNode(Opcodes.DRETURN));
        break;
    case Type.OBJECT:
    case Type.ARRAY:
        ret.add(new InsnNode(Opcodes.ACONST_NULL));
        ret.add(new InsnNode(Opcodes.ARETURN));
        break;
    default:
        throw new IllegalStateException();
    }

    return ret;
}

From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.Inlining.java

License:Open Source License

public static List<Insn> convertInsns(MethodInsn mi, List<Insn> insns, int[] args, int _index, Label end) {
    List<Insn> result = new ArrayList<Insn>();
    HashMap labels = new HashMap();
    int index = _index;
    for (Insn i : insns) {
        if (i.isExpanded()) {
            MethodInsn expanded = (MethodInsn) i;
            // This use of end should be OK because all returns should have been removed when inlined before.
            // What could go wrong?
            result.addAll(convertInsns(expanded, expanded.inlineExpansionInsns, args, _index, end));
        } else if (i instanceof SingleInsn) {
            SingleInsn si = (SingleInsn) i;
            switch (si.opcode) {
            case Opcodes.IRETURN:
            case Opcodes.LRETURN:
            case Opcodes.FRETURN:
            case Opcodes.DRETURN:
            case Opcodes.ARETURN:
            case Opcodes.RETURN:
                result.add(new JumpInsn("RETURN->GOTO", Opcodes.GOTO, end, newIndex(mi, index++)));
                break;
            default:
                result.add(i.copy(newIndex(mi, index++)));
            }/*from   ww w  .j a  v a2  s  . c o m*/
        } else if (i instanceof VarInsn) {
            VarInsn vi = (VarInsn) i;
            switch (vi.opcode) {
            case Opcodes.ILOAD:
            case Opcodes.LLOAD:
            case Opcodes.FLOAD:
            case Opcodes.DLOAD:
            case Opcodes.ALOAD:
            case Opcodes.ISTORE:
            case Opcodes.LSTORE:
            case Opcodes.FSTORE:
            case Opcodes.DSTORE:
            case Opcodes.ASTORE:
                VarInsn newVarInsn = new VarInsn(vi.name, vi.opcode, args[vi.var], newIndex(mi, index++));
                result.add(newVarInsn);
                break;
            default:
                result.add(i.copy(newIndex(mi, index++)));
            }
        } else if (i instanceof VisitMaxs) {
        } else if (i instanceof VisitEnd) {
        } else if (i instanceof VisitCode) {
        } else if (i instanceof VisitFrame) {
        } else if (i instanceof LabelInsn) {
            LabelInsn li = (LabelInsn) i;
            if (labels.containsKey(li.label))
                result.add(new LabelInsn(li.name, (Label) labels.get(li.label), newIndex(mi, index++)));
            else {
                Label l = new Label();
                labels.put(li.label, l);
                result.add(new LabelInsn(li.name, l, newIndex(mi, index++)));
            }
        } else if (i instanceof JumpInsn) {
            JumpInsn ji = (JumpInsn) i;
            if (labels.containsKey(ji.label))
                result.add(
                        new JumpInsn(ji.name, ji.opcode, (Label) labels.get(ji.label), newIndex(mi, index++)));
            else {
                Label l = new Label();
                labels.put(ji.label, l);
                result.add(new JumpInsn(ji.name, ji.opcode, l, newIndex(mi, index++)));
            }
        } else if (i instanceof VisitLineNumberInsn) {
            VisitLineNumberInsn vlni = (VisitLineNumberInsn) i;
            if (labels.containsKey(vlni.start))
                result.add(new VisitLineNumberInsn(vlni.name, vlni.line, (Label) labels.get(vlni.start),
                        newIndex(mi, index++)));
            else {
                Label l = new Label();
                labels.put(vlni.start, l);
                result.add(new VisitLineNumberInsn(vlni.name, vlni.line, l, newIndex(mi, index++)));
            }
        } else if (i instanceof LocalVariableInsn) {
            LocalVariableInsn lvi = (LocalVariableInsn) i;
            if (labels.containsKey(lvi.start) && labels.containsKey(lvi.end)) {
                result.add(new LocalVariableInsn(lvi.name, lvi._name, lvi.desc, lvi.sig,
                        (Label) labels.get(lvi.start), (Label) labels.get(lvi.end), args[lvi._index],
                        newIndex(mi, index++)));
            } else
                throw new RuntimeException("NYI");
        } else if (i instanceof TryCatchBlock) {
            TryCatchBlock tcb = (TryCatchBlock) i;
            if (labels.containsKey(tcb.start) && labels.containsKey(tcb.end)
                    && labels.containsKey(tcb.handler)) {
                result.add(
                        new TryCatchBlock(tcb.name, (Label) labels.get(tcb.start), (Label) labels.get(tcb.end),
                                (Label) labels.get(tcb.handler), tcb.type, newIndex(mi, index++)));
            } else if (!labels.containsKey(tcb.start) && !labels.containsKey(tcb.end)
                    && !labels.containsKey(tcb.handler)) {
                Label s = new Label();
                Label e = new Label();
                Label h = new Label();
                labels.put(tcb.start, s);
                labels.put(tcb.end, e);
                labels.put(tcb.handler, h);
                result.add(new TryCatchBlock(tcb.name, s, e, h, tcb.type, newIndex(mi, index++)));
            } else
                throw new RuntimeException("NYI");
            // Need to add TableSwitch, LookupSwitch
        } else {
            result.add(i.copy(newIndex(mi, index++)));
        }
    }
    return result;
}

From source file:com.sun.fortress.runtimeSystem.InstantiatingClassloader.java

License:Open Source License

private byte[] instantiateAbstractArrow(String name, List<String> parameters) {
    ManglingClassWriter cw = new ManglingClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);

    /*// w  w  w. ja v a2s. c om
     * Special case extensions to plumb tuples
     * correctly in the face of generics instantiated
     * with tuple types.
     * 
     * Except, recall that Arrow parameters are domain...;range
     * 
     * if > 1 param then
     *   unwrap = params
     *   wrap = tuple params
     * else 1 param
     *   if tuple
     *     wrap = param
     *     unwrap = untuple params
     *   else
     *     unwrap = param
     *     wrap = null
     *     
     *  Use unwrapped parameters to generate the all-Objects case
     *  for casting; check the generated signature against the input
     *  to see if we are them.
     *   
     */

    Triple<List<String>, List<String>, String> stuff = normalizeArrowParameters(parameters);

    List<String> flat_params_and_ret = stuff.getA();
    List<String> tupled_params_and_ret = stuff.getB();
    String tupleType = stuff.getC();

    List<String> flat_obj_params_and_ret = Useful.applyToAll(flat_params_and_ret, toJLO);
    List<String> norm_obj_params_and_ret = normalizeArrowParametersAndReturn(flat_obj_params_and_ret);
    List<String> norm_params_and_ret = normalizeArrowParametersAndReturn(flat_params_and_ret);

    String obj_sig = stringListToGeneric(ABSTRACT_ARROW, norm_obj_params_and_ret);
    String obj_intf_sig = stringListToGeneric(Naming.ARROW_TAG, norm_obj_params_and_ret);
    String wrapped_sig = stringListToGeneric(WRAPPED_ARROW, norm_params_and_ret);
    String typed_intf_sig = stringListToGeneric(Naming.ARROW_TAG, norm_params_and_ret);
    String unwrapped_apply_sig;

    if (parameters.size() == 2 && parameters.get(0).equals(Naming.INTERNAL_SNOWMAN))
        unwrapped_apply_sig = arrowParamsToJVMsig(parameters.subList(1, 2));
    else
        unwrapped_apply_sig = arrowParamsToJVMsig(flat_params_and_ret);

    String obj_apply_sig = arrowParamsToJVMsig(flat_obj_params_and_ret);

    String[] interfaces = new String[] { stringListToArrow(norm_params_and_ret) };
    /*
     * Note that in the case of foo -> bar,
     * normalized = flattened, and tupled does not exist (is null).
     */
    String typed_tupled_intf_sig = tupled_params_and_ret == null ? null
            : stringListToGeneric(Naming.ARROW_TAG, tupled_params_and_ret);
    String objectified_tupled_intf_sig = tupled_params_and_ret == null ? null
            : stringListToGeneric(Naming.ARROW_TAG, Useful.applyToAll(tupled_params_and_ret, toJLO));

    boolean is_all_objects = norm_obj_params_and_ret.equals(norm_params_and_ret);

    String _super = is_all_objects ? "java/lang/Object" : obj_sig;

    cw.visit(JVM_BYTECODE_VERSION, ACC_PUBLIC + ACC_SUPER + ACC_ABSTRACT, name, null, _super, interfaces);

    simpleInitMethod(cw, _super);

    /* */
    if (!is_all_objects) {
        // implement method for the object version.
        // cast parameters, invoke this.apply on cast parameters, ARETURN

        // note cut and paste from apply below, work in progress.

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, Naming.APPLY_METHOD, obj_apply_sig, null, null);

        mv.visitVarInsn(Opcodes.ALOAD, 0); // this

        int unwrapped_l = flat_params_and_ret.size();

        for (int i = 0; i < unwrapped_l - 1; i++) {
            String t = flat_params_and_ret.get(i);
            if (!t.equals(Naming.INTERNAL_SNOWMAN) || unwrapped_l > 2) {
                mv.visitVarInsn(Opcodes.ALOAD, i + 1); // element
                // mv.visitTypeInsn(CHECKCAST, t);
                generalizedCastTo(mv, Naming.internalToType(t));
            }
        }

        mv.visitMethodInsn(INVOKEVIRTUAL, name, Naming.APPLY_METHOD, unwrapped_apply_sig);
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);

        mv.visitEnd();
    }

    // is instance method -- takes an Object
    {
        String sig = "(Ljava/lang/Object;)Z";
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, IS_A, sig, null, null);

        Label fail = new Label();

        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.INSTANCEOF, Naming.ANY_TYPE_CLASS);
        mv.visitJumpInsn(Opcodes.IFEQ, fail);

        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.CHECKCAST, Naming.ANY_TYPE_CLASS);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, name, IS_A,
                "(" + Naming.internalToDesc(Naming.ANY_TYPE_CLASS) + ")Z");
        mv.visitInsn(Opcodes.IRETURN);

        mv.visitLabel(fail);
        mv.visitIntInsn(BIPUSH, 0);
        mv.visitInsn(Opcodes.IRETURN);

        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // is instance method -- takes an Any
    {
        String sig = "(" + Naming.internalToDesc(Naming.ANY_TYPE_CLASS) + ")Z";
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, IS_A, sig, null, null);
        Label fail = new Label();

        //get RTTI to compare to
        mv.visitFieldInsn(GETSTATIC, name, Naming.RTTI_FIELD, Naming.RTTI_CONTAINER_DESC);
        //get RTTI of object
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(INVOKEINTERFACE, Naming.ANY_TYPE_CLASS, Naming.RTTI_GETTER,
                "()" + Naming.RTTI_CONTAINER_DESC);
        // mv.visitJumpInsn(IFNONNULL, fail);
        mv.visitMethodInsn(INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME,
                Naming.RTTI_SUBTYPE_METHOD_SIG);

        //mv.visitIntInsn(BIPUSH, 0);
        mv.visitJumpInsn(Opcodes.IFEQ, fail);

        mv.visitIntInsn(BIPUSH, 1);
        mv.visitInsn(Opcodes.IRETURN);

        mv.visitLabel(fail);
        mv.visitIntInsn(BIPUSH, 0);
        mv.visitInsn(Opcodes.IRETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // castTo
    {
        /*
         *  If arg0 instanceof typed_intf_sig
         *     return arg0
         *  arg0 = arg0.getWrappee()
         *  if arg0 instanceof typed_intf_sig
         *     return arg0
         *  new WrappedArrow
         *  dup
         *  push argo
         *  init
         *  return tos
         */

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, CAST_TO, "(" +
        // Naming.internalToDesc(obj_intf_sig)
                "Ljava/lang/Object;" + ")" + Naming.internalToDesc(typed_intf_sig), null, null);

        Label not_instance1 = new Label();
        Label not_instance2 = new Label();

        // try bare instanceof
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.INSTANCEOF, typed_intf_sig);
        mv.visitJumpInsn(Opcodes.IFEQ, not_instance1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.CHECKCAST, typed_intf_sig);
        mv.visitInsn(Opcodes.ARETURN);

        // unwrap
        mv.visitLabel(not_instance1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.CHECKCAST, obj_intf_sig);
        mv.visitMethodInsn(INVOKEINTERFACE, obj_intf_sig, getWrappee,
                "()" + Naming.internalToDesc(obj_intf_sig));
        mv.visitVarInsn(Opcodes.ASTORE, 0);

        // try instanceof on unwrapped
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.INSTANCEOF, typed_intf_sig);
        mv.visitJumpInsn(Opcodes.IFEQ, not_instance2);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.CHECKCAST, typed_intf_sig);
        mv.visitInsn(Opcodes.ARETURN);

        // wrap and return
        mv.visitLabel(not_instance2);
        mv.visitTypeInsn(NEW, wrapped_sig);
        mv.visitInsn(DUP);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, wrapped_sig, "<init>",
                "(" + Naming.internalToDesc(obj_intf_sig) + ")V");

        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);

        mv.visitEnd();
    }

    if (typed_tupled_intf_sig != null) {
        /*
         *  If arg0 instanceof typed_intf_sig
         *     return arg0
         *  arg0 = arg0.getWrappee()
         *  if arg0 instanceof typed_intf_sig
         *     return arg0
         *  new WrappedArrow
         *  dup
         *  push argo
         *  init
         *  return tos
         */

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, CAST_TO,
                "(" + Naming.internalToDesc(objectified_tupled_intf_sig) + ")"
                        + Naming.internalToDesc(typed_intf_sig),
                null, null);

        Label not_instance1 = new Label();
        Label not_instance2 = new Label();

        // try bare instanceof
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.INSTANCEOF, typed_intf_sig);
        mv.visitJumpInsn(Opcodes.IFEQ, not_instance1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ARETURN);

        // unwrap
        mv.visitLabel(not_instance1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(INVOKEINTERFACE, objectified_tupled_intf_sig, getWrappee,
                "()" + Naming.internalToDesc(objectified_tupled_intf_sig));
        mv.visitVarInsn(Opcodes.ASTORE, 0);

        // try instanceof on unwrapped
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.INSTANCEOF, typed_intf_sig);
        mv.visitJumpInsn(Opcodes.IFEQ, not_instance2);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ARETURN);

        // wrap and return - untupled should be okay here, since it subtypes
        mv.visitLabel(not_instance2);
        mv.visitTypeInsn(NEW, wrapped_sig);
        mv.visitInsn(DUP);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, wrapped_sig, "<init>",
                "(" + Naming.internalToDesc(obj_intf_sig) + ")V");

        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);

        mv.visitEnd();
    }

    // getWrappee
    {
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getWrappee, "()" + Naming.internalToDesc(obj_intf_sig),
                null, null);

        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd(); // return this
    }

    if (tupled_params_and_ret == null) {
        /* Single abstract method */
        if (LOG_LOADS)
            System.err.println(name + ".apply" + unwrapped_apply_sig + " abstract for abstract");
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, Naming.APPLY_METHOD, unwrapped_apply_sig,
                null, null);
        mv.visitEnd();

    } else {
        /*
         * Establish two circular forwarding methods;
         * the eventual implementer will break the cycle.
         * 
         */
        String tupled_apply_sig = arrowParamsToJVMsig(tupled_params_and_ret);

        {
            /* Given tupled args, extract, and invoke apply. */

            if (LOG_LOADS)
                System.err.println(name + ".apply" + tupled_apply_sig + " abstract for abstract");
            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, Naming.APPLY_METHOD, tupled_apply_sig, null, null);

            mv.visitVarInsn(Opcodes.ALOAD, 0); // closure

            int unwrapped_l = flat_params_and_ret.size();

            for (int i = 0; i < unwrapped_l - 1; i++) {
                String param = flat_params_and_ret.get(i);
                mv.visitVarInsn(Opcodes.ALOAD, 1); // tuple
                mv.visitMethodInsn(INVOKEINTERFACE, tupleType, TUPLE_TYPED_ELT_PFX + (Naming.TUPLE_ORIGIN + i),
                        "()" + Naming.internalToDesc(param));
            }

            mv.visitMethodInsn(INVOKEVIRTUAL, name, Naming.APPLY_METHOD, unwrapped_apply_sig);
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);

            mv.visitEnd();
        }

        { /* Given untupled args, load, make a tuple, invoke apply. */
            if (LOG_LOADS)
                System.err.println(name + ".apply" + unwrapped_apply_sig + " abstract for abstract");
            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, Naming.APPLY_METHOD, unwrapped_apply_sig, null, null);

            mv.visitVarInsn(Opcodes.ALOAD, 0); // closure

            int unwrapped_l = flat_params_and_ret.size();

            for (int i = 0; i < unwrapped_l - 1; i++) {
                mv.visitVarInsn(Opcodes.ALOAD, i + 1); // element
            }

            List<String> tuple_elements = flat_params_and_ret.subList(0, unwrapped_l - 1);

            String make_sig = toJvmSig(tuple_elements, Naming.javaDescForTaggedFortressType(tupleType));
            mv.visitMethodInsn(INVOKESTATIC, stringListToGeneric(CONCRETE_TUPLE, tuple_elements), "make",
                    make_sig);

            mv.visitMethodInsn(INVOKEVIRTUAL, name, Naming.APPLY_METHOD, tupled_apply_sig);
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);

            mv.visitEnd();
        }

    }

    //RTTI comparison field

    final String final_name = name;
    ArrayList<InitializedStaticField> isf_list = new ArrayList<InitializedStaticField>();
    if (!parameters.contains("java/lang/Object")) {

        isf_list.add(new InitializedStaticField.StaticForUsualRttiField(final_name, this));
    } else {
        isf_list.add(new InitializedStaticField.StaticForJLOParameterizedRttiField(final_name));
    }
    cw.visitEnd();
    //      //RTTI getter
    {
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, Naming.RTTI_GETTER, "()" + Naming.RTTI_CONTAINER_DESC,
                null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, name, Naming.RTTI_FIELD, Naming.RTTI_CONTAINER_DESC);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }

    optionalStaticsAndClassInitForTO(isf_list, cw);
    return cw.toByteArray();
}

From source file:com.sun.fortress.runtimeSystem.InstantiatingClassloader.java

License:Open Source License

private byte[] instantiateConcreteTuple(String dename, List<String> parameters) {
    /*//from   w  w w . j  ava2s  . co  m
     * extends AnyConcreteTuple[\ N \]
     * 
     * implements Tuple[\ parameters \]
     * 
     * defines f1 ... fN
     * defines e1 ... eN
     * defines o1 ... oN
     */

    ManglingClassWriter cw = new ManglingClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);

    final int n = parameters.size();
    final String any_tuple_n = ANY_TUPLE + Naming.LEFT_OXFORD + n + Naming.RIGHT_OXFORD;
    final String any_concrete_tuple_n = ANY_CONCRETE_TUPLE + Naming.LEFT_OXFORD + n + Naming.RIGHT_OXFORD;
    final String tuple_params = stringListToTuple(parameters);

    String[] superInterfaces = { tuple_params };

    cw.visit(JVM_BYTECODE_VERSION, ACC_PUBLIC + ACC_SUPER, dename, null, any_concrete_tuple_n, superInterfaces);

    /* Outline of what must be generated:
            
    // fields
            
    // init method
            
    // factory method
              
    // getRTTI method
            
    // is instance method -- takes an Object
            
    // is instance method
              
    // cast method
            
    // typed getters
            
    // untyped getters
             
    */

    // fields
    {
        for (int i = 0; i < n; i++) {
            String f = TUPLE_FIELD_PFX + (i + Naming.TUPLE_ORIGIN);
            String sig = Naming.internalToDesc(parameters.get(i));
            cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, f, sig, null /* for non-generic */,
                    null /* instance has no value */);
        }
    }
    // init method
    {
        String init_sig = tupleParamsToJvmInitSig(parameters);
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", init_sig, null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, any_concrete_tuple_n, "<init>", Naming.voidToVoid);

        for (int i = 0; i < n; i++) {
            String f = TUPLE_FIELD_PFX + (i + Naming.TUPLE_ORIGIN);
            String sig = Naming.internalToDesc(parameters.get(i));

            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ALOAD, i + 1);
            mv.visitFieldInsn(Opcodes.PUTFIELD, dename, f, sig);
        }
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // factory method -- same args as init, returns a new one.
    {
        String init_sig = tupleParamsToJvmInitSig(parameters);
        String make_sig = toJvmSig(parameters, Naming.javaDescForTaggedFortressType(tuple_params));
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "make", make_sig, null,
                null);

        mv.visitCode();
        // eep(mv, "before new");
        mv.visitTypeInsn(NEW, dename);
        mv.visitInsn(DUP);
        // push params for init
        for (int i = 0; i < n; i++) {
            mv.visitVarInsn(Opcodes.ALOAD, i);
        }
        // eep(mv, "before init");
        mv.visitMethodInsn(INVOKESPECIAL, dename, "<init>", init_sig);

        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // getRTTI method/field and static initialization
    {
        final String classname = dename;
        MethodVisitor mv = cw.visitNoMangleMethod(Opcodes.ACC_PUBLIC, // acccess
                Naming.RTTI_GETTER, // name
                Naming.STATIC_PARAMETER_GETTER_SIG, // sig
                null, // generics sig?
                null); // exceptions
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, classname, Naming.RTTI_FIELD, Naming.RTTI_CONTAINER_DESC);

        areturnEpilogue(mv);

        MethodVisitor imv = cw.visitMethod(ACC_STATIC, "<clinit>", Naming.voidToVoid, null, null);
        //taken from codegen.emitRttiField   
        InitializedStaticField isf = new InitializedStaticField.StaticForRttiFieldOfTuple(classname, this);
        isf.forClinit(imv);
        cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, isf.asmName(), isf.asmSignature(),
                null /* for non-generic */, null /* instance has no value */);

        imv.visitInsn(RETURN);
        imv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        imv.visitEnd();

    }

    // is instance method -- takes an Object
    {
        String sig = "(Ljava/lang/Object;)Z";
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, IS_A, sig, null, null);

        Label fail = new Label();

        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.INSTANCEOF, any_tuple_n);
        mv.visitJumpInsn(Opcodes.IFEQ, fail);

        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.CHECKCAST, any_tuple_n);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, dename, IS_A, "(" + Naming.internalToDesc(any_tuple_n) + ")Z");
        mv.visitInsn(Opcodes.IRETURN);

        mv.visitLabel(fail);
        mv.visitIntInsn(BIPUSH, 0);
        mv.visitInsn(Opcodes.IRETURN);

        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // is instance method -- takes an AnyTuple[\N\]
    {
        String sig = "(" + Naming.internalToDesc(any_tuple_n) + ")Z";
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, IS_A, sig, null, null);

        Label fail = new Label();

        for (int i = 0; i < n; i++) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(INVOKEINTERFACE, any_tuple_n, TUPLE_OBJECT_ELT_PFX + (Naming.TUPLE_ORIGIN + i),
                    UNTYPED_GETTER_SIG);

            String cast_to = parameters.get(i);

            generalizedInstanceOf(mv, cast_to);

            mv.visitJumpInsn(Opcodes.IFEQ, fail);

        }

        mv.visitIntInsn(BIPUSH, 1);
        mv.visitInsn(Opcodes.IRETURN);

        mv.visitLabel(fail);
        mv.visitIntInsn(BIPUSH, 0);
        mv.visitInsn(Opcodes.IRETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // cast method
    {
        String sig = "(" + Naming.internalToDesc(any_tuple_n) + ")" + Naming.internalToDesc(tuple_params);
        String make_sig = toJvmSig(parameters, Naming.javaDescForTaggedFortressType(tuple_params));

        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, CAST_TO, sig, null, null);

        // Get the parameters to make, and cast them.
        for (int i = 0; i < n; i++) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(INVOKEINTERFACE, any_tuple_n, TUPLE_OBJECT_ELT_PFX + (Naming.TUPLE_ORIGIN + i),
                    UNTYPED_GETTER_SIG);
            String cast_to = parameters.get(i);
            generalizedCastTo(mv, cast_to);
        }

        mv.visitMethodInsn(INVOKESTATIC, dename, "make", make_sig);

        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // typed getters
    // untyped getters
    for (int i = 0; i < n; i++) {
        String untyped = TUPLE_OBJECT_ELT_PFX + (Naming.TUPLE_ORIGIN + i);
        String typed = TUPLE_TYPED_ELT_PFX + (Naming.TUPLE_ORIGIN + i);
        String field = TUPLE_FIELD_PFX + (Naming.TUPLE_ORIGIN + i);
        String param_type = parameters.get(i);
        String param_desc = Naming.internalToDesc(param_type);
        {
            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, untyped, UNTYPED_GETTER_SIG, null, null);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, dename, field, param_desc);
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
            mv.visitEnd();
        }
        {
            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, typed, "()" + param_desc, null, null);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, dename, field, param_desc);
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
            mv.visitEnd();
        }
    }

    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.sun.tdk.jcov.instrument.StaticInvokeMethodAdapter.java

License:Open Source License

@Override
public void visitInsn(int opcode) {

    switch (opcode) {
    case Opcodes.IRETURN:
    case Opcodes.FRETURN:
    case Opcodes.ARETURN:
    case Opcodes.LRETURN:
    case Opcodes.DRETURN:
    case Opcodes.RETURN:
        if (params.isInnerInvacationsOff()
                && Utils.isAdvanceStaticInstrAllowed(className, methName/*"<init>"*/)) {
            if (!methName.equals("<clinit>")) {
                int id = 0;
                super.visitLdcInsn(id);
                super.visitMethodInsn(INVOKESTATIC, "com/sun/tdk/jcov/runtime/CollectDetect", "setExpected",
                        "(I)V");
            } else {
                super.visitMethodInsn(INVOKESTATIC, "com/sun/tdk/jcov/runtime/CollectDetect", "leaveClinit",
                        "()V");
            }//  w  ww  . j a v  a2s. c  o m
        }
        break;
    default:
        break;
    }

    super.visitInsn(opcode);
}

From source file:com.taobao.profile.instrument.ProfMethodAdapter.java

License:Open Source License

public void visitInsn(int inst) {
    switch (inst) {
    case Opcodes.ARETURN:
    case Opcodes.DRETURN:
    case Opcodes.FRETURN:
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.RETURN:
    case Opcodes.ATHROW:
        this.visitLdcInsn(mMethodId);
        this.visitMethodInsn(INVOKESTATIC, "com/taobao/profile/Profiler", "End", "(I)V");
        break;/*from   w  w  w.  j  a v a2 s .com*/
    default:
        break;
    }

    super.visitInsn(inst);
}

From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java

License:MIT License

/**
 * Generate a new method "boolean name()", which returns a constant value.
 *
 * @param clazz Class to add method to/*  www .j  av  a2 s  .c o m*/
 * @param name Name of method
 * @param retval Return value of method
 */
public static void generateBooleanMethodConst(ClassNode clazz, String name, boolean retval) {
    MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()Z",
            null, null);
    InsnList code = method.instructions;

    code.add(ASMHelper.pushIntConstant(retval ? 1 : 0));
    code.add(new InsnNode(Opcodes.IRETURN));

    clazz.methods.add(method);
}