use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.
the class LocalsPool method realloc.
/* public Local newLocal(boolean isStack) {
int index = cache.size();
while(true) {
String key = key(index, isStack);
if(!cache.containsKey(key)) {
return get(index, isStack);
}
}
} */
public int realloc(ControlFlowGraph cfg) {
NullPermeableHashMap<Local, Set<Type>> types = new NullPermeableHashMap<>(SetCreator.getInstance());
int min = 0;
Set<Local> safe = new HashSet<>();
for (BasicBlock b : cfg.vertices()) {
for (Stmt stmt : b) {
if (stmt.getOpcode() == Opcode.LOCAL_STORE) {
CopyVarStmt cp = (CopyVarStmt) stmt;
VarExpr var = cp.getVariable();
Local local = var.getLocal();
if (!cp.isSynthetic()) {
types.getNonNull(local).add(var.getType());
} else {
safe.add(local);
}
types.getNonNull(local).add(var.getType());
}
for (Expr s : stmt.enumerateOnlyChildren()) {
if (s.getOpcode() == Opcode.LOCAL_LOAD) {
VarExpr var = (VarExpr) s;
Local local = var.getLocal();
types.getNonNull(local).add(var.getType());
}
}
}
}
Map<Local, Type> stypes = new HashMap<>();
for (Entry<Local, Set<Type>> e : types.entrySet()) {
Set<Type> set = e.getValue();
Set<Type> refined = new HashSet<>();
if (set.size() > 1) {
for (Type t : set) {
refined.add(TypeUtils.asSimpleType(t));
}
if (refined.size() != 1) {
boolean valid = false;
if (refined.size() == 2) {
// TODO: proper check
Iterator<Type> it = refined.iterator();
if (it.next().getSize() == it.next().getSize()) {
Type t = refined.iterator().next();
refined.clear();
refined.add(t);
valid = true;
}
}
if (!valid) {
for (Entry<Local, Set<Type>> e1 : types.entrySet()) {
System.err.println(e1.getKey() + " == " + e1.getValue());
}
// String.format("illegal typesets for %s, set=%s, refined=%s", args)
throw new RuntimeException("illegal typesets for " + e.getKey());
}
}
Local l = e.getKey();
stypes.put(l, refined.iterator().next());
// if(!safe.contains(l)) {
// stypes.put(l, refined.iterator().next());
// }
} else {
Local l = e.getKey();
stypes.put(l, set.iterator().next());
// if(!safe.contains(l)) {
// }
}
}
// for(Entry<Local, Type> e : stypes.entrySet()) {
// System.out.println(e.getKey() + " == " + e.getValue());
// }
// lvars then svars, ordered of course,
List<Local> wl = new ArrayList<>(stypes.keySet());
// System.out.println("safe: " + safe);
wl.sort(new Comparator<Local>() {
@Override
public int compare(Local o1, Local o2) {
boolean s1 = safe.contains(o1);
boolean s2 = safe.contains(o2);
if (s1 && !s2) {
return -1;
} else if (!s1 && s2) {
return 1;
} else {
return o1.compareTo(o2);
}
}
});
// System.out.println("wl: " + wl);
Map<Local, Local> remap = new HashMap<>();
int idx = min;
for (Local l : wl) {
Type type = stypes.get(l);
Local newL = get(idx, false);
if (l != newL) {
remap.put(l, newL);
}
idx += type.getSize();
}
remap(cfg, remap);
return idx;
}
use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.
the class LocalsPool method remap.
public static void remap(ControlFlowGraph cfg, Map<? extends Local, ? extends Local> remap) {
for (BasicBlock b : cfg.vertices()) {
for (Stmt stmt : b) {
if (stmt.getOpcode() == Opcode.LOCAL_STORE) {
VarExpr v = ((CopyVarStmt) stmt).getVariable();
Local l = v.getLocal();
if (remap.containsKey(l)) {
Local l2 = remap.get(l);
v.setLocal(l2);
}
}
for (Expr s : stmt.enumerateOnlyChildren()) {
if (s.getOpcode() == Opcode.LOCAL_LOAD) {
VarExpr v = (VarExpr) s;
Local l = v.getLocal();
if (remap.containsKey(l)) {
v.setLocal(remap.get(l));
}
}
}
}
}
}
use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.
the class SwitchStmt method sort.
private void sort() {
List<Integer> keys = new ArrayList<>(targets.keySet());
Collections.sort(keys);
LinkedHashMap<Integer, BasicBlock> newMap = new LinkedHashMap<>();
for (int key : keys) {
BasicBlock targ = targets.get(key);
newMap.put(key, targ);
}
}
use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.
the class ConcreteStaticInvocationPass method accept.
@Override
public int accept(AnalysisContext cxt, IPass prev, List<IPass> completed) {
int fixed = 0;
InvocationResolver resolver = cxt.getInvocationResolver();
for (ClassNode cn : cxt.getApplication().iterate()) {
for (MethodNode mn : cn.methods) {
ControlFlowGraph cfg = cxt.getIRCache().getFor(mn);
for (BasicBlock b : cfg.vertices()) {
for (Stmt stmt : b) {
for (Expr e : stmt.enumerateOnlyChildren()) {
if (e.getOpcode() == Opcode.INVOKE) {
InvocationExpr invoke = (InvocationExpr) e;
if (invoke.isStatic()) {
MethodNode invoked = resolver.resolveStaticCall(invoke.getOwner(), invoke.getName(), invoke.getDesc());
if (invoked != null) {
if (!invoked.owner.name.equals(invoke.getOwner())) {
invoke.setOwner(invoked.owner.name);
fixed++;
}
}
}
}
}
}
}
}
}
System.out.printf(" corrected %d dodgy static calls.%n", fixed);
return fixed;
}
use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.
the class ConstantExpressionReorderPass method transform.
private int transform(ControlFlowGraph ir) {
int i = 0;
for (BasicBlock b : ir.vertices()) {
for (Stmt stmt : b) {
if (stmt.getOpcode() == COND_JUMP) {
ConditionalJumpStmt cjs = (ConditionalJumpStmt) stmt;
Expr r = cjs.getRight();
Expr l = cjs.getLeft();
ComparisonType type = cjs.getComparisonType();
if (type == ComparisonType.EQ || type == ComparisonType.NE) {
if (shouldReorder(r, l)) {
cjs.setRight(null);
cjs.setLeft(null);
cjs.setLeft(r);
cjs.setRight(l);
i++;
}
}
}
for (Expr e : stmt.enumerateOnlyChildren()) {
if (e.getOpcode() == ARITHMETIC) {
ArithmeticExpr arith = (ArithmeticExpr) e;
Expr r = arith.getRight();
Expr l = arith.getLeft();
Operator op = arith.getOperator();
if (!op.doesOrderMatter()) {
if (shouldReorder(r, l)) {
arith.setRight(null);
arith.setLeft(null);
arith.setLeft(r);
arith.setRight(l);
i++;
}
}
}
}
}
}
return i;
}
Aggregations