Search in sources :

Example 66 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class GenerationPass method handler.

protected void handler(TryCatchBlockNode tc) {
    LabelNode label = tc.handler;
    BasicBlock handler = resolveTarget(label);
    marks.add(tc.start);
    marks.add(tc.end);
    if (getInputStackFor(handler) != null) {
        // System.err.println("Double handler: " + handler.getId() + " " + tc);
        return;
    }
    ExpressionStack stack = new ExpressionStack(16);
    setInputStack(handler, stack);
    CaughtExceptionExpr expr = new CaughtExceptionExpr(tc.type);
    Type type = expr.getType();
    VarExpr var = _var_expr(0, type, true);
    CopyVarStmt stmt = copy(var, expr, handler);
    handler.add(stmt);
    stack.push(load_stack(0, type));
    queue(label);
    stacks.set(handler.getNumericId());
}
Also used : Type(org.objectweb.asm.Type) ComparisonType(org.mapleir.ir.code.stmt.ConditionalJumpStmt.ComparisonType) ValueComparisonType(org.mapleir.ir.code.expr.ComparisonExpr.ValueComparisonType) ArrayType(org.mapleir.ir.TypeUtils.ArrayType) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) ExpressionStack(org.mapleir.ir.code.ExpressionStack)

Example 67 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class NaturalisationPass1 method mergeImmediates.

int mergeImmediates() {
    class MergePair {

        final BasicBlock src;

        final BasicBlock dst;

        MergePair(BasicBlock src, BasicBlock dst) {
            this.src = src;
            this.dst = dst;
        }
    }
    List<MergePair> merges = new ArrayList<>();
    Map<BasicBlock, BasicBlock> remap = new HashMap<>();
    Map<BasicBlock, List<ExceptionRange<BasicBlock>>> ranges = new HashMap<>();
    for (BasicBlock b : builder.graph.vertices()) {
        BasicBlock in = b.getIncomingImmediate();
        if (in == null) {
            continue;
        }
        if (in.isFlagSet(BasicBlock.FLAG_NO_MERGE)) {
            continue;
        }
        Set<FlowEdge<BasicBlock>> inSuccs = in.getSuccessors(e -> !(e instanceof TryCatchEdge));
        if (inSuccs.size() != 1 || builder.graph.getReverseEdges(b).size() != 1) {
            continue;
        }
        List<ExceptionRange<BasicBlock>> range1 = b.getProtectingRanges();
        List<ExceptionRange<BasicBlock>> range2 = in.getProtectingRanges();
        if (!range1.equals(range2)) {
            continue;
        }
        ranges.put(b, range1);
        ranges.put(in, range2);
        merges.add(new MergePair(in, b));
        remap.put(in, in);
        remap.put(b, b);
    }
    for (MergePair p : merges) {
        BasicBlock src = remap.get(p.src);
        BasicBlock dst = p.dst;
        dst.transfer(src);
        for (FlowEdge<BasicBlock> e : builder.graph.getEdges(dst)) {
            // to clone these.
            if (e.getType() != FlowEdges.TRYCATCH) {
                BasicBlock edst = e.dst();
                edst = remap.getOrDefault(edst, edst);
                builder.graph.addEdge(src, e.clone(src, edst));
            }
        }
        builder.graph.removeVertex(dst);
        remap.put(dst, src);
        for (ExceptionRange<BasicBlock> r : ranges.get(src)) {
            r.removeVertex(dst);
        }
        for (ExceptionRange<BasicBlock> r : ranges.get(dst)) {
            r.removeVertex(dst);
        }
    // System.out.printf("Merged %s into %s.%n", dst.getId(), src.getId());
    }
    // we need to update the assigns map if we change the cfg.
    for (Entry<Local, Set<BasicBlock>> e : builder.assigns.entrySet()) {
        Set<BasicBlock> set = e.getValue();
        Set<BasicBlock> copy = new HashSet<>(set);
        for (BasicBlock b : copy) {
            BasicBlock r = remap.getOrDefault(b, b);
            if (r != b) {
                set.remove(b);
                set.add(r);
            }
        }
    }
    return merges.size();
}
Also used : FlowEdge(org.mapleir.flowgraph.edges.FlowEdge) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) BasicBlock(org.mapleir.ir.cfg.BasicBlock) ArrayList(java.util.ArrayList) Local(org.mapleir.ir.locals.Local) TryCatchEdge(org.mapleir.flowgraph.edges.TryCatchEdge) ExceptionRange(org.mapleir.flowgraph.ExceptionRange) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 68 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class SSAGenPass method insertPhis.

private void insertPhis() {
    int i = 0;
    for (Local l : builder.locals) {
        i++;
        LinkedList<BasicBlock> queue = new LinkedList<>();
        for (BasicBlock b : builder.assigns.get(l)) {
            process.put(b, i);
            queue.add(b);
        }
        while (!queue.isEmpty()) {
            insertPhis(queue.poll(), l, i, queue);
        }
    }
}
Also used : 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)

Example 69 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class SSAGenPass method rename.

private void rename() {
    for (Local l : builder.locals) {
        counters.put(l, 0);
        stacks.put(l, new Stack<>());
    }
    Set<BasicBlock> vis = new HashSet<>();
    for (BasicBlock e : builder.graph.getEntries()) {
        search(e, vis);
    }
    updatePhiArgTypes(vis);
}
Also used : 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)

Example 70 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class MethodNodePrinter method emitHandlers.

private void emitHandlers(ControlFlowGraph cfg) {
    List<ExceptionRange<BasicBlock>> ranges = cfg.getRanges();
    if (ranges.size() > 0) {
        List<BasicBlock> allNodes = new ArrayList<>(cfg.vertices());
        List<Map<String, String>> handlerEntries = new ArrayList<>();
        for (ExceptionRange<BasicBlock> range : ranges) {
            List<BasicBlock> nodes = range.getNodes();
            // key order for nice print
            Map<String, String> entry = new LinkedHashMap<>();
            entry.put("start", nodes.get(0).getDisplayName());
            BasicBlock end = nodes.get(nodes.size() - 1);
            // exclusive
            BasicBlock endNext = allNodes.get(allNodes.indexOf(end) + 1);
            entry.put("end", endNext.getDisplayName());
            entry.put("handler", range.getHandler().getDisplayName());
            handlerEntries.add(entry);
        }
        this.emitDirective("handlers", handlerEntries);
    }
}
Also used : ExceptionRange(org.mapleir.flowgraph.ExceptionRange) BasicBlock(org.mapleir.ir.cfg.BasicBlock) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

BasicBlock (org.mapleir.ir.cfg.BasicBlock)70 Stmt (org.mapleir.ir.code.Stmt)37 Expr (org.mapleir.ir.code.Expr)34 Local (org.mapleir.ir.locals.Local)30 VarExpr (org.mapleir.ir.code.expr.VarExpr)29 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)25 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)24 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)22 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)19 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)19 HashSet (java.util.HashSet)12 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)11 UnconditionalJumpStmt (org.mapleir.ir.code.stmt.UnconditionalJumpStmt)10 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)9 BasicLocal (org.mapleir.ir.locals.impl.BasicLocal)9 NullPermeableHashMap (org.mapleir.stdlib.collections.map.NullPermeableHashMap)9 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)8 Type (org.objectweb.asm.Type)7 ArrayList (java.util.ArrayList)6 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)6