Example usage for org.objectweb.asm Opcodes INSTANCEOF

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

Introduction

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

Prototype

int INSTANCEOF

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

Click Source Link

Usage

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 ww  . j  a  va  2s .  c  o  m*/
     * 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) {
    /*/*w w w . j av a 2  s.c  o  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.fortress.runtimeSystem.InstantiatingClassloader.java

License:Open Source License

/**
 * @param mv//w  w  w.j  a v a 2  s . co  m
 * @param cast_to
 */
public static void generalizedInstanceOf(MethodVisitor mv, String cast_to) {
    if (cast_to.startsWith(Naming.UNION_OX)) {
        List<String> cast_to_parameters = RTHelpers.extractStringParameters(cast_to);
        Label done = new Label();
        for (int i = 0; i < cast_to_parameters.size(); i++) {
            mv.visitInsn(DUP); // object to test
            generalizedInstanceOf(mv, cast_to_parameters.get(i)); // replaces obj w/ 1/0
            mv.visitInsn(DUP); // copy for branch test. leave one on TOS
            // eepI(mv,"union instanceof subtest " + cast_to_parameters.get(i));
            mv.visitJumpInsn(IFNE, done);
            mv.visitInsn(POP); // discard unnecessary zero.
        }
        mv.visitLdcInsn(0); // failure
        mv.visitLabel(done);
        mv.visitInsn(SWAP); // put tested obj on TOS
        mv.visitInsn(POP); // discard
    } else if (cast_to.startsWith(Naming.TUPLE_OX)) {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CONCRETE_ + cast_to, IS_A, "(Ljava/lang/Object;)Z");
    } else if (cast_to.startsWith(Naming.ARROW_OX)) {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ABSTRACT_ + cast_to, IS_A, "(Ljava/lang/Object;)Z");
    } else {
        String type = cast_to.equals(Naming.INTERNAL_SNOWMAN)
                ? Naming.specialFortressTypes.get(Naming.INTERNAL_SNOWMAN)
                : cast_to;
        mv.visitTypeInsn(Opcodes.INSTANCEOF, type);
    }
}

From source file:com.trigersoft.jaque.expression.ExpressionMethodVisitor.java

License:Apache License

@Override
public void visitTypeInsn(int opcode, String type) {
    Class<?> resultType = _classVisitor.getClass(Type.getObjectType(type));
    Expression e;/* w ww  .j ava  2  s .  c  o  m*/
    switch (opcode) {
    case Opcodes.NEW:
        e = Expression.constant(null, resultType);
        break;
    case Opcodes.CHECKCAST:
        if (resultType == Object.class)
            // there is no point in casting to object
            return;
        // TODO
        return;
    case Opcodes.ANEWARRAY:
    default:
        throw notLambda(opcode);
    case Opcodes.INSTANCEOF:
        e = Expression.instanceOf(_exprStack.pop(), resultType);
        break;
    }

    _exprStack.push(e);
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.CodeEmitter.java

public void emitInstanceOf(TypeWidget targetType, Class<?> clazz, Label isNotInstance) {
    Preconditions.checkArgument(!clazz.isPrimitive());
    Preconditions.checkArgument(!targetType.isPrimitive());
    if (targetType.getJVMType().getDescriptor().equals(Type.getDescriptor(clazz))) {
        // trivial -- we already know this to be true!
        // we don't need to emit any code here
    } else {//from w  ww .jav  a 2  s  .  co  m
        // naively emit the instanceof -- be nice to statically determine if it's even possible for targetType to be an instance of clazz
        // --- and also to know if we don't need a cast to do so
        dup(targetType);
        getMethodVisitor().visitTypeInsn(Opcodes.INSTANCEOF, Type.getInternalName(clazz));
        getMethodVisitor().visitJumpInsn(Opcodes.IFEQ, isNotInstance);
        getMethodVisitor().visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(clazz));
    }
}

From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java

License:Creative Commons License

private static void transformGetTotalArmorValue(MethodNode method) {
    InsnList needle = new InsnList();
    needle.add(new LabelNode());
    needle.add(new LineNumberNode(-1, new LabelNode()));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_HORSE_ARMOR_VALUES));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_HORSE_FUNC110241CB, false));

    AbstractInsnNode pointer = ASMHelper.findFirstNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();

    newInstr.add(new LabelNode());
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(//from w ww  .j  av a 2 s  .  co m
            ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_GET_CUSTOM_ARMOR_ITEM, false));
    newInstr.add(new VarInsnNode(Opcodes.ASTORE, 1));
    newInstr.add(new LabelNode());
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 1));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false));
    newInstr.add(new TypeInsnNode(Opcodes.INSTANCEOF, ASMNames.CL_ITEM_HORSE_ARMOR));
    LabelNode l2 = new LabelNode();
    newInstr.add(new JumpInsnNode(Opcodes.IFEQ, l2));
    newInstr.add(new LabelNode());
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 1));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false));
    newInstr.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ITEM_HORSE_ARMOR));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 1));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_GET_ARMOR_VALUE, false));
    newInstr.add(new InsnNode(Opcodes.IRETURN));
    newInstr.add(l2);

    method.instructions.insertBefore(pointer, newInstr);
}

From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java

License:Creative Commons License

private static void transformArmorTexture(MethodNode method) {
    InsnList needle = new InsnList();
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_HORSE_FUNC110241CB, false));
    needle.add(new VarInsnNode(Opcodes.ISTORE, 3));

    AbstractInsnNode node = ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    LabelNode l17 = new LabelNode();
    newInstr.add(l17);//from ww w.j a  v  a 2 s .  co m
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_GET_CUSTOM_ARMOR_ITEM, false));
    newInstr.add(new VarInsnNode(Opcodes.ASTORE, 4));
    LabelNode l18 = new LabelNode();
    newInstr.add(l18);
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false));
    newInstr.add(new TypeInsnNode(Opcodes.INSTANCEOF, ASMNames.CL_ITEM_HORSE_ARMOR));
    LabelNode l19 = new LabelNode();
    newInstr.add(new JumpInsnNode(Opcodes.IFEQ, l19));
    LabelNode l20 = new LabelNode();
    newInstr.add(l20);
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_FIELD110280BR));
    newInstr.add(new InsnNode(Opcodes.ICONST_2));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false));
    newInstr.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ITEM_HORSE_ARMOR));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_GET_ARMOR_TEXTURE, false));
    newInstr.add(new InsnNode(Opcodes.AASTORE));
    LabelNode l21 = new LabelNode();
    newInstr.add(l21);
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(new TypeInsnNode(Opcodes.NEW, ASMNames.CL_STRING_BUILDER));
    newInstr.add(new InsnNode(Opcodes.DUP));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_FIELD110286BQ));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESTATIC, ASMNames.MD_STRING_VALUE_OF, false));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_STRINGBUILDER_INIT, false));
    newInstr.add(new LdcInsnNode("cst-"));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_STRINGBUILDER_APPEND, false));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_UNLOC_NAME, false));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_STRINGBUILDER_APPEND, false));
    newInstr.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_STRINGBUILDER_TO_STRING, false));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.PUTFIELD, ASMNames.FD_HORSE_FIELD110286BQ));
    newInstr.add(new InsnNode(Opcodes.RETURN));
    newInstr.add(l19);

    method.instructions.insert(node, newInstr);
}

From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java

License:Creative Commons License

private static void transformIsValidArmor(MethodNode method) {
    InsnList needle = new InsnList();
    needle.add(new LabelNode());
    needle.add(new LineNumberNode(-1, new LabelNode()));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_ITEMS_IRON_HORSE_ARMOR));
    needle.add(new JumpInsnNode(Opcodes.IF_ACMPEQ, new LabelNode()));

    AbstractInsnNode node = ASMHelper.findFirstNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    newInstr.add(new LabelNode());
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(new TypeInsnNode(Opcodes.INSTANCEOF, ASMNames.CL_ITEM_HORSE_ARMOR));
    LabelNode ln = new LabelNode();
    newInstr.add(new JumpInsnNode(Opcodes.IFEQ, ln));
    newInstr.add(new LabelNode());
    newInstr.add(new InsnNode(Opcodes.ICONST_1));
    newInstr.add(new InsnNode(Opcodes.IRETURN));
    newInstr.add(ln);//from w  w  w . j ava 2 s  .  c o  m

    method.instructions.insertBefore(node, newInstr);
}

From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java

License:Creative Commons License

private static void transformInteract(MethodNode method) {

    InsnList needle = new InsnList();
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_ITEMS_DIAMOND_HORSE_ARMOR));
    needle.add(new JumpInsnNode(Opcodes.IF_ACMPNE, new LabelNode()));
    needle.add(new LabelNode());
    needle.add(new LineNumberNode(-1, new LabelNode()));
    needle.add(new InsnNode(Opcodes.ICONST_3));
    needle.add(new VarInsnNode(Opcodes.ISTORE, 4));
    needle.add(new LabelNode());
    needle.add(new LineNumberNode(-1, new LabelNode()));
    needle.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

    AbstractInsnNode node = ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    newInstr.add(new LabelNode());
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 2));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false));
    newInstr.add(new TypeInsnNode(Opcodes.INSTANCEOF, ASMNames.CL_ITEM_HORSE_ARMOR));
    LabelNode l8 = new LabelNode();
    newInstr.add(new JumpInsnNode(Opcodes.IFEQ, l8));
    newInstr.add(new LabelNode());
    newInstr.add(new InsnNode(Opcodes.ICONST_4));
    newInstr.add(new VarInsnNode(Opcodes.ISTORE, 4));
    newInstr.add(l8);//w w  w.j  ava  2s . c  o  m
    newInstr.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

    method.instructions.insert(node, newInstr);
}

From source file:de.scoopgmbh.copper.instrument.TryCatchBlockHandler.java

License:Apache License

@SuppressWarnings("unchecked")
public void instrument(ClassNode cn) {
    //if (1 == 1) return;

    for (MethodNode m : (List<MethodNode>) cn.methods) {
        if (!m.exceptions.contains(INTERRUPT_EXCEPTION_NAME) || m.tryCatchBlocks.isEmpty()) {
            continue;
        }/*w w  w .j av a2  s. com*/
        logger.info("Instrument " + cn.name + "." + m.name);
        HashSet<Label> labels = new HashSet<Label>();
        for (TryCatchBlockNode catchNode : (List<TryCatchBlockNode>) m.tryCatchBlocks) {
            if (labels.contains(catchNode.handler.getLabel())) {
                // some handlers share their handling code - check it out to prevent double instrumentation
                logger.info("skipping node");
                continue;
            }
            labels.add(catchNode.handler.getLabel());

            LabelNode labelNode = catchNode.handler;
            AbstractInsnNode lineNumberNode = labelNode.getNext() instanceof LineNumberNode
                    ? labelNode.getNext()
                    : labelNode;
            FrameNode frameNode = (FrameNode) lineNumberNode.getNext();
            VarInsnNode varInsnNode = (VarInsnNode) frameNode.getNext();
            AbstractInsnNode insertPoint = varInsnNode;

            if (catchNode.type == null) {
                // this is probably a finally block;
                if (insertPoint.getNext() != null && insertPoint.getNext() instanceof LabelNode) {
                    insertPoint = insertPoint.getNext();
                }
            }

            LabelNode labelNode4ifeg = new LabelNode();
            InsnList newCode = new InsnList();
            newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
            newCode.add(new TypeInsnNode(Opcodes.INSTANCEOF, INTERRUPT_EXCEPTION_NAME));
            newCode.add(new JumpInsnNode(Opcodes.IFEQ, labelNode4ifeg));
            newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
            newCode.add(new TypeInsnNode(Opcodes.CHECKCAST, INTERRUPT_EXCEPTION_NAME));
            newCode.add(new InsnNode(Opcodes.ATHROW));
            newCode.add(labelNode4ifeg);
            m.instructions.insert(insertPoint, newCode);
        }
    }
}