List of usage examples for org.objectweb.asm.tree AbstractInsnNode getOpcode
public int getOpcode()
From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInliner.java
License:Open Source License
private void handleStore(final AbstractInsnNode currentNode, final Map<Integer, Object> knownValues) { final int index = NodeHelper.getVarIndex(currentNode); if (LoopMatcher.isStoreOfLoop(currentNode)) { knownValues.remove(index);/*from www . j av a2s.com*/ return; } final AbstractInsnNode previous = currentNode.getPrevious(); if (previous.getOpcode() != NEWARRAY && NodeHelper.isValue(previous)) { final Object value = NodeHelper.getValue(previous); knownValues.put(index, value); } else { knownValues.remove(index); } }
From source file:de.tuberlin.uebb.jbop.optimizer.var.RemoveUnusedLocalVars.java
License:Open Source License
@Override public InsnList optimize(final InsnList original, final MethodNode methodNode) { optimized = false;/*from w ww .j a va2 s . c o m*/ final List<VarInsnNode> stores = new ArrayList<>(); final List<VarInsnNode> users = new ArrayList<>(); final List<IincInsnNode> iincs = new ArrayList<>(); findNodes(original, stores, users, iincs); final List<AbstractInsnNode> toBeRemoved = new ArrayList<>(); for (final VarInsnNode node : stores) { if (!usersContains(users, node)) { toBeRemoved.add(node); toBeRemoved.addAll(iincsContains(iincs, node)); } } // final InsnList newList = new InsnList(); final Iterator<AbstractInsnNode> iterator = original.iterator(); while (iterator.hasNext()) { final AbstractInsnNode currentNode = iterator.next(); if (toBeRemoved.contains(currentNode)) { if (currentNode.getOpcode() == Opcodes.IINC) { original.remove(currentNode); optimized = true; continue; } final AbstractInsnNode firstOfStack = NodeHelper.getFirstOfStack(currentNode); if (firstOfStack != null) { optimized = true; AbstractInsnNode remove = firstOfStack; while (remove != currentNode) { final AbstractInsnNode toRemove = remove; remove = remove.getNext(); original.remove(toRemove); } original.remove(currentNode); } } } return original; }
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); }//from www . j ava2 s.c o m } }
From source file:de.unisb.cs.st.javaslicer.tracer.instrumentation.PauseTracingInstrumenter.java
License:Open Source License
@SuppressWarnings("unchecked") public void transformMethod(final MethodNode method, final ListIterator<MethodNode> methodIt, final String className) { if ((method.access & ACC_ABSTRACT) != 0 || (method.access & ACC_NATIVE) != 0) return;//w ww . j a v a2 s . co m int tracerLocalVarIndex = (method.access & Opcodes.ACC_STATIC) == 0 ? 1 : 0; for (final Type t : Type.getArgumentTypes(method.desc)) tracerLocalVarIndex += t.getSize(); // increment number of local variables by one (for the threadtracer) ++method.maxLocals; // and increment all local variable indexes after the new one by one for (final Object o : method.localVariables) { final LocalVariableNode localVar = (LocalVariableNode) o; if (localVar.index >= tracerLocalVarIndex) ++localVar.index; } final LabelNode l0 = new LabelNode(); final LabelNode l1 = new LabelNode(); final ListIterator<AbstractInsnNode> insnIt = method.instructions.iterator(); insnIt.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(Tracer.class), "getInstance", "()L" + Type.getInternalName(Tracer.class) + ";", false)); insnIt.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(Tracer.class), "getThreadTracer", "()L" + Type.getInternalName(ThreadTracer.class) + ";", false)); insnIt.add(new InsnNode(DUP)); insnIt.add(new VarInsnNode(ASTORE, tracerLocalVarIndex)); insnIt.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "pauseTracing", "()V", true)); insnIt.add(l0); while (insnIt.hasNext()) { final AbstractInsnNode insn = insnIt.next(); switch (insn.getType()) { case AbstractInsnNode.INSN: switch (insn.getOpcode()) { case IRETURN: case LRETURN: case FRETURN: case DRETURN: case ARETURN: case RETURN: insnIt.previous(); insnIt.add(new VarInsnNode(ALOAD, tracerLocalVarIndex)); insnIt.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "resumeTracing", "()V", true)); insnIt.next(); } break; case AbstractInsnNode.IINC_INSN: if (((IincInsnNode) insn).var >= tracerLocalVarIndex) ++((IincInsnNode) insn).var; break; case AbstractInsnNode.VAR_INSN: if (((VarInsnNode) insn).var >= tracerLocalVarIndex) ++((VarInsnNode) insn).var; break; default: break; } } method.instructions.add(l1); method.instructions.add(new VarInsnNode(ALOAD, tracerLocalVarIndex)); method.instructions.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "resumeTracing", "()V", true)); method.instructions.add(new InsnNode(ATHROW)); method.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, null)); // finally: create a copy of the method that gets the ThreadTracer as argument if (!"<clinit>".equals(method.name) && this.tracer.wasRedefined(className)) { final Type[] oldMethodArguments = Type.getArgumentTypes(method.desc); final Type[] newMethodArguments = Arrays.copyOf(oldMethodArguments, oldMethodArguments.length + 1); newMethodArguments[oldMethodArguments.length] = Type.getType(ThreadTracer.class); final String newMethodDesc = Type.getMethodDescriptor(Type.getReturnType(method.desc), newMethodArguments); final MethodNode newMethod = new MethodNode(method.access, method.name, newMethodDesc, method.signature, (String[]) method.exceptions.toArray(new String[method.exceptions.size()])); methodIt.add(newMethod); final Map<LabelNode, LabelNode> newMethodLabels = new LazyLabelMap(); // copy the local variables information to the new method for (final Object o : method.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 = method.maxLocals; newMethod.maxStack = method.maxStack; // copy the try-catch-blocks for (final Object o : method.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 4 instructions, replace them with this: newMethod.instructions.add(new VarInsnNode(ALOAD, tracerLocalVarIndex)); final Iterator<AbstractInsnNode> oldInsnIt = method.instructions.iterator(4); // and add all the other instructions while (oldInsnIt.hasNext()) { final AbstractInsnNode insn = oldInsnIt.next(); newMethod.instructions.add(insn.clone(newMethodLabels)); } } }
From source file:de.unisb.cs.st.javaslicer.tracer.instrumentation.TracingMethodInstrumenter.java
License:Open Source License
private void transformTypeInsn(final TypeInsnNode insn) { if (insn.getOpcode() == ANEWARRAY) { int newObjectIdSeqIndex = this.tracer.newLongTraceSequence(); transformArrayAllocation(new TypeInstruction(this.readMethod, insn.getOpcode(), this.currentLine, insn.desc, newObjectIdSeqIndex), newObjectIdSeqIndex); } else if (insn.getOpcode() == NEW) { // after a NEW, we store the sequence number in the ThreadTracer object. // after the constructor has been called (which is guaranteed), its // id is written to the stored sequence number final int newObjectIdSeqIndex = this.tracer.newLongTraceSequence(); final AbstractInstruction instruction = new TypeInstruction(this.readMethod, insn.getOpcode(), this.currentLine, insn.desc, newObjectIdSeqIndex); if (!this.instructionIterator.hasNext()) { if (this.tracer.debug) Transformer.printMethod(System.err, this.methodNode); throw new TracerException("Bytecode of method " + this.classNode.name + "." + this.methodNode.name + " has unsupported form (constructor call does not immediately follow NEW instruction). " + "Maybe it was compiled for JRE < 1.5?"); } else if (this.instructionIterator.next().getOpcode() != DUP) { // this code handles a special kind of object initialization, which is sometimes used // in a catch block which throws a new exceptions which encapsulates the old one. // the bytecode of this construct looks like this: // NEW <newExceptionType> // DUP_X1 // SWAP // INVOKESPECIAL <newExceptionType>.<init> (Ljava/lang/Throwable;)V // ATHROW this.instructionIterator.previous(); AbstractInsnNode dup_x1 = this.instructionIterator.next(); boolean matches = false; if (dup_x1.getOpcode() == DUP_X1 && this.instructionIterator.hasNext()) { AbstractInsnNode swap = this.instructionIterator.next(); if (swap.getOpcode() == SWAP && this.instructionIterator.hasNext()) { matches = true;//from w w w . j a v a 2s.c o m } } if (matches) { // add another DUP_X1 before the existing one this.instructionIterator.previous(); this.instructionIterator.previous(); this.instructionIterator.add(new InsnNode(DUP_X1)); //if (this.tracer.debug) { // System.err.println("Added DUP_X1 at position " + this.instructionIterator.previousIndex() + "!"); // Transformer.printMethod(System.err, this.methodNode); //} } else { if (this.tracer.debug) Transformer.printMethod(System.err, this.methodNode); throw new TracerException("Bytecode of method " + this.classNode.name + "." + this.methodNode.name + " has unsupported form (constructor call does not immediately follow NEW instruction). " + "Maybe it was compiled for JRE < 1.5?"); } } else { this.instructionIterator.previous(); this.instructionIterator.add(new InsnNode(DUP)); } ++this.outstandingInitializations; // modified code of registerInstruction(): this.readMethod.addInstruction(instruction); this.instructionIterator.previous(); this.instructionIterator.add(new VarInsnNode(ALOAD, this.tracerLocalVarIndex)); this.instructionIterator.add(getIntConstInsn(instruction.getIndex())); this.instructionIterator.add(getIntConstInsn(newObjectIdSeqIndex)); this.instructionIterator.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "objectAllocated", "(II)V", true)); this.instructionIterator.next(); ++TracingMethodInstrumenter.statsInstructions; } else { registerInstruction( new TypeInstruction(this.readMethod, insn.getOpcode(), this.currentLine, insn.desc, -1), InstructionType.UNSAFE); } }
From source file:edu.mit.streamjit.impl.common.MessageConstraint.java
License:Open Source License
/** * Parse the given getHandle() call instruction and preceding instructions * into a WorkerData. This is a rather brittle pattern-matching job and * will fail on obfuscated bytecodes.//www. jav a2 s.co m * @param call * @return */ private static WorkerData dataFromCall(Class<?> klass, MethodInsnNode call) { //Latency is either an integer constant or a getfield on this. Field latencyField = null; int constantLatency = Integer.MIN_VALUE; AbstractInsnNode latencyInsn = call.getPrevious(); if (latencyInsn instanceof FieldInsnNode) { FieldInsnNode fieldInsn = (FieldInsnNode) latencyInsn; if (fieldInsn.getOpcode() != Opcodes.GETFIELD) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": latency field insn opcode " + fieldInsn.getOpcode()); if (!fieldInsn.desc.equals(Type.INT_TYPE.getDescriptor())) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": latency field desc " + fieldInsn.desc); if (!fieldInsn.owner.equals(Type.getType(klass).getInternalName())) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": latency field owner " + fieldInsn.owner); //Move latencyInsn to sync up with the other else-if branches. latencyInsn = latencyInsn.getPrevious(); //We must be loading from this. if (latencyInsn.getOpcode() != Opcodes.ALOAD) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": getfield subject opcode " + latencyInsn.getOpcode()); int varIdx = ((VarInsnNode) latencyInsn).var; if (varIdx != 0) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": getfield not from this but from " + varIdx); //Check the field we're loading from is constant (final). //A static field is okay here since it isn't a reference parameter. try { latencyField = klass.getDeclaredField(fieldInsn.name); if (!Modifier.isFinal(latencyField.getModifiers())) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": latency field not final: " + latencyField.toGenericString()); } catch (NoSuchFieldException ex) { throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": getfield not from this but from " + varIdx); } } else if (latencyInsn instanceof LdcInsnNode) { Object constant = ((LdcInsnNode) latencyInsn).cst; if (!(constant instanceof Integer)) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": ldc " + constant); constantLatency = ((Integer) constant); } else switch (latencyInsn.getOpcode()) { case Opcodes.ICONST_M1: constantLatency = -1; break; case Opcodes.ICONST_0: constantLatency = 0; break; case Opcodes.ICONST_1: constantLatency = 1; break; case Opcodes.ICONST_2: constantLatency = 2; break; case Opcodes.ICONST_3: constantLatency = 3; break; case Opcodes.ICONST_4: constantLatency = 4; break; case Opcodes.ICONST_5: constantLatency = 5; break; case Opcodes.BIPUSH: case Opcodes.SIPUSH: constantLatency = ((IntInsnNode) latencyInsn).operand; break; default: throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": latencyInsn opcode " + latencyInsn.getOpcode()); } //Finally, we've parsed the latency parameter. //Next is an aload_0 for the sender parameter. AbstractInsnNode senderInsn = latencyInsn.getPrevious(); if (senderInsn.getOpcode() != Opcodes.ALOAD || ((VarInsnNode) senderInsn).var != 0) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": bad sender"); //Finally, a getfield of this for a final Portal instance field. AbstractInsnNode portalInsn = senderInsn.getPrevious(); if (!(portalInsn instanceof FieldInsnNode)) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal getfield opcode " + portalInsn.getOpcode()); FieldInsnNode fieldInsn = (FieldInsnNode) portalInsn; if (fieldInsn.getOpcode() != Opcodes.GETFIELD) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal field insn opcode " + fieldInsn.getOpcode()); if (!fieldInsn.desc.equals(Type.getType(Portal.class).getDescriptor())) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": portal field desc " + fieldInsn.desc); if (!fieldInsn.owner.equals(Type.getType(klass).getInternalName())) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": portal field owner " + fieldInsn.owner); portalInsn = portalInsn.getPrevious(); //We must be loading from this. if (portalInsn.getOpcode() != Opcodes.ALOAD) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal getfield subject opcode " + portalInsn.getOpcode()); int varIdx = ((VarInsnNode) portalInsn).var; if (varIdx != 0) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal getfield not from this but from " + varIdx); //Check the field we're loading from is constant (final) and nonstatic. Field portalField; try { portalField = klass.getDeclaredField(fieldInsn.name); if (!Modifier.isFinal(portalField.getModifiers())) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal field not final: " + portalField.toGenericString()); if (Modifier.isStatic(portalField.getModifiers())) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal field is static: " + portalField.toGenericString()); } catch (NoSuchFieldException ex) { throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal getfield not from this but from " + varIdx); } return latencyField != null ? new WorkerData(portalField, latencyField) : new WorkerData(portalField, constantLatency); }
From source file:edu.mit.streamjit.util.bytecode.MethodResolver.java
License:Open Source License
private void findBlockBoundaries() { InsnList insns = methodNode.instructions; //We find the indices of any block-ending instruction and of any jump //target, sort, remove duplicates, then use pairs to define blocks. Note //these are end-exclusive indices, thus one after the block-enders, but //right on the jump targets (they're one-past-the-end of the preceding //block)./*from ww w .j av a 2 s . co m*/ List<Integer> indices = new ArrayList<>(); indices.add(0); for (int i = 0; i < insns.size(); ++i) { AbstractInsnNode insn = insns.get(i); int opcode = insn.getOpcode(); //Terminator opcodes end blocks. if (insn instanceof JumpInsnNode || insn instanceof LookupSwitchInsnNode || insn instanceof TableSwitchInsnNode || opcode == Opcodes.ATHROW || opcode == Opcodes.IRETURN || opcode == Opcodes.LRETURN || opcode == Opcodes.FRETURN || opcode == Opcodes.DRETURN || opcode == Opcodes.ARETURN || opcode == Opcodes.RETURN) { indices.add(i + 1); } //Jump targets of this instruction end blocks. if (insn instanceof JumpInsnNode) indices.add(insns.indexOf(((JumpInsnNode) insn).label)); else if (insn instanceof LookupSwitchInsnNode) { indices.add(insns.indexOf(((LookupSwitchInsnNode) insn).dflt)); for (Object label : ((LookupSwitchInsnNode) insn).labels) indices.add(insns.indexOf((LabelNode) label)); } else if (insn instanceof TableSwitchInsnNode) { indices.add(insns.indexOf(((TableSwitchInsnNode) insn).dflt)); for (Object label : ((TableSwitchInsnNode) insn).labels) indices.add(insns.indexOf((LabelNode) label)); } //While we're scanning the instructions, make the UninitializedValue //values for 'new' opcodes. if (opcode == Opcodes.NEW) { Klass k = getKlassByInternalName(((TypeInsnNode) insn).desc); ReferenceType t = typeFactory.getReferenceType(k); newValues.put(insn, new UninitializedValue(t, "new" + (counter++))); } } //Remove duplicates and sort via TreeSet. indices = new ArrayList<>(new TreeSet<>(indices)); for (int i = 1; i < indices.size(); ++i) blocks.add(new BBInfo(indices.get(i - 1), indices.get(i), i - 1)); }
From source file:edu.mit.streamjit.util.bytecode.MethodResolver.java
License:Open Source License
private void buildInstructions(BBInfo block) { FrameState frame = block.entryState.copy(); for (int i = block.start; i < block.end; ++i) { AbstractInsnNode insn = methodNode.instructions.get(i); if (insn.getOpcode() == -1) continue;//pseudo-instruction node if (insn instanceof FieldInsnNode) interpret((FieldInsnNode) insn, frame, block); else if (insn instanceof IincInsnNode) interpret((IincInsnNode) insn, frame, block); else if (insn instanceof InsnNode) interpret((InsnNode) insn, frame, block); else if (insn instanceof IntInsnNode) interpret((IntInsnNode) insn, frame, block); else if (insn instanceof InvokeDynamicInsnNode) interpret((InvokeDynamicInsnNode) insn, frame, block); else if (insn instanceof JumpInsnNode) interpret((JumpInsnNode) insn, frame, block); else if (insn instanceof LdcInsnNode) interpret((LdcInsnNode) insn, frame, block); else if (insn instanceof LookupSwitchInsnNode) interpret((LookupSwitchInsnNode) insn, frame, block); else if (insn instanceof MethodInsnNode) interpret((MethodInsnNode) insn, frame, block); else if (insn instanceof MultiANewArrayInsnNode) interpret((MultiANewArrayInsnNode) insn, frame, block); else if (insn instanceof TableSwitchInsnNode) interpret((TableSwitchInsnNode) insn, frame, block); else if (insn instanceof TypeInsnNode) interpret((TypeInsnNode) insn, frame, block); else if (insn instanceof VarInsnNode) interpret((VarInsnNode) insn, frame, block); }/*from w ww . j a v a 2 s . c o m*/ //If the block doesn't have a TerminatorInst, add a JumpInst to the //fallthrough block. (This occurs when blocks begin due to being a //jump target rather than due to a terminator opcode.) if (block.block.getTerminator() == null) block.block.instructions().add(new JumpInst(blocks.get(blocks.indexOf(block) + 1).block)); for (BasicBlock b : block.block.successors()) for (BBInfo bi : blocks) if (bi.block == b) { merge(block, frame, bi); break; } }
From source file:edu.mit.streamjit.util.bytecode.MethodResolver.java
License:Open Source License
private static AbstractInsnNode nextRealInstruction(AbstractInsnNode node) { assert node.getOpcode() == -1; while (node.getOpcode() == -1) node = node.getNext();/*from ww w.j a v a2 s . c om*/ return node; }
From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java
License:Open Source License
/** * Removes "xLOAD N xSTORE N".//from w w w .java 2 s .c om * @return true iff changes were made */ private boolean removeLoadStore() { InsnList insns = methodNode.instructions; for (int i = 0; i < insns.size() - 1; ++i) { AbstractInsnNode first = insns.get(i); int index = LOADS.indexOf(first.getOpcode()); if (index == -1) continue; AbstractInsnNode second = insns.get(i + 1); if (second.getOpcode() != STORES.get(index)) continue; if (((VarInsnNode) first).var != ((VarInsnNode) second).var) continue; insns.remove(first); insns.remove(second); return true; } return false; }