Example usage for org.objectweb.asm MethodVisitor visitVarInsn

List of usage examples for org.objectweb.asm MethodVisitor visitVarInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitVarInsn.

Prototype

public void visitVarInsn(final int opcode, final int var) 

Source Link

Document

Visits a local variable instruction.

Usage

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

/**
 * @see #generateDecodeInt32Value(boolean, org.objectweb.asm.MethodVisitor) 
 *//*  w w w  .ja  v a2s . c  o m*/
protected void generateDecodeEnumValue(boolean required, MethodVisitor mv, TypeDef type,
        String genClassInternalName, String fieldIdentifier, LocalVariable nextVar, Class<?> javaClass,
        String debugValueLabel) throws IllegalArgumentException {

    Label endLabel = new Label();
    if (required) {
        generateDecodeInt32Value(true, mv);
        box(mv, Integer.class);
    } else {
        generateDecodeInt32Value(false, mv);
        mv.visitInsn(DUP);
        mv.visitJumpInsn(IFNULL, endLabel);

        //            Label nonNullLabel = new Label();
        //            mv.visitJumpInsn(IFNONNULL, nonNullLabel);
        //            mv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass));
        //            mv.visitJumpInsn(GOTO, endLabel);

        // not null
        //            mv.visitLabel(nonNullLabel);
        //            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false); // unbox(mv, Integer.class);
    }

    // SymbolMapping
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, genClassInternalName, "symbolMapping_" + fieldIdentifier,
            "Lcom/cinnober/msgcodec/SymbolMapping;");
    mv.visitInsn(SWAP);
    mv.visitMethodInsn(INVOKEINTERFACE, "com/cinnober/msgcodec/SymbolMapping", "lookup",
            "(Ljava/lang/Integer;)Ljava/lang/Object;", true);

    //        // switch
    //        TypeDef.Enum enumType = (TypeDef.Enum) type;
    //        List<Symbol> symbols = enumType.getSymbols();
    //        int numSymbols = symbols.size();
    //        int[] ids = new int[numSymbols];
    //        Label[] labels = new Label[numSymbols];
    //        for (int i=0; i<numSymbols; i++) {
    //            ids[i] = symbols.get(i).getId();
    //            labels[i] = new Label();
    //        }
    //        Label defaultLabel = new Label();
    //        int symbolIdVar = nextVar.next();
    //        mv.visitInsn(DUP);
    //        mv.visitVarInsn(ISTORE, symbolIdVar);
    //        EnumSymbols enumSymbols = null;
    //        if (javaClass.isEnum()) {
    //            enumSymbols = new EnumSymbols(enumType, javaClass);
    //        }
    //        mv.visitLookupSwitchInsn(defaultLabel, ids, labels);
    //        for (int i=0; i<numSymbols; i++) {
    //            boolean addGotoEnd = true;
    //            mv.visitLabel(labels[i]);
    //            if (javaClass.isEnum()) {
    //                Enum enumValue = enumSymbols.getEnum(ids[i]);
    //                if (enumValue != null) {
    //                    //mv.visitLdcInsn(Type.getType(javaClass));
    //                    mv.visitFieldInsn(GETSTATIC, Type.getInternalName(javaClass),
    //                            enumValue.name(),
    //                            Type.getDescriptor(javaClass));
    //                } else {
    //                    mv.visitLdcInsn(debugValueLabel);
    //                    mv.visitLdcInsn(ids[i]);
    //                    mv.visitLdcInsn(Type.getType(javaClass));
    //                    mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "unmappableEnumSymbolId",
    //                            "(Ljava/lang/String;ILjava/lang/Class;)Lcom/cinnober/msgcodec/DecodeException;", false);
    //                    mv.visitInsn(ATHROW);
    //                    addGotoEnd = false;
    //                }
    //            } else if (javaClass == int.class || javaClass == Integer.class) {
    //                mv.visitLdcInsn(ids[i]);
    //                if (!required) {
    //                    box(mv, Integer.class);
    //                }
    //            } else {
    //                throw new IllegalArgumentException("Illegal enum javaClass: " + javaClass);
    //            }
    //            if (addGotoEnd) {
    //                mv.visitJumpInsn(GOTO, endLabel);
    //            }
    //        }
    //        mv.visitLabel(defaultLabel);
    //        mv.visitLdcInsn(debugValueLabel);
    //        mv.visitVarInsn(ILOAD, symbolIdVar);
    //        mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "unknownEnumSymbol",
    //                "(Ljava/lang/String;I)Lcom/cinnober/msgcodec/DecodeException;", false);
    //        mv.visitInsn(ATHROW);

    // end
    mv.visitLabel(endLabel);

    if (!javaClass.isEnum()) {
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Integer.class));
        if (required) {
            unbox(mv, Integer.class);
        }
    } else {
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass));
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateDecodeDynRefValue(MethodVisitor mv, boolean required, GroupDef refGroup,
        Class<?> javaClass) {
    mv.visitVarInsn(ALOAD, 0); // this
    mv.visitInsn(SWAP); // this and in
    if (required) {
        mv.visitMethodInsn(INVOKEVIRTUAL, baseclassIName, "readDynamicGroup",
                "(Lcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Object;", false);
    } else {//from  w w  w.j a  va 2 s  . co  m
        mv.visitMethodInsn(INVOKEVIRTUAL, baseclassIName, "readDynamicGroupNull",
                "(Lcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Object;", false);
    }
    if (refGroup != null) {
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass));
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateDecodeRefValue(GroupDef refGroup, boolean required, MethodVisitor mv, int byteSourceVar,
        String genClassInternalName, Class<?> javaClass, TypeDef type, boolean javaClassCodec)
        throws IllegalArgumentException {
    if (refGroup != null) {
        String groupDescriptor = getTypeDescriptor(javaClass, javaClassCodec);
        if (required) {
            mv.visitInsn(POP); // input stream
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, byteSourceVar);
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + refGroup.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSource;)" + groupDescriptor, false);
        } else {/*from   ww w  . j av a 2s  .  c  om*/
            mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readPresenceByte",
                    "(Lcom/cinnober/msgcodec/io/ByteSource;)Z", false);
            Label nonNullLabel = new Label();
            Label endLabel = new Label();
            mv.visitJumpInsn(IFNE, nonNullLabel); // not false, i.e. true
            // null
            mv.visitInsn(ACONST_NULL);
            mv.visitJumpInsn(GOTO, endLabel);

            // not null
            mv.visitLabel(nonNullLabel);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, byteSourceVar);
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + refGroup.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSource;)" + groupDescriptor, false);

            mv.visitLabel(endLabel);
            // PENDING: mv.visitFrame?
            mv.visitFrame(F_SAME, 0, null, 0, null);
        }
    } else {
        throw new IllegalArgumentException("Illegal reference: " + type);
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateDecodeSequenceValue(Class<?> javaClass, LocalVariable nextVar, boolean required,
        MethodVisitor mv, Class<?> componentJavaClass, int byteSourceVar, TypeDef type, Schema schema,
        String genClassInternalName, String fieldIdentifier, String debugValueLabel, boolean javaClassCodec)
        throws IllegalArgumentException {
    if (!javaClass.isArray() && javaClass != List.class) {
        throw new IllegalArgumentException("Illegal sequence javaClass: " + javaClass);
    }/*  w  w  w  .  j av a2  s  . c o m*/

    int lengthVar = nextVar.next();
    int sequenceVar = nextVar.next();
    Label finalEndLabel = new Label();
    if (required) {
        mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readUInt32",
                "(Lcom/cinnober/msgcodec/io/ByteSource;)I", false);
        mv.visitVarInsn(ISTORE, lengthVar);
    } else {
        mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readUInt32Null",
                "(Lcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Integer;", false);
        mv.visitInsn(DUP);
        mv.visitJumpInsn(IFNULL, finalEndLabel);
        unbox(mv, Integer.class);
        mv.visitVarInsn(ISTORE, lengthVar);
    }

    if (javaClass.isArray()) {
        mv.visitVarInsn(ILOAD, lengthVar);
        generateNewArray(mv, componentJavaClass);
    } else {
        mv.visitTypeInsn(NEW, "java/util/ArrayList");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ILOAD, lengthVar);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "(I)V", false);
    }
    mv.visitVarInsn(ASTORE, sequenceVar);

    // for loop
    Label endLabel = new Label();
    int loopVar = nextVar.next();
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, loopVar);
    Label loopLabel = new Label();
    mv.visitLabel(loopLabel);
    // PENDING: mv.visitFrame?
    mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ILOAD, loopVar);
    mv.visitVarInsn(ILOAD, lengthVar);
    mv.visitJumpInsn(IF_ICMPGE, endLabel);

    mv.visitVarInsn(ALOAD, sequenceVar);
    mv.visitVarInsn(ILOAD, loopVar);
    mv.visitVarInsn(ALOAD, byteSourceVar);

    // decode the element
    TypeDef.Sequence seqType = (TypeDef.Sequence) type;
    generateDecodeValue(mv, byteSourceVar, nextVar, true, seqType.getComponentType(), componentJavaClass, null,
            schema, genClassInternalName, fieldIdentifier, debugValueLabel + ".component", javaClassCodec);

    // store the value
    if (javaClass.isArray()) {
        generateArrayStore(mv, componentJavaClass);
    } else {
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/ArrayList", "add", "(ILjava/lang/Object;)V", false);
    }

    mv.visitIincInsn(loopVar, 1);
    mv.visitJumpInsn(GOTO, loopLabel);
    mv.visitLabel(endLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, sequenceVar);
    mv.visitLabel(finalEndLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);
    if (javaClass.isArray()) {
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass));
    }
}

From source file:com.cinnober.msgcodec.blink.NativeByteCodeGenerator.java

License:Open Source License

@Override
protected void generateWriteStaticGroupForTypeWithId(Schema schema, ClassVisitor cv,
        String genClassInternalName, boolean javaClassCodec) {
    for (GroupDef group : schema.getGroups()) {
        Object groupType = group.getGroupType();
        String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec);
        MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, "writeStaticGroupWithId_" + group.getName(),
                "(Lcom/cinnober/msgcodec/io/ByteSink;" + groupDescriptor + ")V", null,
                new String[] { "java/io/IOException" });
        mv.visitCode();/*from  ww  w.j  a v  a  2  s .co  m*/
        int nextWriteidVar = 3;

        if (group.getId() != -1) {
            // write with id
            mv.visitVarInsn(ALOAD, 1); // out
            mv.visitLdcInsn(group.getId() & 0xffffffffL);
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeUInt64",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;J)V", false);

            // add extension offset (four zero bytes)
            mv.visitVarInsn(ALOAD, 1); // out
            mv.visitInsn(ICONST_0);
            generateEncodeUInt32Value(true, mv);

            mv.visitVarInsn(ALOAD, 0); // this
            mv.visitVarInsn(ALOAD, 1); // out
            mv.visitVarInsn(ALOAD, 2); // obj
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "writeStaticGroup_" + group.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSink;" + groupDescriptor + ")V", false);
            mv.visitInsn(RETURN);
        } else {
            // write with id
            mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
            mv.visitInsn(DUP);
            mv.visitLdcInsn("No group id");
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>",
                    "(Ljava/lang/String;)V", false);
            mv.visitInsn(ATHROW);
        }

        // end
        mv.visitMaxs(3, nextWriteidVar);
        mv.visitEnd();
    }
}

From source file:com.e2info.helloasm.HelloAsmApp.java

License:Open Source License

public static void main(String[] args) throws IOException {
    String name = "HelloAsm";
    int flag = ClassWriter.COMPUTE_MAXS;
    ClassWriter cw = new ClassWriter(flag);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, name, null, "java/lang/Object", null);

    cw.visitSource(name + ".java", null);

    {/*from  ww w  .  ja  v a  2 s. c  om*/
        MethodVisitor mv;
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(Opcodes.RETURN);
        // we need this call to take effect ClassWriter.COMPUTE_MAXS flag.
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    {
        MethodVisitor mv;
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null,
                null);
        mv.visitCode();
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("hello ASM");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
        mv.visitInsn(Opcodes.RETURN);
        // we need this call to take effect ClassWriter.COMPUTE_MAXS flag.
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    cw.visitEnd();

    // build binary
    byte[] bin = cw.toByteArray();

    // save asm trace for human readable
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        new ClassReader(bin).accept(new TraceClassVisitor(pw), 0);
        File f = new File(name + ".txt");
        FileUtils.writeStringToFile(f, sw.toString());
    }

    // save as calss file
    {
        File f = new File(name + ".class");
        FileUtils.writeByteArrayToFile(f, bin);
    }

}

From source file:com.gargoylesoftware.js.nashorn.internal.codegen.types.ArrayType.java

License:Open Source License

@Override
public Type load(final MethodVisitor method, final int slot) {
    assert slot != -1;
    method.visitVarInsn(ALOAD, slot);
    return this;
}

From source file:com.gargoylesoftware.js.nashorn.internal.codegen.types.BooleanType.java

License:Open Source License

@Override
public Type load(final MethodVisitor method, final int slot) {
    assert slot != -1;
    method.visitVarInsn(ILOAD, slot);
    return BOOLEAN;
}

From source file:com.gargoylesoftware.js.nashorn.internal.codegen.types.BooleanType.java

License:Open Source License

@Override
public void store(final MethodVisitor method, final int slot) {
    assert slot != -1;
    method.visitVarInsn(ISTORE, slot);
}

From source file:com.gargoylesoftware.js.nashorn.internal.codegen.types.IntType.java

License:Open Source License

@Override
public Type load(final MethodVisitor method, final int slot) {
    assert slot != -1;
    method.visitVarInsn(ILOAD, slot);
    return INT;//from  w  ww  . j a v  a  2  s. co  m
}