Example usage for org.objectweb.asm MethodVisitor visitMultiANewArrayInsn

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

Introduction

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

Prototype

public void visitMultiANewArrayInsn(final String descriptor, final int numDimensions) 

Source Link

Document

Visits a MULTIANEWARRAY instruction.

Usage

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

License:Open Source License

@Override
public Type newarray(final MethodVisitor method, final int dims) {
    method.visitMultiANewArrayInsn(getInternalName(), dims);
    return this;
}

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 w w  . j a  v a  2s. c  om

        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;
    }
}

From source file:com.googlecode.ddom.weaver.asm.MethodVisitorTee.java

License:Apache License

public void visitMultiANewArrayInsn(String desc, int dims) {
    for (MethodVisitor visitor : visitors) {
        visitor.visitMultiANewArrayInsn(desc, dims);
    }/*from  w  ww .  j a  v  a  2s  . c  o  m*/
}

From source file:org.ballerinalang.nativeimpl.jvm.methodvisitor.VisitMultiANewArrayInsn.java

License:Open Source License

public static void visitMultiANewArrayInsn(Strand strand, ObjectValue oMv, String arrayDescriptor,
        long dimension) {
    MethodVisitor mv = ASMUtil.getRefArgumentNativeData(oMv);
    mv.visitMultiANewArrayInsn(arrayDescriptor, (int) dimension);
}

From source file:org.codehaus.groovy.classgen.AsmClassGenerator.java

License:Apache License

@Override
public void visitArrayExpression(final ArrayExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    ClassNode elementType = expression.getElementType();
    String arrayTypeName = BytecodeHelper.getClassInternalName(elementType);
    List<Expression> sizeExpression = expression.getSizeExpression();

    int size = 0;
    int dimensions = 0;
    if (sizeExpression != null) {
        for (Expression element : sizeExpression) {
            if (element == ConstantExpression.EMPTY_EXPRESSION)
                break;
            dimensions += 1;//from   w  ww. ja  v a2  s.  c om
            // let's convert to an int
            element.visit(this);
            controller.getOperandStack().doGroovyCast(ClassHelper.int_TYPE);
        }
        controller.getOperandStack().remove(dimensions);
    } else {
        size = expression.getExpressions().size();
        BytecodeHelper.pushConstant(mv, size);
    }

    int storeIns = AASTORE;
    if (sizeExpression != null) {
        arrayTypeName = BytecodeHelper.getTypeDescription(expression.getType());
        mv.visitMultiANewArrayInsn(arrayTypeName, dimensions);
    } else if (ClassHelper.isPrimitiveType(elementType)) {
        int primType = 0;
        if (elementType == ClassHelper.boolean_TYPE) {
            primType = T_BOOLEAN;
            storeIns = BASTORE;
        } else if (elementType == ClassHelper.char_TYPE) {
            primType = T_CHAR;
            storeIns = CASTORE;
        } else if (elementType == ClassHelper.float_TYPE) {
            primType = T_FLOAT;
            storeIns = FASTORE;
        } else if (elementType == ClassHelper.double_TYPE) {
            primType = T_DOUBLE;
            storeIns = DASTORE;
        } else if (elementType == ClassHelper.byte_TYPE) {
            primType = T_BYTE;
            storeIns = BASTORE;
        } else if (elementType == ClassHelper.short_TYPE) {
            primType = T_SHORT;
            storeIns = SASTORE;
        } else if (elementType == ClassHelper.int_TYPE) {
            primType = T_INT;
            storeIns = IASTORE;
        } else if (elementType == ClassHelper.long_TYPE) {
            primType = T_LONG;
            storeIns = LASTORE;
        }
        mv.visitIntInsn(NEWARRAY, primType);
    } else {
        mv.visitTypeInsn(ANEWARRAY, arrayTypeName);
    }

    for (int i = 0; i < size; i += 1) {
        mv.visitInsn(DUP);
        BytecodeHelper.pushConstant(mv, i);
        Expression elementExpression = expression.getExpression(i);
        if (elementExpression == null) {
            ConstantExpression.NULL.visit(this);
        } else {
            elementExpression.visit(this);
            controller.getOperandStack().doGroovyCast(elementType);
        }
        mv.visitInsn(storeIns);
        controller.getOperandStack().remove(1);
    }

    controller.getOperandStack().push(expression.getType());
}

From source file:org.jboss.byteman.rule.expression.ArrayInitExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    Type baseType = getType().getBaseType();
    int currentStack = compileContext.getStackCount();
    int expected = 1;
    int length = elements.size();

    // stack array size and then create the array
    mv.visitLdcInsn(length);//from www. ja  va2 s .  c  o  m
    compileContext.addStackCount(1);
    // new array pops count and pushes array so no change to stack size
    if (baseType.isArray()) {
        mv.visitMultiANewArrayInsn(getType().getInternalName(), 1);
    } else if (baseType.isObject()) {
        mv.visitTypeInsn(Opcodes.ANEWARRAY, baseType.getInternalName());
    } else {
        int operand = 0;
        if (baseType.equals(Type.Z)) {
            operand = Opcodes.T_BOOLEAN;
        } else if (baseType.equals(Type.B)) {
            operand = Opcodes.T_BYTE;
        } else if (baseType.equals(Type.S)) {
            operand = Opcodes.T_SHORT;
        } else if (baseType.equals(Type.C)) {
            operand = Opcodes.T_CHAR;
        } else if (baseType.equals(Type.I)) {
            operand = Opcodes.T_INT;
        } else if (baseType.equals(Type.J)) {
            operand = Opcodes.T_LONG;
        } else if (baseType.equals(Type.F)) {
            operand = Opcodes.T_FLOAT;
        } else if (baseType.equals(Type.D)) {
            operand = Opcodes.T_DOUBLE;
        }
        mv.visitIntInsn(Opcodes.NEWARRAY, operand);
    }

    int idx = 0;
    boolean isTwoWords = (baseType.getNBytes() > 4);

    for (Expression element : elements) {
        int toPop = 0;
        // copy array so we can assign it -- adds one to height
        mv.visitInsn(Opcodes.DUP);
        // compile expression index -- adds 1 to height
        mv.visitLdcInsn(idx++);
        compileContext.addStackCount(2);
        // compile value -- adds one or two words to height
        element.compile(mv, compileContext);
        // ensure we have the correct value type
        compileContext.compileTypeConversion(element.type, baseType);
        // now we can do the array store
        if (baseType.isObject() || baseType.isArray()) {
            // compile load object - pops 3
            mv.visitInsn(Opcodes.AASTORE);
            toPop = -3;
        } else if (baseType == Type.Z || baseType == Type.B) {
            // compile load byte - pops 3
            mv.visitInsn(Opcodes.BASTORE);
            toPop = -3;
        } else if (baseType == Type.S) {
            // compile load short - pops 3
            mv.visitInsn(Opcodes.SASTORE);
            toPop = -3;
        } else if (baseType == Type.C) {
            // compile load char - pops 3
            mv.visitInsn(Opcodes.CASTORE);
            toPop = -3;
        } else if (baseType == Type.I) {
            // compile load int - pops 3
            mv.visitInsn(Opcodes.IASTORE);
            toPop = -3;
        } else if (baseType == Type.J) {
            // compile load long - pops 4
            mv.visitInsn(Opcodes.LASTORE);
            toPop = -4;
        } else if (baseType == Type.F) {
            // compile load float - pops 3
            mv.visitInsn(Opcodes.FASTORE);
            toPop = -3;
        } else if (baseType == Type.D) {
            // compile load double - pops 4
            mv.visitInsn(Opcodes.DASTORE);
            toPop = -4;
        }
        // pop the appropriate number of elements off the stack
        compileContext.addStackCount(toPop);
    }

    // check stack height
    if (compileContext.getStackCount() != currentStack + expected) {
        throw new CompileException("ArrayInitExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + (currentStack + expected));
    }

    // no need to update stack max
}

From source file:org.jboss.byteman.rule.expression.NewExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    int currentStack = compileContext.getStackCount();
    int expected = 1;
    int extraParams = 0;

    if (arrayDimCount == 0) {
        // ok, we need to create the new instance and then initialise it.

        // create the new instance -- adds 1 to stack
        String instantiatedClassName = type.getInternalName();
        mv.visitTypeInsn(Opcodes.NEW, instantiatedClassName);
        compileContext.addStackCount(1);
        // copy the exception so we can init it
        mv.visitInsn(Opcodes.DUP);//from  ww  w .  j  a v a  2s. c o m
        compileContext.addStackCount(1);

        int argCount = arguments.size();

        // stack each of the arguments to the constructor
        for (int i = 0; i < argCount; i++) {
            Type argType = argumentTypes.get(i);
            Type paramType = paramTypes.get(i);
            int paramCount = (paramType.getNBytes() > 4 ? 2 : 1);

            // track extra storage used after type conversion
            extraParams += (paramCount);
            arguments.get(i).compile(mv, compileContext);
            compileTypeConversion(argType, paramType, mv, compileContext);
        }

        // construct the exception
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, instantiatedClassName, "<init>", getDescriptor());

        // modify the stack height to account for the removed exception and params
        compileContext.addStackCount(-(extraParams + 1));
    } else {
        // TODO !!! implement compilation for array types !!!
        if (arrayDimCount == 1) {
            // we can use a NEWARRAY or ANEWARRAY
            Type baseType = type.getBaseType();
            // compile first array dimension adds 1 to stack
            arrayDims.get(0).compile(mv, compileContext);
            // compile new array op -- pops 1 and adds 1 to stack
            if (baseType.isObject()) {
                mv.visitTypeInsn(Opcodes.ANEWARRAY, baseType.getInternalName());
                // } else if (baseType.isArray()) {  // cannot happen!!!
            } else {
                int operand = 0;
                if (baseType.equals(Type.Z)) {
                    operand = Opcodes.T_BOOLEAN;
                } else if (baseType.equals(Type.B)) {
                    operand = Opcodes.T_BYTE;
                } else if (baseType.equals(Type.S)) {
                    operand = Opcodes.T_SHORT;
                } else if (baseType.equals(Type.C)) {
                    operand = Opcodes.T_CHAR;
                } else if (baseType.equals(Type.I)) {
                    operand = Opcodes.T_INT;
                } else if (baseType.equals(Type.J)) {
                    operand = Opcodes.T_LONG;
                } else if (baseType.equals(Type.F)) {
                    operand = Opcodes.T_FLOAT;
                } else if (baseType.equals(Type.D)) {
                    operand = Opcodes.T_DOUBLE;
                }
                mv.visitIntInsn(Opcodes.NEWARRAY, operand);
            }
        } else {
            // we need to use MULTIANEWARRAY

            for (int i = 0; i < arrayDimDefinedCount; i++) {
                // compile next array dimension adds 1 to stack
                arrayDims.get(i).compile(mv, compileContext);
            }
            // execute the MULTIANEWARRAY operation -- pops arrayDims operands and pushes 1
            mv.visitMultiANewArrayInsn(type.getInternalName(), arrayDimDefinedCount);
            compileContext.addStackCount(1 - arrayDimDefinedCount);
        }
    }

    if (compileContext.getStackCount() != currentStack + expected) {
        throw new CompileException("NewExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + (currentStack + expected));
    }
}

From source file:org.mbte.groovypp.compiler.asm.VisitMultiANewArrayInsn.java

License:Apache License

public void visit(MethodVisitor mv) {
    mv.visitMultiANewArrayInsn(desc, dims);
}

From source file:org.mbte.groovypp.compiler.transformers.ArrayExpressionTransformer.java

License:Apache License

public Expression transform(final ArrayExpression exp, final CompilerTransformer compiler) {
    final ClassNode elementType = exp.getElementType();
    final List sizeExpression = exp.getSizeExpression();

    return new BytecodeExpr(exp, exp.getType()) {
        protected void compile(MethodVisitor mv) {
            int size = 0;
            int dimensions = 0;
            if (sizeExpression != null) {
                for (Iterator iter = sizeExpression.iterator(); iter.hasNext();) {
                    Expression element = (Expression) iter.next();
                    if (element == ConstantExpression.EMPTY_EXPRESSION)
                        break;
                    dimensions++;//from w  w w .  j  a  va 2  s . co  m

                    final BytecodeExpr expression = (BytecodeExpr) compiler.transform(element);
                    expression.visit(mv);
                    box(expression.getType(), mv);
                    unbox(ClassHelper.int_TYPE, mv);
                }
            } else {
                size = exp.getExpressions().size();
                pushConstant(size, mv);
            }

            int storeIns = AASTORE;
            if (sizeExpression != null) {
                mv.visitMultiANewArrayInsn(BytecodeHelper.getTypeDescription(exp.getType()), dimensions);
            } else if (ClassHelper.isPrimitiveType(elementType)) {
                int primType = 0;
                if (elementType == ClassHelper.boolean_TYPE) {
                    primType = T_BOOLEAN;
                    storeIns = BASTORE;
                } else if (elementType == ClassHelper.char_TYPE) {
                    primType = T_CHAR;
                    storeIns = CASTORE;
                } else if (elementType == ClassHelper.float_TYPE) {
                    primType = T_FLOAT;
                    storeIns = FASTORE;
                } else if (elementType == ClassHelper.double_TYPE) {
                    primType = T_DOUBLE;
                    storeIns = DASTORE;
                } else if (elementType == ClassHelper.byte_TYPE) {
                    primType = T_BYTE;
                    storeIns = BASTORE;
                } else if (elementType == ClassHelper.short_TYPE) {
                    primType = T_SHORT;
                    storeIns = SASTORE;
                } else if (elementType == ClassHelper.int_TYPE) {
                    primType = T_INT;
                    storeIns = IASTORE;
                } else if (elementType == ClassHelper.long_TYPE) {
                    primType = T_LONG;
                    storeIns = LASTORE;
                }
                mv.visitIntInsn(NEWARRAY, primType);
            } else {
                mv.visitTypeInsn(ANEWARRAY,
                        BytecodeHelper.getClassInternalName(exp.getType().getComponentType()));
            }

            for (int i = 0; i < size; i++) {
                mv.visitInsn(DUP);
                pushConstant(i, mv);
                BytecodeExpr elementExpression = (BytecodeExpr) compiler.transform(exp.getExpression(i));
                elementExpression = compiler.cast(elementExpression, elementType);
                elementExpression.visit(mv);
                box(elementExpression.getType(), mv);
                unbox(elementType, mv);
                mv.visitInsn(storeIns);
            }
        }
    };
}

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**
 * Create the Java code for a given class instance creation expression. The new object reference will be pushed onto
 * the operand stack. /* w  w  w. j  a  v  a  2 s  .co  m*/
 * 
 * @param cice the class instance creation expression
 * @param context 
 * @return JavaTypeName    
 * @throws JavaGenerationException
 */
private static JavaTypeName encodeClassInstanceCreationExpr(
        final JavaExpression.ClassInstanceCreationExpression cice, final GenerationContext context)
        throws JavaGenerationException {

    final MethodVisitor mv = context.getMethodVisitor();
    final JavaTypeName classType = cice.getClassName();
    if (classType instanceof JavaTypeName.Reference.Array) {

        final JavaTypeName.Reference.Array arrayType = (JavaTypeName.Reference.Array) classType;
        final int nSizedDims = cice.getNArgs();
        if (nSizedDims == 1) {

            //for example, new String[10][][] will hit this case since it has 1 sized dimension (even though a multi-dimensional array is 
            //being created

            //push the size of the dimension
            encodeExpr(cice.getArg(0), context);

            final JavaTypeName arrayElementType = arrayType.getIncrementalElementType();
            switch (arrayElementType.getTag()) {

            case JavaTypeName.VOID_TAG:
                throw new JavaGenerationException("Cannot have an array of with void element types.");

            case JavaTypeName.BOOLEAN_TAG:
            case JavaTypeName.BYTE_TAG:
            case JavaTypeName.SHORT_TAG:
            case JavaTypeName.CHAR_TAG:
            case JavaTypeName.INT_TAG:
            case JavaTypeName.LONG_TAG:
            case JavaTypeName.DOUBLE_TAG:
            case JavaTypeName.FLOAT_TAG: {
                //push the instruction to create a 1-dimensonal array of primitive values
                mv.visitIntInsn(Opcodes.NEWARRAY, getNewArrayArgCode(arrayElementType));
                break;
            }

            case JavaTypeName.ARRAY_TAG:
            case JavaTypeName.OBJECT_TAG: {
                //push the instruction to create a 1-dimensonal array of reference values
                mv.visitTypeInsn(Opcodes.ANEWARRAY, arrayElementType.getJVMInternalName());
                break;
            }

            default: {
                throw new IllegalArgumentException();
            }
            }

            return arrayType;

        } else {
            //the case of multi-dimensional arrays where more than 1 sizing dimension is supplied.

            // push args onto the stack
            for (int i = 0; i < nSizedDims; i++) {
                encodeExpr(cice.getArg(i), context);
            }

            mv.visitMultiANewArrayInsn(arrayType.getJVMInternalName(), nSizedDims);

            return arrayType;
        }

    } else if (classType instanceof JavaTypeName.Reference.Object) {

        String internalClassName = classType.getJVMInternalName();

        // create uninitialized object, duplicate the ref.
        mv.visitTypeInsn(Opcodes.NEW, internalClassName);
        mv.visitInsn(Opcodes.DUP);

        // push args onto the stack
        for (int i = 0, nArgs = cice.getNArgs(); i < nArgs; i++) {
            encodeExpr(cice.getArg(i), context);
        }

        //descriptor for the constructor
        StringBuilder descriptor = new StringBuilder("(");
        for (int i = 0, nArgs = cice.getNArgs(); i < nArgs; ++i) {
            descriptor.append(cice.getParamType(i).getJVMDescriptor());
        }
        descriptor.append(")V");

        // initialize - consumes the args and the duplicate reference.
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, internalClassName, "<init>", descriptor.toString());

        return classType;

    } else {
        throw new JavaGenerationException("cannot create a new instance of a primitive type.");
    }
}