List of usage examples for org.objectweb.asm.tree InsnList add
public void add(final InsnList insnList)
From source file:de.tuberlin.uebb.jbop.optimizer.loop.ForLoop.java
License:Open Source License
/** * Gets the insn list.//from w ww. j a v a2 s . c o m * * @param method * the method * @return the insn list with the unroled loop. */ public InsnList getInsnList(final MethodNode method) { final int loopCount = footer.getLoopCount(); final int ifNode = footer.getIfNode().getOpcode(); final InsnList unfolded = new InsnList(); for (int i = start.intValue(); eval(i, loopCount, ifNode); i += footer.getIinc().incr) { unfolded.add(body.getInsnList(i, footer, method)); } return unfolded; }
From source file:de.tuberlin.uebb.jbop.optimizer.loop.ForLoopBody.java
License:Open Source License
/** * Gets the insn list./*from w ww . j a va2s . c om*/ * * @param i * the curretn counter * @param footer * the footer * @param method * the method * @return the insn list of the body in respect to the current counter (<code>i</code>). */ public InsnList getInsnList(final int i, final ForLoopFooter footer, final MethodNode method) { final InsnList list = new InsnList(); if (body.isEmpty()) { return list; } final LabelMap labelMap = new LabelMap(); final int newLocalVar = method.maxLocals + 1; final int oldLocalVar = footer.getVarIndex(); list.add(NodeHelper.getInsnNodeFor(i)); list.add(new VarInsnNode(Opcodes.ISTORE, newLocalVar)); for (final AbstractInsnNode node : body) { final AbstractInsnNode copyNode = node.clone(labelMap); if (copyNode instanceof VarInsnNode) { if (((VarInsnNode) copyNode).var == oldLocalVar) { ((VarInsnNode) copyNode).var = newLocalVar; } } list.add(copyNode); } list.add(new SplitMarkNode()); method.maxLocals++; return list; }
From source file:de.tuberlin.uebb.jbop.optimizer.loop.ForLoopUnroller.java
License:Open Source License
@Override public InsnList optimize(final InsnList original, final MethodNode method) { optimized = false;/*from ww w . j av a 2 s . c om*/ final Iterator<AbstractInsnNode> iterator = original.iterator(); final InsnList insn = new InsnList(); final LinkedList<AbstractInsnNode> skipped = new LinkedList<>(); while (iterator.hasNext()) { final AbstractInsnNode currentNode = iterator.next(); final Loop loop = LoopMatcher.getLoop(currentNode); if ((loop == null) || !loop.isPlain()) { skipped.add(currentNode); continue; } final AbstractInsnNode last = skipped.getLast(); skipped.remove(last); correctIteratorPosition(iterator, loop.getEndOfLoop()); optimized = true; for (final AbstractInsnNode node : skipped) { insn.add(node); } skipped.clear(); original.remove(last); final ForLoop forLoop = LoopMatcher.toForLoop(loop); insn.add(forLoop.getInsnList(method)); } for (final AbstractInsnNode node : skipped) { insn.add(node); } return insn; }
From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.BlockTest.java
License:Open Source License
/** * Tests the type erasure.//from w w w. ja va 2 s .co 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.methodsplitter.MethodSplitter.java
License:Open Source License
@Override public InsnList optimize(final InsnList original, final MethodNode methodNode) throws JBOPClassException { LocalVariablesSorter sorter = new LocalVariablesSorter(methodNode.access, methodNode.desc, new EmptyMethodVisitor(Opcodes.ASM5)); methodNode.accept(sorter);//w w w. jav a 2 s . com if (getLength(methodNode) < maxInsns) { return clean(original); } final List<Block> blocks = getBlocks(original, methodNode); final String baseName = methodNode.name; final String[] exceptions = getExceptions(methodNode); final InsnList returnList = new InsnList(); InsnList list = returnList; Block block = blocks.get(0); block.renameInsns(block.getVarMap()); final String name = baseName + "__split__part__"; final Type endType = Type.getReturnType(methodNode.desc); for (int i = 1; i < blocks.size(); ++i) { final Map<Integer, Integer> paramRenameMap = block.getVarMap(); add(list, block.getInstructions(), original); block = blocks.get(i); block.renameInsns(paramRenameMap); final String methodDescriptor = block.getDescriptor(); final String newMethodName = name + block.getBlockNumber(); final MethodNode splitMethod = new MethodNode(Opcodes.ASM5, ACCESS, newMethodName, methodDescriptor, null, exceptions); additionalMethods.add(splitMethod); list.add(new VarInsnNode(Opcodes.ALOAD, 0)); list.add(block.getPushParameters()); list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, classNode.name, splitMethod.name, methodDescriptor)); list.add(new InsnNode(endType.getOpcode(IRETURN))); sorter = new LocalVariablesSorter(splitMethod.access, splitMethod.desc, new EmptyMethodVisitor(Opcodes.ASM5)); splitMethod.accept(sorter); list = splitMethod.instructions; } add(list, block.getInstructions(), original); return returnList; }
From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.MethodSplitter.java
License:Open Source License
private void add(final InsnList list, final List<AbstractInsnNode> insns, final InsnList original) { for (final AbstractInsnNode node : insns) { original.remove(node);/*ww w .ja va 2 s . c o m*/ list.add(node); } }
From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.removeSystemExit.RemoveSystemExitMethodNode.java
License:Open Source License
@SuppressWarnings("unchecked") // Call to pre-1.5 Code @Override/*from ww w . j av a 2s . co m*/ public void visitEnd() { MethodNode mn = (MethodNode) mv; InsnList insns = mn.instructions; Iterator i = insns.iterator(); AbstractInsnNode prev = null; InsnList newInstrucionList = new InsnList(); while (i.hasNext()) { boolean addInstruction = true; AbstractInsnNode i1 = (AbstractInsnNode) i.next(); if (i1 instanceof MethodInsnNode) { MethodInsnNode methotInsnNode = (MethodInsnNode) i1; if (methotInsnNode.name.equals("exit") && methotInsnNode.owner.equals("java/lang/System")) { logger.info("Replacing System.exit "); newInstrucionList.remove(prev); // insns.remove(i1); InsnList il = new InsnList(); Label mutationStartLabel = new Label(); mutationStartLabel.info = new MutationMarker(true); il.add(new LabelNode(mutationStartLabel)); il.add(new TypeInsnNode(Opcodes.NEW, "java/lang/RuntimeException")); il.add(new InsnNode(Opcodes.DUP)); il.add(new LdcInsnNode("Replaced System.exit()")); il.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V")); il.add(new InsnNode(Opcodes.ATHROW)); Label mutationEndLabel = new Label(); mutationEndLabel.info = new MutationMarker(false); il.add(new LabelNode(mutationEndLabel)); newInstrucionList.add(il); addInstruction = false; } } if (addInstruction) { try { insns.remove(i1); newInstrucionList.add(i1); } catch (Exception e) { logger.error(e); } } prev = i1; } mn.instructions = newInstrucionList; mn.accept(next); }
From source file:de.unisb.cs.st.javaslicer.tracer.instrumentation.TracingMethodInstrumenter.java
License:Open Source License
@SuppressWarnings("unchecked") public void transform(final ListIterator<MethodNode> methodIt) { // do not modify abstract or native methods if ((this.methodNode.access & ACC_ABSTRACT) != 0 || (this.methodNode.access & ACC_NATIVE) != 0) return;//w w w. j a v a2 s . com // check out what labels are jump targets (only these have to be traced) analyze(this.methodNode); this.instructionIterator = new FixedInstructionIterator(this.methodNode.instructions); // in the old method, initialize the new local variable for the threadtracer this.instructionIterator.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(Tracer.class), "getInstance", "()L" + Type.getInternalName(Tracer.class) + ";", false)); this.instructionIterator.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(Tracer.class), "getThreadTracer", "()L" + Type.getInternalName(ThreadTracer.class) + ";", false)); this.instructionIterator.add(new InsnNode(DUP)); this.instructionIterator.add(new VarInsnNode(ASTORE, this.tracerLocalVarIndex)); this.instructionIterator.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "isPaused", "()Z", true)); final LabelNode noTracingLabel = new LabelNode(); this.instructionIterator.add(new JumpInsnNode(IFNE, noTracingLabel)); // create a copy of the (uninstrumented) instructions (later, while iterating through the instructions) final InsnList oldInstructions = new InsnList(); final Map<LabelNode, LabelNode> labelCopies = new LazyLabelMap(); // copy the try-catch-blocks final Object[] oldTryCatchblockNodes = this.methodNode.tryCatchBlocks.toArray(); for (final Object o : oldTryCatchblockNodes) { final TryCatchBlockNode tcb = (TryCatchBlockNode) o; final TryCatchBlockNode newTcb = new TryCatchBlockNode(labelCopies.get(tcb.start), labelCopies.get(tcb.end), labelCopies.get(tcb.handler), tcb.type); this.methodNode.tryCatchBlocks.add(newTcb); } // increment number of local variables by one (for the threadtracer) ++this.methodNode.maxLocals; // and increment all local variable indexes after the new one by one for (final Object o : this.methodNode.localVariables) { final LocalVariableNode localVar = (LocalVariableNode) o; if (localVar.index >= this.tracerLocalVarIndex) ++localVar.index; } // store information about local variables in the ReadMethod object List<LocalVariable> localVariables = new ArrayList<LocalVariable>(); for (final Object o : this.methodNode.localVariables) { final LocalVariableNode localVar = (LocalVariableNode) o; while (localVariables.size() <= localVar.index) localVariables.add(null); localVariables.set(localVar.index, new LocalVariable(localVar.index, localVar.name, localVar.desc)); } this.readMethod.setLocalVariables(localVariables.toArray(new LocalVariable[localVariables.size()])); localVariables = null; // each method must start with a (dedicated) label: assert this.readMethod.getInstructions().isEmpty(); traceLabel(null, InstructionType.METHODENTRY); assert this.readMethod.getInstructions().size() == 1 && this.readMethod.getInstructions().get(0) instanceof LabelMarker && ((LabelMarker) this.readMethod.getInstructions().get(0)).isAdditionalLabel(); this.readMethod.setMethodEntryLabel((LabelMarker) this.readMethod.getInstructions().get(0)); // needed later: final LabelNode l0 = new LabelNode(); this.instructionIterator.add(l0); // then, visit the instructions that were in the method before while (this.instructionIterator.hasNext()) { final AbstractInsnNode insnNode = this.instructionIterator.next(); switch (insnNode.getType()) { case AbstractInsnNode.INSN: transformInsn((InsnNode) insnNode); break; case AbstractInsnNode.INT_INSN: transformIntInsn((IntInsnNode) insnNode); break; case AbstractInsnNode.VAR_INSN: transformVarInsn((VarInsnNode) insnNode); break; case AbstractInsnNode.TYPE_INSN: transformTypeInsn((TypeInsnNode) insnNode); break; case AbstractInsnNode.FIELD_INSN: transformFieldInsn((FieldInsnNode) insnNode); break; case AbstractInsnNode.METHOD_INSN: transformMethodInsn((MethodInsnNode) insnNode); break; case AbstractInsnNode.JUMP_INSN: transformJumpInsn((JumpInsnNode) insnNode); break; case AbstractInsnNode.LABEL: transformLabel((LabelNode) insnNode); break; case AbstractInsnNode.LDC_INSN: transformLdcInsn((LdcInsnNode) insnNode); break; case AbstractInsnNode.IINC_INSN: transformIincInsn((IincInsnNode) insnNode); break; case AbstractInsnNode.TABLESWITCH_INSN: transformTableSwitchInsn((TableSwitchInsnNode) insnNode); break; case AbstractInsnNode.LOOKUPSWITCH_INSN: transformLookupSwitchInsn((LookupSwitchInsnNode) insnNode); break; case AbstractInsnNode.MULTIANEWARRAY_INSN: transformMultiANewArrayInsn((MultiANewArrayInsnNode) insnNode); break; case AbstractInsnNode.FRAME: // ignore break; case AbstractInsnNode.LINE: // ignore break; default: throw new RuntimeException("Unknown instruction type " + insnNode.getType() + " (" + insnNode.getClass().getSimpleName() + ")"); } oldInstructions.add(insnNode.clone(labelCopies)); } assert this.outstandingInitializations == 0; // add the (old) try-catch blocks to the readMethods // (can only be done down here since we use the information in the // labels map) for (final Object o : oldTryCatchblockNodes) { final TryCatchBlockNode tcb = (TryCatchBlockNode) o; this.readMethod.addTryCatchBlock(new TryCatchBlock(this.labels.get(tcb.start), this.labels.get(tcb.end), this.labels.get(tcb.handler), tcb.type)); } final LabelNode l1 = new LabelNode(); this.instructionIterator.add(l1); final int newPos = this.readMethod.getInstructions().size(); traceLabel(null, InstructionType.METHODEXIT); assert this.readMethod.getInstructions().size() == newPos + 1; final AbstractInstruction abnormalTerminationLabel = this.readMethod.getInstructions().get(newPos); assert abnormalTerminationLabel instanceof LabelMarker; this.readMethod.setAbnormalTerminationLabel((LabelMarker) abnormalTerminationLabel); this.methodNode.instructions.add(new InsnNode(ATHROW)); // add a try catch block around the method so that we can trace when this method is left // by a thrown exception this.methodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, null)); // now add the code that is executed if no tracing should be performed this.methodNode.instructions.add(noTracingLabel); if (this.firstLine != -1) this.methodNode.instructions.add(new LineNumberNode(this.firstLine, noTracingLabel)); this.methodNode.instructions.add(new InsnNode(ACONST_NULL)); this.methodNode.instructions.add(new VarInsnNode(ASTORE, this.tracerLocalVarIndex)); this.methodNode.instructions.add(oldInstructions); // finally: create a copy of the method that gets the ThreadTracer as argument // this is only necessary for private methods or "<init>" if (this.tracer.wasRedefined(this.readMethod.getReadClass().getName()) && (this.methodNode.access & ACC_PRIVATE) != 0) { final Type[] oldMethodArguments = Type.getArgumentTypes(this.methodNode.desc); final Type[] newMethodArguments = Arrays.copyOf(oldMethodArguments, oldMethodArguments.length + 1); newMethodArguments[oldMethodArguments.length] = Type.getType(ThreadTracer.class); final String newMethodDesc = Type.getMethodDescriptor(Type.getReturnType(this.methodNode.desc), newMethodArguments); final MethodNode newMethod = new MethodNode(this.methodNode.access, this.methodNode.name, newMethodDesc, this.methodNode.signature, (String[]) this.methodNode.exceptions.toArray(new String[this.methodNode.exceptions.size()])); methodIt.add(newMethod); int threadTracerParamPos = ((this.readMethod.getAccess() & Opcodes.ACC_STATIC) == 0 ? 1 : 0); for (final Type t : oldMethodArguments) threadTracerParamPos += t.getSize(); final Map<LabelNode, LabelNode> newMethodLabels = new LazyLabelMap(); // copy the local variables information to the new method for (final Object o : this.methodNode.localVariables) { final LocalVariableNode lv = (LocalVariableNode) o; newMethod.localVariables.add(new LocalVariableNode(lv.name, lv.desc, lv.signature, newMethodLabels.get(lv.start), newMethodLabels.get(lv.end), lv.index)); } newMethod.maxLocals = this.methodNode.maxLocals; newMethod.maxStack = this.methodNode.maxStack; // copy the try-catch-blocks for (final Object o : this.methodNode.tryCatchBlocks) { final TryCatchBlockNode tcb = (TryCatchBlockNode) o; newMethod.tryCatchBlocks.add(new TryCatchBlockNode(newMethodLabels.get(tcb.start), newMethodLabels.get(tcb.end), newMethodLabels.get(tcb.handler), tcb.type)); } // skip the first 6 instructions, replace them with these: newMethod.instructions.add(new VarInsnNode(ALOAD, threadTracerParamPos)); newMethod.instructions.add(new InsnNode(DUP)); newMethod.instructions.add(new VarInsnNode(ASTORE, this.tracerLocalVarIndex)); newMethod.instructions.add(new JumpInsnNode(IFNULL, newMethodLabels.get(noTracingLabel))); final Iterator<AbstractInsnNode> oldInsnIt = this.methodNode.instructions.iterator(6); // and add all the other instructions while (oldInsnIt.hasNext()) { final AbstractInsnNode insn = oldInsnIt.next(); newMethod.instructions.add(insn.clone(newMethodLabels)); } } ready(); }
From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java
License:Open Source License
private InsnList emit(BasicBlock block) { FluentIterable<TerminatorInst> terminators = FluentIterable.from(block.instructions()) .filter(TerminatorInst.class); if (terminators.isEmpty()) throw new IllegalArgumentException("block " + block.getName() + " in method " + block.getParent().getName() + " lacks a terminator"); if (terminators.size() > 1) throw new IllegalArgumentException("block " + block.getName() + " in method " + block.getParent().getName() + " has multiple terminators: " + terminators); InsnList insns = new InsnList(); insns.add(labels.get(block)); for (Instruction i : block.instructions()) { if (i instanceof TerminatorInst) emitPhiMoves(block, insns);// w w w . ja v a 2 s . c o m if (i instanceof ArrayLengthInst) emit((ArrayLengthInst) i, insns); else if (i instanceof ArrayLoadInst) emit((ArrayLoadInst) i, insns); else if (i instanceof ArrayStoreInst) emit((ArrayStoreInst) i, insns); else if (i instanceof BinaryInst) emit((BinaryInst) i, insns); else if (i instanceof BranchInst) emit((BranchInst) i, insns); else if (i instanceof CallInst) emit((CallInst) i, insns); else if (i instanceof CastInst) emit((CastInst) i, insns); else if (i instanceof InstanceofInst) emit((InstanceofInst) i, insns); else if (i instanceof JumpInst) emit((JumpInst) i, insns); else if (i instanceof LoadInst) emit((LoadInst) i, insns); else if (i instanceof NewArrayInst) emit((NewArrayInst) i, insns); else if (i instanceof PhiInst) //PhiInst deliberately omitted ; else if (i instanceof ReturnInst) emit((ReturnInst) i, insns); else if (i instanceof StoreInst) emit((StoreInst) i, insns); else if (i instanceof SwitchInst) emit((SwitchInst) i, insns); else if (i instanceof ThrowInst) emit((ThrowInst) i, insns); else throw new AssertionError("can't emit " + i); } return insns; }
From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java
License:Open Source License
private void emit(ArrayLengthInst i, InsnList insns) { load(i.getOperand(0), insns);//from w ww.jav a 2s . co m insns.add(new InsnNode(Opcodes.ARRAYLENGTH)); store(i, insns); }