Example usage for org.objectweb.asm.tree AbstractInsnNode getNext

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getNext

Introduction

In this page you can find the example usage for org.objectweb.asm.tree AbstractInsnNode getNext.

Prototype

public AbstractInsnNode getNext() 

Source Link

Document

Returns the next instruction in the list to which this instruction belongs, if any.

Usage

From source file:pxb.android.dex2jar.optimize.B.java

License:Apache License

/**
 * <pre>//from  w w w.j a  v a2  s .  co m
 * BEFORE:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     ASTORE 1
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     ASTORE 2
 *     ALOAD 1
 *     ALOAD 2
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ASTORE 0
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * <pre>
 * AFTER:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ASTORE 0
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * @param block
 */
private void doLdc(Block block) {
    Map<Integer, LdcInsnNode> map = new HashMap<Integer, LdcInsnNode>();
    AbstractInsnNode p = block.first.getNext();
    while (p != null && p != block.last) {
        if (p.getOpcode() == Opcodes.LDC) {
            AbstractInsnNode q = p.getNext();
            if (isWrite(q)) {
                Integer var = var(q);
                if (block.out.get(var) == null || block.out.get(var) != q) {
                    map.put(var, (LdcInsnNode) p);
                    insnList.remove(q); // remove store
                    q = p.getPrevious();
                    insnList.remove(p); // remove ldc
                    p = q;
                }
            }
        } else if (isRead(p)) {
            Integer var = var(p);
            if (block.out.get(var) == null || block.out.get(var) != p) {
                LdcInsnNode ldc = map.get(var);
                if (ldc != null) {
                    AbstractInsnNode _ldc_copy = ldc.clone(null);
                    insnList.insert(p, _ldc_copy);
                    insnList.remove(p);
                    p = _ldc_copy;
                }
            }
        } else if (isWrite(p)) {
            Integer var = var(p);
            map.remove(var);
        }
        p = p.getNext();
    }
}

From source file:pxb.android.dex2jar.optimize.B.java

License:Apache License

/**
 * /*from w w  w.  j  ava 2s . co m*/
 * 
 * NEWINVOKESPECIAL??
 * 
 * <pre>
 * BEFORE:
 *     NEW Ljava/util/PropertyResourceBundle;
 *     ASTORE 0
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     ASTORE 1
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     ASTORE 2
 *     ALOAD 1
 *     ALOAD 2
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     ALOAD 0
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * 
 * <pre>
 * AFTER:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     ASTORE 1
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     ASTORE 2
 *     ALOAD 1
 *     ALOAD 2
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ASTORE 0
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * @param block
 */
private void doNew(Block block) {
    Map<String, AbstractInsnNode> map = new HashMap<String, AbstractInsnNode>();
    AbstractInsnNode p = block.first.getNext();
    while (p != null && p != block.last) {
        switch (p.getOpcode()) {
        case Opcodes.NEW: {
            AbstractInsnNode store = p.getNext();
            if (store instanceof VarInsnNode) {
                map.put(((TypeInsnNode) p).desc + var(store), p);
                p = store.getNext();
            } else {
                p = store;
            }
            break;
        }
        case Opcodes.INVOKESPECIAL: {
            MethodInsnNode m = (MethodInsnNode) p;
            p = p.getNext();
            if (m.name.equals("<init>")) {
                int length = Type.getArgumentTypes(m.desc).length;
                AbstractInsnNode q = m.getPrevious();
                while (length-- > 0) {
                    q = q.getPrevious();
                }
                AbstractInsnNode _new = map.remove(m.owner + var(q));
                if (_new != null) {
                    AbstractInsnNode _store = _new.getNext();
                    insnList.remove(_new);// remove new
                    insnList.remove(_store); // remove store
                    insnList.insertBefore(q, _new);
                    insnList.insert(_new, new InsnNode(DUP));
                    insnList.remove(q);
                    insnList.insert(m, _store);
                }
            }
            break;
        }
        default:
            p = p.getNext();
        }
    }
}

From source file:pxb.android.dex2jar.optimize.B.java

License:Apache License

private boolean doOptmizeFirstBlockLdc(LdcInsnNode node, int r, Block block,
        Map<Block, Boolean> couldReplaceBlockIds, Set<Block> replacedBlockIds) {
    if (replacedBlockIds.contains(block)) {
        return true;
    }/*w  w  w.java2s . c o m*/
    replacedBlockIds.add(block);
    if (couldReplace(r, block, couldReplaceBlockIds)) {
        AbstractInsnNode p = block.first.getNext();
        while (p != null && p != block.last) {
            if (isRead(p)) {
                int var = var(p);
                if (r == var) {
                    LdcInsnNode nLdc = (LdcInsnNode) node.clone(null);
                    AbstractInsnNode q = p.getNext();
                    insnList.remove(p);
                    insnList.insertBefore(q, nLdc);
                    p = q;
                    continue;
                }
            } else if (isWrite(p)) {
                int var = var(p);
                if (r == var) {
                    break;
                }
            }
            p = p.getNext();
        }
    } else {
        return false;
    }
    boolean remove = true;
    if (!block.out.containsKey(r)) {
        for (Block subBlockId : block.tos) {
            boolean x = doOptmizeFirstBlockLdc(node, r, subBlockId, couldReplaceBlockIds, replacedBlockIds);
            if (!x) {
                remove = false;
            }
        }
    }
    return remove;
}

From source file:pxb.android.dex2jar.optimize.B.java

License:Apache License

/**
 * <pre>//from   w  w  w  .j  a  v a2  s  . co  m
 * BEFORE:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ASTORE 0
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * <pre>
 * AFTER:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * @param block
 */
private void doVar(Block block) {
    AbstractInsnNode p = block.first.getNext();
    while (p != null && p != block.last) {
        if (isWrite(p)) {
            AbstractInsnNode q = p.getNext();
            if (isRead(q)) {
                if (isSameVar(p, q)) {
                    int var = var(p);
                    boolean canDel = true;
                    for (AbstractInsnNode i = q.getNext(); i != null && i != block.last; i = i.getNext()) {
                        if (isRead(i) && var == var(i)) {
                            canDel = false;
                            break;
                        }
                        if (isWrite(i) && var == var(i)) {
                            canDel = true;
                            break;
                        }
                    }
                    if (canDel && block.out.get(var) != p) {
                        AbstractInsnNode t = q.getNext();
                        insnList.remove(p);
                        insnList.remove(q);
                        p = t.getPrevious();
                    }
                }
            }
        }
        p = p.getNext();
    }
}

From source file:pxb.android.dex2jar.optimize.B.java

License:Apache License

/**
 * @param block//  w  w  w. j a  va 2  s.  c  o  m
 */
private void OptmizeFirstBlockLdc() {
    Block block = this.blocks.get(0);
    Map<Integer, LdcInsnNode> map = new HashMap<Integer, LdcInsnNode>();
    AbstractInsnNode p = block.first.getNext();
    while (p != null && p != block.last) {
        if (p.getOpcode() == Opcodes.LDC) {
            AbstractInsnNode q = p.getNext();
            if (isWrite(q)) {
                int var = var(q);
                if (block.out.get(var) == q) {
                    Map<Block, Boolean> couldReplace = new HashMap<Block, Boolean>();
                    Set<Block> replacedBlock = new HashSet<Block>();
                    replacedBlock.add(block);
                    couldReplace.put(block, true);
                    LdcInsnNode ldc = (LdcInsnNode) p;
                    boolean remove = true;
                    for (Block subBlock : block.tos) {
                        boolean x = doOptmizeFirstBlockLdc(ldc, var, subBlock, couldReplace, replacedBlock);
                        if (!x) {
                            remove = false;
                        }
                    }
                    if (remove) {
                        insnList.remove(p);
                        p = q.getNext();
                        insnList.remove(q);
                        map.put(var, ldc);
                        continue;
                    }

                }
            }
        } else if (isRead(p)) {
            int var = var(p);
            LdcInsnNode ldc = map.get(var);
            if (ldc != null) {
                AbstractInsnNode q = p.getNext();
                insnList.remove(p);
                insnList.insertBefore(q, ldc.clone(null));
                p = q;
                continue;
            }
        }
        p = p.getNext();
    }
}

From source file:pxb.android.dex2jar.optimize.D.java

License:Apache License

private static LabelNode getNextLabelNode(AbstractInsnNode p, InsnList insns) {
    AbstractInsnNode q = p.getNext();
    if (q.getType() == AbstractInsnNode.LABEL) {
        return (LabelNode) q;
    } else {//www  .j  a  v a  2s .  co m
        LabelNode r = new LabelNode();
        insns.insert(p, r);
        return r;
    }
}

From source file:pxb.android.dex2jar.optimize.D.java

License:Apache License

private void cut(MethodNode method) {
    InsnList insns = method.instructions;
    method.instructions = null;/*from w w w .  jav  a  2s. c o  m*/
    {
        AbstractInsnNode p = insns.getFirst();
        if (!(p instanceof LabelNode)) {
            insns.insertBefore(p, new LabelNode());
        }
        AbstractInsnNode q = insns.getLast();
        if (!(q instanceof LabelNode)) {
            insns.insert(q, new LabelNode());
        }
    }
    @SuppressWarnings("serial")
    Map<LabelNode, LabelNode> cloneMap = new HashMap<LabelNode, LabelNode>() {
        public LabelNode get(Object key) {

            LabelNode l = super.get(key);
            if (l == null) {
                l = new LabelNode();
                put((LabelNode) key, l);
            }
            return l;
        }
    };
    Map<LabelNode, Block> preBlockMap = new HashMap<LabelNode, Block>();
    int i = 0;
    LabelNode label = null;
    Block block = null;
    List<AbstractInsnNode> currentInsnList = null;
    for (AbstractInsnNode p = insns.getFirst(); p != null; p = p.getNext()) {
        final AbstractInsnNode cp = p.clone(cloneMap);
        switch (cp.getType()) {
        case AbstractInsnNode.LABEL: {
            if (label != null) {
                block = new Block(i++, label);
                block.insns = currentInsnList;
                block.next = (LabelNode) cp;
                addToMap(block);
            }
            currentInsnList = new ArrayList<AbstractInsnNode>();
            label = (LabelNode) cp;
            preBlockMap.put(label, block);
            break;
        }
        case AbstractInsnNode.JUMP_INSN:
        case AbstractInsnNode.LOOKUPSWITCH_INSN:
        case AbstractInsnNode.TABLESWITCH_INSN: {
            if (cp.getOpcode() == GOTO) {
                block = new Block(i++, label);
                block.next = (LabelNode) ((JumpInsnNode) cp).label;
            } else {//
                block = new BranchBlock(i++, label, cp);
                block.next = (LabelNode) getNextLabelNode(p, insns).clone(cloneMap);
            }
            block.insns = currentInsnList;
            addToMap(block);
            currentInsnList = null;
            label = null;
            break;
        }
        case AbstractInsnNode.FRAME:
        case AbstractInsnNode.LINE:
            // ignore
            break;
        default:
            switch (cp.getOpcode()) {
            case IRETURN:
            case LRETURN:
            case FRETURN:
            case DRETURN:
            case ARETURN:
            case RETURN:
            case ATHROW:
                block = new EndBlock(i++, label, cp);
                block.next = null;
                getNextLabelNode(p, insns);
                block.insns = currentInsnList;
                addToMap(block);
                currentInsnList = null;
                label = null;
                break;
            default:
                currentInsnList.add(cp);
            }

        }
    }

    for (Iterator<?> it = method.tryCatchBlocks.iterator(); it.hasNext();) {
        TryCatchBlockNode tcn = (TryCatchBlockNode) it.next();

        Block s = map.get((LabelNode) tcn.start.clone(cloneMap));
        Block e = map.get((LabelNode) tcn.end.clone(cloneMap));
        Block handler = map.get(tcn.handler.clone(cloneMap));
        TcbK key = new TcbK(s, e);

        Map<Block, String> handlers = tcbs.get(key);
        if (handlers == null) {
            handlers = new TreeMap<Block, String>(new Comparator<Block>() {
                @Override
                public int compare(Block o1, Block o2) {
                    return o1.id - o2.id;
                }
            });
            tcbs.put(key, handlers);
        }
        handlers.put(handler, tcn.type);
        tcn.start = s.label;
        tcn.end = e.label;
        tcn.handler = handler.label;
    }
}

From source file:soot.asm.AsmMethodSource.java

License:Open Source License

private void convert() {
    ArrayDeque<Edge> worklist = new ArrayDeque<Edge>();
    for (LabelNode ln : trapHandlers.keySet())
        worklist.add(new Edge(ln, new ArrayList<Operand>()));
    worklist.add(new Edge(instructions.getFirst(), new ArrayList<Operand>()));
    conversionWorklist = worklist;//from www.  j a v  a 2  s  .  c om
    edges = HashBasedTable.create(1, 1);
    do {
        Edge edge = worklist.pollLast();
        AbstractInsnNode insn = edge.insn;
        stack = edge.stack;
        edge.stack = null;
        do {
            int type = insn.getType();
            if (type == FIELD_INSN) {
                convertFieldInsn((FieldInsnNode) insn);
            } else if (type == IINC_INSN) {
                convertIincInsn((IincInsnNode) insn);
            } else if (type == INSN) {
                convertInsn((InsnNode) insn);
                int op = insn.getOpcode();
                if ((op >= IRETURN && op <= RETURN) || op == ATHROW) {
                    break;
                }
            } else if (type == INT_INSN) {
                convertIntInsn((IntInsnNode) insn);
            } else if (type == LDC_INSN) {
                convertLdcInsn((LdcInsnNode) insn);
            } else if (type == JUMP_INSN) {
                JumpInsnNode jmp = (JumpInsnNode) insn;
                convertJumpInsn(jmp);
                int op = jmp.getOpcode();
                if (op == JSR)
                    throw new UnsupportedOperationException("JSR!");
                if (op != GOTO) {
                    /* ifX opcode, i.e. two successors */
                    AbstractInsnNode next = insn.getNext();
                    addEdges(insn, next, Collections.singletonList(jmp.label));
                } else {
                    addEdges(insn, jmp.label, null);
                }
                break;
            } else if (type == LOOKUPSWITCH_INSN) {
                LookupSwitchInsnNode swtch = (LookupSwitchInsnNode) insn;
                convertLookupSwitchInsn(swtch);
                LabelNode dflt = swtch.dflt;
                addEdges(insn, dflt, swtch.labels);
                break;
            } else if (type == METHOD_INSN) {
                convertMethodInsn((MethodInsnNode) insn);
            } else if (type == MULTIANEWARRAY_INSN) {
                convertMultiANewArrayInsn((MultiANewArrayInsnNode) insn);
            } else if (type == TABLESWITCH_INSN) {
                TableSwitchInsnNode swtch = (TableSwitchInsnNode) insn;
                convertTableSwitchInsn(swtch);
                LabelNode dflt = swtch.dflt;
                addEdges(insn, dflt, swtch.labels);
            } else if (type == TYPE_INSN) {
                convertTypeInsn((TypeInsnNode) insn);
            } else if (type == VAR_INSN) {
                if (insn.getOpcode() == RET)
                    throw new UnsupportedOperationException("RET!");
                convertVarInsn((VarInsnNode) insn);
            } else if (type == LABEL) {
                convertLabel((LabelNode) insn);
            } else if (type == LINE || type == FRAME) {
                // we can ignore it
            } else
                throw new RuntimeException("Unknown instruction type: " + type);
        } while ((insn = insn.getNext()) != null);
    } while (!worklist.isEmpty());
    conversionWorklist = null;
    edges = null;
}

From source file:soot.asm.AsmMethodSource.java

License:Open Source License

private void emitUnits() {
    AbstractInsnNode insn = instructions.getFirst();
    ArrayDeque<LabelNode> labls = new ArrayDeque<LabelNode>();
    while (insn != null) {
        // Save the label to assign it to the next real unit
        if (insn instanceof LabelNode)
            labls.add((LabelNode) insn);

        // Get the unit associated with the current instruction
        Unit u = units.get(insn);/*w  ww  . j  av a 2s . c  om*/
        if (u == null) {
            insn = insn.getNext();
            continue;
        }

        emitUnits(u);

        // If this is an exception handler, register the starting unit for it
        if (insn instanceof LabelNode && u instanceof IdentityStmt
                && ((IdentityStmt) u).getRightOp() instanceof CaughtExceptionRef) {
            // We directly place this label
            Collection<UnitBox> traps = trapHandlers.get((LabelNode) insn);
            for (UnitBox ub : traps)
                ub.setUnit(u);
        }

        // Register this unit for all targets of the labels ending up at it
        while (!labls.isEmpty()) {
            LabelNode ln = labls.poll();
            Collection<UnitBox> boxes = labels.get(ln);
            if (boxes != null) {
                for (UnitBox box : boxes) {
                    Unit uu = u;
                    while (uu instanceof UnitContainer)
                        uu = ((UnitContainer) u).units[0];
                    box.setUnit(uu);
                }
            }
        }
        insn = insn.getNext();
    }
    /* set remaining labels & boxes to last unit of chain */
    if (labls.isEmpty())
        return;
    Unit end = Jimple.v().newNopStmt();
    body.getUnits().add(end);
    while (!labls.isEmpty()) {
        LabelNode ln = labls.poll();
        Collection<UnitBox> boxes = labels.get(ln);
        if (boxes != null) {
            for (UnitBox box : boxes)
                box.setUnit(end);
        }
    }
}

From source file:soot.asm.JimpleSource.java

License:Open Source License

private void emitUnits() {
    AbstractInsnNode insn = instructions.getFirst();
    ArrayDeque<LabelNode> labls = new ArrayDeque<LabelNode>();
    while (insn != null) {
        if (insn instanceof LabelNode)
            labls.add((LabelNode) insn);
        Unit u = units.get(insn);/*from  w  ww. ja  v a2 s.com*/
        if (u != null) {
            emitUnits(u);
            if (u instanceof IdentityStmt && ((IdentityStmt) u).getRightOp() instanceof CaughtExceptionRef) {
                //FIXME not sure if this cast is always correct...
                for (UnitBox ub : trapHandlers.get((LabelNode) insn))
                    ub.setUnit(u);
            } else {
                while (!labls.isEmpty()) {
                    LabelNode ln = labls.poll();
                    Collection<UnitBox> boxes = labels.get(ln);
                    if (boxes != null) {
                        for (UnitBox box : boxes) {
                            Unit uu = u;
                            while (uu instanceof UnitContainer)
                                uu = ((UnitContainer) u).units[0];
                            box.setUnit(uu);
                        }
                    }
                }
            }
        }
        insn = insn.getNext();
    }
    /* set remaining labels & boxes to last unit of chain */
    if (labls.isEmpty())
        return;
    Unit end = Jimple.v().newNopStmt();
    body.getUnits().add(end);
    while (!labls.isEmpty()) {
        LabelNode ln = labls.poll();
        Collection<UnitBox> boxes = labels.get(ln);
        if (boxes != null) {
            for (UnitBox box : boxes)
                box.setUnit(end);
        }
    }
}