Example usage for org.objectweb.asm Opcodes ALOAD

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

Introduction

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

Prototype

int ALOAD

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

Click Source Link

Usage

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

License:Apache License

/**
 * Loads this (alias arg0) on top of the stack.
 * //from w w w. ja v  a 2 s.c  o m
 * @throws IllegalArgumentException if the method is static.
 */
public void loadThis() {
    if (staticFlag) {
        throw new IllegalArgumentException("Cannot invoke this in a Method marked as static.");
    }
    mv.visitVarInsn(Opcodes.ALOAD, 0);
}

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

License:Apache License

/**
 * Test method for {@link TypeDescriptor#getLoadOpcode()}.
 *///  w  w w.  j  a  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.//  ww  w.  ja  v  a  2s.com
 */
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 assertThisNotNull() {
    delegate.visitVarInsn(Opcodes.ALOAD, 0);
    Label notNull = new Label();
    delegate.visitJumpInsn(Opcodes.IFNONNULL, notNull);
    delegate.visitTypeInsn(Opcodes.NEW, NULL_POINTER_EXCEPTION);
    delegate.visitInsn(Opcodes.DUP);//from  ww  w . ja  v  a  2  s.c  o m
    delegate.visitMethodInsn(Opcodes.INVOKESPECIAL, NULL_POINTER_EXCEPTION, "<init>", "()V");
    delegate.visitInsn(Opcodes.ATHROW);
    delegate.visitLabel(notNull);
}

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

License:Apache License

private void dispatch_getDelegate() {
    if (isStatic) {
        delegate.visitInsn(Opcodes.ACONST_NULL);
    } else {/*from   ww  w. j  av a2  s . com*/
        delegate.visitVarInsn(Opcodes.ALOAD, 0); // this
    }
    delegate.visitLdcInsn(className);
    delegate.visitLdcInsn(methodName);
    delegate.visitLdcInsn(descriptor.getMethodDesc());
    delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, GwtNativeDispatch, "getDelegate",
            GwtNativeDispatch_getDelegate);
}

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 av  a  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;
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

private void pushProductOfIntArrayOnStack() {
    final Label beginScopeLabel = new Label();
    final Label endScopeLabel = new Label();

    final int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
    final int counterIndex = newLocal("I", beginScopeLabel, endScopeLabel);
    final int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
    final Label loopLabel = new Label();
    final Label endLabel = new Label();

    super.visitLabel(beginScopeLabel);

    // stack: ... intArray
    super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
    // -> stack: ...

    // counter = 0
    super.visitInsn(Opcodes.ICONST_0);
    super.visitVarInsn(Opcodes.ISTORE, counterIndex);
    // product = 1
    super.visitInsn(Opcodes.ICONST_1);
    super.visitVarInsn(Opcodes.ISTORE, productIndex);
    // loop://from  ww w . j a v  a  2s. c  om
    super.visitLabel(loopLabel);
    // if index >= arraylength goto end:
    super.visitVarInsn(Opcodes.ILOAD, counterIndex);
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    super.visitInsn(Opcodes.ARRAYLENGTH);
    super.visitJumpInsn(Opcodes.IF_ICMPGE, endLabel);
    // product = product * max(array[counter],1)
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    super.visitVarInsn(Opcodes.ILOAD, counterIndex);
    super.visitInsn(Opcodes.IALOAD);
    super.visitInsn(Opcodes.DUP);
    final Label nonZeroDimension = new Label();
    super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
    super.visitInsn(Opcodes.POP);
    super.visitInsn(Opcodes.ICONST_1);
    super.visitLabel(nonZeroDimension);
    super.visitVarInsn(Opcodes.ILOAD, productIndex);
    super.visitInsn(Opcodes.IMUL); // if overflow happens it happens.
    super.visitVarInsn(Opcodes.ISTORE, productIndex);
    // iinc counter 1
    super.visitIincInsn(counterIndex, 1);
    // goto loop
    super.visitJumpInsn(Opcodes.GOTO, loopLabel);
    // end:
    super.visitLabel(endLabel);
    // re-push dimensions array
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    // push product
    super.visitVarInsn(Opcodes.ILOAD, productIndex);

    super.visitLabel(endScopeLabel);
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

/**
 * Reflection-based allocation (@see java.lang.reflect.Array#newInstance) is
 * triggered with a static method call (INVOKESTATIC), so we hook it here.
 * Class initialization is triggered with a constructor call (INVOKESPECIAL)
 * so we hook that here too as a proxy for the new bytecode which leaves an
 * uninitialized object on the stack that we're not allowed to touch.
 * {@link java.lang.Object#clone} is also a call to INVOKESPECIAL,
 * and is hooked here.  {@link java.lang.Class#newInstance} and
 * {@link java.lang.reflect.Constructor#newInstance} are both
 * INVOKEVIRTUAL calls, so they are hooked here, as well.
 *///from  w w w  . j  a va2 s.c  o m
@Override
public void visitMethodInsn(final int opcode, final String owner, final String name, final String signature,
        final boolean itf) {
    if (opcode == Opcodes.INVOKESTATIC &&
    // Array does its own native allocation.  Grr.
            owner.equals("java/lang/reflect/Array") && name.equals("newInstance")) {
        if (signature.equals("(Ljava/lang/Class;I)Ljava/lang/Object;")) {

            final Label beginScopeLabel = new Label();
            final Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            // stack: ... class count
            final int countIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, countIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            final int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... class count
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj
            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... newobj newobj count
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj count newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        } else if (signature.equals("(Ljava/lang/Class;[I)Ljava/lang/Object;")) {
            final Label beginScopeLabel = new Label();
            final Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            final int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
            // stack: ... class dimsArray
            pushProductOfIntArrayOnStack();
            // -> stack: ... class dimsArray product
            final int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, productIndex);
            // -> stack: ... class dimsArray

            super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            final int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
            // -> stack: ... class dimsArray
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj

            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, productIndex);
            // -> stack: ... newobj newobj product
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj product newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        }
    }

    if (opcode == Opcodes.INVOKEVIRTUAL) {
        if ("clone".equals(name) && owner.startsWith("[")) {
            super.visitMethodInsn(opcode, owner, name, signature, itf);

            int i = 0;
            while (i < owner.length()) {
                if (owner.charAt(i) != '[') {
                    break;
                }
                i++;
            }
            if (i > 1) {
                // -> stack: ... newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... arrayref
                calculateArrayLengthAndDispatch(owner.substring(i), i);
            } else {
                // -> stack: ... newobj
                super.visitInsn(Opcodes.DUP);
                // -> stack: ... newobj newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... newobj arrayref
                super.visitInsn(Opcodes.ARRAYLENGTH);
                // -> stack: ... newobj length
                super.visitInsn(Opcodes.SWAP);
                // -> stack: ... length newobj
                invokeRecordAllocation(owner.substring(i));
            }
            return;
        } else if ("newInstance".equals(name)) {
            if ("java/lang/Class".equals(owner) && "()Ljava/lang/Object;".equals(signature)) {
                super.visitInsn(Opcodes.DUP);
                // -> stack: ... Class Class
                super.visitMethodInsn(opcode, owner, name, signature, itf);
                // -> stack: ... Class newobj
                super.visitInsn(Opcodes.DUP_X1);
                // -> stack: ... newobj Class newobj
                super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, CLASS_RECORDER_SIG,
                        false);
                // -> stack: ... newobj
                return;
            } else if ("java/lang/reflect/Constructor".equals(owner)
                    && "([Ljava/lang/Object;)Ljava/lang/Object;".equals(signature)) {
                buildRecorderFromObject(opcode, owner, name, signature, itf);
                return;
            }
        }
    }

    if (opcode == Opcodes.INVOKESPECIAL) {
        if ("clone".equals(name) && "java/lang/Object".equals(owner)) {
            buildRecorderFromObject(opcode, owner, name, signature, itf);
            return;
        } else if ("<init>".equals(name) && outstandingAllocs > 0) {
            // Tricky because superclass initializers mean there can be more calls
            // to <init> than calls to NEW; hence outstandingAllocs.
            --outstandingAllocs;

            // Most of the time (i.e. in bytecode generated by javac) it is the case
            // that following an <init> call the top of the stack has a reference ot
            // the newly-initialized object.  But nothing in the JVM Spec requires
            // this, so we need to play games with the stack to make an explicit
            // extra copy (and then discard it).

            dupStackElementBeforeSignatureArgs(signature);
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            super.visitLdcInsn(-1);
            super.visitInsn(Opcodes.SWAP);
            invokeRecordAllocation(owner);
            super.visitInsn(Opcodes.POP);
            return;
        }
    }

    super.visitMethodInsn(opcode, owner, name, signature, itf);
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

private void dupStackElementBeforeSignatureArgs(final String sig) {
    final Label beginScopeLabel = new Label();
    final Label endScopeLabel = new Label();
    super.visitLabel(beginScopeLabel);

    final Type[] argTypes = Type.getArgumentTypes(sig);
    final int[] args = new int[argTypes.length];

    for (int i = argTypes.length - 1; i >= 0; --i) {
        args[i] = newLocal(argTypes[i], beginScopeLabel, endScopeLabel);
        super.visitVarInsn(argTypes[i].getOpcode(Opcodes.ISTORE), args[i]);
    }/* w  w w . j a  v  a 2s.  co  m*/
    super.visitInsn(Opcodes.DUP);
    for (int i = 0; i < argTypes.length; ++i) {
        final int op = argTypes[i].getOpcode(Opcodes.ILOAD);
        super.visitVarInsn(op, args[i]);
        if (op == Opcodes.ALOAD) {
            super.visitInsn(Opcodes.ACONST_NULL);
            super.visitVarInsn(Opcodes.ASTORE, args[i]);
        }
    }
    super.visitLabel(endScopeLabel);
}

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

License:Apache License

public void visitVarInsn(final int opcode, final int var) {
    switch (opcode) {
    case Opcodes.ILOAD:
        load(var, JavaType.INT);
        break;//from  w  ww.  j ava 2 s  .co  m
    case Opcodes.LLOAD:
        load(var, JavaType.LONG);
        break;
    case Opcodes.FLOAD:
        load(var, JavaType.FLOAT);
        break;
    case Opcodes.DLOAD:
        load(var, JavaType.DOUBLE);
        break;
    case Opcodes.ALOAD:
        load(var, JavaType.OBJECT);
        break;

    case Opcodes.ISTORE:
        store(var, JavaType.INT);
        break;
    case Opcodes.LSTORE:
        store(var, JavaType.LONG);
        break;
    case Opcodes.FSTORE:
        store(var, JavaType.FLOAT);
        break;
    case Opcodes.DSTORE:
        store(var, JavaType.DOUBLE);
        break;
    case Opcodes.ASTORE:
        store(var, JavaType.OBJECT);
        break;

    case Opcodes.RET:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new RetSub(lineNumber));
            }
        });
        break;
    default:
        throw new UnsupportedOperationException("opcode: " + opcode);
    }
}