List of usage examples for org.objectweb.asm.tree AbstractInsnNode getPrevious
public AbstractInsnNode getPrevious()
From source file:de.tuberlin.uebb.jbop.optimizer.utils.NodeHelper.java
License:Open Source License
/** * returns the first Element of the "Stack" that produces the value, that is stored * by this node./* w w w . j a va2 s .c om*/ * * returns null if node is null. * returns null if node is not a store. * * @param node * the node * @return the first of stack */ public static AbstractInsnNode getFirstOfStack(final AbstractInsnNode node) { if (node == null) { return null; } int opcode = node.getOpcode(); if (opcode < ISTORE || opcode > ASTORE) { return null; } int stackCounter = 1; AbstractInsnNode prev = getPrevious(node); while (prev != null) { opcode = prev.getOpcode(); stackCounter = getStackCounter(opcode, stackCounter, prev); if (stackCounter == 0) { return prev; } prev = prev.getPrevious(); } return null; }
From source file:de.tuberlin.uebb.jbop.optimizer.utils.NodeHelperTest.java
License:Open Source License
@Test public void testGetFirstOfStack2() { // INIT/*w w w .ja v a2 s. co m*/ final ClassNodeBuilder builder = ClassNodeBuilder.createClass("de.tuberlin.uebb.example.Test").// addMethod("test", "(I)V").// add(ICONST_0).// add(ISTORE, 2).// add(ILOAD, 1).// add(ISTORE, 2).// addReturn(); final MethodNode method = builder.getMethod("test"); final AbstractInsnNode store = method.instructions.get(method.instructions.size() - 2); // RUN final AbstractInsnNode firstOfStack = NodeHelper.getFirstOfStack(store); // ASSERT assertEquals(store.getPrevious(), firstOfStack); }
From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInliner.java
License:Open Source License
private AbstractInsnNode hanldeLoad(final InsnList original, final AbstractInsnNode currentNode, final Map<Integer, Object> knownValues) { final int index = NodeHelper.getVarIndex(currentNode); if (knownValues.containsKey(index) && currentNode.getNext() != null && currentNode.getPrevious() != null) { final Object value = knownValues.get(index); final AbstractInsnNode replacement = NodeHelper.getInsnNodeFor(value); original.set(currentNode, replacement); optimized = true;/*from ww w. j a v a 2 s. c o m*/ return replacement; } return currentNode; }
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);/* w w w.j a v a 2s . 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: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.//w w w. j a v a 2s . com * @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.ubc.mirrors.holograms.FrameAnalyzer.java
License:Open Source License
public Object toFrameObject(FrameValue value) { if (value.isUninitializedThis()) { return Opcodes.UNINITIALIZED_THIS; } else if (value.isUninitialized()) { AbstractInsnNode newInsn = value.getNewInsn(); // TODO: Could also search up to the previous actual instruction... AbstractInsnNode labelNode = newInsn.getPrevious(); if (labelNode == null || !(labelNode instanceof LabelNode)) { labelNode = new LabelNode(); m.instructions.insertBefore(newInsn, labelNode); }/*from w w w . j ava 2 s. c o m*/ return ((LabelNode) labelNode).getLabel(); } else { Type type = value.getBasicValue().getType(); if (type == null) { return Opcodes.TOP; } else { switch (type.getSort()) { case Type.VOID: // TODO: Not sure what to do here. Used for BasicValue.RETURNADDRESS_VALUE, // but I can't figure out the correct verifier type for the return addresses // used by JSR/RET. return Opcodes.TOP; case Type.BOOLEAN: case Type.BYTE: case Type.CHAR: case Type.SHORT: case Type.INT: return Opcodes.INTEGER; case Type.LONG: return Opcodes.LONG; case Type.FLOAT: return Opcodes.FLOAT; case Type.DOUBLE: return Opcodes.DOUBLE; case Type.OBJECT: case Type.ARRAY: return type.getInternalName(); case Type.METHOD: // Shouldn't happen default: throw new RuntimeException("Bad sort: " + type.getSort()); } } } }
From source file:gemlite.core.internal.measurement.MeasureHelper.java
License:Apache License
public final static void instrumentCheckPoint(String className, MethodNode mn) { if (LogUtil.getCoreLog().isTraceEnabled()) LogUtil.getCoreLog().trace("Found check point, class:" + className + " method:" + mn.name); InsnList insn = mn.instructions;/*from ww w .java 2 s . co m*/ List<AbstractInsnNode> returnIndex = new ArrayList<>(); // return int localVarCount = mn.localVariables.size(); for (int i = 0; i < insn.size(); i++) { AbstractInsnNode insnNode = (AbstractInsnNode) insn.get(i); switch (insnNode.getOpcode()) { case Opcodes.ARETURN: case Opcodes.IRETURN: case Opcodes.DRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: returnIndex.add(insnNode.getPrevious()); break; case Opcodes.RETURN: returnIndex.add(insnNode); break; } } // insn.insert(new VarInsnNode(Opcodes.LSTORE, localVarCount + 2)); insn.insert( new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false)); // ? for (AbstractInsnNode insnNode : returnIndex) { insn.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false)); insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LSTORE, localVarCount + 4)); insn.insertBefore(insnNode, new LdcInsnNode(className)); insn.insertBefore(insnNode, new LdcInsnNode(mn.name)); insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 2)); insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 4)); insn.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "gemlite/core/internal/measurement/MeasureHelper", "recordCheckPoint", "(Ljava/lang/String;Ljava/lang/String;JJ)V", false)); } }
From source file:hellfirepvp.astralsorcery.core.patch.helper.PatchBlockModify.java
License:Open Source License
@Override public void patch(ClassNode cn) { MethodNode mn = getMethod(cn, "setBlockState", "func_177436_a", "(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;)Lnet/minecraft/block/state/IBlockState;"); for (int i = 0; i < mn.instructions.size(); i++) { AbstractInsnNode aNode = mn.instructions.get(i); if (aNode.getOpcode() == Opcodes.ARETURN) { AbstractInsnNode prev = aNode.getPrevious(); if (prev instanceof VarInsnNode && prev.getOpcode() == Opcodes.ALOAD && ((VarInsnNode) prev).var == 8) { mn.instructions.insertBefore(prev, new FieldInsnNode(Opcodes.GETSTATIC, "net/minecraftforge/common/MinecraftForge", "EVENT_BUS", "Lnet/minecraftforge/fml/common/eventhandler/EventBus;")); mn.instructions.insertBefore(prev, new TypeInsnNode(Opcodes.NEW, "hellfirepvp/astralsorcery/common/event/BlockModifyEvent")); mn.instructions.insertBefore(prev, new InsnNode(Opcodes.DUP)); mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 0)); //Chunk mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 1)); //Pos mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 8)); //OldState mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 2)); //NewState mn.instructions.insertBefore(prev, new MethodInsnNode(Opcodes.INVOKESPECIAL, "hellfirepvp/astralsorcery/common/event/BlockModifyEvent", "<init>", "(Lnet/minecraft/world/chunk/Chunk;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/block/state/IBlockState;)V", false));/*from w w w. j av a 2s . c o m*/ mn.instructions.insertBefore(prev, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraftforge/fml/common/eventhandler/EventBus", "post", "(Lnet/minecraftforge/fml/common/eventhandler/Event;)Z", false)); mn.instructions.insertBefore(prev, new InsnNode(Opcodes.POP)); return; } } } throw new ASMTransformationException( "Could not find the expected return statement in the setBlockState method!"); }
From source file:io.moshisho.plugins.MyMojo.java
License:Apache License
private static void betterCompile(String classFile) throws IOException { InputStream is = new FileInputStream(classFile); ClassReader cr = new ClassReader(is); is.close();/*from www . j a v a2 s.c om*/ ClassNode cn = new ClassNode(); cr.accept(cn, 0); List methods = cn.methods; for (int i = 0; i < methods.size(); ++i) { MethodNode method = (MethodNode) methods.get(i); if (!isStaticllyBound(method)) { continue; } InsnList instructions = method.instructions; AbstractInsnNode last = instructions.getLast(); while (last != null && last.getType() == AbstractInsnNode.LABEL) { last = last.getPrevious(); } if (last == null || !isReturnInstruction(last) || last.getPrevious().getType() != AbstractInsnNode.METHOD_INSN) { continue; } MethodInsnNode methodInv = (MethodInsnNode) last.getPrevious(); if (!isRecursionCall(cn, method, methodInv)) { continue; } System.out.println("TailRec Optimizaing: " + method.name); // get arguments and types String methodDesc = method.desc; String argsDesc = methodDesc.substring(methodDesc.indexOf('(') + 1, methodDesc.indexOf(')')); System.out.println(argsDesc); // work with Type.getArgumentTypes List<AbstractInsnNode> listInstNodes = new LinkedList<AbstractInsnNode>(); for (int j = argsDesc.length() - 1; j >= 0; j--) { char c = argsDesc.charAt(j); switch (c) { case 'I': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'Z': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'C': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'B': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'S': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'F': listInstNodes.add(new VarInsnNode(FSTORE, argsDesc.length() - j)); break; case 'J': listInstNodes.add(new VarInsnNode(LSTORE, argsDesc.length() - j)); break; case 'D': listInstNodes.add(new VarInsnNode(DSTORE, argsDesc.length() - j)); break; case '[': // TODO: case 'L': // TODO: default: System.out.println("NOT TREATING: " + c); } } // remove the last aload_0 of the recursion AbstractInsnNode pnt = last; while (pnt != null && pnt.getOpcode() != 42 && !(pnt.getOpcode() == 25 && ((VarInsnNode) pnt).var == 0)) { pnt = pnt.getPrevious(); } method.instructions.remove(pnt); Collections.reverse(listInstNodes); for (AbstractInsnNode abstractInsnNode : listInstNodes) { method.instructions.insertBefore(last.getPrevious(), abstractInsnNode); } // place instead of return //goto LabelNode startOfMethodLabel = new LabelNode(new Label()); method.instructions.insertBefore(method.instructions.getFirst(), startOfMethodLabel); JumpInsnNode gotoInst = new JumpInsnNode(GOTO, startOfMethodLabel); method.instructions.set(last.getPrevious(), gotoInst); method.instructions.remove(last); ClassWriter cw = new ClassWriter(0); cn.accept(cw); FileOutputStream fos = new FileOutputStream(classFile); fos.write(cw.toByteArray()); fos.close(); } }
From source file:io.moshisho.plugins.OptimizeClasses.java
License:Apache License
private void optimize(File clz) throws Exception { InputStream is = new FileInputStream(clz); ClassReader cr = new ClassReader(is); is.close();/*from w w w .j a v a2s .c o m*/ ClassNode cn = new ClassNode(); cr.accept(cn, 0); List methods = cn.methods; for (int i = 0; i < methods.size(); ++i) { MethodNode method = (MethodNode) methods.get(i); if (!isStaticllyBound(method)) { continue; } InsnList instructions = method.instructions; AbstractInsnNode last = instructions.getLast(); while (last != null && last.getType() == AbstractInsnNode.LABEL) { last = last.getPrevious(); } if (last == null || !isReturnInstruction(last) || last.getPrevious().getType() != AbstractInsnNode.METHOD_INSN) { continue; } MethodInsnNode methodInv = (MethodInsnNode) last.getPrevious(); if (!isRecursionCall(cn, method, methodInv)) { continue; } getLog().info("TailRec Optimizaing: " + method.name); // get arguments and types String methodDesc = method.desc; String argsDesc = methodDesc.substring(methodDesc.indexOf('(') + 1, methodDesc.indexOf(')')); //System.out.println(argsDesc); // work with Type.getArgumentTypes List<AbstractInsnNode> listInstNodes = new LinkedList<AbstractInsnNode>(); for (int j = argsDesc.length() - 1; j >= 0; j--) { char c = argsDesc.charAt(j); switch (c) { case 'I': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'Z': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'C': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'B': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'S': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'F': listInstNodes.add(new VarInsnNode(FSTORE, argsDesc.length() - j)); break; case 'J': listInstNodes.add(new VarInsnNode(LSTORE, argsDesc.length() - j)); break; case 'D': listInstNodes.add(new VarInsnNode(DSTORE, argsDesc.length() - j)); break; case '[': // TODO: case 'L': // TODO: default: System.out.println("NOT TREATING: " + c); } } // remove the last aload_0 of the recursion AbstractInsnNode pnt = last; while (pnt != null && pnt.getOpcode() != 42 && !(pnt.getOpcode() == 25 && ((VarInsnNode) pnt).var == 0)) { pnt = pnt.getPrevious(); } method.instructions.remove(pnt); Collections.reverse(listInstNodes); for (AbstractInsnNode abstractInsnNode : listInstNodes) { method.instructions.insertBefore(last.getPrevious(), abstractInsnNode); } // place instead of return //goto LabelNode startOfMethodLabel = new LabelNode(new Label()); method.instructions.insertBefore(method.instructions.getFirst(), startOfMethodLabel); JumpInsnNode gotoInst = new JumpInsnNode(GOTO, startOfMethodLabel); method.instructions.set(last.getPrevious(), gotoInst); method.instructions.remove(last); ClassWriter cw = new ClassWriter(0); cn.accept(cw); FileOutputStream fos = new FileOutputStream(clz); fos.write(cw.toByteArray()); fos.close(); } }