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:org.cacheonix.impl.transformer.ByteInstructionTest.java

License:LGPL

/**
 * Test method for {@link ByteInstruction#getInstruction(ByteInstruction, Type)} .
 *//*from  ww  w  .j a v a  2 s .  c o  m*/
public void testGetInstruction() {

    final int code = Opcodes.NOP;
    final ByteInstruction bi = new ByteInstruction(code, ByteInstruction.START_STACK_INDEX);

    ByteInstruction.getInstruction(bi, Type.INT_TYPE);
    assertEquals(Opcodes.ILOAD, bi.code);
    assertEquals(ByteInstruction.START_STACK_INDEX, bi.stackIndex);
    assertEquals(ByteInstruction.START_STACK_INDEX + 1, bi.nextStackIndex);

    int stackIndex = bi.nextStackIndex;
    ByteInstruction.getInstruction(bi, Type.LONG_TYPE);
    assertEquals(Opcodes.LLOAD, bi.code);
    assertEquals(stackIndex, bi.stackIndex);
    assertEquals(stackIndex + 2, bi.nextStackIndex);

    stackIndex = bi.nextStackIndex;
    ByteInstruction.getInstruction(bi, Type.DOUBLE_TYPE);
    assertEquals(Opcodes.DLOAD, bi.code);
    assertEquals(stackIndex, bi.stackIndex);
    assertEquals(stackIndex + 2, bi.nextStackIndex);
}

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public void visitVarInsn(final int opcode, final int var) {
    T t = null;//ww w .  j ava2 s  . c om

    switch (opcode) {
    /********
     * LOAD *
     ********/
    case Opcodes.ALOAD:
        t = T.REF;
        // fall through
    case Opcodes.DLOAD:
        if (t == null) {
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FLOAD:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ILOAD:
        if (t == null) {
            t = T.AINT;
        }
        // fall through
    case Opcodes.LLOAD:
        if (t == null) {
            t = T.LONG;
        }
        add(new LOAD(this.ops.size(), opcode, this.line, t, var));
        break;
    /*********
     * STORE *
     *********/
    case Opcodes.ASTORE:
        t = T.AREF; // RET allowed too
        // fall through
    case Opcodes.DSTORE:
        if (t == null) {
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FSTORE:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ISTORE:
        if (t == null) {
            t = T.AINT;
        }
        // fall through
    case Opcodes.LSTORE:
        if (t == null) {
            t = T.LONG;
        }
        add(new STORE(this.ops.size(), opcode, this.line, t, var));
        break;
    /*******
     * RET *
     *******/
    case Opcodes.RET: {
        add(new RET(this.ops.size(), opcode, this.line, var));
        break;
    }
    default:
        log.warn(getM() + ": Unknown var insn opcode '" + opcode + "'!");
    }
}

From source file:org.devinprogress.YAIF.Transformer.ASMTransformer.java

public static void hookGuiTextFocusChange(MethodNode mn) {
    //Hook to GuiTextField onFocusChange
    //org.devinprogress.YAIF.YetAnotherInputFix.TextFieldFocusChange(this, p_146195_1_);
    //At the beginning/ending of setFocused (Z)V

    //ALOAD 0//from w  w w.  ja  v  a 2  s .co  m
    //ILOAD 1
    //INVOKESTATIC org/devinprogress/YAIF/YetAnotherInputFix.TextFieldFocusChange
    //   (Lnet/minecraft/client/gui/GuiTextField;Z)V  for for version 1.7.2

    //Remember to use the obfuscated name for Lnet/minecraft/client/gui/GuiTextField;
    AbstractInsnNode n = mn.instructions.getFirst();
    mn.instructions.insertBefore(n, new VarInsnNode(Opcodes.ALOAD, 0));
    mn.instructions.insertBefore(n, new VarInsnNode(Opcodes.ILOAD, 1));
    mn.instructions.insertBefore(n,
            new MethodInsnNode(Opcodes.INVOKESTATIC, "org/devinprogress/YAIF/YetAnotherInputFix",
                    "TextFieldFocusChange", "(L" + obfedClassName.replace('.', '/') + ";Z)V", false));
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * Returns instructions, that are needed to pack all arguments of a method
 * in an {@link Object} Array/*from   w ww  .ja va2 s. c  o m*/
 * @param args The Types of the arguments
 * @param isStatic is this method static or not
 * @return
 */
protected InsnList getBoxingInstructions(Type[] args, boolean isStatic) {
    int firstArgIndex = 1;
    if (isStatic) {
        firstArgIndex = 0;
    }
    InsnList instructions = new InsnList();
    instructions.add(createLoadIntConstant(args.length));
    instructions.add(new TypeInsnNode(Opcodes.ANEWARRAY, ClassNames.OBJECT_SLASH));
    for (int i = 0, slot = 0; i < args.length; slot += args[i++].getSize()) {
        instructions.add(new InsnNode(Opcodes.DUP));
        instructions.add(createLoadIntConstant(i));
        instructions.add(new IntInsnNode(args[i].getOpcode(Opcodes.ILOAD), slot + firstArgIndex));
        if (args[i].getSort() != Type.OBJECT && args[i].getSort() != Type.ARRAY) {
            instructions.add(AsmTypeHelper.getBoxingInstructionForType(args[i]));
        }
        instructions.add(new InsnNode(Opcodes.AASTORE));
    }

    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * Adds instructions to put all arguments of a method on the stack.
 * @param instructions// ww  w.ja  v  a  2  s.c  om
 * @param args
 * @param isStatic
 */
protected void addInstructionsForLoadArguments(InsnList instructions, Type[] args, boolean isStatic) {
    int firstArgIndex = 1;
    if (isStatic) {
        firstArgIndex = 0;
    }
    // put "this" on the stack for an non-static method
    if (!isStatic) {
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    }
    for (int i = 0, slot = firstArgIndex; i < args.length; slot += args[i++].getSize()) {
        instructions.add(new IntInsnNode(args[i].getOpcode(Opcodes.ILOAD), slot));
    }
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AddEmptyMethodAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
    if ((this.access & Opcodes.ACC_ABSTRACT) != 0) {
        mv.visitEnd();//from w  w  w .  jav a  2  s  .c om
        return;
    }
    mv.visitCode();
    boolean needConstValue = true;
    if (superToCall != null) {
        needConstValue = false;
        boolean isStatic = (this.access & Opcodes.ACC_STATIC) != 0;
        int firstArgIndex = isStatic ? 0 : 1;
        if (!isStatic)
            mv.visitVarInsn(Opcodes.ALOAD, 0); // "this"
        Type[] args = Type.getArgumentTypes(desc);
        for (int i = 0, slot = firstArgIndex; i < args.length; slot += args[i++].getSize())
            mv.visitVarInsn(args[i].getOpcode(Opcodes.ILOAD), slot);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superToCall, name, desc, false);
    }
    Type returnType = Type.getReturnType(this.desc);
    switch (returnType.getSort()) {
    case Type.VOID:
        mv.visitInsn(Opcodes.RETURN);
        break;
    case Type.INT:
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
        if (needConstValue)
            mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IRETURN);
        break;
    case Type.FLOAT:
        if (needConstValue)
            mv.visitInsn(Opcodes.FCONST_1);
        mv.visitInsn(Opcodes.FRETURN);
        break;
    case Type.LONG:
        if (needConstValue)
            mv.visitInsn(Opcodes.LCONST_1);
        mv.visitInsn(Opcodes.LRETURN);
        break;
    case Type.DOUBLE:
    case Type.OBJECT:
    case Type.ARRAY:
        if (needConstValue)
            mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitInsn(Opcodes.ARETURN);
        break;
    }
    mv.visitMaxs(1, maxLocals);
    mv.visitEnd();
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AddThreadNotificationAdapter.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String methodName, String desc, String signature,
        String[] exceptions) {/*from w  ww  .j a  v  a  2 s .com*/
    if (INIT.equals(methodName)) {
        // into each constructor ...
        final MethodVisitor methodVisitor = cv.visitMethod(access, methodName, desc, null, null);
        return new AdviceAdapter(this.api, methodVisitor, access, methodName, desc) {
            @Override
            public void invokeConstructor(Type type, Method method) {
                super.invokeConstructor(type, method);
                // ... that contains a super(..) call (rather than this(..)):
                if (type.getInternalName().equals(clazz.getInternalSuperClassName())) {
                    // insert:
                    // this._OT$creationThread = Thread.currentThread();
                    methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                    methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.THREAD_SLASH, CURRENT_THREAD,
                            CURRENT_THREAD_DESC, false);
                    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, clazz.getInternalName(), CREATION_THREAD,
                            THREAD_DESC);
                }
            }
        };
    } else if (RUN.equals(methodName) && RUN_DESC.equals(desc)) {
        final MethodVisitor methodVisitor = cv.visitMethod(access, methodName, desc, null, null);
        return new AdviceAdapter(this.api, methodVisitor, access, methodName, desc) {

            Label start = new Label(); // start of method (scope of new local)
            Label end = new Label(); // end of method
            int isThreadStartIdx; // new local: boolean _OT$isThreadStart

            @Override
            protected void onMethodEnter() {
                methodVisitor.visitLabel(start);
                isThreadStartIdx = newLocal(Type.BOOLEAN_TYPE);
                methodVisitor.visitLocalVariable("_OT$isThreadStart", "Z", null, start, end, isThreadStartIdx);
                // TeamThreadManager.newThreadStarted(false, this._OT$creationThread)
                methodVisitor.visitInsn(Opcodes.ICONST_0);
                methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                methodVisitor.visitFieldInsn(Opcodes.GETFIELD, clazz.getInternalName(), CREATION_THREAD,
                        THREAD_DESC);
                methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.TEAM_THREAD_MANAGER_SLASH,
                        NEW_THREAD_STARTED, NEW_THREAD_STARTED_DESC, false);
                methodVisitor.visitIntInsn(Opcodes.ISTORE, isThreadStartIdx);
                // this._OT$creationThread = null; // avoid leak
                methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                methodVisitor.visitInsn(Opcodes.ACONST_NULL);
                methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, clazz.getInternalName(), CREATION_THREAD,
                        THREAD_DESC);
            }

            @Override
            protected void onMethodExit(int opcode) {
                insertThreadEndedNotification();
            }

            @Override
            public void endMethod() {
                methodVisitor.visitLabel(end);

                // insert another threadEnded notification as a handler for Throwable
                Label handler = new Label();
                methodVisitor.visitLabel(handler);
                insertThreadEndedNotification();
                methodVisitor.visitInsn(Opcodes.ATHROW); // rethrow caught exception

                methodVisitor.visitTryCatchBlock(start, end, handler, ClassNames.THROWABLE_SLASH);
                methodVisitor.visitMaxs(0, 0);
            }

            void insertThreadEndedNotification() {
                Label skip = new Label();
                // insert:
                // if (_OT$isThreadStart) TeamThreadManager.threadEnded();
                methodVisitor.visitIntInsn(Opcodes.ILOAD, isThreadStartIdx);
                methodVisitor.visitJumpInsn(Opcodes.IFEQ, skip);
                methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.TEAM_THREAD_MANAGER_SLASH,
                        THREAD_ENDED, THREAD_ENDED_DESC, false);
                methodVisitor.visitLabel(skip);
            }
        };
    }
    return null;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateAddRemoveRoleMethod.java

License:Open Source License

@Override
protected boolean transform() {
    // void _OT$addRemoveRole(Object role, boolean isAdding) {
    MethodNode method = getMethod(ConstantMembers.addOrRemoveRole);
    final int ROLE_SLOT = 1, IS_ADDING_SLOT = 2;
    Label start = new Label(), end = new Label();
    method.instructions.clear();//from  w  w w .  j  a va2s .  c  o m
    method.visitLabel(start);

    // set = <initialized _OT$roleSet;>
    final int SET_SLOT = 3;
    method.visitLocalVariable("set", "Ljava/util/Set;", null, start, end, SET_SLOT);
    genGetInitializedRoleSet(method.instructions, SET_SLOT);

    // if (isAdding) {
    method.instructions.add(new IntInsnNode(Opcodes.ILOAD, IS_ADDING_SLOT));
    LabelNode jumpToRemove = new LabelNode();
    method.instructions.add(new JumpInsnNode(Opcodes.IFEQ, jumpToRemove));

    // set.add(role);
    method.instructions.add(new IntInsnNode(Opcodes.ALOAD, SET_SLOT));
    method.instructions.add(new IntInsnNode(Opcodes.ALOAD, ROLE_SLOT));
    method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ClassNames.HASH_SET_SLASH, "add",
            "(Ljava/lang/Object;)Z", false));
    method.instructions.add(new InsnNode(Opcodes.POP));

    LabelNode jumpToEnd = new LabelNode();
    method.instructions.add(new JumpInsnNode(Opcodes.GOTO, jumpToEnd));
    // } else {
    method.instructions.add(jumpToRemove);
    // set.remove(role);
    method.instructions.add(new IntInsnNode(Opcodes.ALOAD, SET_SLOT));
    method.instructions.add(new IntInsnNode(Opcodes.ALOAD, ROLE_SLOT));
    method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ClassNames.HASH_SET_SLASH, "remove",
            "(Ljava/lang/Object;)Z", false));
    method.instructions.add(new InsnNode(Opcodes.POP));

    method.instructions.add(jumpToEnd);
    // }

    method.instructions.add(new InsnNode(Opcodes.RETURN));
    method.visitLabel(end);
    // }
    // maxs are computed, maxStack from flow, maxLocals from localVariable-slots
    return true;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateFieldAccessAdapter.java

License:Open Source License

@Override
public boolean transform() {

    InsnList instructions = new InsnList();
    // put accessId on the stack
    instructions.add(new IntInsnNode(Opcodes.ILOAD, firstArgIndex + 1));
    // read or write access
    LabelNode writeAccess = new LabelNode();
    instructions.add(new JumpInsnNode(Opcodes.IFNE, writeAccess));
    // read access
    if (field.isStatic()) {
        // get value of field
        instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, name, field.getName(), field.getSignature()));
    } else {/*from  w w  w  . j a v  a2s. com*/
        // put "this" on the stack
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
        // get value of field
        instructions.add(new FieldInsnNode(Opcodes.GETFIELD, name, field.getName(), field.getSignature()));
    }

    //box value as "Object"
    Type type = Type.getType(field.getSignature());
    instructions.add(AsmTypeHelper.getBoxingInstructionForType(type));
    instructions.add(new InsnNode(Opcodes.ARETURN));

    //write access
    instructions.add(writeAccess);
    //put "args" on the stack 
    instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2));
    //get the first element of "args"
    instructions.add(new InsnNode(Opcodes.ICONST_0));
    instructions.add(new InsnNode(Opcodes.AALOAD));
    //unbox it
    if (type.getSort() != Type.ARRAY && type.getSort() != Type.OBJECT) {
        String objectType = AsmTypeHelper.getObjectType(type);
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
        instructions.add(AsmTypeHelper.getUnboxingInstructionForType(type, objectType));
    } else {
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, type.getInternalName()));
    }

    if (field.isStatic()) {
        //save value in field
        instructions.add(new FieldInsnNode(Opcodes.PUTSTATIC, name, field.getName(), field.getSignature()));
    } else {
        //put "this" on the stack
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
        instructions.add(new InsnNode(Opcodes.SWAP));
        //save value in field
        instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, name, field.getName(), field.getSignature()));
    }

    //dummy return 
    instructions.add(new InsnNode(Opcodes.ACONST_NULL));
    instructions.add(new InsnNode(Opcodes.ARETURN));

    //add the instructions to a new label in the existing switch
    MethodNode method = getMethod(access);
    addNewLabelToSwitch(method.instructions, instructions, accessId);

    return true;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateSwitchAdapter.java

License:Open Source License

/**
 * Adds instructions before the switch statement.
 * @param method/* w w w  . j  a v a 2  s  . c o m*/
 */
protected void addPreSwitchInstructions(MethodNode method) {
    method.instructions.add(new IntInsnNode(Opcodes.ILOAD, firstArgIndex));
}