Search in sources :

Example 31 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project soot by Sable.

the class AsmMethodSource method convertLookupSwitchInsn.

private void convertLookupSwitchInsn(LookupSwitchInsnNode insn) {
    StackFrame frame = getFrame(insn);
    if (units.containsKey(insn)) {
        frame.mergeIn(pop());
        return;
    }
    Operand key = popImmediate();
    UnitBox dflt = Jimple.v().newStmtBox(null);
    List<UnitBox> targets = new ArrayList<UnitBox>(insn.labels.size());
    labels.put(insn.dflt, dflt);
    for (LabelNode ln : insn.labels) {
        UnitBox box = Jimple.v().newStmtBox(null);
        targets.add(box);
        labels.put(ln, box);
    }
    List<IntConstant> keys = new ArrayList<IntConstant>(insn.keys.size());
    for (Integer i : insn.keys) keys.add(IntConstant.v(i));
    LookupSwitchStmt lss = Jimple.v().newLookupSwitchStmt(key.stackOrValue(), keys, targets, dflt);
    key.addBox(lss.getKeyBox());
    frame.in(key);
    frame.boxes(lss.getKeyBox());
    setUnit(insn, lss);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) UnitBox(soot.UnitBox) ArrayList(java.util.ArrayList) IntConstant(soot.jimple.IntConstant) LookupSwitchStmt(soot.jimple.LookupSwitchStmt)

Example 32 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project CodeChickenLib by Chicken-Bones.

the class ASMBlock method applyLabels.

public ASMBlock applyLabels(InsnListSection list2) {
    if (labels.isEmpty())
        return new ASMBlock(list2);
    Set<LabelNode> cFlowLabels1 = labels.values();
    Set<LabelNode> cFlowLabels2 = InsnComparator.getControlFlowLabels(list2);
    ASMBlock block = new ASMBlock(list2);
    HashMap<LabelNode, LabelNode> labelMap = new HashMap<LabelNode, LabelNode>();
    for (int i = 0, k = 0; i < list.size() && k < list2.size(); ) {
        AbstractInsnNode insn1 = list.get(i);
        if (!InsnComparator.insnImportant(insn1, cFlowLabels1)) {
            i++;
            continue;
        }
        AbstractInsnNode insn2 = list2.get(k);
        if (!InsnComparator.insnImportant(insn2, cFlowLabels2)) {
            k++;
            continue;
        }
        if (insn1.getOpcode() != insn2.getOpcode())
            throw new IllegalArgumentException("Lists do not match:\n" + list + "\n\n" + list2);
        switch(insn1.getType()) {
            case LABEL:
                labelMap.put((LabelNode) insn1, (LabelNode) insn2);
                break;
            case JUMP_INSN:
                labelMap.put(((JumpInsnNode) insn1).label, ((JumpInsnNode) insn2).label);
                break;
        }
        i++;
        k++;
    }
    for (Entry<String, LabelNode> entry : labels.entrySet()) block.labels.put(entry.getKey(), labelMap.get(entry.getValue()));
    return block;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 33 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project spring-loaded by spring-projects.

the class TypeDiffComputer method sameTableSwitchInsn.

@SuppressWarnings("unchecked")
private static boolean sameTableSwitchInsn(AbstractInsnNode o, AbstractInsnNode n) {
    if (!(n instanceof TableSwitchInsnNode)) {
        return false;
    }
    TableSwitchInsnNode tsio = (TableSwitchInsnNode) o;
    TableSwitchInsnNode tsin = (TableSwitchInsnNode) n;
    if (sameLabels(tsio.dflt, tsin.dflt)) {
        return false;
    }
    if (tsio.min != tsin.min) {
        return false;
    }
    if (tsio.max != tsin.max) {
        return false;
    }
    List<LabelNode> labelso = tsio.labels;
    List<LabelNode> labelsn = tsin.labels;
    if (labelso.size() != labelsn.size()) {
        return false;
    }
    for (int i = 0, max = labelso.size(); i < max; i++) {
        if (!sameLabelNode(labelso.get(i), labelsn.get(i))) {
            return false;
        }
    }
    return true;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode)

Example 34 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project maple-ir by LLVM-but-worse.

the class ControlFlowGraphDumper method dump.

public void dump() {
    // Clear methodnode
    m.instructions.removeAll(true);
    m.tryCatchBlocks.clear();
    m.visitCode();
    for (BasicBlock b : cfg.vertices()) {
        b.resetLabel();
    }
    // Linearize
    linearize();
    // Fix edges
    naturalise();
    // Sanity check linearization
    verifyOrdering();
    // Dump code
    for (BasicBlock b : order) {
        m.visitLabel(b.getLabel());
        for (Stmt stmt : b) {
            stmt.toCode(m, null);
        }
    }
    terminalLabel = new LabelNode();
    m.visitLabel(terminalLabel.getLabel());
    // Dump ranges
    for (ExceptionRange<BasicBlock> er : cfg.getRanges()) {
        dumpRange(er);
    }
    // Sanity check
    verifyRanges();
    m.visitEnd();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) BasicBlock(org.mapleir.ir.cfg.BasicBlock) UnconditionalJumpStmt(org.mapleir.ir.code.stmt.UnconditionalJumpStmt) Stmt(org.mapleir.ir.code.Stmt)

Example 35 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project maple-ir by LLVM-but-worse.

the class SSAGenPass method splitBlock.

private BasicBlock splitBlock(BasicBlock b, int to) {
    /* eg. split the block as follows:
		 * 
		 *  NAME:
		 *    stmt1
		 *    stmt2
		 *    stmt3
		 *    stmt4
		 *    stmt5
		 *    jump L1, L2
		 *   [jump edge to L1]
		 *   [jump edge to L2]
		 *   [exception edges]
		 * 
		 * split at 3, create a new block (incoming 
		 * immediate), transfer instruction from 0
		 * to index into new block, create immediate
		 * edge to old block, clone exception edges,
		 * redirect pred edges.
		 * 
		 * 1/9/16: we also need to modify the last
		 *         statement of the pred blocks to
		 *         point to NAME'.
		 * 
		 *  NAME':
		 *    stmt1
		 *    stmt2
		 *    stmt3
		 *   [immediate to NAME]
		 *  NAME:
		 *    stmt4
		 *    stmt5
		 *    jump L1, L2
		 *   [jump edge to L1]
		 *   [jump edge to L2]
		 *   [exception edges]
		 */
    // split block
    ControlFlowGraph cfg = builder.graph;
    BasicBlock newBlock = new BasicBlock(cfg, graphSize++, new LabelNode());
    b.transferUp(newBlock, to);
    cfg.addVertex(newBlock);
    // redo ranges
    for (ExceptionRange<BasicBlock> er : cfg.getRanges()) {
        if (er.containsVertex(b))
            er.addVertexBefore(b, newBlock);
    }
    // redirect b preds into newBlock and remove them.
    Set<FlowEdge<BasicBlock>> oldEdges = new HashSet<>(cfg.getReverseEdges(b));
    for (FlowEdge<BasicBlock> e : oldEdges) {
        BasicBlock p = e.src();
        FlowEdge<BasicBlock> c;
        if (e instanceof TryCatchEdge) {
            // b is ehandler
            TryCatchEdge<BasicBlock> tce = (TryCatchEdge<BasicBlock>) e;
            if (tce.dst() != tce.erange.getHandler()) {
                System.err.println(builder.method.owner + "#" + builder.method.name);
                System.err.println(cfg);
                System.err.println("Very odd split case. please investigate");
                System.err.println("Offending postsplit block: " + b);
                System.err.println("Offending newblock: " + newBlock);
                System.err.println("Offending edge: " + tce);
                System.err.println("Offending erange: " + tce.erange);
            }
            if (tce.erange.getHandler() != newBlock) {
                tce.erange.setHandler(newBlock);
                cfg.addEdge(tce.src(), tce.clone(tce.src(), null));
                cfg.removeEdge(tce.src(), tce);
            }
        } else {
            c = e.clone(p, newBlock);
            cfg.addEdge(p, c);
            cfg.removeEdge(p, e);
        }
        // Fix flow instruction targets
        if (!p.isEmpty()) {
            Stmt last = p.get(p.size() - 1);
            int op = last.getOpcode();
            if (e instanceof ConditionalJumpEdge) {
                if (op != Opcode.COND_JUMP)
                    throw new IllegalArgumentException("wrong flow instruction");
                ConditionalJumpStmt j = (ConditionalJumpStmt) last;
                // assertTarget(last, j.getTrueSuccessor(), b);
                if (j.getTrueSuccessor() == b)
                    j.setTrueSuccessor(newBlock);
            } else if (e instanceof UnconditionalJumpEdge) {
                if (op != Opcode.UNCOND_JUMP)
                    throw new IllegalArgumentException("wrong flow instruction");
                UnconditionalJumpStmt j = (UnconditionalJumpStmt) last;
                assertTarget(j, j.getTarget(), b);
                j.setTarget(newBlock);
            } else if (e instanceof SwitchEdge) {
                if (op != Opcode.SWITCH_JUMP)
                    throw new IllegalArgumentException("wrong flow instruction.");
                SwitchStmt s = (SwitchStmt) last;
                for (Entry<Integer, BasicBlock> en : s.getTargets().entrySet()) {
                    BasicBlock t = en.getValue();
                    if (t == b) {
                        en.setValue(newBlock);
                    }
                }
            }
        }
    }
    if (!checkCloneHandler(newBlock)) {
        System.err.println(cfg);
        System.err.println(newBlock.getDisplayName());
        System.err.println(b.getDisplayName());
        throw new IllegalStateException("the new block should always need a handler..?");
    }
    // clone exception edges
    for (FlowEdge<BasicBlock> e : cfg.getEdges(b)) {
        if (e.getType() == FlowEdges.TRYCATCH) {
            // second param is discarded (?)
            TryCatchEdge<BasicBlock> c = ((TryCatchEdge<BasicBlock>) e).clone(newBlock, null);
            cfg.addEdge(newBlock, c);
        }
    }
    // create immediate to newBlock
    cfg.addEdge(newBlock, new ImmediateEdge<>(newBlock, b));
    // update assigns
    Set<Local> assignedLocals = new HashSet<>();
    for (Stmt stmt : b) if (stmt.getOpcode() == Opcode.LOCAL_STORE)
        assignedLocals.add(((CopyVarStmt) stmt).getVariable().getLocal());
    for (Stmt stmt : newBlock) {
        if (stmt.getOpcode() == Opcode.LOCAL_STORE) {
            Local copyLocal = ((CopyVarStmt) stmt).getVariable().getLocal();
            Set<BasicBlock> set = builder.assigns.get(copyLocal);
            set.add(newBlock);
            if (!assignedLocals.contains(copyLocal))
                set.remove(b);
        }
    }
    return newBlock;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TryCatchEdge(org.mapleir.flowgraph.edges.TryCatchEdge) SwitchStmt(org.mapleir.ir.code.stmt.SwitchStmt) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) PopStmt(org.mapleir.ir.code.stmt.PopStmt) ThrowStmt(org.mapleir.ir.code.stmt.ThrowStmt) UnconditionalJumpStmt(org.mapleir.ir.code.stmt.UnconditionalJumpStmt) Stmt(org.mapleir.ir.code.Stmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) ConditionalJumpStmt(org.mapleir.ir.code.stmt.ConditionalJumpStmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) UnconditionalJumpEdge(org.mapleir.flowgraph.edges.UnconditionalJumpEdge) FlowEdge(org.mapleir.flowgraph.edges.FlowEdge) SwitchStmt(org.mapleir.ir.code.stmt.SwitchStmt) SwitchEdge(org.mapleir.flowgraph.edges.SwitchEdge) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) BasicLocal(org.mapleir.ir.locals.impl.BasicLocal) Local(org.mapleir.ir.locals.Local) VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) Constraint(org.mapleir.ir.cfg.builder.ssaopt.Constraint) UnconditionalJumpStmt(org.mapleir.ir.code.stmt.UnconditionalJumpStmt) ConditionalJumpStmt(org.mapleir.ir.code.stmt.ConditionalJumpStmt) ConditionalJumpEdge(org.mapleir.flowgraph.edges.ConditionalJumpEdge) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph)

Aggregations

LabelNode (org.objectweb.asm.tree.LabelNode)89 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)37 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)32 Label (org.objectweb.asm.Label)28 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)23 InsnList (org.objectweb.asm.tree.InsnList)22 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)21 InsnNode (org.objectweb.asm.tree.InsnNode)20 MethodNode (org.objectweb.asm.tree.MethodNode)19 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)17 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)16 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)15 ClassNode (org.objectweb.asm.tree.ClassNode)14 ClassReader (org.objectweb.asm.ClassReader)12 LineNumberNode (org.objectweb.asm.tree.LineNumberNode)10 Type (org.objectweb.asm.Type)8 LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)8 LinkedList (java.util.LinkedList)6 ArrayList (java.util.ArrayList)5 LookupSwitchInsnNode (org.objectweb.asm.tree.LookupSwitchInsnNode)5