Example usage for org.objectweb.asm MethodVisitor visitInsn

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

Introduction

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

Prototype

public void visitInsn(final int opcode) 

Source Link

Document

Visits a zero operand instruction.

Usage

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void decodeScalar(final MethodVisitor mv, final DecodeCGS cgs, final JavaColumnModel f)
        throws OrmException {
    cgs.setFieldReference(f);//w  w  w .  j  av  a  2  s  . c  o  m
    cgs.fieldSetBegin();
    switch (Type.getType(f.getPrimitiveType()).getSort()) {
    case Type.BOOLEAN:
        cgs.call("readBool", Type.BOOLEAN_TYPE);
        break;

    case Type.CHAR:
        cgs.call("readUInt32", Type.INT_TYPE);
        break;

    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
        cgs.call("readSInt32", Type.INT_TYPE);
        break;

    case Type.FLOAT:
        cgs.call("readFloat", Type.FLOAT_TYPE);
        break;

    case Type.DOUBLE:
        cgs.call("readDouble", Type.DOUBLE_TYPE);
        break;

    case Type.LONG:
        cgs.call("readSInt64", Type.LONG_TYPE);
        break;

    default:
        if (f.getPrimitiveType() == byte[].class) {
            cgs.call("readBytes", byteString);
            mv.visitMethodInsn(INVOKEVIRTUAL, byteString.getInternalName(), "toByteArray",
                    Type.getMethodDescriptor(Type.getType(byte[].class), new Type[] {}));

        } else if (f.getPrimitiveType() == String.class) {
            cgs.call("readString", string);

        } else if (f.getPrimitiveType() == java.sql.Timestamp.class
                || f.getPrimitiveType() == java.util.Date.class
                || f.getPrimitiveType() == java.sql.Date.class) {
            String tsType = Type.getType(f.getPrimitiveType()).getInternalName();
            mv.visitTypeInsn(NEW, tsType);
            mv.visitInsn(DUP);
            cgs.call("readFixed64", Type.LONG_TYPE);
            mv.visitMethodInsn(INVOKESPECIAL, tsType, "<init>",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));

        } else {
            throw new OrmException(
                    "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
        }
        break;
    }
    cgs.fieldSetEnd();
}

From source file:com.google.gwtorm.server.SchemaConstructorGen.java

License:Apache License

private void implementConstructor() {
    final Type ft = Type.getType(schemaArg.getClass());
    final String consName = "<init>";
    final String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { ft });
    final MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, consName, consDesc, null, null);
    mv.visitCode();/*  w w w .  ja  va2s  . c  o m*/

    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), consName,
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, implTypeName, CTX, ft.getDescriptor());

    mv.visitInsn(RETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.server.SchemaConstructorGen.java

License:Apache License

private void implementNewInstance() {
    final Type ft = Type.getType(schemaArg.getClass());
    final String typeName = Type.getType(schemaImpl).getInternalName();
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "open",
            Type.getMethodDescriptor(Type.getType(Schema.class), new Type[] {}), null, null);
    mv.visitCode();//from  w w w .j ava  2  s  .c o m

    Constructor<?> c = schemaImpl.getDeclaredConstructors()[0];
    Type argType = Type.getType(c.getParameterTypes()[0]);

    mv.visitTypeInsn(NEW, typeName);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, implTypeName, CTX, ft.getDescriptor());
    mv.visitMethodInsn(INVOKESPECIAL, typeName, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { argType }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.server.SchemaGen.java

License:Apache License

private void implementConstructor() {
    final String consName = "<init>";
    final Type superType = Type.getType(schemaSuperClass);
    final Type dbType = Type.getType(databaseClass);

    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, consName,
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { dbType }), null, null);
    mv.visitCode();/* w  ww .  j  ava  2 s. com*/

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, superType.getInternalName(), consName, Type.getMethodDescriptor(
            Type.VOID_TYPE,
            new Type[] { Type.getType(schemaSuperClass.getDeclaredConstructors()[0].getParameterTypes()[0]) }));

    for (final RelationGen info : relations) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, info.accessType.getInternalName());
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, info.accessType.getInternalName(), consName,
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { superType }));
        mv.visitFieldInsn(PUTFIELD, implTypeName, info.getAccessInstanceFieldName(),
                info.accessType.getDescriptor());
    }

    mv.visitInsn(RETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.server.SchemaGen.java

License:Apache License

private void implementSequenceMethods() {
    for (final SequenceModel seq : schema.getSequences()) {
        final Type retType = Type.getType(seq.getResultType());
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, seq.getMethodName(),
                Type.getMethodDescriptor(retType, new Type[] {}), null,
                new String[] { Type.getType(OrmException.class).getInternalName() });
        mv.visitCode();//from   ww  w.jav  a  2s .  c  o m

        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(seq.getSequenceName());
        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(schemaSuperClass), "nextLong",
                Type.getMethodDescriptor(Type.getType(Long.TYPE), new Type[] { Type.getType(String.class) }));
        if (retType.getSize() == 1) {
            mv.visitInsn(L2I);
            mv.visitInsn(IRETURN);
        } else {
            mv.visitInsn(LRETURN);
        }
        mv.visitMaxs(-1, -1);
        mv.visitEnd();
    }
}

From source file:com.google.gwtorm.server.SchemaGen.java

License:Apache License

private void implementAllRelationsMethod() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "allRelations",
            Type.getMethodDescriptor(Type.getType(Access[].class), new Type[] {}), null, null);
    mv.visitCode();//from  w w  w . j  a va2  s  .c om

    final int r = 1;
    CodeGenSupport cgs = new CodeGenSupport(mv);
    cgs.push(relations.size());
    mv.visitTypeInsn(ANEWARRAY, Type.getType(Access.class).getInternalName());
    mv.visitVarInsn(ASTORE, r);

    int index = 0;
    for (RelationGen info : relations) {
        mv.visitVarInsn(ALOAD, r);
        cgs.push(index++);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, getImplTypeName(), info.model.getMethodName(), info.getDescriptor());

        mv.visitInsn(AASTORE);
    }

    mv.visitVarInsn(ALOAD, r);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.googlecode.d2j.converter.IR2JConverter.java

License:Apache License

private void reBuildInstructions(IrMethod ir, MethodVisitor asm) {
    asm = new LdcOptimizeAdapter(asm);
    int maxLocalIndex = 0;
    for (Local local : ir.locals) {
        maxLocalIndex = Math.max(maxLocalIndex, local._ls_index);
    }//from w w w .j  ava2s. c  o m
    Map<String, Integer> lockMap = new HashMap<String, Integer>();
    for (Stmt st : ir.stmts) {
        switch (st.st) {
        case LABEL:
            LabelStmt labelStmt = (LabelStmt) st;
            Label label = (Label) labelStmt.tag;
            asm.visitLabel(label);
            if (labelStmt.lineNumber >= 0) {
                asm.visitLineNumber(labelStmt.lineNumber, label);
            }
            break;
        case ASSIGN: {
            E2Stmt e2 = (E2Stmt) st;
            Value v1 = e2.op1;
            Value v2 = e2.op2;
            switch (v1.vt) {
            case LOCAL:

                Local local = ((Local) v1);
                int i = local._ls_index;

                if (v2.vt == VT.LOCAL && (i == ((Local) v2)._ls_index)) {//
                    continue;
                }

                boolean skipOrg = false;
                if (v1.valueType.charAt(0) == 'I') {// check for IINC
                    if (v2.vt == VT.ADD) {
                        E2Expr e = (E2Expr) v2;
                        if ((e.op1 == local && e.op2.vt == VT.CONSTANT)
                                || (e.op2 == local && e.op1.vt == VT.CONSTANT)) {
                            int increment = (Integer) ((Constant) (e.op1 == local ? e.op2 : e.op1)).value;
                            if (increment >= Short.MIN_VALUE && increment <= Short.MAX_VALUE) {
                                asm.visitIincInsn(i, increment);
                                skipOrg = true;
                            }
                        }
                    } else if (v2.vt == VT.SUB) {
                        E2Expr e = (E2Expr) v2;
                        if (e.op1 == local && e.op2.vt == VT.CONSTANT) {
                            int increment = -(Integer) ((Constant) e.op2).value;
                            if (increment >= Short.MIN_VALUE && increment <= Short.MAX_VALUE) {
                                asm.visitIincInsn(i, increment);
                                skipOrg = true;
                            }
                        }
                    }
                }
                if (!skipOrg) {
                    accept(v2, asm);
                    if (i >= 0) {
                        asm.visitVarInsn(getOpcode(v1, ISTORE), i);
                    } else if (!v1.valueType.equals("V")) { // skip void type locals
                        switch (v1.valueType.charAt(0)) {
                        case 'J':
                        case 'D':
                            asm.visitInsn(POP2);
                            break;
                        default:
                            asm.visitInsn(POP);
                            break;
                        }
                    }
                }
                break;
            case STATIC_FIELD: {
                StaticFieldExpr fe = (StaticFieldExpr) v1;
                accept(v2, asm);
                insertI2x(v2.valueType, fe.type, asm);
                asm.visitFieldInsn(PUTSTATIC, toInternal(fe.owner), fe.name, fe.type);
                break;
            }
            case FIELD: {
                FieldExpr fe = (FieldExpr) v1;
                accept(fe.op, asm);
                accept(v2, asm);
                insertI2x(v2.valueType, fe.type, asm);
                asm.visitFieldInsn(PUTFIELD, toInternal(fe.owner), fe.name, fe.type);
                break;
            }
            case ARRAY:
                ArrayExpr ae = (ArrayExpr) v1;
                accept(ae.op1, asm);
                accept(ae.op2, asm);
                accept(v2, asm);
                String tp1 = ae.op1.valueType;
                String tp2 = ae.valueType;
                if (tp1.charAt(0) == '[') {
                    String arrayElementType = tp1.substring(1);
                    insertI2x(v2.valueType, arrayElementType, asm);
                    asm.visitInsn(getOpcode(arrayElementType, IASTORE));
                } else {
                    asm.visitInsn(getOpcode(tp2, IASTORE));
                }
                break;
            }
        }
            break;
        case IDENTITY: {
            E2Stmt e2 = (E2Stmt) st;
            if (e2.op2.vt == VT.EXCEPTION_REF) {
                int index = ((Local) e2.op1)._ls_index;
                if (index >= 0) {
                    asm.visitVarInsn(ASTORE, index);
                } else {
                    asm.visitInsn(POP);
                }
            }
        }
            break;

        case FILL_ARRAY_DATA: {
            E2Stmt e2 = (E2Stmt) st;
            Object arrayData = ((Constant) e2.getOp2()).value;
            int arraySize = Array.getLength(arrayData);
            String arrayValueType = e2.getOp1().valueType;
            String elementType;
            if (arrayValueType.charAt(0) == '[') {
                elementType = arrayValueType.substring(1);
            } else {
                elementType = "I";
            }
            int iastoreOP = getOpcode(elementType, IASTORE);
            accept(e2.getOp1(), asm);
            for (int i = 0; i < arraySize; i++) {
                asm.visitInsn(DUP);
                asm.visitLdcInsn(i);
                asm.visitLdcInsn(Array.get(arrayData, i));
                asm.visitInsn(iastoreOP);
            }
            asm.visitInsn(POP);
        }
            break;
        case GOTO:
            asm.visitJumpInsn(GOTO, (Label) ((GotoStmt) st).target.tag);
            break;
        case IF:
            reBuildJumpInstructions((IfStmt) st, asm);
            break;
        case LOCK: {
            Value v = ((UnopStmt) st).op;
            accept(v, asm);
            if (optimizeSynchronized) {
                switch (v.vt) {
                case LOCAL:
                    // FIXME do we have to disable local due to OptSyncTest ?
                    // break;
                case CONSTANT: {
                    String key;
                    if (v.vt == VT.LOCAL) {
                        key = "L" + ((Local) v)._ls_index;
                    } else {
                        key = "C" + ((Constant) v).value;
                    }
                    Integer integer = lockMap.get(key);
                    int nIndex = integer != null ? integer : ++maxLocalIndex;
                    asm.visitInsn(DUP);
                    asm.visitVarInsn(getOpcode(v, ISTORE), nIndex);
                    lockMap.put(key, nIndex);
                }
                    break;
                default:
                    throw new RuntimeException();
                }
            }
            asm.visitInsn(MONITORENTER);
        }
            break;
        case UNLOCK: {
            Value v = ((UnopStmt) st).op;
            if (optimizeSynchronized) {
                switch (v.vt) {
                case LOCAL:
                case CONSTANT: {
                    String key;
                    if (v.vt == VT.LOCAL) {
                        key = "L" + ((Local) v)._ls_index;
                    } else {
                        key = "C" + ((Constant) v).value;
                    }
                    Integer integer = lockMap.get(key);
                    if (integer != null) {
                        asm.visitVarInsn(getOpcode(v, ILOAD), integer);
                    } else {
                        accept(v, asm);
                    }
                }
                    break;
                // TODO other
                default: {
                    accept(v, asm);
                    break;
                }
                }
            } else {
                accept(v, asm);
            }
            asm.visitInsn(MONITOREXIT);
        }
            break;
        case NOP:
            break;
        case RETURN: {
            Value v = ((UnopStmt) st).op;
            accept(v, asm);
            insertI2x(v.valueType, ir.ret, asm);
            asm.visitInsn(getOpcode(v, IRETURN));
        }
            break;
        case RETURN_VOID:
            asm.visitInsn(RETURN);
            break;
        case LOOKUP_SWITCH: {
            LookupSwitchStmt lss = (LookupSwitchStmt) st;
            accept(lss.op, asm);
            Label targets[] = new Label[lss.targets.length];
            for (int i = 0; i < targets.length; i++) {
                targets[i] = (Label) lss.targets[i].tag;
            }
            asm.visitLookupSwitchInsn((Label) lss.defaultTarget.tag, lss.lookupValues, targets);
        }
            break;
        case TABLE_SWITCH: {
            TableSwitchStmt tss = (TableSwitchStmt) st;
            accept(tss.op, asm);
            Label targets[] = new Label[tss.targets.length];
            for (int i = 0; i < targets.length; i++) {
                targets[i] = (Label) tss.targets[i].tag;
            }
            asm.visitTableSwitchInsn(tss.lowIndex, tss.lowIndex + targets.length - 1,
                    (Label) tss.defaultTarget.tag, targets);
        }
            break;
        case THROW:
            accept(((UnopStmt) st).op, asm);
            asm.visitInsn(ATHROW);
            break;
        case VOID_INVOKE:
            InvokeExpr invokeExpr = (InvokeExpr) st.getOp();
            accept(invokeExpr, asm);
            String ret = invokeExpr.ret;
            if (invokeExpr.vt == VT.INVOKE_NEW) {
                asm.visitInsn(POP);
            } else if (!"V".equals(ret)) {
                switch (ret.charAt(0)) {
                case 'J':
                case 'D':
                    asm.visitInsn(POP2);
                    break;
                default:
                    asm.visitInsn(POP);
                    break;
                }
            }
            break;
        default:
            throw new RuntimeException("not support st: " + st.st);
        }

    }
}

From source file:com.googlecode.d2j.converter.IR2JConverter.java

License:Apache License

/**
 * insert I2x instruction//w w  w  .  ja v  a2s  .co  m
 * 
 * @param tos
 * @param expect
 * @param mv
 */
private static void insertI2x(String tos, String expect, MethodVisitor mv) {
    switch (expect.charAt(0)) {
    case 'B':
        switch (tos.charAt(0)) {
        case 'S':
        case 'C':
        case 'I':
            mv.visitInsn(I2B);
        }
        break;
    case 'S':
        switch (tos.charAt(0)) {
        case 'C':
        case 'I':
            mv.visitInsn(I2S);
        }
        break;
    case 'C':
        switch (tos.charAt(0)) {
        case 'I':
            mv.visitInsn(I2C);
        }
        break;
    }
}

From source file:com.googlecode.d2j.converter.IR2JConverter.java

License:Apache License

private static void accept(Value value, MethodVisitor asm) {

    switch (value.et) {
    case E0://w  w  w .j a  v a  2 s  .  c o m
        switch (value.vt) {
        case LOCAL:
            asm.visitVarInsn(getOpcode(value, ILOAD), ((Local) value)._ls_index);
            break;
        case CONSTANT:
            Constant cst = (Constant) value;
            if (cst.value.equals(Constant.Null)) {
                asm.visitInsn(ACONST_NULL);
            } else if (cst.value instanceof Constant.Type) {
                asm.visitLdcInsn(Type.getType(((Constant.Type) cst.value).desc));
            } else {
                asm.visitLdcInsn(cst.value);
            }
            break;
        case NEW:
            asm.visitTypeInsn(NEW, toInternal(((NewExpr) value).type));
            break;
        case STATIC_FIELD:
            StaticFieldExpr sfe = (StaticFieldExpr) value;
            asm.visitFieldInsn(GETSTATIC, toInternal(sfe.owner), sfe.name, sfe.type);
            break;
        }
        break;
    case E1:
        reBuildE1Expression((E1Expr) value, asm);
        break;
    case E2:
        reBuildE2Expression((E2Expr) value, asm);
        break;
    case En:
        reBuildEnExpression((EnExpr) value, asm);
        break;
    }
}

From source file:com.googlecode.d2j.converter.IR2JConverter.java

License:Apache License

private static void reBuildEnExpression(EnExpr value, MethodVisitor asm) {
    if (value.vt == VT.FILLED_ARRAY) {
        FilledArrayExpr fae = (FilledArrayExpr) value;
        reBuildE1Expression(Exprs.nNewArray(fae.type, Exprs.nInt(fae.ops.length)), asm);
        String tp1 = fae.valueType;
        int xastore = IASTORE;
        String elementType = null;
        if (tp1.charAt(0) == '[') {
            elementType = tp1.substring(1);
            xastore = getOpcode(elementType, IASTORE);
        }//from   w  ww.  java 2 s . co m

        for (int i = 0; i < fae.ops.length; i++) {
            if (fae.ops[i] == null)
                continue;
            asm.visitInsn(DUP);
            asm.visitLdcInsn(i);
            accept(fae.ops[i], asm);
            String tp2 = fae.ops[i].valueType;
            if (elementType != null) {
                insertI2x(tp2, elementType, asm);
            }
            asm.visitInsn(xastore);
        }
        return;
    }

    switch (value.vt) {
    case NEW_MUTI_ARRAY:
        for (Value vb : value.ops) {
            accept(vb, asm);
        }
        NewMutiArrayExpr nmae = (NewMutiArrayExpr) value;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nmae.dimension; i++) {
            sb.append('[');
        }
        sb.append(nmae.baseType);
        asm.visitMultiANewArrayInsn(sb.toString(), nmae.dimension);
        break;
    case INVOKE_NEW:
        asm.visitTypeInsn(NEW, toInternal(((InvokeExpr) value).owner));
        asm.visitInsn(DUP);
        // pass through
    case INVOKE_INTERFACE:
    case INVOKE_SPECIAL:
    case INVOKE_STATIC:
    case INVOKE_VIRTUAL:
        InvokeExpr ie = (InvokeExpr) value;
        int i = 0;
        if (value.vt != VT.INVOKE_STATIC && value.vt != VT.INVOKE_NEW) {
            i = 1;
            accept(value.ops[0], asm);
        }
        for (int j = 0; i < value.ops.length; i++, j++) {
            Value vb = value.ops[i];
            accept(vb, asm);
            insertI2x(vb.valueType, ie.args[j], asm);
        }

        int opcode;
        switch (value.vt) {
        case INVOKE_VIRTUAL:
            opcode = INVOKEVIRTUAL;
            break;
        case INVOKE_INTERFACE:
            opcode = INVOKEINTERFACE;
            break;
        case INVOKE_NEW:
        case INVOKE_SPECIAL:
            opcode = INVOKESPECIAL;
            break;
        case INVOKE_STATIC:
            opcode = INVOKESTATIC;
            break;
        default:
            opcode = -1;
        }

        asm.visitMethodInsn(opcode, toInternal(ie.owner), ie.name,
                buildMethodDesc(ie.vt == VT.INVOKE_NEW ? "V" : ie.ret, ie.args));
        break;
    }
}