Example usage for org.objectweb.asm Opcodes ILOAD

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

Introduction

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

Prototype

int ILOAD

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

Click Source Link

Usage

From source file:com.google.code.jconts.instrument.gen.AsyncMethodAdapter.java

License:Apache License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
    checkProlog();//from ww  w .j  av a 2 s . com

    if (opcode == Opcodes.INVOKESTATIC && ASYNC_NAME.equals(owner) && ARETURN_NAME.equals(name)) {

        if (ARETURN_VOID_DESC.equals(desc)) {
            mv.visitInsn(Opcodes.ACONST_NULL);
        }

        // state variable
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, CONTINUATION_FIELD, CONTINUATION_DESC);
        mv.visitInsn(Opcodes.SWAP);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CONTINUATION_NAME, CONTINUATION_INVOKE_NAME,
                CONTINUATION_INVOKE_DESC);

        // Will be dropped while replacing ARETURN with RETURN.
        // FIXME: Should verify this value is NOT used.
        mv.visitInsn(Opcodes.ACONST_NULL);
        return;
    }
    if (opcode == Opcodes.INVOKESTATIC && ASYNC_NAME.equals(owner) && AWAIT_NAME.equals(name)
            && AWAIT_DESC.equals(desc)) {

        // Computation<T> is on stack

        // FIXME: ...
        // if (stack.size() != 1) {
        // throw new IllegalStateException(
        // "Stack preserving is not supported!");
        // }

        int index = dispatchTable.size();

        // Save state
        List<Type> l = new ArrayList<Type>(locals);
        if (!info.isStatic()) {
            l.remove(0);
        }

        // state.varX = locX
        String[] vars = info.tracker.stateFields(l.toArray(new Type[0]));
        for (int i = 0; i < vars.length; ++i) {
            // state variable
            target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
            mv.visitVarInsn(l.get(i).getOpcode(Opcodes.ILOAD), i + info.thisOffset);
            mv.visitFieldInsn(Opcodes.PUTFIELD, info.stateClassName, vars[i], l.get(i).getDescriptor());
        }

        // Create instance of continuation
        // new Continuation([this, ]state, index);
        mv.visitTypeInsn(Opcodes.NEW, info.continuationClassName);
        mv.visitInsn(Opcodes.DUP);

        // "this' for new Continuation([this, ]state, index)
        if (!info.isStatic()) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
        }

        // state and index
        target.visitVarInsn(Opcodes.ALOAD, 0 + info.thisOffset);
        mv.visitIntInsn(Opcodes.BIPUSH, index);

        String ctorDesc;
        if (info.isStatic()) {
            ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE });
        } else {
            ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getObjectType(info.owner), info.stateType, Type.INT_TYPE });
        }
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, info.continuationClassName, CTOR_NAME, ctorDesc);

        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, COMPUTATION_NAME, COMPUTATION_EXECUTE_NAME,
                COMPUTATION_EXECUTE_DESC);
        super.visitInsn(Opcodes.RETURN);

        // Restore state
        // mv.visitFrame(Opcodes.F_SAME, 0, new Object[0], 0, new
        // Object[0]);
        Label label = new Label();

        int invokeIndex = dispatchTable.size();
        dispatchTable.add(label); // for invoke
        dispatchTable.add(label); // for setException
        mv.visitLabel(label);
        for (int i = 0; i < vars.length; ++i) {
            // state variable
            target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
            mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, vars[i], l.get(i).getDescriptor());
            mv.visitVarInsn(l.get(i).getOpcode(Opcodes.ISTORE), i + info.thisOffset);
        }

        // if (index == invokeIndex) goto invokeLabel;
        Label invokeLabel = new Label();
        target.visitVarInsn(Opcodes.ILOAD, 1 + info.thisOffset);
        mv.visitIntInsn(Opcodes.BIPUSH, invokeIndex);
        mv.visitJumpInsn(Opcodes.IF_ICMPEQ, invokeLabel);

        // Throw exception
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, "exception", THROWABLE_DESC);
        mv.visitInsn(Opcodes.ATHROW);

        // Push result value
        // invokeLabel:
        mv.visitLabel(invokeLabel);
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, "result", OBJECT_DESC);
        return;
    }
    super.visitMethodInsn(opcode, owner, name, desc);
}

From source file:com.google.code.jconts.instrument.gen.AsyncMethodAdapter.java

License:Apache License

@Override
public void visitMaxs(int maxStack, int maxLocals) {
    // Table switch at the end of the method
    mv.visitLabel(dispatchLabel);/*from  w  w w.  ja  va 2  s .  c om*/

    Label dflt = new Label();

    // Load index
    target.visitVarInsn(Opcodes.ILOAD, 1 + info.thisOffset);
    int[] keys = new int[dispatchTable.size()];
    for (int i = 0; i < keys.length; ++i) {
        keys[i] = i;
    }
    mv.visitLookupSwitchInsn(dflt, keys, dispatchTable.toArray(new Label[0]));

    // FIXME: ...throw exception
    mv.visitLabel(dflt);
    mv.visitInsn(Opcodes.RETURN);

    // catch block
    mv.visitLabel(catchLabel);

    // invoke Continuation#setException(Throwable t)
    target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
    mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, CONTINUATION_FIELD, CONTINUATION_DESC);
    mv.visitInsn(Opcodes.SWAP);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CONTINUATION_NAME, CONTINUATION_SET_EXCEPTION_NAME,
            CONTINUATION_SET_EXCEPTION_DESC);
    mv.visitInsn(Opcodes.RETURN);

    // FIXME: evaluate properly
    super.visitMaxs(maxStack + 4 + info.thisOffset, maxLocals + 2);
}

From source file:com.google.code.jconts.instrument.gen.ContinuationClassGenerator.java

License:Apache License

private void generateConstructor(ClassVisitor cv) {
    final String name = info.continuationClassName;
    final Type outerType = Type.getObjectType(info.owner);

    // Constructor have form either <init>(OuterClass this$0, State state,
    // int index)
    // or <init>(State state, int index)
    String ctorDesc;/*from w w  w .ja  v a2  s.c  o  m*/
    if (info.isStatic()) {
        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE });
    } else {
        cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "this$0", 'L' + info.owner + ';', null, null);

        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                new Type[] { outerType, info.stateType, Type.INT_TYPE });
    }

    // Generate constructor
    MethodVisitor mv = cv.visitMethod(0, CTOR_NAME, ctorDesc, null, null);
    mv.visitCode();
    Label start = new Label();
    Label end = new Label();
    mv.visitLabel(start);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, OBJECT_NAME, CTOR_NAME, DEFAULT_CTOR_DESC);

    // Save state field
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1 + info.thisOffset);
    mv.visitFieldInsn(Opcodes.PUTFIELD, name, "state", stateDesc);

    // Save index field
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 2 + info.thisOffset);
    mv.visitFieldInsn(Opcodes.PUTFIELD, name, "index", "I");

    // Save outer this
    if (!info.isStatic()) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, name, "this$0", outerType.getDescriptor());
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitLabel(end);

    mv.visitLocalVariable("this", 'L' + name + ';', signature, start, end, 0);
    if (!info.isStatic()) {
        mv.visitLocalVariable("this$0", outerType.getDescriptor(), null, start, end, 1);
    }
    mv.visitLocalVariable("state", stateDesc, null, start, end, 1 + info.thisOffset);
    mv.visitLocalVariable("index", "I", null, start, end, 2 + info.thisOffset);

    mv.visitMaxs(2, 3 + info.thisOffset);
    mv.visitEnd();
}

From source file:com.google.code.jconts.instrument.gen.TramplineMethodGenerator.java

License:Apache License

public void accept(ClassVisitor cv) {
    MethodVisitor mv = cv.visitMethod(info.access, info.name, info.desc, info.signature, info.exceptions);
    mv.visitCode();//from  w ww  .  j  a  v a  2 s.  c  om
    mv.visitTypeInsn(Opcodes.NEW, info.computationClassName);
    mv.visitInsn(Opcodes.DUP);

    // "this' for new Computation(this, state)
    if (!info.isStatic()) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
    }

    // new State()
    mv.visitTypeInsn(Opcodes.NEW, info.stateClassName);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, info.stateClassName, CTOR_NAME, DEFAULT_CTOR_DESC);

    // state.varX = argX
    String[] names = info.entryLocalsVars;
    for (int i = 0; i < info.entryLocals.length; ++i) {
        mv.visitInsn(Opcodes.DUP);
        mv.visitVarInsn(info.entryLocals[i].getOpcode(Opcodes.ILOAD), i + info.thisOffset);
        mv.visitFieldInsn(Opcodes.PUTFIELD, info.stateClassName, names[i], info.entryLocals[i].getDescriptor());
    }

    // new Computation(this, state);
    String ctorDesc;
    if (info.isStatic()) {
        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType });
    } else {
        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                new Type[] { Type.getObjectType(info.owner), info.stateType });
    }
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, info.computationClassName, CTOR_NAME, ctorDesc);

    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(info.isStatic() ? 5 : 6,
            info.isStatic() ? info.entryLocals.length : info.entryLocals.length + 1);
    mv.visitEnd();
}

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

License:Open Source License

@Override
public void visitVarInsn(int opcode, int var) {
    switch (opcode) {
    case Opcodes.ILOAD:
        push(InferredType.INT);/*from w  w  w  .  ja  v  a  2s .  com*/
        break;
    case Opcodes.LLOAD:
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.FLOAD:
        push(InferredType.FLOAT);
        break;
    case Opcodes.DLOAD:
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.ALOAD:
        push(getLocalVariableType(var));
        break;
    case Opcodes.ISTORE:
    case Opcodes.FSTORE:
    case Opcodes.ASTORE: {
        InferredType type = pop();
        setLocalVariableTypes(var, type);
        break;
    }
    case Opcodes.LSTORE:
    case Opcodes.DSTORE: {
        InferredType type = pop(2);
        setLocalVariableTypes(var, type);
        setLocalVariableTypes(var + 1, InferredType.TOP);
        break;
    }
    case Opcodes.RET:
        throw new RuntimeException("The instruction RET is not supported");
    default:
        throw new RuntimeException("Unhandled opcode " + opcode);
    }
    super.visitVarInsn(opcode, var);
}

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

License:Open Source License

/**
 * Emits instructions to load a method's parameters as arguments of a method call assumed to have
 * compatible descriptor, starting at the given local variable slot.
 *//*from w  w w .  jav a  2  s.co  m*/
private static void visitLoadArgs(MethodVisitor dispatchMethod, Type neededType, int slot) {
    for (Type arg : neededType.getArgumentTypes()) {
        dispatchMethod.visitVarInsn(arg.getOpcode(Opcodes.ILOAD), slot);
        slot += arg.getSize();
    }
}

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(/*from www  .  j  a  va2  s.  co 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.TypeDescriptorTest.java

License:Apache License

/**
 * Test method for {@link TypeDescriptor#getLoadOpcode()}.
 *///from ww w  .ja v  a  2 s. c o  m
public void testGetLoadOpcode() {
    try {
        TypeDescriptor.VOID.getLoadOpcode();
        fail("Void should have thrown an exception!");
    } catch (IllegalStateException e) {
        // OK!
    }
    assertEquals("Wrong LOAD instruction", Opcodes.ILOAD, TypeDescriptor.BOOLEAN.getLoadOpcode());
    assertEquals("Wrong STORE instruction", Opcodes.ILOAD, TypeDescriptor.BYTE.getLoadOpcode());
    assertEquals("Wrong STORE instruction", Opcodes.ILOAD, TypeDescriptor.CHAR.getLoadOpcode());
    assertEquals("Wrong STORE instruction", Opcodes.ILOAD, TypeDescriptor.SHORT.getLoadOpcode());
    assertEquals("Wrong STORE instruction", Opcodes.ILOAD, TypeDescriptor.INTEGER.getLoadOpcode());
    assertEquals("Wrong STORE instruction", Opcodes.LLOAD, TypeDescriptor.LONG.getLoadOpcode());
    assertEquals("Wrong STORE instruction", Opcodes.FLOAD, TypeDescriptor.FLOAT.getLoadOpcode());
    assertEquals("Wrong STORE instruction", Opcodes.DLOAD, TypeDescriptor.DOUBLE.getLoadOpcode());
    assertEquals("Wrong STORE instruction", Opcodes.ALOAD, TypeDescriptor.CLASS.getLoadOpcode());
    assertEquals("Wrong STORE instruction", Opcodes.ALOAD, TypeDescriptor.ARRAY.getLoadOpcode());
}

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  www  .  j a  v a2 s  .  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(), 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 int nativeDelegate_addArgument(String parameter, int index) {
    int size = 1;
    delegate.visitInsn(Opcodes.DUP);/*from w w w.  j ava 2  s .  c  o m*/
    switch (parameter.charAt(0)) {
    case 'L':
    case '[':
        parameter = "Ljava/lang/Object;";
        delegate.visitVarInsn(Opcodes.ALOAD, index);
        break;
    case 'Z':
    case 'B':
    case 'C':
    case 'S':
    case 'I':
        delegate.visitVarInsn(Opcodes.ILOAD, index);
        break;
    case 'J':
        delegate.visitVarInsn(Opcodes.LLOAD, index);
        size = 2;
        break;
    case 'F':
        delegate.visitVarInsn(Opcodes.FLOAD, index);
        break;
    case 'D':
        delegate.visitVarInsn(Opcodes.DLOAD, index);
        size = 2;
        break;
    default:
        throw new IllegalStateException(parameter);
    }
    delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "addArg", "(" + parameter + ")V");
    return size;
}