Example usage for org.objectweb.asm Opcodes ICONST_1

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

Introduction

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

Prototype

int ICONST_1

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

Click Source Link

Usage

From source file:name.martingeisse.minimal.compiler.Compiler.java

License:Open Source License

/**
 * Compiles a {@link MethodNode} to MCode.
 * /*from  w  ww  . ja  va2  s.  c  o  m*/
 * @param methodNode the method node to compile
 * @return the compiled code
 */
public ImmutableList<MCodeEntry> compile(final MethodNode methodNode) {
    for (int i = 0; i < methodNode.instructions.size(); i++) {
        final AbstractInsnNode instruction = methodNode.instructions.get(i);
        if (instruction instanceof LineNumberNode) {
            // ignored
        } else if (instruction instanceof FrameNode) {
            // this could be useful in the future
        } else if (instruction instanceof LabelNode) {
            label(((LabelNode) instruction).getLabel());
        } else if (instruction instanceof InsnNode) {
            switch (instruction.getOpcode()) {

            case Opcodes.ICONST_M1:
                iconst(-1);
                break;

            case Opcodes.ICONST_0:
                iconst(0);
                break;

            case Opcodes.ICONST_1:
                iconst(1);
                break;

            case Opcodes.ICONST_2:
                iconst(2);
                break;

            case Opcodes.ICONST_3:
                iconst(3);
                break;

            case Opcodes.ICONST_4:
                iconst(4);
                break;

            case Opcodes.ICONST_5:
                iconst(5);
                break;

            default:
                unsupported(instruction);
                break;

            }
        } else if (instruction instanceof VarInsnNode) {
            final VarInsnNode varInsnNode = (VarInsnNode) instruction;
            switch (varInsnNode.getOpcode()) {

            case Opcodes.ILOAD:
                iload(varInsnNode.var);
                break;

            case Opcodes.ISTORE:
                istore(varInsnNode.var);
                break;

            default:
                unsupported(instruction);
                break;

            }
        } else if (instruction instanceof IincInsnNode) {
            final IincInsnNode iincInsnNode = (IincInsnNode) instruction;
            iinc(iincInsnNode.var, iincInsnNode.incr);
        } else if (instruction instanceof JumpInsnNode) {
            final JumpInsnNode jumpInsnNode = (JumpInsnNode) instruction;
            switch (jumpInsnNode.getOpcode()) {

            case Opcodes.IFEQ:
            case Opcodes.IFNE:
            case Opcodes.IFLT:
            case Opcodes.IFGE:
            case Opcodes.IFGT:
            case Opcodes.IFLE:
                branch1(jumpInsnNode.label.getLabel(), jumpInsnNode.getOpcode());
                break;

            case Opcodes.IF_ICMPEQ:
            case Opcodes.IF_ICMPNE:
            case Opcodes.IF_ICMPLT:
            case Opcodes.IF_ICMPGE:
            case Opcodes.IF_ICMPGT:
            case Opcodes.IF_ICMPLE:
                branch2(jumpInsnNode.label.getLabel(), jumpInsnNode.getOpcode());
                break;

            case Opcodes.IFNULL:
            case Opcodes.IFNONNULL:
                // unsupported: one-argument reference comparison operator
                unsupported(instruction);
                break;

            case Opcodes.IF_ACMPEQ:
            case Opcodes.IF_ACMPNE:
                // unsupported: two-argument reference comparison operator
                unsupported(instruction);
                break;

            case Opcodes.GOTO:
                jump(jumpInsnNode.label.getLabel());
                break;

            case Opcodes.JSR:
                jsr(jumpInsnNode.label.getLabel());
                break;

            default:
                unsupported(instruction);
                break;

            }
        } else if (instruction instanceof IntInsnNode) {
            final IntInsnNode intInsnNode = (IntInsnNode) instruction;
            if (instruction.getOpcode() == Opcodes.BIPUSH || instruction.getOpcode() == Opcodes.SIPUSH) {
                iconst(intInsnNode.operand);
            } else {
                // NEWARRAY
                unsupported(instruction);
            }
        } else if (instruction instanceof MethodInsnNode) {
            final MethodInsnNode methodInsnNode = (MethodInsnNode) instruction;
            if (methodInsnNode.getOpcode() == Opcodes.INVOKESTATIC) {
                if (methodInsnNode.owner.replace('/', '.').equals(Native.class.getName())) {
                    nativeCall(methodInsnNode.name, methodInsnNode.desc);
                } else {
                    unsupported(instruction);
                }
            } else {
                unsupported(instruction);
            }
        } else {
            unsupported(instruction);
        }
    }
    return builder.build();
}

From source file:net.minecraftforge.fml.common.asm.transformers.SoundEngineFixTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (transformedName.equals("paulscode.sound.Source")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        classNode.fields.add(new FieldNode(Opcodes.ACC_PUBLIC, "removed", "Z", null, null)); // adding field 'public boolean removed;'

        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(writer);/*  w w w . j  av  a  2s .com*/
        return writer.toByteArray();
    } else if (transformedName.equals("paulscode.sound.Library")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        MethodNode method = null;
        for (MethodNode m : classNode.methods) {
            if (m.name.equals("removeSource") && m.desc.equals("(Ljava/lang/String;)V")) // trying to find paulscode.sound.Library.removeSource(String)
            {
                method = m;
                break;
            }
        }
        if (method == null)
            throw new RuntimeException(
                    "Error processing " + transformedName + " - no removeSource method found");

        AbstractInsnNode referenceNode = null;

        for (Iterator<AbstractInsnNode> iterator = method.instructions.iterator(); iterator.hasNext();) {
            AbstractInsnNode insn = iterator.next();
            if (insn instanceof MethodInsnNode && ((MethodInsnNode) insn).owner.equals("paulscode/sound/Source") // searching for mySource.cleanup() node (line 1086)
                    && ((MethodInsnNode) insn).name.equals("cleanup")) {
                referenceNode = insn;
                break;
            }
        }

        if (referenceNode != null) {
            LabelNode after = (LabelNode) referenceNode.getNext();

            AbstractInsnNode beginning = referenceNode.getPrevious();

            int varIndex = ((VarInsnNode) beginning).var;

            method.instructions.insertBefore(beginning, new VarInsnNode(Opcodes.ALOAD, varIndex)); // adding extra if (mySource.toStream)
            method.instructions.insertBefore(beginning,
                    new FieldInsnNode(Opcodes.GETFIELD, "paulscode/sound/Source", "toStream", "Z"));
            LabelNode elseNode = new LabelNode();
            method.instructions.insertBefore(beginning, new JumpInsnNode(Opcodes.IFEQ, elseNode)); // if fails (else) -> go to mySource.cleanup();

            method.instructions.insertBefore(beginning, new VarInsnNode(Opcodes.ALOAD, varIndex)); // if (mySource.toStream) { mySource.removed = true; }
            method.instructions.insertBefore(beginning, new InsnNode(Opcodes.ICONST_1));
            method.instructions.insertBefore(beginning,
                    new FieldInsnNode(Opcodes.PUTFIELD, "paulscode/sound/Source", "removed", "Z"));

            method.instructions.insertBefore(beginning, new JumpInsnNode(Opcodes.GOTO, after)); // still inside if -> jump to sourceMap.remove( sourcename );

            method.instructions.insertBefore(beginning, elseNode);
        }

        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(writer);
        return writer.toByteArray();
    } else if (transformedName.equals("paulscode.sound.StreamThread")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        MethodNode method = null;
        for (MethodNode m : classNode.methods) {
            if (m.name.equals("run") && m.desc.equals("()V")) // trying to find paulscode.sound.StreamThread.run();
            {
                method = m;
                break;
            }
        }
        if (method == null)
            throw new RuntimeException("Error processing " + transformedName + " - no run method found");

        AbstractInsnNode referenceNode = null;

        for (Iterator<AbstractInsnNode> iterator = method.instructions.iterator(); iterator.hasNext();) {
            AbstractInsnNode insn = iterator.next();
            if (insn instanceof MethodInsnNode && ((MethodInsnNode) insn).owner.equals("java/util/ListIterator") // searching for 'src = iter.next();' node (line 110)
                    && ((MethodInsnNode) insn).name.equals("next")) {
                referenceNode = insn.getNext().getNext();
                break;
            }
        }

        if (referenceNode != null) {
            int varIndex = ((VarInsnNode) referenceNode).var;

            LabelNode after = (LabelNode) referenceNode.getNext();
            method.instructions.insertBefore(after, new VarInsnNode(Opcodes.ALOAD, varIndex)); // add if(removed)
            method.instructions.insertBefore(after,
                    new FieldInsnNode(Opcodes.GETFIELD, "paulscode/sound/Source", "removed", "Z"));
            method.instructions.insertBefore(after, new JumpInsnNode(Opcodes.IFEQ, after));

            // if the source has been marked as removed, clean it up and set the variable to null so it will be removed from the list
            method.instructions.insertBefore(after, new VarInsnNode(Opcodes.ALOAD, varIndex)); // src.cleanup();
            method.instructions.insertBefore(after, new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                    "paulscode/sound/Source", "cleanup", "()V", false));
            method.instructions.insertBefore(after, new InsnNode(Opcodes.ACONST_NULL)); // src = null;
            method.instructions.insertBefore(after, new VarInsnNode(Opcodes.ASTORE, varIndex));
        }

        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(writer);
        return writer.toByteArray();
    }

    return basicClass;
}

From source file:net.transmutator4j.mutations.BooleanMutation.java

License:Open Source License

private int getOppositeConstantOf(int opcode) {
    if (opcode == Opcodes.ICONST_0) {
        return Opcodes.ICONST_1;
    }/* www  . j  ava 2 s.co m*/
    return Opcodes.ICONST_0;

}

From source file:net.transmutator4j.mutations.TestBooleanMutation.java

License:Open Source License

@Test
public void isBooleanOpcode() {
    assertTrue(BooleanMutation.isMutatable(Opcodes.ICONST_0));
    assertTrue(BooleanMutation.isMutatable(Opcodes.ICONST_1));
}

From source file:net.transmutator4j.mutations.TestBooleanMutation.java

License:Open Source License

@Test
public void mutateBooleanFalse() {
    BooleanMutation mutatedFalse = BooleanMutation.getMutationFor(Opcodes.ICONST_0);
    assertEquals(Opcodes.ICONST_0, mutatedFalse.getOriginalOpCode());
    assertEquals(Opcodes.ICONST_1, mutatedFalse.getMutatedOpCode());
    assertEquals("changed constant 0 (or false) to 1 (or true)", mutatedFalse.description());
}

From source file:net.transmutator4j.mutations.TestBooleanMutation.java

License:Open Source License

@Test
public void mutateBooleanTrue() {
    BooleanMutation mutatedFalse = BooleanMutation.getMutationFor(Opcodes.ICONST_1);
    assertEquals(Opcodes.ICONST_1, mutatedFalse.getOriginalOpCode());
    assertEquals(Opcodes.ICONST_0, mutatedFalse.getMutatedOpCode());
    assertEquals("changed constant 1 (or true) to 0 (or false)", mutatedFalse.description());
}

From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Writes the bean constructor to the given ClassWriter.
 * @param beanClass the original bean class to extend
 * @param bcd the descriptor of the bean
 * @param classNameInternal the internal name of the new class
 * @param cw the ClassWriter to write to
 * @param snippetWriter if not null, this will be invoked to add a snippet
 *                      after the invocation of the super constructor
 *//* w w w  . j a  v a  2s  .c  o  m*/
public static void writeConstructor(Class<?> beanClass, BeanClassDescriptor bcd, String classNameInternal,
        ClassWriter cw, SnippetWriter snippetWriter) {
    String classNameDescriptor = "L" + classNameInternal + ";";

    int localPropertySize = 0;
    ArrayList<PropertyDescriptor> localVarProperties = new ArrayList<PropertyDescriptor>();
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        if (pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null)) {
            localVarProperties.add(pd);
            localPropertySize += Type.getType(pd.getPropertyClass()).getSize();
        }
    }

    final int locVarThis = 0;
    final int locVarController = 1;
    final int locVarProps = 2;
    final int locVarPropertiesOffset = 3;
    final int locVarP = 3 + localPropertySize;
    final int locVarK = 4 + localPropertySize;
    final int locVarV = 5 + localPropertySize;

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)V", null, null);
    mv.visitCode();
    Label lTry = new Label();
    Label lCatch = new Label();
    mv.visitTryCatchBlock(lTry, lCatch, lCatch, "java/lang/ClassCastException");

    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(beanClass), "<init>", "()V");

    if (snippetWriter != null)
        snippetWriter.write(mv);

    Label lPropertyInit = new Label();
    mv.visitLabel(lPropertyInit);
    // load default values into the local variables for each property that must be set
    int varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        if (pd.getDefaultValue() != null)
            mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(pd.getDefaultValue().getDeclaringClass()),
                    pd.getDefaultValue().getName(), Type.getDescriptor(pd.getDefaultValue().getType()));
        else
            GenerationUtils.generateLoadDefault(mv, pd.getPropertyClass());
        mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), locVarPropertiesOffset + varCount);
        varCount += pt.getSize();
    }

    // loop through the props argument's list
    mv.visitVarInsn(Opcodes.ALOAD, locVarProps);
    mv.visitVarInsn(Opcodes.ASTORE, locVarP);
    Label lWhile = new Label();
    Label lEndWhile = new Label();
    Label lWhileBody = new Label();
    mv.visitLabel(lWhile);
    mv.visitJumpInsn(Opcodes.GOTO, lEndWhile);
    mv.visitLabel(lWhileBody);

    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getKey",
            "()Ljava/lang/String;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarK);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getValue",
            "()Ljava/lang/Object;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarV);

    mv.visitLabel(lTry);
    // write an if for each property
    Label lEndIf = new Label();
    varCount = 0;
    int ifCount = 0;
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        boolean usesLocal = pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null);
        Class<?> propClass = pd.getPropertyClass();
        Type pt = Type.getType(propClass);
        mv.visitVarInsn(Opcodes.ALOAD, locVarK);
        mv.visitLdcInsn(pd.getName());
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
        Label lElse = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, lElse);

        if (!usesLocal)
            mv.visitVarInsn(Opcodes.ALOAD, locVarThis); // for setter invocation, load 'this'

        if (propClass.isPrimitive()) {
            mv.visitLdcInsn(pd.getName());
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(BeanHelper.class),
                    String.format("get%s%sFromPropValue",
                            propClass.getName().substring(0, 1).toUpperCase(Locale.US),
                            propClass.getName().substring(1)),
                    "(Ljava/lang/String;Ljava/lang/Object;)" + pt.getDescriptor());
        } else if (!propClass.equals(Object.class)) {
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);
            mv.visitTypeInsn(Opcodes.CHECKCAST, pt.getInternalName());
        } else
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);

        if (!usesLocal)
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(),
                    Type.getMethodDescriptor(pd.getSetter()));
        else
            mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), varCount + locVarPropertiesOffset);

        mv.visitJumpInsn(Opcodes.GOTO, lEndIf);
        mv.visitLabel(lElse);

        ifCount++;
        if (usesLocal)
            varCount += pt.getSize();
    }

    // else (==> if not prop matched) throw IllegalArgumentException
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("Unknown property \"%s\".");
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ALOAD, locVarK);
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            "(Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(lCatch);
    mv.visitInsn(Opcodes.POP); // pop the exception object (not needed)
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("Incompatible type for property \"%s\". Got %s.");
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ALOAD, locVarK);
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitVarInsn(Opcodes.ALOAD, locVarV);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            "(Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(lEndIf);
    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "tail",
            "()Lorg/actorsguildframework/Props;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarP);

    mv.visitLabel(lEndWhile);
    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitJumpInsn(Opcodes.IFNONNULL, lWhileBody);

    // write local variables back into properties 
    varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
        if (pd.getPropertySource() == PropertySource.ABSTRACT_METHOD) {
            mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount);
            mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal,
                    String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()), pt.getDescriptor());
        } else if (pd.getPropertySource() == PropertySource.USER_WRITTEN) {
            mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(),
                    Type.getMethodDescriptor(pd.getSetter()));
        } else
            throw new RuntimeException("Internal error");
        varCount += pt.getSize();
    }

    // if bean is thread-safe, publish all writes now
    if (bcd.isThreadSafe()) {
        mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
        mv.visitInsn(Opcodes.DUP);
        mv.visitInsn(Opcodes.MONITORENTER);
        mv.visitInsn(Opcodes.MONITOREXIT);
    }

    mv.visitInsn(Opcodes.RETURN);
    Label lEnd = new Label();
    mv.visitLabel(lEnd);

    mv.visitLocalVariable("this", classNameDescriptor, null, lBegin, lEnd, locVarThis);
    mv.visitLocalVariable("controller", "Lorg/actorsguildframework/internal/Controller;", null, lBegin, lEnd,
            locVarController);
    mv.visitLocalVariable("props", "Lorg/actorsguildframework/Props;", null, lBegin, lEnd, locVarProps);
    varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        mv.visitLocalVariable("__" + pd.getName(), pt.getDescriptor(),
                GenericTypeHelper.getSignature(pd.getPropertyType()), lPropertyInit, lEnd,
                locVarPropertiesOffset + varCount);
        varCount += pt.getSize();
    }
    mv.visitLocalVariable("p", "Lorg/actorsguildframework/Props;", null, lPropertyInit, lEnd, locVarP);
    mv.visitLocalVariable("k", "Ljava/lang/String;", null, lWhile, lEndWhile, locVarK);
    mv.visitLocalVariable("v", "Ljava/lang/Object;", null, lWhile, lEndWhile, locVarV);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.adjective.stout.tools.StackVisualiserMethodVisitor.java

License:Apache License

public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
        break;/*w  w w  .  j a  v  a  2  s  .  co  m*/
    case Opcodes.ACONST_NULL:
        push("null", Object.class);
        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(Integer.toString(opcode - Opcodes.ICONST_0), Type.INT_TYPE);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        push(Integer.toString(opcode - Opcodes.LCONST_0), Type.LONG_TYPE);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        push(Integer.toString(opcode - Opcodes.FCONST_0), Type.FLOAT_TYPE);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        push(Integer.toString(opcode - Opcodes.DCONST_0), Type.DOUBLE_TYPE);
        break;
    case Opcodes.IALOAD:
    case Opcodes.LALOAD:
    case Opcodes.FALOAD:
    case Opcodes.DALOAD:
    case Opcodes.AALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD: {
        Type opType = getType(Opcodes.IALOAD, opcode);
        StackValue idx = pop(Type.INT_TYPE);
        StackValue arr = popArray(opType);
        push(arr.description + "[" + idx.description + "]", opType);
    }
        break;
    case Opcodes.IASTORE:
    case Opcodes.LASTORE:
    case Opcodes.FASTORE:
    case Opcodes.DASTORE:
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE: {
        Type opType = getType(Opcodes.IASTORE, opcode);
        pop(opType);
        pop(Type.INT_TYPE);
        popArray(opType);
    }
        break;
    case Opcodes.POP:
        pop();
        break;
    case Opcodes.POP2:
        pop(2);
        break;
    case Opcodes.DUP:
        push(peek());
        break;
    case Opcodes.DUP2:
        push(peek(2));
        push(peek(1));
        break;
    case Opcodes.DUP_X1: {
        StackValue a = pop();
        StackValue b = pop();
        push(a);
        push(b);
        push(a);
    }
        break;
    case Opcodes.DUP_X2: {
        StackValue a = pop();
        StackValue b = pop();
        StackValue c = pop();
        push(a);
        push(c);
        push(b);
        push(a);
    }
        break;
    case Opcodes.DUP2_X1: {
        StackValue a = popValue(false);
        StackValue b = pop();
        StackValue c = pop();
        push(b);
        push(a);
        push(c);
        push(b);
        push(a);
    }
    case Opcodes.DUP2_X2: {
        StackValue a = popValue(false);
        StackValue b = pop();
        StackValue c = popValue(false);
        StackValue d = pop();
        push(b);
        push(a);
        push(d);
        push(c);
        push(b);
        push(a);
    }
        break;
    case Opcodes.SWAP: {
        StackValue a = pop();
        StackValue b = pop();
        push(a);
        push(b);
    }
        break;
    case Opcodes.IADD:
    case Opcodes.LADD:
    case Opcodes.FADD:
    case Opcodes.DADD:
        math(Opcodes.IADD, opcode, "+");
        break;
    case Opcodes.ISUB:
    case Opcodes.LSUB:
    case Opcodes.FSUB:
    case Opcodes.DSUB:
        math(Opcodes.ISUB, opcode, "-");
        break;
    case Opcodes.IMUL:
    case Opcodes.LMUL:
    case Opcodes.FMUL:
    case Opcodes.DMUL:
        math(Opcodes.IMUL, opcode, "*");
        break;
    case Opcodes.IDIV:
    case Opcodes.LDIV:
    case Opcodes.FDIV:
    case Opcodes.DDIV:
        math(Opcodes.IDIV, opcode, "/");
        break;
    case Opcodes.IREM:
    case Opcodes.LREM:
    case Opcodes.FREM:
    case Opcodes.DREM:
        math(Opcodes.IREM, opcode, "%");
        break;
    case Opcodes.IAND:
    case Opcodes.LAND:
        math(Opcodes.IAND, opcode, "&");
        break;
    case Opcodes.IOR:
    case Opcodes.LOR:
        math(Opcodes.IOR, opcode, "|");
        break;
    case Opcodes.IXOR:
    case Opcodes.LXOR:
        math(Opcodes.IXOR, opcode, "^");
        break;
    case Opcodes.INEG:
    case Opcodes.LNEG:
    case Opcodes.FNEG:
    case Opcodes.DNEG: {
        Type type = getType(Opcodes.INEG, opcode);
        StackValue a = pop(type);
        push("-" + a.description, type);
    }
        break;
    case Opcodes.ISHL:
    case Opcodes.LSHL: {
        Type type = getType(Opcodes.ISHL, opcode);
        StackValue n = pop(Type.INT_TYPE);
        StackValue a = pop(type);
        push(a.description + "<<" + n.description, type);
    }
        break;
    case Opcodes.ISHR:
    case Opcodes.LSHR: {
        Type type = getType(Opcodes.ISHR, opcode);
        StackValue n = pop(Type.INT_TYPE);
        StackValue a = pop(type);
        push(a.description + ">>" + n.description, type);
    }
        break;
    case Opcodes.IUSHR:
    case Opcodes.LUSHR: {
        Type type = getType(Opcodes.IUSHR, opcode);
        StackValue n = pop(Type.INT_TYPE);
        StackValue a = pop(type);
        push(a.description + ">>>" + n.description, type);
    }
    case Opcodes.LCMP: {
        StackValue a = pop(Type.LONG_TYPE);
        StackValue b = pop(Type.LONG_TYPE);
        push(a.description + " cmp " + b.description + " {-1|0|1}", Type.LONG_TYPE);
    }
        break;
    case Opcodes.I2L:
    case Opcodes.I2F:
    case Opcodes.I2D:
    case Opcodes.L2I:
    case Opcodes.L2F:
    case Opcodes.L2D:
    case Opcodes.F2I:
    case Opcodes.F2L:
    case Opcodes.F2D:
    case Opcodes.D2I:
    case Opcodes.D2L:
    case Opcodes.D2F:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
        cast(opcode);
        break;
    case Opcodes.ARETURN:
    case Opcodes.ATHROW:
        popObject();
        break;
    case Opcodes.RETURN:
        break;
    default:
        throw new IllegalArgumentException("Unsupported opcode " + opcode + " - " + OPCODES[opcode]);
    }
    print(opcode, "");
    /* 
        *        FCMPL, FCMPG, DCMPL, DCMPG, IRETURN, LRETURN,
        *        FRETURN, DRETURN, ARETURN, RETURN, ARRAYLENGTH, ATHROW,
        *        MONITORENTER, or MONITOREXIT
      */

}

From source file:org.apache.drill.exec.compile.AsmUtil.java

License:Apache License

/**
 * Determine if the given opcode is a load of a constant (xCONST_y).
 *
 * @param opcode the opcode//from ww w .j  a  va  2  s  .c o  m
 * @return true if the opcode is one of the constant loading ones, false otherwise
 */
public static boolean isXconst(final int opcode) {
    switch (opcode) {
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
    case Opcodes.ICONST_M1:
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        return true;
    }

    return false;
}

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createMethodCheck(final String enhancedClassName, final String descEnhancer,
        final ClassWriter cw) {
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PRIVATE, Enhancer.METHOD_ENHANCED_CHECK,
            Enhancer.makeDescription(Void.TYPE), null, null);
    mv.visitCode();//from  w  w w  . j av  a  2  s .  co  m

    final Label lCheckInternal = new Label();
    final Label lCheckInitialized = new Label();
    final Label lReturn = new Label();
    final Label lFind = new Label();
    final Label lInitialized = new Label();
    final Label lChanged = new Label();
    final Label lOut = new Label();

    // if (!this.__enhanced__$$__internal) { return }
    mv.visitLabel(lCheckInternal);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INTERNAL,
            Enhancer.DESCRIPTOR_BOOLEAN);
    mv.visitJumpInsn(Opcodes.IFEQ, lCheckInitialized);
    mv.visitInsn(Opcodes.RETURN);

    // if (!this.__enhanced__$$__initialized) {
    mv.visitLabel(lCheckInitialized);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);
    mv.visitJumpInsn(Opcodes.IFNE, lChanged);

    //     if (this.__enhanced_$$__session == null)
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitJumpInsn(Opcodes.IFNONNULL, lFind);

    //         throw new PersistenceException("No session to initialize the instance");
    mv.visitTypeInsn(Opcodes.NEW, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION);
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("No session to initialize the instance");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION,
            Enhancer.CONSTRUCTOR_INIT, Enhancer.makeDescription(Void.TYPE, String.class));
    mv.visitInsn(Opcodes.ATHROW);

    //       this.__enhanced_$$__session.getEntityManager().find(this.__enhanced_$$__type, this.__enhanced__$$__id);
    mv.visitLabel(lFind);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_SESSION, Enhancer.METHOD_GET_ENTITY_MANAGER,
            Enhancer.makeDescription(EntityManagerImpl.class));
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_TYPE,
            Enhancer.DESCRIPTOR_CLASS);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_ID,
            Enhancer.DESCRIPTOR_OBJECT);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_ENTITY_MANAGER, Enhancer.METHOD_FIND,
            Enhancer.makeDescription(Object.class, Class.class, Object.class));
    mv.visitInsn(Opcodes.POP);

    //   this.__enhanced__$$__initialized = true;
    mv.visitLabel(lInitialized);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);

    // if (this.__enhanced_$$__session != null)
    mv.visitLabel(lChanged);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitJumpInsn(Opcodes.IFNULL, lReturn);

    //     this.__enhanced__$$__managedInstance.changed();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_MANAGED_INSTANCE,
            Enhancer.DESCRIPTOR_MANAGED_INSTANCE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_MANAGED_INSTANCE, Enhancer.METHOD_CHANGED,
            Enhancer.makeDescription(Void.TYPE));

    // return;
    mv.visitLabel(lReturn);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitInsn(Opcodes.RETURN);

    mv.visitLabel(lOut);
    mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, lCheckInternal, lOut, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}