Example usage for org.objectweb.asm Opcodes IRETURN

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

Introduction

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

Prototype

int IRETURN

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

Click Source Link

Usage

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

License:Open Source License

private void startInternalMethod() {
    // if (e != null)
    Label notReplacedLabel = new Label();
    mv.visitFieldInsn(Opcodes.GETSTATIC, codeBlock.getClassName(), getReplaceFieldName(), executableDescriptor);
    mv.visitJumpInsn(Opcodes.IFNULL, notReplacedLabel);
    {/*from  www  . j ava  2 s  . c o m*/
        // return e.exec(returnAddress, alternativeReturnAddress, isJump);
        mv.visitFieldInsn(Opcodes.GETSTATIC, codeBlock.getClassName(), getReplaceFieldName(),
                executableDescriptor);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, executableInternalName, getExecMethodName(),
                getExecMethodDesc());
        mv.visitInsn(Opcodes.IRETURN);
    }
    mv.visitLabel(notReplacedLabel);

    if (Profiler.isProfilerEnabled()) {
        loadImm(getCodeBlock().getStartAddress());
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, profilerInternalName, "addCall", "(I)V");
    }

    if (RuntimeContext.debugCodeBlockCalls) {
        loadImm(getCodeBlock().getStartAddress());
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName, RuntimeContext.debugCodeBlockStart,
                "(I)V");
    }
}

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

License:Open Source License

public void compileExecuteInterpreter(int startAddress) {
    loadImm(startAddress);//from ww  w  . j a  v  a  2  s  .c om
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName, "executeInterpreter", "(I)I");
    endMethod();
    mv.visitInsn(Opcodes.IRETURN);
}

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

License:Open Source License

public void compileNativeCodeSequence(NativeCodeSequence nativeCodeSequence,
        NativeCodeInstruction nativeCodeInstruction) {
    // The pc can be used by native code sequences, set it to the start address of the sequence
    storePc();/*  ww  w  . j  av a2  s.c  o m*/

    visitNativeCodeSequence(nativeCodeSequence, nativeCodeInstruction.getAddress(), nativeCodeInstruction);

    if (nativeCodeSequence.isReturning()) {
        loadRegister(_ra);
        endInternalMethod();
        mv.visitInsn(Opcodes.IRETURN);
    } else if (nativeCodeSequence.isMethodReturning()) {
        endInternalMethod();
        mv.visitInsn(Opcodes.IRETURN);
    }

    // Replacing the whole CodeBlock?
    if (getCodeBlock().getLength() == nativeCodeSequence.getNumOpcodes()
            && !nativeCodeSequence.hasBranchInstruction()) {
        nativeCodeManager.setCompiledNativeCodeBlock(getCodeBlock().getStartAddress(), nativeCodeSequence);

        // Be more verbose when Debug enabled.
        // Only log "Nop" native code sequence in debug.
        if (log.isDebugEnabled() || nativeCodeSequence.getNativeCodeSequenceClass().equals(Nop.class)) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Replacing CodeBlock at 0x%08X (%08X-0x%08X, length %d) by %s",
                        getCodeBlock().getStartAddress(), getCodeBlock().getLowestAddress(),
                        codeBlock.getHighestAddress(), codeBlock.getLength(), nativeCodeSequence));
            }
        } else if (log.isInfoEnabled()) {
            log.info(String.format("Replacing CodeBlock at 0x%08X by Native Code '%s'",
                    getCodeBlock().getStartAddress(), nativeCodeSequence.getName()));
        }
    } else {
        // Be more verbose when Debug enabled
        int endAddress = getCodeInstruction().getAddress() + (nativeCodeSequence.getNumOpcodes() - 1) * 4;
        if (log.isDebugEnabled()) {
            log.debug(String.format("Replacing CodeSequence at 0x%08X-0x%08X by Native Code %s",
                    getCodeInstruction().getAddress(), endAddress, nativeCodeSequence));
        } else if (log.isInfoEnabled()) {
            log.info(String.format("Replacing CodeSequence at 0x%08X-0x%08X by Native Code '%s'",
                    getCodeInstruction().getAddress(), endAddress, nativeCodeSequence.getName()));
        }
    }
}

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

License:Open Source License

private void generateCall(CallableInfo ci, Env env) {
    /*/*w w w .jav a2 s .  c  om*/
     * local variables
     * <Callable#call>
     * 0: this
     * 1: args (list of arguments)
     * 2: env
     *
     * <Callable0#call0>
     * 0: this
     * 1: env
     *
     * <Callable1#call1>
     * 0: this
     * 1: arg0
     * 2: env
     *
     * <Callable2#call2>
     * 0: this
     * 1: arg0
     * 2: arg1
     * 3: env
     *
     * ...
     *
     */
    MethodVisitor mv = _cw.visitMethod(
            ci.mi.implCallable() ? Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL : Opcodes.ACC_FINAL, ci.mi.name(),
            Type.getMethodDescriptor(ci.retType, ci.paramTypes), null, null);

    // instruction list
    int len = ci.mi.instLen();
    // label
    Label label = null;

    // generate code
    for (int i = 0; i < len; i++) {
        Object inst = ci.mi.getInst(i);
        if (Logger.tracelevelp(env))
            Logger.trace("[asm:gen]" + i + ":\t~S", Lists.list(inst), env);

        // inst is symbol
        // -> convert tag (Symbol) to label (ASMe Label object)
        if (Data.isSymbol(inst)) {
            Symbol tag = Data.symbol(inst);
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label is null: ~S.", Lists.list(tag));
            } else if (l != label) {
                mv.visitLabel(l);
                label = l;
                if (Logger.tracelevelp(env))
                    Logger.trace("[asm:gen]" + i + ":\ttag ~S -> label ~S", Lists.list(tag, l), env);
            } else {
                if (Logger.tracelevelp(env))
                    Logger.trace("[asm:gen]" + i + ":\ttag ~S -> label ~S" + " (dup)", Lists.list(tag, l), env);
            }
            continue;
        }

        // inst must be the form of (id <arg1> <arg2> ....)
        Object id = Lists.car(inst);
        if (id == Insts.CONST) {
            /* push const on the stack. */
            Object obj = Lists.cadr(inst);
            String val = Data.string(constTable.get(obj));
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val,
                    TYPE_OBJECT.getDescriptor());
        } else if (id == Insts.VAR) {
            /* push var on the stack */
            Object var = Lists.cadr(inst);
            String val = Data.string(varTable.get(var));
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val,
                    TYPE_SYMBOL.getDescriptor());
        } else if (id == Insts.LAMBDA_LIST) {
            /* push lambdaList on the stack */
            Object var = Lists.cadr(inst);
            /* push _ll_<i> on the stack */
            String val = Data.string(llTable.get(var));
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val,
                    TYPE_LAMBDA_LIST.getDescriptor());
        } else if (id == Insts.ENV_GET) {
            /* env.get */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getInt",
                        Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getDouble",
                        Type.getMethodDescriptor(Type.DOUBLE_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getChar",
                        Type.getMethodDescriptor(Type.CHAR_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "get",
                        Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_SYMBOL }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_SET) {
            /* env.set */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.INT_TYPE }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.DOUBLE_TYPE }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.CHAR_TYPE }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, TYPE_OBJECT }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_BIND) {
            /* env.bind */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.INT_TYPE }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.DOUBLE_TYPE }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.CHAR_TYPE }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, TYPE_OBJECT }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_UNBIND) {
            /* env.unbind */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindInt",
                        Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindDouble",
                        Type.getMethodDescriptor(Type.DOUBLE_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindChar",
                        Type.getMethodDescriptor(Type.CHAR_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbind",
                        Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_SYMBOL }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_CHILD) {
            /* env.child */
            Object oldEnvVar = Lists.cadr(inst);
            Object newEnvVar = Lists.caddr(inst);
            Object oldSlot = Lists.cadr(oldEnvVar);
            Object newSlot = Lists.cadr(newEnvVar);
            int oldLocal = Data.fixnum(ci.localTable.get(oldSlot)).intValue();
            int newLocal = Data.fixnum(ci.localTable.get(newSlot)).intValue();
            if (Logger.tracelevelp(env))
                Logger.trace("[asm:gen]" + i + ":\tenv-child: local ~S -> ~S",
                        Lists.list(Data.toFixnum(oldLocal), Data.toFixnum(newLocal)), env);
            mv.visitVarInsn(Opcodes.ALOAD, oldLocal);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "child",
                    Type.getMethodDescriptor(TYPE_ENV, TYPE_NO_ARGS));
            mv.visitVarInsn(Opcodes.ASTORE, newLocal);
        } else if (id == Insts.CALL) {
            /* funcall */
            int nargs = Data.fixnum(Lists.cadr(inst)).intValue();
            String className = "lapin.eval.Funcall";
            String methodName = nargs < 0 ? "funcall" : "funcall" + nargs;
            Class rType = Object.class;
            Class[] pTypes;
            if (nargs < 0) {
                pTypes = new Class[] { Function.class, Object.class, // list of args
                        Env.class };
            } else {
                pTypes = new Class[nargs + 2];
                pTypes[0] = Function.class;
                for (int j = 0; j < nargs; j++) {
                    pTypes[j + 1] = Object.class;
                }
                pTypes[nargs + 1] = Env.class;
            }

            Type retType = Type.getType(rType);
            Type[] paramTypes = new Type[pTypes.length];
            for (int j = 0; j < pTypes.length; j++)
                paramTypes[j] = Type.getType(pTypes[j]);

            mv.visitMethodInsn(Opcodes.INVOKESTATIC, toInternalName(className), methodName,
                    Type.getMethodDescriptor(retType, paramTypes));
        } else if (id == Insts.CALL_DIRECT) {
            /*
             * public class Foo
             *   extends CompiledExpr implements Callable2 {
             *  public Object call2(Object arg0, Object arg1, Env env) {
             *   ...
             *  }
             * }
             */
            MethodInfo mi = (MethodInfo) Lists.cadr(inst);
            String className = mi.classInfo().classname();
            int nargs = mi.nargs();
            boolean rest = mi.rest();
            String methodName = mi.name();
            Class rType = mi.retType();
            Class[] pTypes = mi.paramTypes();

            Type retType = Type.getType(rType);
            Type[] paramTypes = new Type[pTypes.length];
            for (int j = 0; j < pTypes.length; j++)
                paramTypes[j] = Type.getType(pTypes[j]);

            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, toInternalName(className), methodName,
                    Type.getMethodDescriptor(retType, paramTypes));
        } else if (id == Insts.COMPILED_EXPR) {
            /*
             * public class Foo extends CompiledExpr {
             *  static public Foo SELF;
             *  ...
             * }
             */
            String className = Data.string(Lists.cadr(inst));
            String fieldName = "SELF";
            String typeName = className;
            mv.visitFieldInsn(Opcodes.GETSTATIC, toInternalName(className), fieldName,
                    toTypeDescriptor(typeName));
        } else if (id == Insts.RETURN) {
            /* return */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class) || type.equals(short.class) || type.equals(byte.class)
                    || type.equals(char.class)) {
                mv.visitInsn(Opcodes.IRETURN);
            } else if (type.equals(long.class)) {
                mv.visitInsn(Opcodes.LRETURN);
            } else if (type.equals(float.class)) {
                mv.visitInsn(Opcodes.FRETURN);
            } else if (type.equals(double.class)) {
                mv.visitInsn(Opcodes.DRETURN);
            } else if (type.equals(void.class)) {
                //mv.visitInsn(Opcodes.RETURN);
                throw new NotReachedException("unsupported returnType: ~S.", Lists.list(type));
            } else {
                mv.visitInsn(Opcodes.ARETURN);
            }
        } else if (id == Insts.IFEQ) {
            /* conditional jump */
            Symbol tag = Data.symbol(Lists.cadr(inst));
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tag));
            }
            mv.visitJumpInsn(Opcodes.IFEQ, l);
        } else if (id == Insts.IFNE) {
            /* conditional jump */
            Symbol tag = Data.symbol(Lists.cadr(inst));
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tag));
            }
            mv.visitJumpInsn(Opcodes.IFNE, l);
        } else if (id == Insts.GOTO) {
            /* jump */
            Symbol tag = Data.symbol(Lists.cadr(inst));
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tag));
            }
            mv.visitJumpInsn(Opcodes.GOTO, l);
        } else if (id == Insts.LOAD) {
            /* local -> stack */
            Object localVar = Lists.cadr(inst);
            Object slot = Lists.cadr(localVar);
            Class type = Data.javaClass(Lists.caddr(localVar));
            int local = Data.fixnum(ci.localTable.get(slot)).intValue();
            int op = Type.getType(type).getOpcode(Opcodes.ILOAD);
            if (Logger.tracelevelp(env))
                Logger.trace("[asm:gen]" + i + ":\tload: local=~S type=~S",
                        Lists.list(Data.toFixnum(local), type), env);
            mv.visitVarInsn(op, local);
        } else if (id == Insts.STORE) {
            /* stack -> local */
            Object localVar = Lists.cadr(inst);
            Object slot = Lists.cadr(localVar);
            Class type = Data.javaClass(Lists.caddr(localVar));
            int local = Data.fixnum(ci.localTable.get(slot)).intValue();
            int op = Type.getType(type).getOpcode(Opcodes.ISTORE);
            if (Logger.tracelevelp(env))
                Logger.trace("[asm:gen]" + i + ":\tstore: local=~S type=~S",
                        Lists.list(Data.toFixnum(local), type), env);
            mv.visitVarInsn(op, local);
        } else if (id == Insts.POP) {
            /* pop a value and discard it */
            Class type = Data.javaClass(Lists.cadr(inst));
            int op;
            switch (Classes.sizeOf(type)) {
            case 1:
                op = Opcodes.POP;
                break;
            case 2:
                op = Opcodes.POP2;
                break;
            default:
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
            mv.visitInsn(op);
        } else if (id == Insts.DUP) {
            /* peek a value and duplicate it */
            Class type = Data.javaClass(Lists.cadr(inst));
            int op;
            switch (Classes.sizeOf(type)) {
            case 1:
                op = Opcodes.DUP;
                break;
            case 2:
                op = Opcodes.DUP2;
                break;
            default:
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
            mv.visitInsn(op);
        } else if (id == Insts.PUSH) {
            /* push a constant */
            Object val = Lists.cadr(inst);
            if (Data.isJavaBoolean(val)) {
                if (Data.javaBoolean(val).booleanValue())
                    mv.visitInsn(Opcodes.ICONST_1);
                else
                    mv.visitInsn(Opcodes.ICONST_0);
            } else if (val instanceof Byte || val instanceof Short || val instanceof Integer) {
                int n = Data.javaNumber(val).intValue();
                if (n == -1)
                    mv.visitInsn(Opcodes.ICONST_M1);
                else if (n == 0)
                    mv.visitInsn(Opcodes.ICONST_0);
                else if (n == 1)
                    mv.visitInsn(Opcodes.ICONST_1);
                else if (n == 2)
                    mv.visitInsn(Opcodes.ICONST_2);
                else if (n == 3)
                    mv.visitInsn(Opcodes.ICONST_3);
                else if (n == 4)
                    mv.visitInsn(Opcodes.ICONST_4);
                else if (n == 5)
                    mv.visitInsn(Opcodes.ICONST_5);
                else if (Byte.MIN_VALUE <= n && n <= Byte.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.BIPUSH, n);
                else if (Short.MIN_VALUE <= n && n <= Short.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.SIPUSH, n);
                else
                    mv.visitLdcInsn(Data.toFixnum(n));
            } else if (val instanceof Long) {
                long n = Data.javaNumber(val).longValue();
                if (n == 0L)
                    mv.visitInsn(Opcodes.LCONST_0);
                else if (n == 1L)
                    mv.visitInsn(Opcodes.LCONST_1);
                else
                    mv.visitLdcInsn(val);
            } else if (val instanceof Float) {
                float n = Data.javaNumber(val).floatValue();
                if (n == 0.0f)
                    mv.visitInsn(Opcodes.FCONST_0);
                else if (n == 1.0f)
                    mv.visitInsn(Opcodes.FCONST_1);
                else if (n == 2.0f)
                    mv.visitInsn(Opcodes.FCONST_2);
                else
                    mv.visitLdcInsn(val);
            } else if (val instanceof Double) {
                double n = Data.javaNumber(val).doubleValue();
                if (n == 0.0)
                    mv.visitInsn(Opcodes.DCONST_0);
                else if (n == 1.0)
                    mv.visitInsn(Opcodes.DCONST_1);
                else
                    mv.visitLdcInsn(val);
            } else if (Data.isCharacter(val)) {
                Character c = Data.character(val);
                int n = (int) c.charValue();
                if (Byte.MIN_VALUE <= n && n <= Byte.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.BIPUSH, n);
                else if (Short.MIN_VALUE <= n && n <= Short.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.SIPUSH, n);
                else
                    mv.visitLdcInsn(Data.toFixnum(n));
            } else if (Data.isString(val)) {
                mv.visitLdcInsn(val);
            } else {
                throw new NotReachedException("cannot push: ~S.", Lists.list(val));
            }
        } else if (id == Insts.GET) {
            Field f = Data.javaField(Lists.cadr(inst));
            String fieldName = f.getName();
            Class c = f.getDeclaringClass();
            String className = c.getName();
            Class t = f.getType();
            String typeName = t.getName();

            boolean isStatic = Classes.isStatic(f);
            int op = isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD;

            mv.visitFieldInsn(op, toInternalName(className), fieldName, toTypeDescriptor(typeName));
        } else if (id == Insts.PUT) {
            Field f = Data.javaField(Lists.cadr(inst));
            String fieldName = f.getName();
            Class c = f.getDeclaringClass();
            String className = c.getName();
            Class t = f.getType();
            String typeName = t.getName();

            boolean isStatic = Classes.isStatic(f);
            int op = isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD;

            mv.visitFieldInsn(op, toInternalName(className), fieldName, toTypeDescriptor(typeName));
        } else if (id == Insts.INVOKE) {
            Method m = Data.javaMethod(Lists.cadr(inst));
            String methodName = m.getName();
            Class c = m.getDeclaringClass();
            String className = c.getName();
            Class rType = m.getReturnType();
            Class[] pTypes = m.getParameterTypes();
            if (rType.equals(void.class)) {
                throw new NotReachedException("unsupported returnType: ~S.", Lists.list(rType));
            }
            Type retType = Type.getType(rType);
            Type[] paramTypes = new Type[pTypes.length];
            for (int j = 0; j < pTypes.length; j++)
                paramTypes[j] = Type.getType(pTypes[j]);

            boolean isStatic = Classes.isStatic(m);
            boolean isInterface = c.isInterface();
            int op = isStatic ? Opcodes.INVOKESTATIC
                    : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;

            mv.visitMethodInsn(op, toInternalName(className), methodName,
                    Type.getMethodDescriptor(retType, paramTypes));
        } else if (id == Insts.CHECKCAST) {
            Class c = Data.javaClass(Lists.cadr(inst));
            Type t = Type.getType(c);
            mv.visitTypeInsn(Opcodes.CHECKCAST, t.getInternalName());
        } else if (id == Insts.THROW) {
            mv.visitInsn(Opcodes.ATHROW);
        } else if (id == Insts.CATCH) {
            Symbol tagS = Data.symbol(Lists.cadr(inst));
            Symbol tagE = Data.symbol(Lists.caddr(inst));
            Symbol tagH = Data.symbol(Lists.cadddr(inst));
            String className;
            if (Lists.isEnd(Lists.cddddr(inst))) {
                className = null;
            } else {
                Class c = Data.javaClass(Lists.car(Lists.cddddr(inst)));
                className = toInternalName(c.getName());
            }
            Label labelS = (Label) ci.labelTable.get(tagS);
            if (labelS == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tagS));
            }
            Label labelE = (Label) ci.labelTable.get(tagE);
            if (labelE == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tagE));
            }
            Label labelH = (Label) ci.labelTable.get(tagH);
            if (labelH == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tagH));
            }
            mv.visitTryCatchBlock(labelS, labelE, labelH, className);
        }
        //else if (id == Insts.CATCH_FROM ||
        //         id == Insts.CATCH_TO ||
        //         id == Insts.CATCH_HANDLER) {
        //    /* nothing emitted */
        //    continue;
        //}
        else {
            throw new NotReachedException("unknown inst: ~S.", Lists.list(inst));
        }
    }
    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  w  w .ja  v a  2  s . co  m*/

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

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);/*from w ww .ja  v a2 s.  c o 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.util.JavaProxyFactory.java

License:Open Source License

private static void _createMethod(ClassWriter cw, Map<String, Class> mDone, Method src, String className)
        throws IOException {
    Class<?>[] classArgs = src.getParameterTypes();
    Class<?> classRtn = src.getReturnType();

    String str = src.getName() + "(" + Reflector.getDspMethods(classArgs) + ")";
    Class rtnClass = mDone.get(str);
    if (rtnClass != null) {
        if (rtnClass != classRtn)
            throw new IOException("there is a conflict with method [" + str
                    + "], this method is declared more than once with different return types.");
        return;//from  w  w w.  j a  va2  s  . co m
    }
    mDone.put(str, classRtn);

    Type[] typeArgs = ASMUtil.toTypes(classArgs);
    Type typeRtn = Type.getType(classRtn);

    org.objectweb.asm.commons.Method method = new org.objectweb.asm.commons.Method(src.getName(), typeRtn,
            typeArgs);
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, method, null, null,
            cw);
    //BytecodeContext bc = new BytecodeContext(statConstr,constr,null,null,keys,cw,className,adapter,method,writeLog);
    Label start = adapter.newLabel();
    adapter.visitLabel(start);

    //JavaProxy.call(cfc,"add",new Object[]{arg0})
    // config
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitFieldInsn(Opcodes.GETFIELD, className, "config", CONFIG_WEB_NAME);

    // cfc
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitFieldInsn(Opcodes.GETFIELD, className, "cfc", COMPONENT_NAME);

    // name
    adapter.push(src.getName());

    // arguments
    ArrayVisitor av = new ArrayVisitor();
    av.visitBegin(adapter, Types.OBJECT, typeArgs.length);
    for (int y = 0; y < typeArgs.length; y++) {
        av.visitBeginItem(adapter, y);
        adapter.loadArg(y);
        if (classArgs[y] == boolean.class)
            adapter.invokeStatic(JAVA_PROXY, _BOOLEAN);
        else if (classArgs[y] == byte.class)
            adapter.invokeStatic(JAVA_PROXY, _BYTE);
        else if (classArgs[y] == char.class)
            adapter.invokeStatic(JAVA_PROXY, _CHAR);
        else if (classArgs[y] == double.class)
            adapter.invokeStatic(JAVA_PROXY, _DOUBLE);
        else if (classArgs[y] == float.class)
            adapter.invokeStatic(JAVA_PROXY, _FLOAT);
        else if (classArgs[y] == int.class)
            adapter.invokeStatic(JAVA_PROXY, _INT);
        else if (classArgs[y] == long.class)
            adapter.invokeStatic(JAVA_PROXY, _LONG);
        else if (classArgs[y] == short.class)
            adapter.invokeStatic(JAVA_PROXY, _SHORT);
        else {
            adapter.invokeStatic(JAVA_PROXY, _OBJECT);
        }

        av.visitEndItem(adapter);
    }
    av.visitEnd();
    adapter.invokeStatic(JAVA_PROXY, CALL);

    //JavaProxy.to...(...);
    int rtn = Opcodes.IRETURN;
    if (classRtn == boolean.class)
        adapter.invokeStatic(JAVA_PROXY, TO_BOOLEAN);
    else if (classRtn == byte.class)
        adapter.invokeStatic(JAVA_PROXY, TO_BYTE);
    else if (classRtn == char.class)
        adapter.invokeStatic(JAVA_PROXY, TO_CHAR);
    else if (classRtn == double.class) {
        rtn = Opcodes.DRETURN;
        adapter.invokeStatic(JAVA_PROXY, TO_DOUBLE);
    } else if (classRtn == float.class) {
        rtn = Opcodes.FRETURN;
        adapter.invokeStatic(JAVA_PROXY, TO_FLOAT);
    } else if (classRtn == int.class)
        adapter.invokeStatic(JAVA_PROXY, TO_INT);
    else if (classRtn == long.class) {
        rtn = Opcodes.LRETURN;
        adapter.invokeStatic(JAVA_PROXY, TO_LONG);
    } else if (classRtn == short.class)
        adapter.invokeStatic(JAVA_PROXY, TO_SHORT);
    else if (classRtn == void.class) {
        rtn = Opcodes.RETURN;
        adapter.pop();
    } else {
        rtn = Opcodes.ARETURN;
        adapter.checkCast(typeRtn);
    }

    /*mv = cw.visitMethod(ACC_PUBLIC, "add", "(Ljava/lang/Object;)Z", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(20, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "Test", "cfc", "Ljava/lang/Object;");
    mv.visitLdcInsn("add");
    mv.visitInsn(ICONST_1);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKESTATIC, "JavaProxy", "call", "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;");
    mv.visitMethodInsn(INVOKESTATIC, "JavaProxy", "toBoolean", "(Ljava/lang/Object;)Z");
    mv.visitInsn(IRETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "LTest;", null, l0, l1, 0);
    mv.visitLocalVariable("arg0", "Ljava/lang/Object;", null, l0, l1, 1);
    mv.visitMaxs(6, 2);
    mv.visitEnd();*/

    adapter.visitInsn(rtn);
    adapter.endMethod();

}

From source file:me.themallard.bitmmo.api.transformer.Transformer.java

License:Open Source License

protected final void createGetter(ClassNode cn, FieldNode fn, String name) {
    MethodVisitor mv = cn.visitMethod(Opcodes.ACC_PUBLIC, name, "()" + fn.desc, null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);//from w  ww  . ja  va2s  .  c  o m
    mv.visitFieldInsn(Opcodes.GETFIELD, cn.name, fn.name, fn.desc);
    mv.visitInsn(Type.getType(fn.desc).getOpcode(Opcodes.IRETURN));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:me.themallard.bitmmo.impl.plugin.playerhook.PlayerPlugin.java

License:Open Source License

@Override
public void run(ClassNode cn) {
    MethodVisitor mv = cn.visitMethod(Opcodes.ACC_PUBLIC, "getPosition",
            "()" + "Lme/themallard/bitmmo/impl/plugin/position/IPosition;", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);/*from  w  w  w  .  j  av  a2  s. c om*/
    mv.visitFieldInsn(Opcodes.GETFIELD, "Entity", "position", "LPosition;");
    mv.visitInsn(
            Type.getType("Lme/themallard/bitmmo/impl/plugin/position/IPosition;").getOpcode(Opcodes.IRETURN));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:net.cazzar.corelib.asm.MethodTransformer.java

License:Open Source License

private boolean isReturn(AbstractInsnNode node) {
    switch (node.getOpcode()) {
    case Opcodes.RET:
    case Opcodes.RETURN:
    case Opcodes.ARETURN:
    case Opcodes.DRETURN:
    case Opcodes.FRETURN:
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
        return true;

    default:// w  w  w . j  a v a2  s  .  c o  m
        return false;
    }
}