Example usage for org.objectweb.asm Opcodes ASTORE

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

Introduction

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

Prototype

int ASTORE

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

Click Source Link

Usage

From source file:de.tuberlin.uebb.jbop.optimizer.array.AbstractLocalArrayOptimizer.java

License:Open Source License

/**
 * Register values./*from   www  .j a va  2  s. c o m*/
 * 
 * @param currentNode
 *          the current node
 * @param knownArrays
 *          the known arrays
 * @return true, if successful
 * @throws JBOPClassException
 *           the jBOP class exception
 */
protected int registerValues(final AbstractInsnNode currentNode, final Map<Integer, Object> knownArrays)
        throws JBOPClassException {
    if (currentNode.getOpcode() == Opcodes.ASTORE) {
        return registerGetArray(currentNode, knownArrays);
    }
    return registerAdditionalValues(currentNode, knownArrays);
}

From source file:de.tuberlin.uebb.jbop.optimizer.array.LocalArrayLengthInlinerTest.java

License:Open Source License

/**
 * Tests that LocalArrayLengthInliner is working correctly.
 * //from  w w  w .  j  a  v  a2  s .com
 * @throws Exception
 *           the exception
 */
@Test
public void test_newarrayAndSubarray() throws Exception {
    // INIT
    final String owner = "de.tuberlin.uebb.jbop.optimizer.array.LocalArrayLengthTestClass";
    final ClassNodeBuilder builder = ClassNodeBuilder.createClass(owner).//
            addField("doubleArray", "[[D").initArray(15, 42)
            .withModifiers(Opcodes.ACC_PRIVATE, Opcodes.ACC_FINAL).//
            addMethod("getArrayLength", "()I").withAnnotation(Optimizable.class).//
            addArray("[D", 15).// 3 -> 3
            addInsn(new VarInsnNode(Opcodes.ALOAD, 1)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.ARRAYLENGTH)).// 1 -> 0|1
            addGetClassField("doubleArray").// 2 -> 2
            addInsn(new InsnNode(Opcodes.ICONST_0)).// 1 -> 1
            addInsn(new InsnNode(Opcodes.AALOAD)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ASTORE, 2)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ALOAD, 2)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.ARRAYLENGTH)).// 1 -> 0|1
            addInsn(new InsnNode(Opcodes.IADD)).// 1 -> 1
            addInsn(new InsnNode(Opcodes.IRETURN));// 1 -> 1
    // 14 -> 12
    final LocalArrayLengthInliner inliner = new LocalArrayLengthInliner();
    inliner.setInputObject(builder.toClass().instance());

    // RUN STEP 1
    final MethodNode method = builder.getMethod("getArrayLength");
    assertEquals(14, method.instructions.size());
    final InsnList optimized = inliner.optimize(method.instructions, method);
    method.instructions = optimized;

    // ASSERT STEP 1
    assertEquals(12, optimized.size());
    assertEquals(15, NodeHelper.getNumberValue(optimized.get(3)).intValue());
    assertEquals(42, NodeHelper.getNumberValue(optimized.get(9)).intValue());

    // RUN STEP 2
    final InsnList optimized2 = inliner.optimize(method.instructions, method);

    // ASSERT STEP 2
    assertEquals(12, optimized2.size());
}

From source file:de.tuberlin.uebb.jbop.optimizer.array.LocalArrayValueInlinerTest.java

License:Open Source License

/**
 * Tests that LocalArrayValueInliner is working correctly.
 * /*from  w  w w . j av  a  2 s . c o m*/
 * @throws Exception
 *           the exception
 */
@Test
public void testLocalArrayValueInliner() throws Exception {
    // INIT
    final String owner = "de.tuberlin.uebb.jbop.optimizer.array.LocalArrayValueTestClass";
    final ClassNodeBuilder builder = ClassNodeBuilder.createClass(owner).//
            addField("doubleArray", "[[D").//
            withAnnotation(ImmutableArray.class).//
            withModifiers(Opcodes.ACC_PRIVATE, Opcodes.ACC_FINAL).//
            initArray(2, 2).//
            initMultiArrayWith(1.0, 0, 0).//
            initMultiArrayWith(2.0, 0, 1).//
            initMultiArrayWith(3.0, 1, 0).//
            initMultiArrayWith(4.0, 1, 1).//
            addMethod("getArrayValue", "()D").withAnnotation(Optimizable.class).//
            addGetClassField("doubleArray").// 2 -> 2
            addInsn(new InsnNode(Opcodes.ICONST_0)).// 1 -> 1
            addInsn(new InsnNode(Opcodes.AALOAD)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ASTORE, 2)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ALOAD, 2)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.ICONST_1)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.DALOAD)).// 1 -> 0| 1
            addInsn(new InsnNode(Opcodes.DRETURN));// 1 -> 1
    // 14 -> 12
    final LocalArrayValueInliner inliner = new LocalArrayValueInliner();
    inliner.setInputObject(builder.toClass().instance());

    // RUN STEP 1
    final MethodNode method = builder.getMethod("getArrayValue");
    assertEquals(9, method.instructions.size());
    final InsnList optimized = inliner.optimize(method.instructions, method);

    // ASSERT STEP 1

    assertEquals(7, optimized.size());
    assertEquals(2.0, NodeHelper.getNumberValue(optimized.get(5)).doubleValue(), .0001);

    // RUN STEP 2
    final InsnList optimized2 = inliner.optimize(method.instructions, method);

    // ASSERT STEP 2
    assertEquals(7, optimized2.size());
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

private void initArrayInternal(final int opcode, final Object... values) {
    final InsnList list = new InsnList();
    final int length = values.length;
    initArray(length);/*from   ww  w . j  a  va  2s  . c om*/
    int index = 0;
    for (final Object number : values) {
        list.add(new VarInsnNode(Opcodes.ALOAD, lastConstructorVarIndex));
        final AbstractInsnNode indexNode = NodeHelper.getInsnNodeFor(index++);
        list.add(indexNode);
        final AbstractInsnNode numberNode = NodeHelper.getInsnNodeFor(number);
        list.add(numberNode);
        if (number instanceof Number && opcode == Opcodes.ASTORE) {
            list.add(ConstructorBuilder.getBoxingNode(lastField));
        }
        list.add(new InsnNode(opcode));
    }
    addToConstructor(list);
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Initializes a classField of type Array in the default lastConstructor with the given length.
 * (eg: if fieldType of field "field" is "double[]", field = new double[length] is called).
 * //from w w  w. jav  a  2 s .c  o  m
 * @param length
 *          the length
 * @return the abstract optimizer test
 */
public ClassNodeBuilder initArray(final int... length) {
    if (isInterface) {
        return this;
    }
    final InsnList list = new InsnList();
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));
    final AbstractInsnNode node;
    if (length.length == 1) {
        final Type elementType = Type.getType(lastField.desc).getElementType();
        list.add(NodeHelper.getInsnNodeFor(Integer.valueOf(length[0])));
        if (elementType.getDescriptor().startsWith("L")) {
            node = new TypeInsnNode(Opcodes.ANEWARRAY, elementType.getInternalName());
        } else {
            node = new IntInsnNode(Opcodes.NEWARRAY, getSort(elementType));
        }
    } else {
        for (final int currentLength : length) {
            list.add(NodeHelper.getInsnNodeFor(Integer.valueOf(currentLength)));
        }
        node = new MultiANewArrayInsnNode(lastField.desc, length.length);
    }
    list.add(node);
    list.add(new VarInsnNode(Opcodes.ASTORE, constructorVarIndex));
    list.add(new VarInsnNode(Opcodes.ALOAD, constructorVarIndex));
    lastConstructorVarIndex = constructorVarIndex;
    constructorVarIndex++;
    list.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, lastField.name, lastField.desc));
    addToConstructor(list);
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Adds the array to the last created method.
 * //from   ww w. j  ava 2 s. com
 * @param desc
 *          the desc
 * @param length
 *          the length
 * @return the class node builder
 */
public ClassNodeBuilder addArray(final String desc, final int... length) {
    if (isInterface) {
        return this;
    }
    final Type elementType = Type.getType(desc).getElementType();
    if (length.length == 1) {

        addInsn(NodeHelper.getInsnNodeFor(Integer.valueOf(length[0])));
        if (elementType.getDescriptor().startsWith("L")) {
            addInsn(new TypeInsnNode(Opcodes.ANEWARRAY, elementType.getInternalName()));
        } else {
            addInsn(new IntInsnNode(Opcodes.NEWARRAY, getSort(elementType)));
        }
    } else {
        for (final int currentLength : length) {
            addInsn(NodeHelper.getInsnNodeFor(Integer.valueOf(currentLength)));
        }
        addInsn(new MultiANewArrayInsnNode(desc, length.length));
    }
    addInsn(new VarInsnNode(Opcodes.ASTORE, methodVarIndex));
    lastMethodVarIndex = methodVarIndex;
    lastVarElementType = elementType;
    methodVarIndex++;
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.Block.java

License:Open Source License

private static boolean isStore(final AbstractInsnNode currentNode) {
    if (currentNode == null) {
        return false;
    }//from w  ww  . j  av a 2  s.co m
    if ((currentNode.getOpcode() >= Opcodes.ISTORE) && (currentNode.getOpcode() <= Opcodes.ASTORE)) {
        return true;
    }
    if ((currentNode.getOpcode() >= Opcodes.IRETURN) && (currentNode.getOpcode() <= Opcodes.RETURN)) {
        return true;
    }
    return false;
}

From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.BlockTest.java

License:Open Source License

/**
 * Tests the type erasure./*www .jav a  2 s  . c o m*/
 */
@Test
public void testTypeErasure() {
    final InsnList list = new InsnList();
    list.add(new InsnNode(Opcodes.ICONST_5));
    list.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_INT));
    list.add(new VarInsnNode(Opcodes.ASTORE, 3));
    list.add(new VarInsnNode(Opcodes.ILOAD, 1));
    list.add(new VarInsnNode(Opcodes.ILOAD, 2));
    list.add(new InsnNode(Opcodes.IADD));
    list.add(new InsnNode(Opcodes.ICONST_0));
    list.add(new VarInsnNode(Opcodes.ALOAD, 3));
    list.add(new InsnNode(Opcodes.IASTORE));
    list.add(new InsnNode(Opcodes.ICONST_5));
    list.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_INT));
    list.add(new VarInsnNode(Opcodes.ASTORE, 4));
    list.add(new VarInsnNode(Opcodes.ILOAD, 1));
    list.add(new VarInsnNode(Opcodes.ILOAD, 2));
    list.add(new InsnNode(Opcodes.IADD));
    list.add(new InsnNode(Opcodes.ICONST_1));
    final VarInsnNode insn = new VarInsnNode(Opcodes.ALOAD, 3);
    list.add(insn);
    list.add(new InsnNode(Opcodes.IASTORE));
    list.add(new InsnNode(Opcodes.RETURN));

    final ListIterator<AbstractInsnNode> iterator = list.iterator();

    final Block block = new Block(1, new Type[] { Type.INT_TYPE, Type.INT_TYPE }, Type.INT_TYPE);
    while (iterator.hasNext()) {
        block.addInsn(iterator.next(), true);
    }

    final Type findType = block.findType(insn);
    assertEquals(Type.getType("[I"), findType);
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.NodeHelper.java

License:Open Source License

/**
 * Checks if node is astore.// w  w w. ja v a  2  s.c  o  m
 * 
 * @param node
 *          the node
 * @return true if node is astore
 */
public static boolean isAstore(final AbstractInsnNode node) {
    if (node == null) {
        return false;
    }
    return node.getOpcode() == Opcodes.ASTORE;
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.RemoveUnusedLocalVars.java

License:Open Source License

private void findNodes(final InsnList original, final List<VarInsnNode> stores, final List<VarInsnNode> users, //
        final List<IincInsnNode> iincs) {
    final Iterator<AbstractInsnNode> iterator = original.iterator();
    while (iterator.hasNext()) {
        final AbstractInsnNode node = iterator.next();
        if ((node.getOpcode() >= Opcodes.ISTORE) && (node.getOpcode() <= Opcodes.ASTORE)) {
            stores.add((VarInsnNode) node);
        } else if ((node.getOpcode() >= Opcodes.ILOAD) && (node.getOpcode() <= Opcodes.ALOAD)) {
            users.add((VarInsnNode) node);
        } else if (node.getOpcode() == Opcodes.IINC) {
            iincs.add((IincInsnNode) node);
        }/*www. j a  v a2s  .  c  o m*/
    }
}