Example usage for org.objectweb.asm MethodVisitor visitMaxs

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

Introduction

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

Prototype

public void visitMaxs(final int maxStack, final int maxLocals) 

Source Link

Document

Visits the maximum stack size and the maximum number of local variables of the method.

Usage

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

/**
 * Implementing "static reflection" for the method putRaw so the
 * interpreter uses a switch instruction for ***PutRaw
 * based on the hash values of String names in this namespace.
 */// www  . ja v  a2 s  .  c  o m
private static void writeMethodPutRaw(ClassWriter cw, String className, String methodName,
        EnvironmentClass environmentClass, EnvSymbolNames symbolNames) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName,
            "(" + STRING_DESCRIPTOR + environmentClass.descriptor() + ")V", null, null);
    mv.visitCode();

    Label beginFunction = new Label();
    mv.visitLabel(beginFunction);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STRING_INTERNALNAME, "hashCode", "()I");
    mv.visitVarInsn(Opcodes.ISTORE, 3);
    Label beginLoop = new Label();
    mv.visitLabel(beginLoop);

    Relation<String, Integer> hashCodeRelation = symbolNames.makeHashCodeRelation(environmentClass);
    ArrayList<Integer> sortedCodes = new ArrayList<Integer>(hashCodeRelation.secondSet());
    Collections.sort(sortedCodes);
    Label notFound = new Label();
    putRawHelper(mv, className, environmentClass, hashCodeRelation, sortedCodes, notFound);
    mv.visitLabel(notFound);
    mv.visitInsn(Opcodes.RETURN);
    Label endFunction = new Label();
    mv.visitLabel(endFunction);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, beginFunction, endFunction, 0);
    mv.visitLocalVariable("queryString", STRING_DESCRIPTOR, null, beginFunction, endFunction, 1);
    mv.visitLocalVariable("value", environmentClass.descriptor(), null, beginFunction, endFunction, 2);
    mv.visitLocalVariable("queryHashCode", "I", null, beginLoop, endFunction, 3);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(2, 4);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

private static void writeNullGetter(ClassWriter cw, String className, String methodName, String signature) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, signature, null, null);
    mv.visitCode();/*from  www .j  a va  2s. c om*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitInsn(Opcodes.ARETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, l0, l1, 0);
    mv.visitLocalVariable("str", "Ljava/lang/String;", null, l0, l1, 1);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(1, 2);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

private static void writeNullSetter(ClassWriter cw, String className, String methodName, String signature) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, signature, null, null);
    mv.visitCode();/*ww  w.  ja  v  a 2  s. c  o m*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitInsn(Opcodes.RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, l0, l1, 0);
    mv.visitLocalVariable("str", "Ljava/lang/String;", null, l0, l1, 1);
    mv.visitLocalVariable("f2", "Ljava/lang/Number;", null, l0, l1, 2);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(0, 3);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

private static void writeRemoveMethod(ClassWriter cw, String className, String methodName, String invokeMethod,
        EnvironmentClass environmentClass) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(Ljava/lang/String;)V", null, null);
    mv.visitCode();//from  w  w w  . j  a v a 2 s . co m
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, invokeMethod,
            "(Ljava/lang/String;" + environmentClass.descriptor() + ")V");
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitInsn(Opcodes.RETURN);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, l0, l2, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l2, 1);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(3, 2);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

private static void writeDumpMethod(ClassWriter cw, String className, EnvSymbolNames symbolNames) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "dump",
            "(Ljava/lang/Appendable;)Ljava/lang/Appendable;", null, new String[] { "java/io/IOException" });
    mv.visitCode();//ww w  .  j av  a 2 s  .c o  m
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "within", Type.getType(HasAt.class).getDescriptor());
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.IFNULL, l1);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "within", Type.getType(HasAt.class).getDescriptor());
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(HasAt.class).getInternalName(), "at",
            "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append",
            "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;");
    mv.visitInsn(Opcodes.POP);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitLdcInsn("\n");
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append",
            "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;");
    mv.visitInsn(Opcodes.POP);
    Label l4 = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, l4);
    mv.visitLabel(l1);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitLdcInsn("Not within anything.\n");
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append",
            "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;");
    mv.visitInsn(Opcodes.POP);
    mv.visitLabel(l4);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "verboseDump", "Z");
    Label l5 = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, l5);
    int linebreaks = dumpFields(mv, className, EnvironmentClass.FVALUE, symbolNames, 0);
    dumpFields(mv, className, EnvironmentClass.FTYPE, symbolNames, linebreaks);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitLdcInsn("\n");
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append",
            "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;");
    mv.visitInsn(Opcodes.POP);
    mv.visitLabel(l5);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitInsn(Opcodes.ARETURN);
    Label l9 = new Label();
    mv.visitLabel(l9);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, l0, l9, 0);
    mv.visitLocalVariable("a", "Ljava/lang/Appendable;", null, l0, l9, 1);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.nativeInterface.fortressConverter.java

License:Open Source License

private MethodVisitor generateNewBody(int access, String desc, String signature, String[] exceptions,
        String name, String newName) {

    Debug.debug(Debug.Type.COMPILER, 1, "generateNewBody: ", name, " with desc ", desc);

    SignatureParser sp = new SignatureParser(desc);

    List<String> desc_args = sp.getJVMArguments();
    String desc_result = sp.getJVMResult();
    List<String> fortress_args = new ArrayList<String>();
    List<fortressConverter> convert_args = new ArrayList<fortressConverter>();

    String fsig = "(";
    StringBuilder buf = new StringBuilder();
    buf.append(fsig);/*from   w  w w .  j  a v a  2  s. co m*/
    for (String s : desc_args) {

        SignatureAndConverter s_a_c = toImplFFFF(s, name, false);
        buf.append(s_a_c.signature);
        fortress_args.add(s_a_c.signature);

        convert_args.add(s_a_c.converter);
    }
    fsig = buf.toString();
    SignatureAndConverter s_a_c = toImplFFFF(desc_result, name, true);
    fsig = fsig + ")" + s_a_c.signature;

    fortressConverter convert_result = s_a_c.converter;

    // FORWARDING METHOD, only with type conversions on the way in/out!
    MethodVisitor mv = cv.visitMethod(access, name, fsig, signature, exceptions);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    int count = 0;
    for (String s : fortress_args) {
        fortressConverter converter = convert_args.get(count);
        mv.visitVarInsn(Opcodes.ALOAD, count++);
        converter.convertArg(mv, s);
    }

    Debug.debug(Debug.Type.COMPILER, 1, "className = ", inputClassName, " name = ", name, " access = ", access);

    mv.visitMethodInsn(Opcodes.INVOKESTATIC, inputClassName, name, sp.getSignature());

    convert_result.convertResult(mv, s_a_c.signature);

    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
    return mv;
}

From source file:com.sun.fortress.compiler.OverloadSet.java

License:Open Source License

protected void generateBody(MethodVisitor mv, int one_if_method_closure) {
    mv.visitCode();// ww  w. j a v  a  2s.  c  o m
    //   Label fail = new Label();

    generateCall(mv, firstArg(), one_if_method_closure); // Guts of overloaded method

    // Emit failure case
    //        mv.visitLabel(fail);
    //        // Boilerplate for throwing an error.
    //        // mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);

    mv.visitMethodInsn(Opcodes.INVOKESTATIC, NamingCzar.miscCodegen, NamingCzar.matchFailure,
            NamingCzar.errorReturn);
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitMaxs(getParamCount(), getParamCount()); // autocomputed
    mv.visitEnd();
}

From source file:com.sun.fortress.interpreter.env.ClosureMaker.java

License:Open Source License

public static byte[] forTopLevelFunction(APIName apiname, FnDecl fd, String closureClass, String aClass,
        String aMethod) throws Exception {

    //IdOrOp ua_name = fd.getUnambiguousName();
    /*//from  ww w.  j  a  va 2 s. c  om
     This is a cheat; ought to be more algorithmic, so this will
     work in general, not just for native wrappers.  Proper
     algorithm derives it from the FnDecl.
     */
    String md = ForeignJava.only.methodToDecl.inverse().get(fd);

    /*
     translate apiname into package name
     extract aClass from name
     md is the method name.
            
     need to generate a class, extending
     com.sun.fortress.interpreter.Glue.NativeFn#,
     where # is the number of parameters in the decl,
            
     that class must contain a method (say, #=3)
      FValue applyToArgs(FValue x, FValue y, FValue z) {
    return package.class.md(x,y,z);
      }
     and that is all.
    */
    String pkg_dots = NamingCzar.apiNameToPackageName(apiname);
    String pkg_slashes = Useful.replace(pkg_dots, ".", "/");
    int nargs = fd.getHeader().getParams().size();

    String nativeHelper = "com/sun/fortress/interpreter/glue/NativeFn" + nargs;
    String nativeWrapperClass = pkg_slashes + "/" + aClass;
    String LclosureClass = "L" + closureClass + ";";
    String fvalue = "Lcom/sun/fortress/interpreter/evaluator/values/FValue;";

    String signature = "(";
    StringBuilder buf = new StringBuilder();
    buf.append(signature);
    for (int i = 1; i <= nargs; i++) {
        buf.append(fvalue);
    }
    signature = buf.toString();
    signature += ")" + fvalue;

    System.err.println(md);

    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, closureClass, null, nativeHelper, null);

    cw.visitSource(null, null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, nativeHelper, "<init>", "()V");
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", LclosureClass, null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "applyToArgs", signature, null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        for (int i = 1; i <= nargs; i++) {
            mv.visitVarInsn(ALOAD, i);
        }
        mv.visitMethodInsn(INVOKESTATIC, nativeWrapperClass, aMethod, signature);
        mv.visitInsn(ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", LclosureClass, null, l0, l1, 0);
        for (int i = 1; i <= nargs; i++) {
            mv.visitLocalVariable("p" + i, fvalue, null, l0, l1, 1);
        }
        mv.visitMaxs(nargs, nargs + 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

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

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    // necessary?
    name = oprs.getMethodName(name);//  w ww  .j  a  v  a2s  .c o m
    //System.out.println("old desc=" + desc);
    //desc = types.getMethodDesc(desc);
    //System.out.println("new desc=" + desc);
    String newDesc = types.getMethodDesc(desc);
    MethodVisitor mv = cv.visitMethod(access, name, newDesc, signature, exceptions);

    if (!desc.equals(newDesc)) { // catch flattened tuples
        String params = desc.substring(desc.indexOf("(") + 1, //TODO: wrong if nested parens
                desc.indexOf(")"));
        String newParams = newDesc.substring(newDesc.indexOf("(") + 1, newDesc.indexOf(")"));
        if (params.split(";").length == 1 && //single generic parameter 
                newParams.startsWith("LTuple")) { //tuple substituted in
            //System.out.println(access + " " + name + " " + signature + " " +this.instanceName);
            if ((this.access_flags & Opcodes.ACC_INTERFACE) == 0 && //not in an interface
                    (access & Opcodes.ACC_STATIC) == 0) { //and not a static method, so generate a body  

                //extract the parameters and create strings for the types 
                List<String> paramList = InstantiationMap.extractStringParameters(newParams,
                        newParams.indexOf(Naming.LEFT_OXFORD),
                        InstantiationMap.templateClosingRightOxford(newParams), new ArrayList<String>());
                String rawParams = "";
                for (String p : paramList)
                    rawParams = rawParams + Naming.internalToDesc(p);
                final String altDesc = newDesc.substring(0, newDesc.indexOf("(") + 1) + rawParams
                        + newDesc.substring(newDesc.indexOf(")"), newDesc.length());
                String tuple_params = InstantiatingClassloader.stringListToTuple(paramList);
                String make_sig = InstantiatingClassloader.toJvmSig(paramList,
                        Naming.javaDescForTaggedFortressType(tuple_params));

                MethodVisitor altMv = cv.visitMethod(access, name, altDesc, signature, exceptions);

                altMv.visitVarInsn(Opcodes.ALOAD, 0); //load this

                final int n = paramList.size(); //load the parameters
                for (int i = 1; i <= n; i++) {
                    altMv.visitVarInsn(Opcodes.ALOAD, i);
                }
                altMv.visitMethodInsn(Opcodes.INVOKESTATIC, InstantiatingClassloader.CONCRETE_ + tuple_params,
                        "make", make_sig); //create a tuple from the parameters

                if (name.equals("<init>")) {
                    altMv.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instanceName, name, newDesc); //call original method
                    altMv.visitInsn(Opcodes.RETURN); //return
                } else {
                    altMv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.instanceName, name, newDesc); //call original method
                    altMv.visitInsn(Opcodes.ARETURN); //return
                }
                altMv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
                altMv.visitEnd();
            }
        }
    }
    return new MethodInstantiater(mv, types, icl);
}

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

License:Open Source License

public static String closureClassPrefix(String name, ManglingClassWriter cw, String staticClass, String sig,
        boolean is_forwarding_closure, String forceCastParam0, List<InitializedStaticField> statics) {
    int env_loc = name.indexOf(Naming.ENVELOPE);
    int last_dot = name.substring(0, env_loc).lastIndexOf('$');

    String api = name.substring(0, last_dot);
    String suffix = name.substring(last_dot + 1);
    env_loc = suffix.indexOf(Naming.ENVELOPE); // followed by $
    String fn = is_forwarding_closure ? suffix.substring(0, env_loc) : Naming.APPLIED_METHOD;
    String ft = suffix.substring(env_loc + 2); // skip $ following ENVELOPE

    // Normalize out leading HEAVY_CROSS, if there is one.
    {//from www .java  2  s .  c om
        if (ft.charAt(0) == Naming.HEAVY_CROSS_CHAR)
            ft = ft.substring(1);

        // Allow bound-specific disambiguation in a suffix
        int hvy_x = ft.indexOf(Naming.HEAVY_X_CHAR);
        if (hvy_x != -1)
            ft = ft.substring(0, hvy_x);

        int left = ft.indexOf(Naming.LEFT_OXFORD);
        int right = ft.lastIndexOf(Naming.RIGHT_OXFORD);
        List<String> parameters = RTHelpers.extractStringParameters(ft, left, right);

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

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

        if (sig == null)
            sig = arrowParamsToJVMsig(flat_params_and_ret);
    }

    SignatureParser sp = new SignatureParser(sig);

    /*
     * Recipe:
     * need to emit class "name".
     * It needs to extend AbstractArrow[\parameters\]
     * It needs to contain
     *   RT apply (params_except_last) {
     *     return api.fn(params_except_last);
     *   }
     */

    FieldVisitor fv;
    MethodVisitor mv;
    String superClass = ABSTRACT_ + ft; // ft is assumed to begin with "Arrow"
    name = api.replace(".", "/") + '$' + suffix;
    final String final_name = name;

    //String desc = Naming.internalToDesc(name);
    final String field_desc = Naming.internalToDesc(ft);
    // Begin with a class
    cw.visit(JVM_BYTECODE_VERSION, ACC_PUBLIC + ACC_SUPER, name, null, superClass, null);

    statics.add(new InitializedStaticField.StaticForClosureField(field_desc, final_name));

    //RTTI field
    //        statics.add(new InitializedStaticField() {
    //
    //            @Override
    //            public void forClinit(MethodVisitor init_mv) {
    //               MethodInstantiater mi = new MethodInstantiater(init_mv, null, null);
    //               mi.rttiReference(final_name + Naming.RTTI_CLASS_SUFFIX);
    //               init_mv.visitFieldInsn(PUTSTATIC, final_name, Naming.RTTI_FIELD, Naming.RTTI_CONTAINER_DESC);
    //            }
    //
    //            @Override
    //            public String asmName() {
    //                return Naming.RTTI_FIELD;
    //            }
    //
    //            @Override
    //            public String asmSignature() {
    //                return Naming.RTTI_CONTAINER_DESC;
    //            }});

    // Instance init does nothing special
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    // Supertype is mangle("Abstract"+ft)
    mv.visitMethodInsn(INVOKESPECIAL, superClass, "<init>", "()V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    // What if staticClass is compiler builtin?  How do we know?
    if (staticClass == null)
        staticClass = api;
    staticClass = staticClass.replace(".", "/");

    if (LOG_LOADS)
        System.err.println(name + ".apply" + sig + " concrete\nparams = " + sp);

    // KBN 06/2011 handled above now
    // Monkey business to deal with case of "void" args.
    //int sz = parameters.size();
    // Last parameter is actually result type!
    // But we need to include an extra arg in sz to represent the closure itself (this).
    // if (sz==2 && Naming.INTERNAL_SNOWMAN.equals(parameters.get(0))) {
    // Arity 1 (sz 2) with void parameter should actually be arity 0 (sz 1).
    //    sz = 1;
    //}

    // Emit a method with well-known name ("apply", most likely)
    // to forward calls from the instance to the static, which our
    // caller will supply.  Note that the static class can be a
    // different class.
    forwardingMethod(cw, Naming.APPLY_METHOD, ACC_PUBLIC, 0, staticClass, fn, INVOKESTATIC, sig, sig,
            sp.paramCount() + 1, false, forceCastParam0);

    return fn;

}