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());
}
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();
}
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);
}
}
}
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);
}
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);
}
}
Aggregations