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:com.google.devtools.build.android.desugar.BytecodeTypeInference.java

License:Open Source License

@Override
public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
    case Opcodes.INEG:
    case Opcodes.LNEG:
    case Opcodes.FNEG:
    case Opcodes.DNEG:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
    case Opcodes.RETURN:
        break;/*from  w  ww  . j  ava2 s  . c  o m*/
    case Opcodes.ACONST_NULL:
        push(InferredType.NULL);
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        push(InferredType.INT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        push(InferredType.FLOAT);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.IALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD:
        pop(2);
        push(InferredType.INT);
        break;
    case Opcodes.LALOAD:
    case Opcodes.D2L:
        pop(2);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.DALOAD:
    case Opcodes.L2D:
        pop(2);
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.AALOAD:
        InferredType arrayType = pop(2);
        InferredType elementType = arrayType.getElementTypeIfArrayOrThrow();
        push(elementType);
        break;
    case Opcodes.IASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
    case Opcodes.FASTORE:
    case Opcodes.AASTORE:
        pop(3);
        break;
    case Opcodes.LASTORE:
    case Opcodes.DASTORE:
        pop(4);
        break;
    case Opcodes.POP:
    case Opcodes.IRETURN:
    case Opcodes.FRETURN:
    case Opcodes.ARETURN:
    case Opcodes.ATHROW:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
        pop();
        break;
    case Opcodes.POP2:
    case Opcodes.LRETURN:
    case Opcodes.DRETURN:
        pop(2);
        break;
    case Opcodes.DUP:
        push(top());
        break;
    case Opcodes.DUP_X1: {
        InferredType top = pop();
        InferredType next = pop();
        push(top);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP_X2: {
        InferredType top = pop();
        InferredType next = pop();
        InferredType bottom = pop();
        push(top);
        push(bottom);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2: {
        InferredType top = pop();
        InferredType next = pop();
        push(next);
        push(top);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2_X1: {
        InferredType top = pop();
        InferredType next = pop();
        InferredType bottom = pop();
        push(next);
        push(top);
        push(bottom);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2_X2: {
        InferredType t1 = pop();
        InferredType t2 = pop();
        InferredType t3 = pop();
        InferredType t4 = pop();
        push(t2);
        push(t1);
        push(t4);
        push(t3);
        push(t2);
        push(t1);
        break;
    }
    case Opcodes.SWAP: {
        InferredType top = pop();
        InferredType next = pop();
        push(top);
        push(next);
        break;
    }
    case Opcodes.IADD:
    case Opcodes.ISUB:
    case Opcodes.IMUL:
    case Opcodes.IDIV:
    case Opcodes.IREM:
    case Opcodes.ISHL:
    case Opcodes.ISHR:
    case Opcodes.IUSHR:
    case Opcodes.IAND:
    case Opcodes.IOR:
    case Opcodes.IXOR:
    case Opcodes.L2I:
    case Opcodes.D2I:
    case Opcodes.FCMPL:
    case Opcodes.FCMPG:
        pop(2);
        push(InferredType.INT);
        break;

    case Opcodes.LADD:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
    case Opcodes.LDIV:
    case Opcodes.LREM:
    case Opcodes.LAND:
    case Opcodes.LOR:
    case Opcodes.LXOR:
        pop(4);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;

    case Opcodes.LSHL:
    case Opcodes.LSHR:
    case Opcodes.LUSHR:
        pop(3);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.I2L:
    case Opcodes.F2L:
        pop();
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.I2F:
        pop();
        push(InferredType.FLOAT);
        break;

    case Opcodes.LCMP:
    case Opcodes.DCMPG:
    case Opcodes.DCMPL:
        pop(4);
        push(InferredType.INT);
        break;

    case Opcodes.I2D:
    case Opcodes.F2D:
        pop();
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.F2I:
    case Opcodes.ARRAYLENGTH:
        pop();
        push(InferredType.INT);
        break;
    case Opcodes.FALOAD:
    case Opcodes.FADD:
    case Opcodes.FSUB:
    case Opcodes.FMUL:
    case Opcodes.FDIV:
    case Opcodes.FREM:
    case Opcodes.L2F:
    case Opcodes.D2F:
        pop(2);
        push(InferredType.FLOAT);
        break;

    case Opcodes.DADD:
    case Opcodes.DSUB:
    case Opcodes.DMUL:
    case Opcodes.DDIV:
    case Opcodes.DREM:
        pop(4);
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    default:
        throw new RuntimeException("Unhandled opcode " + opcode);
    }
    super.visitInsn(opcode);
}

From source file:com.google.devtools.build.android.desugar.CoreLibrarySupport.java

License:Open Source License

private void makeDispatchHelperMethod(ClassVisitor helper, EmulatedMethod method,
        ImmutableList<Class<?>> typechecks) {
    checkArgument(method.owner().isInterface());
    String owner = method.owner().getName().replace('.', '/');
    Type methodType = Type.getMethodType(method.descriptor());
    String companionDesc = InterfaceDesugaring.companionDefaultMethodDescriptor(owner, method.descriptor());
    MethodVisitor dispatchMethod = helper.visitMethod(method.access() | Opcodes.ACC_STATIC, method.name(),
            companionDesc, /*signature=*/ null, // signature is invalid due to extra "receiver" argument
            method.exceptions().toArray(EMPTY_LIST));

    dispatchMethod.visitCode();/*from   ww w. j  a  v  a2 s .  c o m*/
    {
        // See if the receiver might come with its own implementation of the method, and call it.
        // We do this by testing for the interface type created by EmulatedInterfaceRewriter
        Label fallthrough = new Label();
        String emulationInterface = renameCoreLibrary(owner);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, emulationInterface);
        dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, emulationInterface);

        visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
        dispatchMethod.visitMethodInsn(Opcodes.INVOKEINTERFACE, emulationInterface, method.name(),
                method.descriptor(), /*itf=*/ true);
        dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

        dispatchMethod.visitLabel(fallthrough);
        // Trivial frame for the branch target: same empty stack as before
        dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME);
    }

    // Next, check for subtypes with specialized implementations and call them
    for (Class<?> tested : typechecks) {
        Label fallthrough = new Label();
        String testedName = tested.getName().replace('.', '/');
        // In case of a class this must be a member move; for interfaces use the companion.
        String target = tested.isInterface() ? InterfaceDesugaring.getCompanionClassName(testedName)
                : checkNotNull(memberMoves.get(rewriter.unprefix(testedName) + '#' + method.name()));
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, testedName);
        dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, testedName); // make verifier happy

        visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
        dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, target, method.name(),
                InterfaceDesugaring.companionDefaultMethodDescriptor(testedName, method.descriptor()),
                /*itf=*/ false);
        dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

        dispatchMethod.visitLabel(fallthrough);
        // Trivial frame for the branch target: same empty stack as before
        dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME);
    }

    // Call static type's default implementation in companion class
    dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
    visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
    dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, InterfaceDesugaring.getCompanionClassName(owner),
            method.name(), companionDesc, /*itf=*/ false);
    dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

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

From source file:com.google.devtools.build.android.desugar.LambdaDesugaring.java

License:Open Source License

@Override
public void visitEnd() {
    for (Map.Entry<Handle, MethodReferenceBridgeInfo> bridge : bridgeMethods.entrySet()) {
        Handle original = bridge.getKey();
        Handle neededMethod = bridge.getValue().bridgeMethod();
        checkState(/*w w  w .  java 2s. c  o m*/
                neededMethod.getTag() == Opcodes.H_INVOKESTATIC
                        || neededMethod.getTag() == Opcodes.H_INVOKEVIRTUAL,
                "Cannot generate bridge method %s to reach %s", neededMethod, original);
        checkState(bridge.getValue().referenced() != null, "Need referenced method %s to generate bridge %s",
                original, neededMethod);

        int access = Opcodes.ACC_BRIDGE | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_FINAL;
        if (neededMethod.getTag() == Opcodes.H_INVOKESTATIC) {
            access |= Opcodes.ACC_STATIC;
        }
        MethodVisitor bridgeMethod = super.visitMethod(access, neededMethod.getName(), neededMethod.getDesc(),
                (String) null, toInternalNames(bridge.getValue().referenced().getExceptionTypes()));

        // Bridge is a factory method calling a constructor
        if (original.getTag() == Opcodes.H_NEWINVOKESPECIAL) {
            bridgeMethod.visitTypeInsn(Opcodes.NEW, original.getOwner());
            bridgeMethod.visitInsn(Opcodes.DUP);
        }

        int slot = 0;
        if (neededMethod.getTag() != Opcodes.H_INVOKESTATIC) {
            bridgeMethod.visitVarInsn(Opcodes.ALOAD, slot++);
        }
        Type neededType = Type.getMethodType(neededMethod.getDesc());
        for (Type arg : neededType.getArgumentTypes()) {
            bridgeMethod.visitVarInsn(arg.getOpcode(Opcodes.ILOAD), slot);
            slot += arg.getSize();
        }
        bridgeMethod.visitMethodInsn(invokeOpcode(original), original.getOwner(), original.getName(),
                original.getDesc(), original.isInterface());
        bridgeMethod.visitInsn(neededType.getReturnType().getOpcode(Opcodes.IRETURN));

        bridgeMethod.visitMaxs(0, 0); // rely on class writer to compute these
        bridgeMethod.visitEnd();
    }
    super.visitEnd();
}

From source file:com.google.devtools.build.wireless.testing.java.injector.InjectorMethodAdapter.java

License:Apache License

/**
 * Tells if an opcode is a return instruction.
 * <br>//  w  ww. j  a  va2s. c  o m
 * Helper function.
 * 
 * @param opcode The opcode to check.
 * @return <code>true</code> if the parameter indicates a return instruction.
 */
public static boolean isReturnInstruction(int opcode) {
    return opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN;
}

From source file:com.google.devtools.build.wireless.testing.java.injector.WhiteBoxMethodAdapter.java

License:Apache License

/**
 * Visits the return instruction by logging the return type (if any), the call
 * duration in msec, and the return time.
 *//* ww w  .  j av a 2 s .c  o  m*/
@Override
public void visitInsn(int opcode) {
    // TODO: exceptions are not handled yet.
    if (matchOwnerClass && matchMethod && ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN))) {
        logger.info("\t[WhiteBox][Method return]: " + this.methodName + " " + description);

        if (!stackServant.getReturnType().equals("V")) {
            stackServant.duplicateAndStore(stackServant.getReturnType(), returnIndexInStack);
        }

        printServant.startPrinting();
        printServant.printString("[Return]\t");

        printCurrentClass();

        printServant.printString("." + methodName + " Completed: ");

        timeServant.loadCurrentTimeMillis();
        printServant.print("J");

        printServant.printString(" Execution_time: ");

        // time
        timeServant.stopCountingTime(timeIndexInStack);
        printServant.print("J");

        // add return type to output string
        if (stackServant.returnType.equals("V") == false) {
            printServant.printString(" return ");
            stackServant.load(stackServant.returnType, returnIndexInStack);
            printServant.println(stackServant.getReturnType());
        } else {
            printServant.println();
        }

        // save and print
        this.printServant.stopPrinting();
    }
    mv.visitInsn(opcode);
}

From source file:com.google.gwt.dev.shell.rewrite.RewriteSingleJsoImplDispatches.java

License:Apache License

/**
 * For regular Java objects that implement a SingleJsoImpl interface, write
 * instance trampoline dispatchers for mangled method names to the
 * implementing method.//from w  ww .j  a  va  2  s .  c  o m
 */
private void writeTrampoline(String stubIntr) {
    /*
     * This is almost the same kind of trampoline as the ones generated in
     * WriteJsoImpl, however there are enough small differences between the
     * semantics of the dispatches that would make a common implementation far
     * more awkward than the duplication of code.
     */
    for (String mangledName : toImplement(stubIntr)) {
        for (Method method : jsoData.getDeclarations(mangledName)) {

            Method toCall = new Method(method.getName(), method.getDescriptor());

            // Must not be final
            MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, mangledName,
                    method.getDescriptor(), null, null);
            if (mv != null) {
                mv.visitCode();

                /*
                 * It just so happens that the stack and local variable sizes are the
                 * same, but they're kept distinct to aid in clarity should the
                 * dispatch logic change.
                 *
                 * These start at 1 because we need to load "this" onto the stack
                 */
                int var = 1;
                int size = 1;

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

                // then the rest of the arguments
                for (Type t : toCall.getArgumentTypes()) {
                    size += t.getSize();
                    mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), var);
                    var += t.getSize();
                }

                // Make sure there's enough room for the return value
                size = Math.max(size, toCall.getReturnType().getSize());

                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, currentTypeName, toCall.getName(),
                        toCall.getDescriptor(), false);
                mv.visitInsn(toCall.getReturnType().getOpcode(Opcodes.IRETURN));
                mv.visitMaxs(size, var);
                mv.visitEnd();
            }
        }
    }
}

From source file:com.google.gwt.jvm.asm.NativeMethodDelegatingVisitor.java

License:Apache License

private void nativeDelegate_invokeReturnObject() {
    String methodReturn = descriptor.getReturnDesc();
    if (methodReturn == null) {
        throw new NullPointerException(descriptor.getMethodDesc());
    }/*w  ww . jav  a 2  s.c om*/
    switch (methodReturn.charAt(0)) {
    case 'L':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturnObject",
                "()Ljava/lang/Object;");
        delegate.visitTypeInsn(Opcodes.CHECKCAST, methodReturn.substring(1, methodReturn.length() - 1));
        delegate.visitInsn(Opcodes.ARETURN);
        break;
    case 'Z':
    case 'B':
    case 'S':
    case 'C':
    case 'I':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.IRETURN);
        break;
    case 'J':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.LRETURN);
        break;
    case 'F':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.FRETURN);
        break;
    case 'D':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.DRETURN);
        break;
    case 'V':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.RETURN);
        break;
    default:
        throw new IllegalStateException(methodReturn);
    }
}

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitInsn(final int opcode) {
    switch (opcode) {
    case Opcodes.ACONST_NULL:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Load(lineNumber, new Constant(null, JavaType.OBJECT)));
            }//from  www.ja  va2s .  co  m
        });
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        loadConstant(opcode - Opcodes.ICONST_M1 - 1, JavaType.INT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        loadConstant(opcode - Opcodes.LCONST_0, JavaType.LONG);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        loadConstant(opcode - Opcodes.FCONST_0, JavaType.FLOAT);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        loadConstant(opcode - Opcodes.DCONST_0, JavaType.DOUBLE);
        break;
    case Opcodes.IALOAD:
        recordArrayLoad(JavaType.INT);
        break;
    case Opcodes.LALOAD:
        recordArrayLoad(JavaType.LONG);
        break;
    case Opcodes.FALOAD:
        recordArrayLoad(JavaType.FLOAT);
        break;
    case Opcodes.DALOAD:
        recordArrayLoad(JavaType.DOUBLE);
        break;
    case Opcodes.AALOAD:
        recordArrayLoad(JavaType.OBJECT);
        break;
    case Opcodes.BALOAD:
        recordArrayLoad(JavaType.BYTE);
        break;
    case Opcodes.CALOAD:
        recordArrayLoad(JavaType.CHAR);
        break;
    case Opcodes.SALOAD:
        recordArrayLoad(JavaType.SHORT);
        break;

    case Opcodes.IASTORE:
        recordArrayStore(JavaType.INT);
        break;
    case Opcodes.LASTORE:
        recordArrayStore(JavaType.LONG);
        break;
    case Opcodes.FASTORE:
        recordArrayStore(JavaType.FLOAT);
        break;
    case Opcodes.DASTORE:
        recordArrayStore(JavaType.DOUBLE);
        break;
    case Opcodes.AASTORE:
        recordArrayStore(JavaType.OBJECT);
        break;
    case Opcodes.BASTORE:
        recordArrayStore(JavaType.BYTE);
        break;
    case Opcodes.CASTORE:
        recordArrayStore(JavaType.CHAR);
        break;
    case Opcodes.SASTORE:
        recordArrayStore(JavaType.SHORT);
        break;
    case Opcodes.POP:
    case Opcodes.POP2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Pop(lineNumber, opcode - Opcodes.POP + 1));
            }
        });
        break;
    case Opcodes.DUP:
    case Opcodes.DUP_X1:
    case Opcodes.DUP_X2:
        recorder.add(new Runnable() {
            public void run() {
                int offset = opcode - Opcodes.DUP;
                block.addOp(new Duplicate(lineNumber, offset));
            }
        });
        break;
    case Opcodes.DUP2:
    case Opcodes.DUP2_X1:
    case Opcodes.DUP2_X2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Duplicate2(lineNumber, opcode - Opcodes.DUP2));
            }
        });
        break;
    case Opcodes.SWAP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Swap(lineNumber));
            }
        });
        break;
    case Opcodes.IRETURN:
        _return(JavaType.INT);
        break;
    case Opcodes.FRETURN:
        _return(JavaType.FLOAT);
        break;
    case Opcodes.ARETURN:
        _return(JavaType.OBJECT);
        break;
    case Opcodes.LRETURN:
        _return(JavaType.LONG);
        break;
    case Opcodes.DRETURN:
        _return(JavaType.DOUBLE);
        break;
    case Opcodes.ATHROW:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Throw(lineNumber));
            }
        });
        break;
    case Opcodes.RETURN:
        _return(JavaType.VOID);
        break;
    case Opcodes.LCMP:
        operation("cmp", JavaType.LONG, JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.FCMPL:
        operation("cmpl", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.FCMPG:
        operation("cmpg", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.DCMPL:
        operation("cmpl", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.DCMPG:
        operation("cmpg", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.LSHL:
        operation("shl", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LSHR:
        operation("shr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LUSHR:
        operation("ushr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LADD:
        operation("add", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LSUB:
        operation("sub", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LDIV:
        operation("div", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LREM:
        operation("rem", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LAND:
        operation("and", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LOR:
        operation("or", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LXOR:
        operation("xor", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LMUL:
        operation("mul", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.FADD:
        operation("add", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FSUB:
        operation("sub", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FMUL:
        operation("mul", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FREM:
        operation("rem", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FDIV:
        operation("div", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.ISHL:
        operation("shl", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISHR:
        operation("shr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IUSHR:
        operation("ushr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IADD:
        operation("add", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISUB:
        operation("sub", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IMUL:
        operation("mul", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IDIV:
        operation("div", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IREM:
        operation("rem", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IAND:
        operation("and", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IOR:
        operation("or", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IXOR:
        operation("xor", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.DSUB:
        operation("sub", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DADD:
        operation("add", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DMUL:
        operation("mul", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DDIV:
        operation("div", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DREM:
        operation("rem", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.L2I:
        convert(JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.L2F:
        convert(JavaType.LONG, JavaType.FLOAT);
        break;
    case Opcodes.L2D:
        convert(JavaType.LONG, JavaType.DOUBLE);
        break;
    case Opcodes.LNEG:
        operation("neg", JavaType.LONG, null, JavaType.LONG);
        break;
    case Opcodes.F2I:
        convert(JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.F2L:
        convert(JavaType.FLOAT, JavaType.LONG);
        break;
    case Opcodes.FNEG:
        operation("neg", JavaType.FLOAT, null, JavaType.FLOAT);
        break;
    case Opcodes.F2D:
        convert(JavaType.FLOAT, JavaType.DOUBLE);
        break;
    case Opcodes.D2I:
        convert(JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.D2L:
        convert(JavaType.DOUBLE, JavaType.LONG);
        break;
    case Opcodes.D2F:
        convert(JavaType.DOUBLE, JavaType.FLOAT);
        break;
    case Opcodes.DNEG:
        operation("neg", JavaType.DOUBLE, null, JavaType.DOUBLE);
        break;
    case Opcodes.I2L:
        convert(JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.I2F:
        convert(JavaType.INT, JavaType.FLOAT);
        break;
    case Opcodes.I2D:
        convert(JavaType.INT, JavaType.DOUBLE);
        break;
    case Opcodes.I2B:
        convert(JavaType.INT, JavaType.BYTE);
        break;
    case Opcodes.I2C:
        convert(JavaType.INT, JavaType.CHAR);
        break;
    case Opcodes.I2S:
        convert(JavaType.INT, JavaType.SHORT);
        break;
    case Opcodes.INEG:
        operation("neg", JavaType.INT, null, JavaType.INT);
        break;
    case Opcodes.ARRAYLENGTH:
        operation("arraylength", JavaType.OBJECT.toArray(), null, JavaType.INT);
        break;
    case Opcodes.MONITORENTER:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorEnter(lineNumber));
            }
        });
        break;
    case Opcodes.MONITOREXIT:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorExit(lineNumber));
            }
        });
        break;
    case Opcodes.NOP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Transform(lineNumber, "NOP", null, null, null));
            }
        });
    }
}

From source file:com.googlecode.gwt.test.internal.rewrite.RewriteSingleJsoImplDispatches.java

License:Apache License

/**
 * For regular Java objects that implement a SingleJsoImpl interface, write instance trampoline
 * dispatchers for mangled method names to the implementing method.
 *///  www.  ja va2s  . co m
private void writeTrampoline(String stubIntr) {
    /*
     * This is almost the same kind of trampoline as the ones generated in WriteJsoImpl, however
     * there are enough small differences between the semantics of the dispatches that would make
     * a common implementation far more awkward than the duplication of code.
     */
    for (String mangledName : toImplement(stubIntr)) {
        for (Method method : jsoData.getDeclarations(mangledName)) {

            Method toCall = new Method(method.getName(), method.getDescriptor());

            // Must not be final
            MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, mangledName,
                    method.getDescriptor(), null, null);
            if (mv != null) {
                mv.visitCode();

                /*
                 * It just so happens that the stack and local variable sizes are the same, but
                 * they're kept distinct to aid in clarity should the dispatch logic change.
                 *
                 * These start at 1 because we need to load "this" onto the stack
                 */
                int var = 1;
                int size = 1;

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

                // then the rest of the arguments
                for (Type t : toCall.getArgumentTypes()) {
                    size += t.getSize();
                    mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), var);
                    var += t.getSize();
                }

                // Make sure there's enough room for the return value
                size = Math.max(size, toCall.getReturnType().getSize());

                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, currentTypeName, toCall.getName(),
                        toCall.getDescriptor());
                mv.visitInsn(toCall.getReturnType().getOpcode(Opcodes.IRETURN));
                mv.visitMaxs(size, var);
                mv.visitEnd();
            }
        }
    }
}

From source file:com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtilTest.java

License:Open Source License

private void _doTestCreateProxyMethodNode(Method method, int index, String skeletonId,
        String stubInternalName) {

    MethodNode proxyMethodNode = IntrabandProxyUtil.createProxyMethodNode(method, index, skeletonId,
            Type.getType(stubInternalName));

    _assertMethodNodeSignature(proxyMethodNode, method.getModifiers() & ~Modifier.ABSTRACT, method.getName(),
            Type.getMethodDescriptor(method), method.getExceptionTypes());

    InsnList insnList = proxyMethodNode.instructions;

    Iterator<AbstractInsnNode> iterator = insnList.iterator();

    // NEW com/liferay/portal/kernel/io/Serializer

    _assertTypeInsnNode(iterator.next(), Opcodes.NEW, Serializer.class);

    // DUP/*from ww w . j  av  a 2s  . co  m*/

    _assertInsnNode(iterator.next(), Opcodes.DUP);

    // INVOKESPECIAL com/liferay/portal/kernel/io/Serializer <init> ()V

    _assertMethodInsnNode(iterator.next(), Opcodes.INVOKESPECIAL, Type.getInternalName(Serializer.class),
            "<init>", Type.VOID_TYPE);

    // ASTORE argumentsSize

    Type methodType = Type.getType(method);

    int argumentsAndReturnSizes = methodType.getArgumentsAndReturnSizes();

    int argumentsSize = argumentsAndReturnSizes >> 2;

    _assertVarInsnNode(iterator.next(), Opcodes.ASTORE, argumentsSize);

    // ALOAD argumentsSize

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

    // LDC skeletonId

    _assertLdcInsnNode(iterator.next(), Opcodes.LDC, skeletonId);

    // INVOKEVIRTUAL com/liferay/portal/kernel/io/Serializer writeString
    // (Ljava/lang/String;)V

    _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class),
            "writeString", Type.VOID_TYPE, Type.getType(String.class));

    // ALOAD argumentsSize

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

    // ALOAD 0

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, 0);

    // GETFIELD stubInternalName _id Ljava/lang/String;

    _assertFieldInsnNode(iterator.next(), Opcodes.GETFIELD, stubInternalName, "_id", String.class);

    // INVOKEVIRTUAL com/liferay/portal/kernel/io/Serializer writeString
    // (Ljava/lang/String;)V

    _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class),
            "writeString", Type.VOID_TYPE, Type.getType(String.class));

    // ALOAD argumentsSize

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

    if (index <= 5) {

        // ICONST_index

        _assertInsnNode(iterator.next(), Opcodes.ICONST_0 + index);
    } else {

        // BIPUSH index

        _assertIntInsnNode(iterator.next(), Opcodes.BIPUSH, index);
    }

    // INVOKEVIRTUAL com/liferay/portal/kernel/io/Serializer writeInt (I)V

    _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class),
            "writeInt", Type.VOID_TYPE, Type.INT_TYPE);

    Class<?>[] parameterTypes = method.getParameterTypes();

    int offset = 1;

    for (int i = 0; i < parameterTypes.length; i++) {
        Class<?> parameterClass = parameterTypes[i];

        // ALOAD argumentsSize

        _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

        // xLOAD i

        Type parameterType = Type.getType(parameterClass);

        _assertVarInsnNode(iterator.next(), parameterType.getOpcode(Opcodes.ILOAD), offset);

        offset += parameterType.getSize();

        if (parameterClass.isPrimitive() || (parameterClass == String.class)) {

            String name = TextFormatter.format(parameterClass.getSimpleName(), TextFormatter.G);

            _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                    Type.getInternalName(Serializer.class), "write".concat(name), Type.VOID_TYPE,
                    parameterType);
        } else {
            _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                    Type.getInternalName(Serializer.class), "writeObject", Type.VOID_TYPE,
                    Type.getType(Serializable.class));
        }
    }

    // ALOAD 0

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, 0);

    // ALOAD argumentsSize

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

    Class<?> returnClass = method.getReturnType();

    Type returnType = Type.getType(returnClass);

    if (returnClass == void.class) {

        // INVOKESPECIAL stubInternalName _send
        // (Lcom/liferay/portal/kernel/io/Serializer;)V

        _assertMethodInsnNode(iterator.next(), Opcodes.INVOKESPECIAL, stubInternalName, "_send", Type.VOID_TYPE,
                Type.getType(Serializer.class));

        _assertInsnNode(iterator.next(), Opcodes.RETURN);
    } else {

        // INVOKESPECIAL stubInternalName _syncSend
        // (Lcom/liferay/portal/kernel/io/Serializer;)Ljava/io/Serializable;

        _assertMethodInsnNode(iterator.next(), Opcodes.INVOKESPECIAL, stubInternalName, "_syncSend",
                Type.getType(Serializable.class), Type.getType(Serializer.class));

        if (returnClass.isPrimitive()) {

            // ASTORE argumentsSize + 1

            _assertVarInsnNode(iterator.next(), Opcodes.ASTORE, argumentsSize + 1);

            // ALOAD argumentsSize + 1

            _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize + 1);

            // IFNULL nullCheckLabel

            LabelNode nullCheckLabelNode = _assertJumpInsnNode(iterator.next(), Opcodes.IFNULL);

            // ALOAD argumentsSize + 1

            _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize + 1);

            // CHECKCAST returnType

            _assertTypeInsnNode(iterator.next(), Opcodes.CHECKCAST, _autoboxingMap.get(returnClass));

            if (returnClass == boolean.class) {

                // INVOKEVIRTUAL java/lang/Boolean booleanValue ()Z

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Boolean.class), "booleanValue", Type.BOOLEAN_TYPE);
            } else if (returnClass == byte.class) {

                // INVOKEVIRTUAL java/lang/Number intValue ()I

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "intValue", Type.INT_TYPE);
            } else if (returnClass == char.class) {

                // INVOKEVIRTUAL java/lang/Character charValue ()C

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Character.class), "charValue", Type.CHAR_TYPE);
            } else if (returnClass == double.class) {

                // INVOKEVIRTUAL java/lang/Number doubleValue ()D

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "doubleValue", Type.DOUBLE_TYPE);
            } else if (returnClass == float.class) {

                // INVOKEVIRTUAL java/lang/Number floatValue ()F

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "floatValue", Type.FLOAT_TYPE);
            } else if (returnClass == int.class) {

                // INVOKEVIRTUAL java/lang/Number intValue ()I

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "intValue", Type.INT_TYPE);
            } else if (returnClass == long.class) {

                // INVOKEVIRTUAL java/lang/Number longValue ()J

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "longValue", Type.LONG_TYPE);
            } else if (returnClass == short.class) {

                // INVOKEVIRTUAL java/lang/Number intValue ()I

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "intValue", Type.INT_TYPE);
            }

            // xRETURN

            _assertInsnNode(iterator.next(), returnType.getOpcode(Opcodes.IRETURN));

            // nullCheckLabel

            Assert.assertSame(nullCheckLabelNode, iterator.next());

            // xRETURN null/0

            if (!returnClass.isPrimitive()) {
                _assertInsnNode(iterator.next(), Opcodes.ACONST_NULL);
                _assertInsnNode(iterator.next(), Opcodes.ARETURN);
            } else if (returnClass == void.class) {
                _assertInsnNode(iterator.next(), Opcodes.RETURN);
            } else if (returnClass == float.class) {
                _assertInsnNode(iterator.next(), Opcodes.FCONST_0);
                _assertInsnNode(iterator.next(), Opcodes.FRETURN);
            } else if (returnClass == double.class) {
                _assertInsnNode(iterator.next(), Opcodes.DCONST_0);
                _assertInsnNode(iterator.next(), Opcodes.DRETURN);
            } else if (returnClass == long.class) {
                _assertInsnNode(iterator.next(), Opcodes.LCONST_0);
                _assertInsnNode(iterator.next(), Opcodes.LRETURN);
            } else {
                _assertInsnNode(iterator.next(), Opcodes.ICONST_0);
                _assertInsnNode(iterator.next(), Opcodes.IRETURN);
            }
        } else {
            if (returnClass != Object.class) {

                // CHECKCAST

                _assertTypeInsnNode(iterator.next(), Opcodes.CHECKCAST, returnClass);
            }

            // ARETURN

            _assertInsnNode(iterator.next(), Opcodes.ARETURN);
        }
    }

    Assert.assertFalse(iterator.hasNext());
}