Search in sources :

Example 6 with PhiExpr

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

the class SSAGenPass method fixPhiArgs.

private void fixPhiArgs(BasicBlock b, BasicBlock succ) {
    for (Stmt stmt : succ) {
        if (stmt.getOpcode() == Opcode.PHI_STORE) {
            CopyPhiStmt copy = (CopyPhiStmt) stmt;
            PhiExpr phi = copy.getExpression();
            Expr e = phi.getArgument(b);
            if (e.getOpcode() == Opcode.LOCAL_LOAD) {
                VarExpr v = (VarExpr) e;
                translate(v, true, true);
                VersionedLocal ssaL = (VersionedLocal) v.getLocal();
                Type t = types.get(ssaL);
                copy.getVariable().setType(t);
                phi.setType(t);
            } else {
                throw new IllegalArgumentException(phi + ", " + e);
            }
        } else {
            /* No need to search the rest of the block
				 * after we have visited the phis as they
				 * precede all other statements.
				 */
            break;
        }
    }
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) Type(org.objectweb.asm.Type) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) 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) VarExpr(org.mapleir.ir.code.expr.VarExpr) 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) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 7 with PhiExpr

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

the class CodeUnit method _enumerate.

protected Set<Expr> _enumerate() {
    Set<Expr> set = new HashSet<>();
    if (opcode == Opcode.PHI) {
        /*CopyPhiStmt phi = (CopyPhiStmt) this;
			for(Expr e : phi.getExpression().getArguments().values()) {
				set.add(e);
				set.addAll(e._enumerate());
			}*/
        PhiExpr phi = (PhiExpr) this;
        for (Expr e : phi.getArguments().values()) {
            set.add(e);
            set.addAll(e._enumerate());
        }
    } else {
        for (Expr c : children) {
            if (c != null) {
                set.add(c);
                set.addAll(c._enumerate());
            }
        }
    }
    return set;
}
Also used : PhiExpr(org.mapleir.ir.code.expr.PhiExpr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) HashSet(java.util.HashSet)

Example 8 with PhiExpr

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

the class PoolLocalValueResolver method getValues.

@Override
public TaintableSet<Expr> getValues(ControlFlowGraph cfg, Local l) {
    if (cfg.getLocals() != pool) {
        throw new UnsupportedOperationException();
    }
    AbstractCopyStmt copy = pool.defs.get(l);
    TaintableSet<Expr> set = new TaintableSet<>();
    if (copy.getOpcode() == Opcode.PHI_STORE) {
    // checkRecursive(l);
    // PhiExpr phi = ((CopyPhiStmt) copy).getExpression();
    /*for(Expr e : phi.getArguments().values()) {
				if(e.getOpcode() == Opcode.LOCAL_LOAD) {
					Local l2 = ((VarExpr) e).getLocal();
					
					if(l2 == l) {
						throw new RuntimeException(copy.toString());
					}
				}
			}*/
    // set.addAll(phi.getArguments().values());
    } else {
    // set.add(copy.getExpression());
    }
    set.add(copy.getExpression());
    return set;
}
Also used : VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) TaintableSet(org.mapleir.stdlib.collections.taint.TaintableSet)

Example 9 with PhiExpr

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

the class SSAGenPass method insertPhis.

private void insertPhis(BasicBlock b, Local l, int i, LinkedList<BasicBlock> queue) {
    if (b == null || b == builder.head) {
        // exit
        return;
    }
    Local newl = builder.graph.getLocals().get(l.getIndex(), 0, l.isStack());
    for (BasicBlock x : doms.iteratedFrontier(b)) {
        if (insertion.get(x) < i) {
            // pruned SSA
            if (liveness.in(x).contains(l)) {
                if ((l == svar0) && handlers.contains(x)) /* == faster than contains. */
                {
                    /* Note: this is quite subtle. Since there is a
						 * copy, (svar0 = catch()) at the start of each
						 * handler block, technically any natural flowing
						 * svar0 definition is killed upon entry to the
						 * block, so it is not considered live. One way to
						 * check if the variable is live-in, therefore, is
						 * by checking whether svar0 is live-out of the
						 * catch() definition. We handle it here, since
						 * the previous liveness check which is used for
						 * pruned SSA will fail in this case. */
                    /* Ok fuck that that, it's considered live-in
						 * even if there is a catch()::
						 *  #see SSaBlockLivenessAnalyser.precomputeBlock*/
                    boolean naturalFlow = false;
                    for (FlowEdge<BasicBlock> e : builder.graph.getReverseEdges(x)) {
                        if (e.getType() != FlowEdges.TRYCATCH) {
                            naturalFlow = true;
                            break;
                        }
                    }
                    if (naturalFlow) {
                        CopyVarStmt catcher = null;
                        for (Stmt stmt : x) {
                            if (stmt.getOpcode() == Opcode.LOCAL_STORE) {
                                CopyVarStmt copy = (CopyVarStmt) stmt;
                                Expr e = copy.getExpression();
                                if (e.getOpcode() == Opcode.CATCH) {
                                    catcher = copy;
                                    break;
                                }
                            }
                        }
                        if (catcher == null) {
                            /* Handler but no catch copy?
								 * This can't happen since svar0 is
								 * the only reserved register for
								 * catch copies, and this block cannot
								 * be visited twice to insert a phi or
								 * psi(ephi) node. */
                            throw new IllegalStateException(x.getDisplayName());
                        }
                        /* Map<BasicBlock, Expression> vls = new HashMap<>();
							for(FlowEdge<BasicBlock> fe : builder.graph.getReverseEdges(x)) {
								vls.put(fe.src, new VarExpr(newl, null));
							}
							vls.put(x, catcher.getExpression().copy());
							catcher.delete();
							
							PhiExpr phi = new PhiExceptionExpr(vls);
							CopyPhiStmt assign = new CopyPhiStmt(new VarExpr(l, null), phi);
							
							x.add(0, assign); */
                        throw new UnsupportedOperationException(builder.method.toString());
                    }
                }
                if (builder.graph.getReverseEdges(x).size() > 1) {
                    Map<BasicBlock, Expr> vls = new HashMap<>();
                    for (FlowEdge<BasicBlock> fe : builder.graph.getReverseEdges(x)) {
                        vls.put(fe.src(), new VarExpr(newl, null));
                    }
                    PhiExpr phi = new PhiExpr(vls);
                    CopyPhiStmt assign = new CopyPhiStmt(new VarExpr(l, null), phi);
                    x.add(0, assign);
                }
            }
            insertion.put(x, i);
            if (process.get(x) < i) {
                process.put(x, i);
                queue.add(x);
            }
        }
    }
}
Also used : NullPermeableHashMap(org.mapleir.stdlib.collections.map.NullPermeableHashMap) 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) 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) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) 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) VarExpr(org.mapleir.ir.code.expr.VarExpr)

Example 10 with PhiExpr

use of org.mapleir.ir.code.expr.PhiExpr 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)

Aggregations

PhiExpr (org.mapleir.ir.code.expr.PhiExpr)12 VarExpr (org.mapleir.ir.code.expr.VarExpr)11 Expr (org.mapleir.ir.code.Expr)10 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)9 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)8 Local (org.mapleir.ir.locals.Local)8 BasicBlock (org.mapleir.ir.cfg.BasicBlock)7 Stmt (org.mapleir.ir.code.Stmt)6 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)5 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)5 NullPermeableHashMap (org.mapleir.stdlib.collections.map.NullPermeableHashMap)3 HashSet (java.util.HashSet)2 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)2 InitialisedObjectExpr (org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr)2 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)2 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)2 PopStmt (org.mapleir.ir.code.stmt.PopStmt)2 SwitchStmt (org.mapleir.ir.code.stmt.SwitchStmt)2 ThrowStmt (org.mapleir.ir.code.stmt.ThrowStmt)2 UnconditionalJumpStmt (org.mapleir.ir.code.stmt.UnconditionalJumpStmt)2