Example usage for org.objectweb.asm Opcodes RETURN

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

Introduction

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

Prototype

int RETURN

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

Click Source Link

Usage

From source file:jpcsp.Allegrex.compiler.CodeBlock.java

License:Open Source License

private void addConstructor(ClassVisitor cv) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();/*from   ww  w.  ja va  2s. c o  m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, objectInternalName, "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:jpcsp.Allegrex.compiler.CodeBlock.java

License:Open Source License

private void addNonStaticMethods(CompilerContext context, ClassVisitor cv) {
    MethodVisitor mv;/*from  w w  w. ja v  a2s.co  m*/

    // public int exec(int returnAddress, int alternativeReturnAddress, boolean isJump) throws Exception;
    mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getExecMethodName(), context.getExecMethodDesc(), null,
            exceptions);
    mv.visitCode();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassName(), context.getStaticExecMethodName(),
            context.getStaticExecMethodDesc());
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    // private static IExecutable e;
    FieldVisitor fv = cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, context.getReplaceFieldName(),
            executableDescriptor, null, null);
    fv.visitEnd();

    // public void setExecutable(IExecutable e);
    mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getReplaceMethodName(), context.getReplaceMethodDesc(),
            null, exceptions);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, getClassName(), context.getReplaceFieldName(), executableDescriptor);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();

    // public IExecutable getExecutable();
    mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getGetMethodName(), context.getGetMethodDesc(), null,
            exceptions);
    mv.visitCode();
    mv.visitFieldInsn(Opcodes.GETSTATIC, getClassName(), context.getReplaceFieldName(), executableDescriptor);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:jpcsp.Allegrex.compiler.CompilerContext.java

License:Open Source License

public void endSequenceMethod() {
    flushInstructionCount(false, true);
    mv.visitInsn(Opcodes.RETURN);
}

From source file:lapin.comp.asm.ASMByteCodeGenerator.java

License:Open Source License

private void generateClassInitializer(Env env) {
    MethodVisitor mv = _cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);

    // field for SELF (static field)
    mv.visitCode();//  w w w .ja v  a2 s  . c  om
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, toInternalName(super.classInfo.classname()), "SELF",
            toTypeDescriptor(super.classInfo.classname()));
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:lapin.comp.asm.ASMByteCodeGenerator.java

License:Open Source License

private void generateConstructor(Env env) {
    /*/*from   w  w  w .  ja  v  a  2s . c  o  m*/
     * local variables (reserved)
     * 0: this
     * 1: env
     */
    MethodVisitor mv = _cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            //"(Llapin/lang/Env;)V",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_ENV }), null, null);

    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitLdcInsn(super.classInfo.name().pname());
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "lapin/function/CompiledExpr", "<init>",
            //"(Ljava/lang/String;)V");
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_STRING }));

    Iterator it;

    // fields for constants
    it = constTable.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next(); // key: object (const)
        String val = Data.string(constTable.get(key));
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitLdcInsn(Printer.prin1ToString(key, env));
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "lapin/function/CompiledExpr", "toSexp",
                Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_STRING, TYPE_ENV }));
        mv.visitFieldInsn(Opcodes.PUTFIELD, toInternalName(super.classInfo.classname()), val,
                TYPE_OBJECT.getDescriptor());
    }

    // fields for vars
    it = varTable.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next(); // key: symbol (var)
        String val = Data.string(varTable.get(key));
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitLdcInsn(Printer.prin1ToString(key, env));
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "lapin/function/CompiledExpr", "toSexp",
                Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_STRING, TYPE_ENV }));
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lapin/lang/Data", "symbol",
                Type.getMethodDescriptor(TYPE_SYMBOL, new Type[] { TYPE_OBJECT }));
        mv.visitFieldInsn(Opcodes.PUTFIELD, toInternalName(super.classInfo.classname()), val,
                TYPE_SYMBOL.getDescriptor());
    }

    // fields for lambdaLists
    it = llTable.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next(); // key: lambdaList
        String val = Data.string(llTable.get(key));
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitLdcInsn(Printer.prin1ToString(Data.lambdaList(key).params(), env));
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "lapin/function/CompiledExpr", "toLambdaList",
                Type.getMethodDescriptor(TYPE_LAMBDA_LIST, new Type[] { TYPE_STRING, TYPE_ENV }));
        mv.visitFieldInsn(Opcodes.PUTFIELD, toInternalName(super.classInfo.classname()), val,
                TYPE_LAMBDA_LIST.getDescriptor());
    }

    // field for SELF (static field)
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, toInternalName(super.classInfo.classname()), "SELF",
            toTypeDescriptor(super.classInfo.classname()));
    mv.visitInsn(Opcodes.RETURN);

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:lombok.patcher.MethodLogistics.java

License:Open Source License

private static int returnOpcodeFor(String returnSpec) {
    switch (returnSpec.charAt(0)) {
    case 'D':
        return Opcodes.DRETURN;
    case 'J':
        return Opcodes.LRETURN;
    case 'F':
        return Opcodes.FRETURN;
    case 'I':
    case 'S':
    case 'B':
    case 'Z':
        return Opcodes.IRETURN;
    case 'V':
        return Opcodes.RETURN;
    case 'L':
    case '[':
        return Opcodes.ARETURN;
    }//from   w  ww.  j  a  v a2 s .co  m

    throw new IllegalStateException("Uhoh - bug - unrecognized JVM type: " + returnSpec);
}

From source file:lombok.patcher.scripts.ExitFromMethodEarlyScript.java

License:Open Source License

@Override
protected MethodPatcher createPatcher(ClassWriter writer, final String classSpec,
        TransplantMapper transplantMapper) {
    MethodPatcher patcher = new MethodPatcher(writer, transplantMapper, new MethodPatcherFactory() {
        public MethodVisitor createMethodVisitor(String name, String desc, MethodVisitor parent,
                MethodLogistics logistics) {
            if (valueWrapper == null && !insertCallOnly && logistics.getReturnOpcode() != Opcodes.RETURN) {
                throw new IllegalStateException("method " + name + desc + " must return something, but "
                        + "you did not provide a value hook method.");
            }/*from   ww  w.ja v  a 2  s. c  o m*/
            return new ExitEarly(parent, logistics, classSpec);
        }
    });

    if (transplant) {
        patcher.addTransplant(decisionWrapper);
        if (valueWrapper != null)
            patcher.addTransplant(valueWrapper);
    }
    return patcher;
}

From source file:lucee.transformer.bytecode.reflection.ASMProxyFactory.java

License:Open Source License

private static byte[] _createMethod(Type type, Class clazz, Method method, Resource classRoot, String className)
        throws IOException {
    Class<?> rtn = method.getReturnType();
    Type rtnType = Type.getType(rtn);

    className = className.replace('.', File.separatorChar);
    ClassWriter cw = ASMUtil.getClassWriter();
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, className, null, ASM_METHOD.getInternalName(), null);

    // CONSTRUCTOR

    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR, null, null, cw);

    Label begin = new Label();
    adapter.visitLabel(begin);/* w w w .ja  v a2 s. co  m*/
    adapter.loadThis();

    adapter.visitVarInsn(Opcodes.ALOAD, 1);
    adapter.visitVarInsn(Opcodes.ALOAD, 2);

    adapter.invokeConstructor(ASM_METHOD, CONSTRUCTOR);
    adapter.visitInsn(Opcodes.RETURN);

    Label end = new Label();
    adapter.visitLabel(end);

    adapter.endMethod();

    /*
             
        GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC,CONSTRUCTOR,null,null,cw);
                
        Label begin = new Label();
         adapter.visitLabel(begin);
       adapter.loadThis();
                 
        // clazz
         adapter.visitVarInsn(Opcodes.ALOAD, 2);
                 
         // parameterTypes 
         Class<?>[] params = method.getParameterTypes();
         Type[] paramTypes = new Type[params.length];
        ArrayVisitor av=new ArrayVisitor();
        av.visitBegin(adapter, Types.CLASS, params.length);
        for(int i=0;i<params.length;i++){
           paramTypes[i]=Type.getType(params[i]);
           av.visitBeginItem(adapter, i);
     loadClass(adapter,params[i]);
           av.visitEndItem(adapter);
        }
        av.visitEnd();
                
       adapter.invokeConstructor(ASM_METHOD, ASM_METHOD_CONSTRUCTOR);
       adapter.visitInsn(Opcodes.RETURN);
               
       Label end = new Label();
       adapter.visitLabel(end);
               
       adapter.endMethod();
     */

    // METHOD getName();
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_NAME, null, null, cw);
    adapter.push(method.getName());
    adapter.visitInsn(Opcodes.ARETURN);
    adapter.endMethod();

    // METHOD getModifiers();
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_MODIFIERS, null, null, cw);
    adapter.push(method.getModifiers());
    adapter.visitInsn(Opcodes.IRETURN);
    adapter.endMethod();

    // METHOD getReturnType();
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_RETURN_TYPE_AS_STRING, null, null, cw);

    adapter.push(method.getReturnType().getName());
    adapter.visitInsn(Opcodes.ARETURN);

    adapter.endMethod();

    // METHOD INVOKE
    boolean isStatic = Modifier.isStatic(method.getModifiers());
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, INVOKE, null, null, cw);
    Label start = adapter.newLabel();
    adapter.visitLabel(start);

    // load Object
    if (!isStatic) {
        adapter.visitVarInsn(Opcodes.ALOAD, 1);
        adapter.checkCast(type);
    }

    // load params
    Class<?>[] params = method.getParameterTypes();

    Type[] paramTypes = new Type[params.length];
    for (int i = 0; i < params.length; i++) {
        paramTypes[i] = Type.getType(params[i]);
    }

    for (int i = 0; i < params.length; i++) {
        adapter.visitVarInsn(Opcodes.ALOAD, 2);
        adapter.push(i);
        //adapter.visitInsn(Opcodes.ICONST_0);
        adapter.visitInsn(Opcodes.AALOAD);

        adapter.checkCast(toReferenceType(params[i], paramTypes[i]));

        // cast
        if (params[i] == boolean.class)
            adapter.invokeVirtual(Types.BOOLEAN, BOOL_VALUE);
        else if (params[i] == short.class)
            adapter.invokeVirtual(Types.SHORT, SHORT_VALUE);
        else if (params[i] == int.class)
            adapter.invokeVirtual(Types.INTEGER, INT_VALUE);
        else if (params[i] == float.class)
            adapter.invokeVirtual(Types.FLOAT, FLT_VALUE);
        else if (params[i] == long.class)
            adapter.invokeVirtual(Types.LONG, LONG_VALUE);
        else if (params[i] == double.class)
            adapter.invokeVirtual(Types.DOUBLE, DBL_VALUE);
        else if (params[i] == char.class)
            adapter.invokeVirtual(Types.CHARACTER, CHR_VALUE);
        else if (params[i] == byte.class)
            adapter.invokeVirtual(Types.BYTE, BYT_VALUE);
        //else adapter.checkCast(paramTypes[i]);

    }

    // call method
    final org.objectweb.asm.commons.Method m = new org.objectweb.asm.commons.Method(method.getName(), rtnType,
            paramTypes);
    if (isStatic)
        adapter.invokeStatic(type, m);
    else
        adapter.invokeVirtual(type, m);

    // return
    if (rtn == void.class)
        ASMConstants.NULL(adapter);

    // cast result to object
    if (rtn == boolean.class)
        adapter.invokeStatic(Types.BOOLEAN, BOOL_VALUE_OF);
    else if (rtn == short.class)
        adapter.invokeStatic(Types.SHORT, SHORT_VALUE_OF);
    else if (rtn == int.class)
        adapter.invokeStatic(Types.INTEGER, INT_VALUE_OF);
    else if (rtn == long.class)
        adapter.invokeStatic(Types.LONG, LONG_VALUE_OF);
    else if (rtn == float.class)
        adapter.invokeStatic(Types.FLOAT, FLT_VALUE_OF);
    else if (rtn == double.class)
        adapter.invokeStatic(Types.DOUBLE, DBL_VALUE_OF);
    else if (rtn == char.class)
        adapter.invokeStatic(Types.CHARACTER, CHR_VALUE_OF);
    else if (rtn == byte.class)
        adapter.invokeStatic(Types.BYTE, BYT_VALUE_OF);

    adapter.visitInsn(Opcodes.ARETURN);

    adapter.endMethod();

    if (classRoot != null) {
        Resource classFile = classRoot.getRealResource(className + ".class");
        return store(cw.toByteArray(), classFile);
    }
    return cw.toByteArray();
}

From source file:lucee.transformer.bytecode.statement.Return.java

License:Open Source License

public void _writeOut(BytecodeContext bc) throws BytecodeException {
    GeneratorAdapter adapter = bc.getAdapter();

    if (expr == null)
        ASMConstants.NULL(adapter);/* w  ww  .  j  a  v a2s  .  c  o m*/
    else
        expr.writeOut(bc, Expression.MODE_REF);

    // call finallies
    Stack<OnFinally> finallies = bc.getOnFinallyStack();
    int len = finallies.size();
    OnFinally onFinally;
    if (len > 0) {
        int rtn = adapter.newLocal(Types.OBJECT);
        adapter.storeLocal(rtn, Types.OBJECT);
        for (int i = len - 1; i >= 0; i--) {
            onFinally = finallies.get(i);
            if (!bc.insideFinally(onFinally))
                onFinally.writeOut(bc);
        }
        adapter.loadLocal(rtn, Types.OBJECT);
    }

    if (bc.getMethod().getReturnType().equals(Types.VOID)) {
        adapter.pop();
        adapter.visitInsn(Opcodes.RETURN);
    } else
        adapter.visitInsn(Opcodes.ARETURN);
}

From source file:lucee.transformer.bytecode.util.ASMUtil.java

License:Open Source License

private static void createProperty(ClassWriter cw, String classType, ASMProperty property)
        throws PageException {
    String name = property.getName();
    Type type = property.getASMType();
    Class clazz = property.getClazz();

    cw.visitField(Opcodes.ACC_PRIVATE, name, type.toString(), null, null).visitEnd();

    int load = loadFor(type);
    //int sizeOf=sizeOf(type);

    // get<PropertyName>():<type>
    Type[] types = new Type[0];
    Method method = new Method((clazz == boolean.class ? "get" : "get") + StringUtil.ucFirst(name), type,
            types);//ww  w . jav  a  2 s .  c  om
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, method, null, null, cw);

    Label start = new Label();
    adapter.visitLabel(start);

    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitFieldInsn(Opcodes.GETFIELD, classType, name, type.toString());
    adapter.returnValue();

    Label end = new Label();
    adapter.visitLabel(end);
    adapter.visitLocalVariable("this", "L" + classType + ";", null, start, end, 0);
    adapter.visitEnd();

    adapter.endMethod();

    // set<PropertyName>(object):void
    types = new Type[] { type };
    method = new Method("set" + StringUtil.ucFirst(name), Types.VOID, types);
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, method, null, null, cw);

    start = new Label();
    adapter.visitLabel(start);
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitVarInsn(load, 1);
    adapter.visitFieldInsn(Opcodes.PUTFIELD, classType, name, type.toString());

    adapter.visitInsn(Opcodes.RETURN);
    end = new Label();
    adapter.visitLabel(end);
    adapter.visitLocalVariable("this", "L" + classType + ";", null, start, end, 0);
    adapter.visitLocalVariable(name, type.toString(), null, start, end, 1);
    //adapter.visitMaxs(0, 0);//.visitMaxs(sizeOf+1, sizeOf+1);// hansx
    adapter.visitEnd();

    adapter.endMethod();

}