Search in sources :

Example 41 with VarExpr

use of org.mapleir.ir.code.expr.VarExpr in project maple-ir by LLVM-but-worse.

the class PoolLocalValueResolver method checkRecursive.

private void checkRecursive(Local l) {
    Set<Local> visited = new HashSet<>();
    Queue<Local> worklist = new LinkedList<>();
    worklist.add(l);
    while (!worklist.isEmpty()) {
        l = worklist.poll();
        AbstractCopyStmt copy = pool.defs.get(l);
        Set<Local> set = new HashSet<>();
        Expr rhs = copy.getExpression();
        if (rhs.getOpcode() == Opcode.LOCAL_LOAD) {
            set.add(((VarExpr) rhs).getLocal());
        } else if (rhs.getOpcode() == Opcode.PHI) {
            for (Expr e : ((PhiExpr) rhs).getArguments().values()) {
                set.add(((VarExpr) e).getLocal());
            }
        }
        for (Local v : set) {
            if (visited.contains(v)) {
                System.err.println(copy.getBlock().getGraph());
                System.err.printf("visited: %s%n", visited);
                System.err.printf(" copy: %s%n", copy);
                System.err.printf("  dup: %s%n", v);
                throw new RuntimeException();
            }
        }
        worklist.addAll(set);
        visited.addAll(set);
    }
}
Also used : VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) Local(org.mapleir.ir.locals.Local) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 42 with VarExpr

use of org.mapleir.ir.code.expr.VarExpr in project maple-ir by LLVM-but-worse.

the class AbstractCopyStmt method toCode.

@Override
public // todo: this probably needs a refactoring
void toCode(MethodVisitor visitor, ControlFlowGraph cfg) {
    if (expression instanceof VarExpr) {
        if (((VarExpr) expression).getLocal() == variable.getLocal()) {
            return;
        }
    }
    variable.getLocal().setTempLocal(false);
    expression.toCode(visitor, cfg);
    Type type = variable.getType();
    if (TypeUtils.isPrimitive(type)) {
        int[] cast = TypeUtils.getPrimitiveCastOpcodes(expression.getType(), type);
        for (int i = 0; i < cast.length; i++) visitor.visitInsn(cast[i]);
    }
    Local local = variable.getLocal();
    if (local.isStack()) {
        visitor.visitVarInsn(TypeUtils.getVariableStoreOpcode(getType()), variable.getLocal().getCodeIndex());
        variable.getLocal().setTempLocal(true);
    } else {
        visitor.visitVarInsn(TypeUtils.getVariableStoreOpcode(getType()), variable.getLocal().getCodeIndex());
    }
}
Also used : Type(org.objectweb.asm.Type) VarExpr(org.mapleir.ir.code.expr.VarExpr) Local(org.mapleir.ir.locals.Local)

Example 43 with VarExpr

use of org.mapleir.ir.code.expr.VarExpr in project maple-ir by LLVM-but-worse.

the class BoissinotDestructor method copyPhiOperands.

private void copyPhiOperands(BasicBlock b) {
    NullPermeableHashMap<BasicBlock, List<PhiRes>> wl = new NullPermeableHashMap<>(new ListCreator<>());
    ParallelCopyVarStmt dst_copy = new ParallelCopyVarStmt();
    for (Stmt stmt : b) {
        // phis only appear at the start of a block.
        if (stmt.getOpcode() != Opcode.PHI_STORE) {
            break;
        }
        CopyPhiStmt copy = (CopyPhiStmt) stmt;
        PhiExpr phi = copy.getExpression();
        // so that we can parallelise the copy when we insert it.
        for (Entry<BasicBlock, Expr> e : phi.getArguments().entrySet()) {
            BasicBlock h = e.getKey();
            // these are validated in init().
            VarExpr v = (VarExpr) e.getValue();
            PhiRes r = new PhiRes(copy.getVariable().getLocal(), phi, h, v.getLocal(), v.getType());
            wl.getNonNull(h).add(r);
        }
        // for each x0, where x0 is a phi copy target, create a new variable z0 for
        // a copy x0 = z0 and replace the phi copy target to z0.
        Local x0 = copy.getVariable().getLocal();
        Local z0 = locals.makeLatestVersion(x0);
        // x0 = z0
        dst_copy.pairs.add(new CopyPair(x0, z0, copy.getVariable().getType()));
        // z0 = phi(...)
        copy.getVariable().setLocal(z0);
    }
    // resolve
    if (dst_copy.pairs.size() > 0)
        insertStart(b, dst_copy);
    for (Entry<BasicBlock, List<PhiRes>> e : wl.entrySet()) {
        BasicBlock p = e.getKey();
        ParallelCopyVarStmt copy = new ParallelCopyVarStmt();
        for (PhiRes r : e.getValue()) {
            // for each xi source in a phi, create a new variable zi, and insert the copy
            // zi = xi in the pred Li. then replace the phi arg from Li with zi.
            Local xi = r.l;
            Local zi = locals.makeLatestVersion(xi);
            copy.pairs.add(new CopyPair(zi, xi, r.type));
            // we consider phi args to be used in the pred instead of the block
            // where the phi is, so we need to update the def/use maps here.
            r.phi.setArgument(r.pred, new VarExpr(zi, r.type));
        }
        insertEnd(p, copy);
    }
}
Also used : BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) Stmt(org.mapleir.ir.code.Stmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) NullPermeableHashMap(org.mapleir.stdlib.collections.map.NullPermeableHashMap) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr)

Example 44 with VarExpr

use of org.mapleir.ir.code.expr.VarExpr in project maple-ir by LLVM-but-worse.

the class BoissinotDestructor method sequentialize.

private void sequentialize(BasicBlock b) {
    // TODO: just rebuild the instruction list
    LinkedHashMap<ParallelCopyVarStmt, Integer> p = new LinkedHashMap<>();
    for (int i = 0; i < b.size(); i++) {
        Stmt stmt = b.get(i);
        if (stmt instanceof ParallelCopyVarStmt)
            p.put((ParallelCopyVarStmt) stmt, i);
    }
    if (p.isEmpty())
        return;
    int indexOffset = 0;
    Local spill = locals.makeLatestVersion(p.entrySet().iterator().next().getKey().pairs.get(0).targ);
    for (Entry<ParallelCopyVarStmt, Integer> e : p.entrySet()) {
        ParallelCopyVarStmt pcvs = e.getKey();
        int index = e.getValue();
        if (pcvs.pairs.size() == 0)
            throw new IllegalArgumentException("pcvs is empty");
        else if (pcvs.pairs.size() == 1) {
            // constant sequentialize for trivial parallel copies
            CopyPair pair = pcvs.pairs.get(0);
            CopyVarStmt newCopy = new CopyVarStmt(new VarExpr(pair.targ, pair.type), new VarExpr(pair.source, pair.type));
            b.set(index + indexOffset, newCopy);
        } else {
            List<CopyVarStmt> sequentialized = pcvs.sequentialize(spill);
            b.remove(index + indexOffset--);
            for (CopyVarStmt cvs : sequentialized) {
                // warning: O(N^2) operation
                b.add(index + ++indexOffset, cvs);
            }
        }
    }
}
Also used : CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) Local(org.mapleir.ir.locals.Local) VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) Stmt(org.mapleir.ir.code.Stmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr)

Example 45 with VarExpr

use of org.mapleir.ir.code.expr.VarExpr in project maple-ir by LLVM-but-worse.

the class SSADefUseMap method buildIndex.

protected void buildIndex(BasicBlock b, Stmt stmt, int index, Set<Local> usedLocals) {
    if (stmt instanceof AbstractCopyStmt) {
        AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
        defIndex.put(copy.getVariable().getLocal(), index);
        if (copy instanceof CopyPhiStmt) {
            PhiExpr phi = ((CopyPhiStmt) copy).getExpression();
            for (Entry<BasicBlock, Expr> en : phi.getArguments().entrySet()) {
                lastUseIndex.getNonNull(((VarExpr) en.getValue()).getLocal()).put(en.getKey(), en.getKey().size());
            // lastUseIndex.get(ul).put(b, -1);
            }
            return;
        }
    }
    for (Local usedLocal : usedLocals) lastUseIndex.getNonNull(usedLocal).put(b, index);
}
Also used : VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) BasicBlock(org.mapleir.ir.cfg.BasicBlock) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr) Local(org.mapleir.ir.locals.Local) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Aggregations

VarExpr (org.mapleir.ir.code.expr.VarExpr)51 Expr (org.mapleir.ir.code.Expr)37 Local (org.mapleir.ir.locals.Local)32 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)31 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)30 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)29 BasicBlock (org.mapleir.ir.cfg.BasicBlock)26 Stmt (org.mapleir.ir.code.Stmt)25 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)25 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)21 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)15 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)13 InitialisedObjectExpr (org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr)12 BasicLocal (org.mapleir.ir.locals.impl.BasicLocal)7 NullPermeableHashMap (org.mapleir.stdlib.collections.map.NullPermeableHashMap)7 Type (org.objectweb.asm.Type)7 HashSet (java.util.HashSet)6 Constraint (org.mapleir.ir.cfg.builder.ssaopt.Constraint)6 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)5 PopStmt (org.mapleir.ir.code.stmt.PopStmt)5