Example usage for org.objectweb.asm MethodVisitor visitLdcInsn

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

Introduction

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

Prototype

public void visitLdcInsn(final Object value) 

Source Link

Document

Visits a LDC instruction.

Usage

From source file:erjang.beam.CompilerVisitor.java

License:Apache License

static public byte[] make_invoker(String module, String function, Type self_type, String mname, String fname,
        int arity, boolean proc, boolean exported, Lambda lambda, Type return_type, boolean is_tail_call,
        final boolean is_pausable) {

    int freevars = lambda == null ? 0 : lambda.freevars;

    String outer_name = self_type.getInternalName();
    String inner_name = "FN_" + mname;
    String full_inner_name = outer_name + "$" + inner_name;

    ClassWriter cw = new ClassWriter(true);
    int residual_arity = arity - freevars;
    String super_class_name = EFUN_NAME + residual_arity + (exported ? "Exported" : "");
    if (exported)
        EFun.ensure_exported(residual_arity);
    else//from   w  w  w  .j a  v  a2 s. c  o  m
        EFun.ensure(residual_arity);

    cw.visit(V1_6, ACC_FINAL | ACC_PUBLIC, full_inner_name, null, super_class_name, null);

    if (lambda != null) {
        cw.visitField(ACC_STATIC | ACC_PUBLIC | ACC_FINAL, "index", "I", null, new Integer(lambda.index));
        cw.visitField(ACC_STATIC | ACC_PUBLIC | ACC_FINAL, "old_index", "I", null,
                new Integer(lambda.old_index));
        cw.visitField(ACC_STATIC | ACC_PUBLIC | ACC_FINAL, "old_uniq", "I", null, new Integer(lambda.old_uniq));

        /** */
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "encode",
                "(" + Type.getDescriptor(EOutputStream.class) + ")V", null, null);
        mv.visitCode();

        mv.visitVarInsn(ALOAD, 1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, full_inner_name, "pid", EPID_TYPE.getDescriptor());
        mv.visitLdcInsn(module);

        mv.visitFieldInsn(GETSTATIC, full_inner_name, "old_index", "I");
        mv.visitInsn(I2L);
        mv.visitLdcInsn(new Integer(arity));
        mv.visitFieldInsn(GETSTATIC, outer_name, "module_md5", EBINARY_TYPE.getDescriptor());

        mv.visitFieldInsn(GETSTATIC, full_inner_name, "index", "I");
        mv.visitInsn(I2L);
        mv.visitFieldInsn(GETSTATIC, full_inner_name, "old_uniq", "I");
        mv.visitInsn(I2L);

        mv.visitLdcInsn(new Integer(freevars));
        mv.visitTypeInsn(ANEWARRAY, EOBJECT_NAME);

        for (int i = 0; i < freevars; i++) {
            mv.visitInsn(DUP);

            if (i <= 5) {
                mv.visitInsn(ICONST_0 + i);
            } else {
                mv.visitLdcInsn(new Integer(i));
            }

            mv.visitVarInsn(ALOAD, 0); // load self
            mv.visitFieldInsn(GETFIELD, full_inner_name, "fv" + i, EOBJECT_DESC);

            mv.visitInsn(AASTORE);
        }

        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(EOutputStream.class), "write_fun",
                "(" + EPID_TYPE.getDescriptor() + "Ljava/lang/String;" + "JI" + EBINARY_TYPE.getDescriptor()
                        + "JJ" + Type.getDescriptor(EObject[].class) + ")V");

        mv.visitInsn(RETURN);
        mv.visitMaxs(10, 3);
        mv.visitEnd();
    }

    make_constructor(cw, module, function, full_inner_name, super_class_name, lambda, exported);

    make_go_method(cw, outer_name, fname, full_inner_name, arity, proc, freevars, return_type, is_tail_call,
            is_pausable);

    make_go2_method(cw, outer_name, fname, full_inner_name, arity, proc, freevars, return_type, is_tail_call,
            is_pausable);

    return cw.toByteArray();

}

From source file:erjang.beam.CompilerVisitor.java

License:Apache License

private static void make_constructor(ClassWriter cw, String module_name, String function_name,
        String full_inner_name, String super_class_name, Lambda lambda, boolean exported) {
    StringBuilder sb = new StringBuilder("(");
    int freevars = lambda == null ? 0 : lambda.freevars;
    if (lambda != null) {
        sb.append(EPID_TYPE.getDescriptor());
        cw.visitField(ACC_PUBLIC | ACC_FINAL, "pid", EPID_TYPE.getDescriptor(), null, null);
    }//from  www .  ja v a  2 s.  co m
    // create the free vars
    for (int i = 0; i < freevars; i++) {
        cw.visitField(ACC_PUBLIC | ACC_FINAL, "fv" + i, EOBJECT_DESC, null, null);
        sb.append(EOBJECT_DESC);
    }
    sb.append(")V");

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", sb.toString(), null, null);
    mv.visitCode();

    mv.visitVarInsn(ALOAD, 0);
    if (exported) {
        mv.visitLdcInsn(module_name);
        mv.visitLdcInsn(function_name);
        mv.visitMethodInsn(INVOKESPECIAL, super_class_name, "<init>",
                "(Ljava/lang/String;Ljava/lang/String;)V");
    } else {
        mv.visitMethodInsn(INVOKESPECIAL, super_class_name, "<init>", "()V");
    }

    if (lambda != null) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(PUTFIELD, full_inner_name, "pid", EPID_TYPE.getDescriptor());

    }

    for (int i = 0; i < freevars; i++) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, i + 2);
        mv.visitFieldInsn(PUTFIELD, full_inner_name, "fv" + i, EOBJECT_DESC);
    }

    mv.visitInsn(RETURN);
    mv.visitMaxs(3, 3);
    mv.visitEnd();

    if (lambda != null) {
        mv = cw.visitMethod(ACC_PROTECTED, "get_env", "()" + ESEQ_DESC, null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, ERT_NAME, "NIL", ENIL_TYPE.getDescriptor());
        for (int i = freevars - 1; i >= 0; i--) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, full_inner_name, "fv" + i, EOBJECT_DESC);
            mv.visitMethodInsn(INVOKEVIRTUAL, ESEQ_NAME, "cons", "(" + EOBJECT_DESC + ")" + ESEQ_DESC);
        }
        mv.visitInsn(ARETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PROTECTED, "get_pid", "()" + EOBJECT_DESC, null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, full_inner_name, "pid", EPID_TYPE.getDescriptor());
        mv.visitInsn(ARETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PROTECTED, "get_id", "()" + Type.getDescriptor(FunID.class), null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, full_inner_name.substring(0, full_inner_name.indexOf('$')),
                anon_fun_name(lambda), Type.getDescriptor(LocalFunID.class));
        mv.visitInsn(ARETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();

    }
}

From source file:erjang.EAtom.java

License:Apache License

@Override
public org.objectweb.asm.Type emit_const(MethodVisitor fa) {

    Type type = EATOM_TYPE;/*  w w  w . ja va  2 s  .c  o  m*/

    fa.visitLdcInsn(value);
    fa.visitMethodInsn(Opcodes.INVOKESTATIC, type.getInternalName(), "intern",
            "(" + STRING_TYPE.getDescriptor() + ")" + type.getDescriptor());

    return type;
}

From source file:erjang.EBig.java

License:Apache License

@Override
public org.objectweb.asm.Type emit_const(MethodVisitor fa) {

    Type type = EBIG_TYPE;/*from w ww  .ja  va  2s. co m*/

    fa.visitLdcInsn(value.toString());
    fa.visitMethodInsn(Opcodes.INVOKESTATIC, type.getInternalName(), "fromString",
            "(" + STRING_TYPE.getDescriptor() + ")" + type.getDescriptor());

    return type;
}

From source file:erjang.EBigString.java

License:Apache License

@Override
public Type emit_const(MethodVisitor fa) {

    Type type = ESTRING_TYPE;//from  w  w  w  .  j  av a2 s.  c  o  m

    fa.visitLdcInsn(this.stringValue());
    fa.visitMethodInsn(Opcodes.INVOKESTATIC, type.getInternalName(), "fromString",
            "(" + STRING_TYPE.getDescriptor() + ")" + type.getDescriptor());

    return type;
}

From source file:erjang.EBinary.java

License:Apache License

@Override
public Type emit_const(MethodVisitor fa) {
    char[] chs = new char[byteSize()];
    for (int i = 0; i < byteSize(); i++) {
        chs[i] = (char) (0xff & octetAt(i));
    }//from   ww w.j  a  v a 2  s  . com
    String str = new String(chs);

    fa.visitLdcInsn(str);
    fa.visitMethodInsn(Opcodes.INVOKESTATIC, EBINARY_NAME, "fromString",
            "(Ljava/lang/String;)L" + EBINARY_NAME + ";");

    return EBINARY_TYPE;
}

From source file:erjang.EBinList.java

License:Apache License

@Override
public Type emit_const(MethodVisitor fa) {

    Type type = EBINLIST_TYPE;/*from  w  ww  .j av a2s  .  c o  m*/

    char[] ch = new char[len];
    for (int i = 0; i < len; i++) {
        ch[i] = (char) (0xff & (int) data[off + i]);
    }

    fa.visitLdcInsn(new String(ch));
    tail.emit_const(fa);
    fa.visitMethodInsn(Opcodes.INVOKESTATIC, type.getInternalName(), "fromString",
            "(" + STRING_TYPE.getDescriptor() + EOBJECT_DESC + ")" + type.getDescriptor());

    return type;
}

From source file:erjang.EBitString.java

License:Apache License

@Override
public Type emit_const(MethodVisitor fa) {
    char[] chs = new char[dataByteSize()];

    for (int byteIdx = 0; byteIdx < byteSize(); byteIdx += 1) {
        chs[byteIdx] = (char) (data[byteIdx] & 0xff);
    }//from   ww  w  .ja v  a2 s.  co m

    if (extra_bits != 0) {
        int rest = intBitsAt(byteSize() * 8, extra_bits);
        rest <<= (8 - extra_bits);
        chs[byteSize()] = (char) rest;
    }

    String str = new String(chs);

    fa.visitLdcInsn(str);
    fa.visitLdcInsn(new Integer(extra_bits));
    fa.visitMethodInsn(Opcodes.INVOKESTATIC, EBITSTRING_NAME, "make",
            "(Ljava/lang/String;I)L" + EBITSTRING_NAME + ";");

    return EBITSTRING_TYPE;
}

From source file:erjang.EDouble.java

License:Apache License

@Override
public org.objectweb.asm.Type emit_const(MethodVisitor fa) {

    Type type = EDOUBLE_TYPE;//from  ww w .  ja v a 2  s .com

    fa.visitTypeInsn(Opcodes.NEW, type.getInternalName());
    fa.visitInsn(Opcodes.DUP);
    fa.visitLdcInsn(new Double(value));
    fa.visitMethodInsn(Opcodes.INVOKESPECIAL, type.getInternalName(), "<init>", "(D)V");

    return type;
}

From source file:erjang.EFun.java

License:Apache License

static byte[] gen_fun_class_data(int arity) {

    String self_type = EFUN_TYPE.getInternalName() + arity;

    ClassWriter cw = new ClassWriter(true);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, self_type, null,
            EFUN_TYPE.getInternalName(), null);

    make_invoke_method(cw, self_type, arity);

    CompilerVisitor.make_invoketail_method(cw, self_type, arity, 0);

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "arity", "()I", null, null);
    mv.visitCode();//  w w  w  .j a va 2 s  . co  m
    mv.visitLdcInsn(new Integer(arity));
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke", "(" + EPROC_TYPE.getDescriptor()
            + EOBJECT_ARR_TYPE.getDescriptor() + ")" + EOBJECT_TYPE.getDescriptor(), null, PAUSABLE_EX);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0); // load this
    mv.visitVarInsn(Opcodes.ALOAD, 1); // load proc
    for (int i = 0; i < arity; i++) {
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        push_int(mv, i);
        mv.visitInsn(Opcodes.AALOAD);
    }

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, self_type, "invoke", EUtil.getSignature(arity, true));

    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(arity + 2, arity + 2);
    mv.visitEnd();

    mv = cw.visitMethod(Opcodes.ACC_PROTECTED, "<init>", "()V", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, EFUN_TYPE.getInternalName(), "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    make_cast_method(cw, arity);

    cw.visitEnd();

    byte[] data = cw.toByteArray();
    return data;
}