Example usage for org.objectweb.asm MethodVisitor visitLabel

List of usage examples for org.objectweb.asm MethodVisitor visitLabel

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitLabel.

Prototype

public void visitLabel(final Label label) 

Source Link

Document

Visits a label.

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 w w.j a v a 2  s  . 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 static byte[] instantiateAnyConcreteTuple(String dename, List<String> parameters) {
    /*//from ww  w. ja  va2 s.  com
     * Single parameter, N, which is the arity of the tuple.
     * 
     * extends Ljava/util/AbstractList;
     * implements LAnyTuple[\N\];
     * int size() { return N; }
     * Object get(int n) {
     *    if (n >= N || n < 0) {
     *       throw new IndexOutOfBoundsException();
     *    } else {
     *      // binary search tree returning o1 ... oN
     *    }
     * }
     */

    ManglingClassWriter cw = new ManglingClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    final String super_type = "java/lang/Object";

    final int n = Integer.parseInt(parameters.get(0));
    final String any_tuple_n = ANY_TUPLE + Naming.LEFT_OXFORD + n + Naming.RIGHT_OXFORD;
    String[] superInterfaces = { any_tuple_n };
    cw.visit(JVM_BYTECODE_VERSION, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, dename, null, super_type,
            superInterfaces);

    simpleInitMethod(cw, super_type);

    { // size method
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "size", "()I", null, null);
        mv.visitCode();
        mv.visitIntInsn(BIPUSH, n);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }

    { // get method  
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get", "(I)Ljava/lang/Object;", null, null);
        mv.visitCode();
        mv.visitVarInsn(ILOAD, 1);
        mv.visitIntInsn(BIPUSH, n);
        Label l1 = new Label();
        mv.visitJumpInsn(IF_ICMPGE, l1);
        mv.visitVarInsn(ILOAD, 1);
        Label l2 = new Label();
        mv.visitJumpInsn(IFGE, l2);
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitTypeInsn(NEW, "java/lang/IndexOutOfBoundsException");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IndexOutOfBoundsException", "<init>", "()V");
        mv.visitInsn(ATHROW);

        FnVoidVoid geti = new FnVoidVoid() {

            @Override
            public void apply() {
                mv.visitVarInsn(ILOAD, 1);
            }

        };

        FnVoid<Integer> leaf = new FnVoid<Integer>() {

            @Override
            public void apply(Integer x) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEINTERFACE, any_tuple_n,
                        TUPLE_OBJECT_ELT_PFX + (Naming.TUPLE_ORIGIN + x), UNTYPED_GETTER_SIG);
                mv.visitInsn(ARETURN);
            }

        };

        visitBinaryTree(mv, 0, n - 1, l2, geti, leaf);

        mv.visitMaxs(2, 2);
        mv.visitEnd();

    }

    cw.visitEnd();
    return cw.toByteArray();
}

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

License:Open Source License

/**
 * Generates a binary search tree for integers in the range [lo,hi]
 * (INCLUSIVE!).  Target, if not null, is to be attached to the generated code.
 * geti pushes the integer in question onto the top of the stack.
 * leaf handles the leaf case where lo=hi.
 * //from w w  w. j ava 2  s  .  co m
 * Cases are generated into ascending order, just because.
 * 
 * @param mv
 * @param lo
 * @param hi
 * @param target
 * @param geti
 * @param leaf
 */

static void visitBinaryTree(MethodVisitor mv, int lo, int hi, Label target, FnVoidVoid geti,
        FnVoid<Integer> leaf) {
    if (target != null)
        mv.visitLabel(target);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);

    if (lo == hi) {
        leaf.apply(lo);
    } else {
        /*
         * 0,1 -> 0,0; 1,1
         * 0,2 -> 0,1; 2,2 
         */
        int mid = (lo + hi) / 2;
        Label small = null;
        Label large = new Label();
        geti.apply();
        mv.visitIntInsn(BIPUSH, mid);
        mv.visitJumpInsn(IF_ICMPGT, large);
        visitBinaryTree(mv, lo, mid, small, geti, leaf);
        visitBinaryTree(mv, mid + 1, hi, large, geti, leaf);
    }
}

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

License:Open Source License

private byte[] instantiateConcreteTuple(String dename, List<String> parameters) {
    /*// w  w w. jav  a  2s.  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/* ww w  .jav a2 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.sun.fortress.runtimeSystem.InstantiatingClassloader.java

License:Open Source License

/**
 * @param rttiClassName//from w ww  .  j  av  a 2s  .c  o  m
 * @param sparams_size
 */
static public void emitDictionaryAndFactoryForGenericRTTIclass(ManglingClassWriter cw, String rttiClassName,
        int sparams_size, final Naming.XlationData xldata) {

    // Push nulls for opr parameters in the factory call.
    List<Boolean> spks;
    int type_sparams_size = sparams_size;
    if (xldata != null) {
        spks = xldata.isOprKind();
        sparams_size = spks.size();
    } else {
        spks = new InfiniteList<Boolean>(false);
    }

    // FIELD
    // static, initialized to Map-like thing
    cw.visitField(ACC_PRIVATE + ACC_STATIC + ACC_FINAL, "DICTIONARY", Naming.RTTI_MAP_DESC, null, null);

    // CLINIT
    // factory, consulting map, optionally invoking constructor.
    MethodVisitor mv = cw.visitNoMangleMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    // new
    mv.visitTypeInsn(NEW, Naming.RTTI_MAP_TYPE);
    // init
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, Naming.RTTI_MAP_TYPE, "<init>", "()V");
    // store
    mv.visitFieldInsn(PUTSTATIC, rttiClassName, "DICTIONARY", Naming.RTTI_MAP_DESC);

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

    // FACTORY

    boolean useSparamsArray = sparams_size > 6;
    int sparamsArrayIndex = sparams_size;

    String fact_sig = Naming.rttiFactorySig(type_sparams_size);
    String init_sig = InstantiatingClassloader.jvmSignatureForOnePlusNTypes("java/lang/Class",
            type_sparams_size, Naming.RTTI_CONTAINER_TYPE, "V");
    String get_sig;
    String put_sig;
    String getClass_sig;
    if (useSparamsArray) {
        get_sig = Naming.makeMethodDesc(Naming.RTTI_CONTAINER_ARRAY_DESC, Naming.RTTI_CONTAINER_DESC);
        put_sig = Naming.makeMethodDesc(Naming.RTTI_CONTAINER_ARRAY_DESC + Naming.RTTI_CONTAINER_DESC,
                Naming.RTTI_CONTAINER_DESC);
        getClass_sig = Naming.makeMethodDesc(NamingCzar.descString + Naming.RTTI_CONTAINER_ARRAY_DESC,
                NamingCzar.descClass);
    } else {
        get_sig = InstantiatingClassloader.jvmSignatureForNTypes(sparams_size, Naming.RTTI_CONTAINER_TYPE,
                Naming.RTTI_CONTAINER_DESC);
        put_sig = InstantiatingClassloader.jvmSignatureForNTypes(sparams_size + 1, Naming.RTTI_CONTAINER_TYPE,
                Naming.RTTI_CONTAINER_DESC);
        getClass_sig = InstantiatingClassloader.jvmSignatureForOnePlusNTypes(NamingCzar.internalString,
                sparams_size, Naming.RTTI_CONTAINER_TYPE, NamingCzar.descClass);
    }

    mv = cw.visitNoMangleMethod(ACC_PUBLIC + ACC_STATIC, Naming.RTTI_FACTORY, fact_sig, null, null);
    mv.visitCode();
    /* 
     * First arg is java class, necessary for creation of type.
     * 
     * rCN x = DICTIONARY.get(args)
     * if  x == null then
     *   x = new rCN(args)
     *   x = DICTIONARY.put(args, x)
     * end
     * return x
     */

    // object
    mv.visitFieldInsn(GETSTATIC, rttiClassName, "DICTIONARY", Naming.RTTI_MAP_DESC);
    // push args
    int l = sparams_size;
    if (useSparamsArray) {
        mv.visitLdcInsn(sparams_size);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, Naming.RTTI_CONTAINER_TYPE);
        mv.visitVarInsn(Opcodes.ASTORE, sparamsArrayIndex);
        InstantiatingClassloader.pushArgsIntoArray(mv, 0, l, sparamsArrayIndex, spks);
    } else {
        InstantiatingClassloader.pushArgs(mv, 0, l, spks);
    }
    // invoke Dictionary.get
    mv.visitMethodInsn(INVOKEVIRTUAL, Naming.RTTI_MAP_TYPE, "get", get_sig);
    Label not_null = new Label();
    mv.visitInsn(DUP);
    mv.visitJumpInsn(IFNONNULL, not_null);
    mv.visitInsn(POP); // discard dup'd null
    // doing it all on the stack -- (unless too many static params, then use an array for human coded stuff)
    // 1) first push the dictionary and args (array if used) 
    // 2) create new RTTI object
    // 3) push args again (array if used) and create the class for this object
    // 4) push the args again (never array) to init RTTI object
    // 5) add to dictionary

    //1)
    mv.visitFieldInsn(GETSTATIC, rttiClassName, "DICTIONARY", Naming.RTTI_MAP_DESC);

    if (useSparamsArray) {
        mv.visitVarInsn(ALOAD, sparamsArrayIndex);
    } else {
        InstantiatingClassloader.pushArgs(mv, 0, l, spks);
    }

    // 2) invoke constructor
    mv.visitTypeInsn(NEW, rttiClassName);
    mv.visitInsn(DUP);

    // 3) create class for this object
    String stem = Naming.rttiClassToBaseClass(rttiClassName);
    if (true || xldata == null) {
        // NOT symbolic (and a problem if we pretend that it is)
        mv.visitLdcInsn(stem);
    } else {
        RTHelpers.symbolicLdc(mv, stem);
    }
    if (useSparamsArray) {
        mv.visitVarInsn(ALOAD, sparamsArrayIndex);
    } else {
        InstantiatingClassloader.pushArgs(mv, 0, l, spks);
    }

    //(mv, "before getRTTIclass");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Naming.RT_HELPERS, "getRTTIclass", getClass_sig);
    //eep(mv, "after getRTTIclass");

    // 4) init RTTI object (do not use array)
    // NOTE only pushing type_sparams here.
    InstantiatingClassloader.pushArgs(mv, 0, type_sparams_size);
    mv.visitMethodInsn(INVOKESPECIAL, rttiClassName, "<init>", init_sig);
    // 5) add to dictionary
    mv.visitMethodInsn(INVOKEVIRTUAL, Naming.RTTI_MAP_TYPE, "putIfNew", put_sig);

    mv.visitLabel(not_null);
    mv.visitInsn(ARETURN);

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

From source file:com.tencent.tinker.build.auxiliaryclass.AuxiliaryClassInjectAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    // If method <clinit> and <init> are not found, we should generate a <clinit>.
    if (!this.isClInitExists && !this.isInitExists) {
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();/*from w ww  .j a v  a 2s  .c  o  m*/
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "lineSeparator", "()Ljava/lang/String;",
                false);
        Label lblSkipInvalidInsn = new Label();
        mv.visitJumpInsn(Opcodes.IFNONNULL, lblSkipInvalidInsn);
        mv.visitLdcInsn(Type.getType(this.auxiliaryClassDesc));
        mv.visitVarInsn(Opcodes.ASTORE, 0);
        mv.visitLabel(lblSkipInvalidInsn);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    super.visitEnd();
}

From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java

License:Apache License

public static byte[] dumpArrayList(final TypeModel tm) {
    final ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//from w w w .  j a  va 2 s  .com
    MethodVisitor mv;

    final Class interfaceClass = tm.type();
    final String name = interfaceClass.getName().replace('.', '/');

    cw.visit(
            V1_5, ACC_PUBLIC + ACC_SUPER, name + "ArrayList", "L" + collections + "impl/AbstractHugeArrayList<L"
                    + name + ";L" + name + "Allocation;L" + name + "Element;>;",
            collections + "impl/AbstractHugeArrayList", null);

    cw.visitSource(tm.type().getSimpleName() + "ArrayList.java", null);

    for (final FieldModel fm : tm.fields()) {
        if (fm instanceof Enum8FieldModel) {
            fv = cw.visitField(ACC_FINAL, fm.fieldName() + "FieldModel",
                    "L" + collections + "model/Enum8FieldModel;",
                    "L" + collections + "model/Enum8FieldModel<Ljava/lang/annotation/ElementType;>;", null);
            fv.visitEnd();
        } else if (fm instanceof Enumerated16FieldModel) {
            fv = cw.visitField(ACC_FINAL, fm.fieldName() + "FieldModel",
                    "L" + collections + "model/Enumerated16FieldModel;",
                    "L" + collections + "model/Enumerated16FieldModel<Ljava/lang/String;>;", null);
            fv.visitEnd();
        }
    }
    final List<FieldModel> fields = new ArrayList<FieldModel>();
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(L" + collections + "HugeArrayBuilder;)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(35, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, collections + "impl/AbstractHugeArrayList", "<init>",
                "(L" + collections + "HugeArrayBuilder;)V");
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(29, l1);

        for (final FieldModel fm : tm.fields()) {
            if (fm instanceof Enum8FieldModel) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitTypeInsn(NEW, fm.bcModelType());
                mv.visitInsn(DUP);
                mv.visitLdcInsn(fm.fieldName());
                mv.visitIntInsn(BIPUSH, fm.fieldNumber());
                mv.visitLdcInsn(Type.getType(fm.bcLFieldType()));
                mv.visitMethodInsn(INVOKESTATIC, fm.bcFieldType(), "values", "()[" + fm.bcLFieldType());
                mv.visitMethodInsn(INVOKESPECIAL, fm.bcModelType(), "<init>",
                        "(Ljava/lang/String;ILjava/lang/Class;[Ljava/lang/Enum;)V");
                mv.visitFieldInsn(PUTFIELD, name + "ArrayList", fm.fieldName() + "FieldModel",
                        fm.bcLModelType());
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel",
                        fm.bcLModelType());
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEVIRTUAL, collections + "HugeArrayBuilder", "baseDirectory",
                        "()Ljava/lang/String;");
                mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "baseDirectory", "(Ljava/lang/String;)V");
                fields.add(fm);

            } else if (fm instanceof Enumerated16FieldModel) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitTypeInsn(NEW, fm.bcModelType());
                mv.visitInsn(DUP);
                mv.visitLdcInsn(fm.fieldName());
                mv.visitIntInsn(BIPUSH, fm.fieldNumber());
                mv.visitLdcInsn(Type.getType(fm.bcLFieldType()));
                mv.visitMethodInsn(INVOKESPECIAL, fm.bcModelType(), "<init>",
                        "(Ljava/lang/String;ILjava/lang/Class;)V");
                mv.visitFieldInsn(PUTFIELD, name + "ArrayList", fm.fieldName() + "FieldModel",
                        fm.bcLModelType());
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel",
                        fm.bcLModelType());
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEVIRTUAL, collections + "HugeArrayBuilder", "baseDirectory",
                        "()Ljava/lang/String;");
                mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "baseDirectory", "(Ljava/lang/String;)V");
                fields.add(fm);
            }
        }

        mv.visitInsn(RETURN);
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l4, 0);
        mv.visitLocalVariable("hab", "L" + collections + "HugeArrayBuilder;", null, l0, l4, 1);
        mv.visitMaxs(7, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "createAllocation",
                "(L" + collections + "impl/MappedFileChannel;)L" + name + "Allocation;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(43, l0);
        mv.visitTypeInsn(NEW, name + "Allocation");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "ArrayList", "allocationSize", "I");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, name + "Allocation", "<init>",
                "(IL" + collections + "impl/MappedFileChannel;)V");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("mfc", "L" + collections + "impl/MappedFileChannel;", null, l0, l1, 1);
        mv.visitMaxs(4, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "createElement", "(J)L" + name + "Element;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(48, l0);
        mv.visitTypeInsn(NEW, name + "Element");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(LLOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, name + "Element", "<init>",
                "(L" + collections + "impl/AbstractHugeArrayList;J)V");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("n", "J", null, l0, l1, 1);
        mv.visitMaxs(5, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "createImpl", "()L" + name + ";", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(53, l0);
        mv.visitTypeInsn(NEW, name + "Impl");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, name + "Impl", "<init>", "()V");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "compactStart", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(57, l0);
        for (final FieldModel fm : fields) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "compactStart", "()V");
        }
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(58, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l2, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "compactOnAllocation", "(L" + name + "Allocation;J)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(65, l0);
        for (final FieldModel fm : fields) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            mv.visitVarInsn(ALOAD, 1);
            mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_string", fm.bcLStoreType());
            mv.visitVarInsn(LLOAD, 2);
            mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "compactScan", "(" + fm.bcLStoreType() + "J)V");
        }
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(66, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l2, 0);
        mv.visitLocalVariable("allocation", "L" + name + "Allocation;", null, l0, l2, 1);
        mv.visitLocalVariable("thisSize", "J", null, l0, l2, 2);
        mv.visitMaxs(4, 4);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "compactEnd", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(69, l0);
        for (final FieldModel fm : fields) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "compactEnd", "()V");
        }
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(70, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l2, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(74, l0);
        for (final FieldModel fm : fields) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "clear", "()V");
        }
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLineNumber(77, l3);
        mv.visitInsn(RETURN);
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l4, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "createImpl", "()Ljava/lang/Object;",
                null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(29, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "createImpl", "()L" + name + ";");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "createElement",
                "(J)L" + collections + "impl/AbstractHugeElement;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(29, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(LLOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "createElement", "(J)L" + name + "Element;");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("x0", "J", null, l0, l1, 1);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "compactOnAllocation",
                "(L" + collections + "api/HugeAllocation;J)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(29, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, name + "Allocation");
        mv.visitVarInsn(LLOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "compactOnAllocation",
                "(L" + name + "Allocation;J)V");
        mv.visitInsn(RETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("x0", "L" + collections + "api/HugeAllocation;", null, l0, l1, 1);
        mv.visitLocalVariable("x1", "J", null, l0, l1, 2);
        mv.visitMaxs(4, 4);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "createAllocation",
                "(L" + collections + "impl/MappedFileChannel;)L" + collections + "api/HugeAllocation;", null,
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(29, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "createAllocation",
                "(L" + collections + "impl/MappedFileChannel;)L" + name + "Allocation;");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("x0", "L" + collections + "impl/MappedFileChannel;", null, l0, l1, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();
    // ClassReader cr = new ClassReader(bytes);
    // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0);
    return bytes;
}

From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java

License:Apache License

public static byte[] dumpAllocation(final TypeModel tm) {

    final ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/*from   ww  w . jav  a  2 s. c  o  m*/
    MethodVisitor mv;

    final Class interfaceClass = tm.type();
    final String name = interfaceClass.getName().replace('.', '/');

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name + "Allocation", null, "java/lang/Object",
            new String[] { collections + "api/HugeAllocation" });

    cw.visitSource(tm.type().getSimpleName() + "Allocation.java", null);

    for (final FieldModel fm : tm.fields()) {
        fv = cw.visitField(0, "m_" + fm.fieldName(), fm.bcLStoreType(), null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(IL" + collections + "impl/MappedFileChannel;)V", null,
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(46, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

        for (final FieldModel fm : tm.fields()) {
            mv.visitVarInsn(ALOAD, 0);

            if (fm instanceof ObjectFieldModel) {
                mv.visitLdcInsn(Type.getType(fm.bcLFieldType()));
                mv.visitVarInsn(ILOAD, 1);
                mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "newArrayOfField",
                        "(Ljava/lang/Class;I)[Ljava/lang/Object;");
                mv.visitTypeInsn(CHECKCAST, fm.bcLStoreType());
            } else {
                mv.visitVarInsn(ILOAD, 1);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "newArrayOfField",
                        "(IL" + collections + "impl/MappedFileChannel;)" + fm.bcLStoreType());
            }
            mv.visitFieldInsn(PUTFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
        }

        mv.visitInsn(RETURN);
        final Label l14 = new Label();
        mv.visitLabel(l14);
        mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l14, 0);
        mv.visitLocalVariable("allocationSize", "I", null, l0, l14, 1);
        mv.visitLocalVariable("mfc", "L" + collections + "impl/MappedFileChannel;", null, l0, l14, 2);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(62, l0);
        for (final FieldModel fm : tm.fields()) {
            if (fm instanceof ObjectFieldModel) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
                mv.visitInsn(ACONST_NULL);
                mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "fill",
                        "([Ljava/lang/Object;Ljava/lang/Object;)V");
            }
        }
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(63, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l2, 0);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "destroy", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(66, l0);
        for (final FieldModel fm : tm.fields()) {
            if (!fm.isBufferStore()) {
                continue;
            }
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
            mv.visitTypeInsn(CHECKCAST, "java/nio/Buffer");
            mv.visitMethodInsn(INVOKESTATIC, GenerateHugeArrays.class.getName().replace('.', '/'), "clean",
                    "(Ljava/nio/Buffer;)V");

        }
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLineNumber(69, l3);
        mv.visitInsn(RETURN);
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l4, 0);
        mv.visitLocalVariable("m", "Ljava/lang/Object;", null, l3, l4, 1);
        mv.visitMaxs(1, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "finalize", "()V", null, new String[] { "java/lang/Throwable" });
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(76, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "finalize", "()V");
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(77, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "Allocation", "destroy", "()V");
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLineNumber(78, l2);
        mv.visitInsn(RETURN);
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l3, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }

    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();
    // ClassReader cr = new ClassReader(bytes);
    // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0);
    return bytes;
}

From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java

License:Apache License

public static byte[] dumpElement(final TypeModel tm) {

    final ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/*ww  w .  ja  va  2 s  .  c  o  m*/
    MethodVisitor mv;

    final String name = tm.bcType();

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name + "Element",
            "L" + collections + "impl/AbstractHugeElement<L" + name + "Allocation;>;L" + name
                    + ";Ljava/io/Externalizable;",
            collections + "impl/AbstractHugeElement", new String[] { name, "java/io/Externalizable" });

    cw.visitSource(tm.type().getSimpleName() + "Element.java", null);

    {
        fv = cw.visitField(0, "allocation", "L" + name + "Allocation;", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(L" + collections + "impl/AbstractHugeArrayList;J)V",
                "(L" + collections + "impl/AbstractHugeArrayList<L" + name + ";L" + name + "Allocation;L" + name
                        + "Element;>;J)V",
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(34, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(LLOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, collections + "impl/AbstractHugeElement", "<init>",
                "(L" + collections + "impl/AbstractHugeContainer;J)V");
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(35, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "Element;", null, l0, l2, 0);
        mv.visitLocalVariable("list", "L" + collections + "impl/AbstractHugeArrayList;", "L" + collections
                + "impl/AbstractHugeArrayList<L" + name + ";L" + name + "Allocation;L" + name + "Element;>;",
                l0, l2, 1);
        mv.visitLocalVariable("n", "J", null, l0, l2, 2);
        mv.visitMaxs(4, 4);
        mv.visitEnd();
    }
    for (final FieldModel fm : tm.fields()) {
        // /////// SETTER //////////

        int maxLocals = 2 + fm.bcFieldSize();

        mv = cw.visitMethod(ACC_PUBLIC, "set" + fm.titleFieldName(), "(" + fm.bcLFieldType() + ")V", null,
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(39, l0);

        int invoke = INVOKESTATIC;
        if (fm.virtualGetSet()) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "Element", "container",
                    "L" + collections + "impl/AbstractHugeContainer;");
            mv.visitTypeInsn(CHECKCAST, name + "ArrayList");
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            invoke = INVOKEVIRTUAL;
            maxLocals++;
        }

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "allocation", "L" + name + "Allocation;");
        mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "offset", "I");
        mv.visitVarInsn(loadFor(fm.bcType()), 1);

        mv.visitMethodInsn(invoke, fm.bcModelType(), "set",
                "(" + fm.bcLStoreType() + "I" + fm.bcLSetType() + ")V");
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "Element;", null, l0, l2, 0);
        mv.visitLocalVariable("elementType", "Ljava/lang/annotation/ElementType;", null, l0, l2, 1);
        mv.visitMaxs(maxLocals, 1 + fm.bcFieldSize());
        mv.visitEnd();

        // /////// GETTER //////////

        mv = cw.visitMethod(ACC_PUBLIC, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType(), null, null);
        mv.visitCode();
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLineNumber(144, l3);

        BCType bcType = fm.bcType();
        if (fm.virtualGetSet()) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "Element", "container",
                    "L" + collections + "impl/AbstractHugeContainer;");
            mv.visitTypeInsn(CHECKCAST, name + "ArrayList");
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            bcType = BCType.Reference;
        }

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "allocation", "L" + name + "Allocation;");
        mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "offset", "I");
        mv.visitMethodInsn(invoke, fm.bcModelType(), "get", "(" + fm.bcLStoreType() + "I)" + fm.bcLSetType());
        if (!fm.bcLSetType().equals(fm.bcLFieldType())) {
            mv.visitTypeInsn(CHECKCAST, fm.bcFieldType());
        }
        mv.visitInsn(returnFor(bcType));
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + name + "Element;", null, l3, l4, 0);
        mv.visitMaxs(4, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "updateAllocation0", "(I)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(170, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "container",
                "L" + collections + "impl/AbstractHugeContainer;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "index", "J");
        mv.visitMethodInsn(INVOKEVIRTUAL, collections + "impl/AbstractHugeContainer", "getAllocation",
                "(J)L" + collections + "api/HugeAllocation;");
        mv.visitTypeInsn(CHECKCAST, name + "Allocation");
        mv.visitFieldInsn(PUTFIELD, name + "Element", "allocation", "L" + name + "Allocation;");
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(171, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "Element;", null, l0, l2, 0);
        mv.visitLocalVariable("allocationSize", "I", null, l0, l2, 1);
        mv.visitMaxs(4, 2);
        mv.visitEnd();
    }
    appendToStringHashCodeEqualsCopyOf(tm, cw, name + "Element", true);
    cw.visitEnd();

    final byte[] bytes = cw.toByteArray();
    // ClassReader cr = new ClassReader(bytes);
    // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0);
    return bytes;
}