List of usage examples for org.objectweb.asm.tree InsnList get
public AbstractInsnNode get(final int index)
From source file:de.tuberlin.uebb.jbop.optimizer.arithmetic.ArithmeticExpressionInterpreterTest.java
License:Open Source License
/** * int x = 0xFF << 1;/*w w w.ja va2 s . c o m*/ */ @Test public void testArithmeticExpressionShiftLeft() { // INIT builder.getMethod("testMethod").desc = "()I"; builder.// add(BIPUSH, 0x1).// add(ICONST_1).// add(ISHL).// add(BIPUSH, 0x2).// add(ICONST_1).// add(ISHL).// add(IOR).// addReturn(); final InsnList optimized = interpreter.optimize(builder.getMethod("testMethod").instructions, builder.getMethod("testMethod")); assertEquals(2, optimized.size()); assertEquals(6, NodeHelper.getNumberValue(optimized.get(0)).intValue()); }
From source file:de.tuberlin.uebb.jbop.optimizer.array.FieldArrayValueInlinerTest.java
License:Open Source License
/** * Tests that the FieldArrayValueInliner is working correctly with field-chains. * /*w ww .j av a 2 s.c om*/ * @throws Exception * the exception */ @Test public void testFieldArrayValueInlinerWithFieldChain() throws Exception { // INIT final ClassNodeBuilder builderC = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.array.C").// addField("d", "[D").// withModifiers(ACC_PRIVATE, ACC_FINAL).// withAnnotation(ImmutableArray.class).// initArrayWith(1.0).// toClass(); final ClassNodeBuilder builderB = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.array.B").// addField("c", Type.getDescriptor(builderC.getBuildedClass())).// withModifiers(ACC_PRIVATE, ACC_FINAL).// initWith(null).// toClass(); final ClassNodeBuilder builderA = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.array.A").// addField("b", Type.getDescriptor(builderB.getBuildedClass())).// withModifiers(ACC_PRIVATE, ACC_FINAL).// initWith(null).// toClass(); final ClassNodeBuilder builderTestClass = ClassNodeBuilder .createClass("de.tuberlin.uebb.jbop.optimizer.array.ChainedTestClass").// addField("a", "[" + Type.getDescriptor(builderA.getBuildedClass())).// withAnnotation(ImmutableArray.class).// withModifiers(ACC_PRIVATE, ACC_FINAL).// addToConstructor(initArray()).// addMethod("get", "()D").// addGetClassField("a").// ; addInsn(new InsnNode(ICONST_0)).// addInsn(new InsnNode(AALOAD)).// addGetField(builderA, "b").// addGetField(builderB, "c").// addGetField(builderC, "d").// addInsn(new InsnNode(ICONST_0)).// addInsn(new InsnNode(DALOAD)).// addInsn(new InsnNode(DRETURN));// final Object instance = builderTestClass.instance(); // RUN final FieldArrayValueInliner inliner = new FieldArrayValueInliner(); inliner.setClassNode(builderTestClass.getClassNode()); inliner.setInputObject(instance); final MethodNode method = builderTestClass.getMethod("get"); final InsnList optimized = inliner.optimize(method.instructions, method); // ASSERT assertEquals(2, optimized.size()); assertEquals(1.0, NodeHelper.getNumberValue(optimized.get(0)).doubleValue(), .00001); }
From source file:de.tuberlin.uebb.jbop.optimizer.array.LocalArrayLengthInlinerTest.java
License:Open Source License
/** * Tests that LocalArrayLengthInliner is working correctly. * //from ww w .j a va 2 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.LocalArrayLengthInlinerTest.java
License:Open Source License
@Test public void test_multiArray() throws Exception { final ClassNodeBuilder builder = ClassNodeBuilder .createClass("de.tuberlin.uebb.jbop.optimizer.array.Test1"); builder.addField("field", "[[[I").// withModifiers(ACC_PRIVATE, ACC_FINAL).// withAnnotation(ImmutableArray.class); builder.removeMethod("<init>", "()V").// addConstructor("([[[I)V", "()V").// add(ALOAD, 0).// add(ALOAD, 1).// addPutClassField("field"); builder.///*w w w. ja v a2s.co m*/ addMethod("doit", "()V").// addGetClassField("field").// add(ICONST_0).// add(AALOAD).// add(ASTORE, 1).// add(ALOAD, 1).// add(ICONST_0).// add(AALOAD).// add(ASTORE, 2).// add(ALOAD, 2).// add(ICONST_1).// add(IALOAD).// add(ISTORE, 3).// add(ALOAD, 1).// add(ARRAYLENGTH).// add(ALOAD, 2).// add(ARRAYLENGTH).// addReturn(); final Object instance = builder.instance(new Object[] { new int[][][] { { { 1, 2, 3 } } } }); final LocalArrayLengthInliner inliner = new LocalArrayLengthInliner(); inliner.setInputObject(instance); final MethodNode method = builder.getMethod("doit"); final InsnList optimized = inliner.optimize(method.instructions, method); method.instructions = optimized; assertEquals(16, optimized.size()); assertEquals(ICONST_1, optimized.get(13).getOpcode()); assertEquals(ICONST_3, optimized.get(14).getOpcode()); }
From source file:de.tuberlin.uebb.jbop.optimizer.array.LocalArrayValueInlinerTest.java
License:Open Source License
/** * Tests that LocalArrayValueInliner is working correctly. * // ww w .j a v a2 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.array.LocalArrayValueInlinerTest.java
License:Open Source License
@Test public void test() throws Exception { final ClassNodeBuilder builder = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.array.Test"); builder.addField("field", "[[[I").// withModifiers(ACC_PRIVATE, ACC_FINAL).// withAnnotation(ImmutableArray.class); builder.removeMethod("<init>", "()V").// addConstructor("([[[I)V", "()V").// add(ALOAD, 0).// add(ALOAD, 1).// addPutClassField("field"); builder.//// ww w .jav a2s. c o m addMethod("doit", "()V").// addGetClassField("field").// add(ICONST_0).// add(AALOAD).// add(ASTORE, 1).// add(ALOAD, 1).// add(ICONST_0).// add(AALOAD).// add(ASTORE, 2).// add(ALOAD, 2).// add(ICONST_1).// add(IALOAD).// add(ISTORE, 3).// addReturn(); final Object instance = builder.instance(new Object[] { new int[][][] { { { 1, 2, 3 } } } }); final LocalArrayValueInliner inliner = new LocalArrayValueInliner(); inliner.setInputObject(instance); final MethodNode method = builder.getMethod("doit"); final InsnList optimized = inliner.optimize(method.instructions, method); method.instructions = optimized; assertEquals(12, optimized.size()); assertEquals(ICONST_2, optimized.get(9).getOpcode()); }
From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInlinerTest.java
License:Open Source License
/** * Tests that constantIfInliner is working correctly. * //from ww w. j a v a2 s . c o m * Input is * * <pre> * if(1<2) * ... * </pre> * * @throws JBOPClassException * the jBOP class exception */ @Test public void testConstantIfInlinerIF_CMPEG() throws JBOPClassException { // INIT final LabelNode label = new LabelNode(); builder.addInsn(new InsnNode(Opcodes.ICONST_1)).// addInsn(new InsnNode(Opcodes.ICONST_2)).// addInsn(new JumpInsnNode(Opcodes.IF_ICMPGE, label)).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(label).// addInsn(new InsnNode(Opcodes.RETURN)); // RUN assertEquals(6, method.instructions.size()); final InsnList optimized = constantIfInliner.optimize(method.instructions, method); // ASSERT assertEquals(3, optimized.size()); assertEquals(Opcodes.NOP, optimized.get(0).getOpcode()); }
From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInlinerTest.java
License:Open Source License
/** * Tests that constantIfInliner is working correctly. * // www . j ava 2s. c o m * Input is * * <pre> * if(1<=2) * ... * </pre> * * @throws JBOPClassException * the jBOP class exception */ @Test public void testConstantIfInlinerIF_ICMPGT() throws JBOPClassException { // INIT final LabelNode label = new LabelNode(); builder.addInsn(new InsnNode(Opcodes.ICONST_1)).// addInsn(new InsnNode(Opcodes.ICONST_2)).// addInsn(new JumpInsnNode(Opcodes.IF_ICMPGT, label)).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(label).// addInsn(new InsnNode(Opcodes.RETURN)); // RUN assertEquals(6, method.instructions.size()); final InsnList optimized = constantIfInliner.optimize(method.instructions, method); // ASSERT assertEquals(3, optimized.size()); assertEquals(Opcodes.NOP, optimized.get(0).getOpcode()); }
From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInlinerTest.java
License:Open Source License
/** * Tests that constantIfInliner is working correctly. * /*ww w . j a v a 2 s . c om*/ * Input is * * <pre> * if(2>1) * ... * </pre> * * @throws JBOPClassException * the jBOP class exception */ @Test public void testConstantIfInlinerIF_ICMPLE() throws JBOPClassException { // INIT final LabelNode label = new LabelNode(); builder.addInsn(new InsnNode(Opcodes.ICONST_2)).// addInsn(new InsnNode(Opcodes.ICONST_1)).// addInsn(new JumpInsnNode(Opcodes.IF_ICMPLE, label)).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(label).// addInsn(new InsnNode(Opcodes.RETURN)); // RUN assertEquals(6, method.instructions.size()); final InsnList optimized = constantIfInliner.optimize(method.instructions, method); // ASSERT assertEquals(3, optimized.size()); assertEquals(Opcodes.NOP, optimized.get(0).getOpcode()); }
From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInlinerTest.java
License:Open Source License
/** * Tests that constantIfInliner is working correctly. * /*from w w w. j a va 2 s .c o m*/ * Input is * * <pre> * if(2>=1) * ... * </pre> * * @throws JBOPClassException * the jBOP class exception */ @Test public void testConstantIfInlinerIF_ICMPLT() throws JBOPClassException { // INIT final LabelNode label = new LabelNode(); builder.addInsn(new InsnNode(Opcodes.ICONST_2)).// addInsn(new InsnNode(Opcodes.ICONST_1)).// addInsn(new JumpInsnNode(Opcodes.IF_ICMPLT, label)).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(label).// addInsn(new InsnNode(Opcodes.RETURN)); // RUN assertEquals(6, method.instructions.size()); final InsnList optimized = constantIfInliner.optimize(method.instructions, method); // ASSERT assertEquals(3, optimized.size()); assertEquals(Opcodes.NOP, optimized.get(0).getOpcode()); }