Ejemplo n.º 1
0
        public static void LowContinueLabels(Statement stat, HashSet <StatEdge> edges)
        {
            bool ok = (stat.type != Statement.Type_Do);

            if (!ok)
            {
                DoStatement dostat = (DoStatement)stat;
                ok = dostat.GetLooptype() == DoStatement.Loop_Do || dostat.GetLooptype() == DoStatement
                     .Loop_While || (dostat.GetLooptype() == DoStatement.Loop_For && dostat.GetIncExprent
                                         () == null);
            }
            if (ok)
            {
                Sharpen.Collections.AddAll(edges, stat.GetPredecessorEdges(StatEdge.Type_Continue
                                                                           ));
            }
            if (ok && stat.type == Statement.Type_Do)
            {
                foreach (StatEdge edge in edges)
                {
                    if (stat.ContainsStatementStrict(edge.GetSource()))
                    {
                        edge.GetDestination().RemovePredecessor(edge);
                        edge.GetSource().ChangeEdgeNode(Statement.Direction_Forward, edge, stat);
                        stat.AddPredecessor(edge);
                        stat.AddLabeledEdge(edge);
                    }
                }
            }
            foreach (Statement st in stat.GetStats())
            {
                if (st == stat.GetFirst())
                {
                    LowContinueLabels(st, edges);
                }
                else
                {
                    LowContinueLabels(st, new HashSet <StatEdge>());
                }
            }
        }
Ejemplo n.º 2
0
        private bool IterateStatements(RootStatement root, SSAUConstructorSparseEx ssa)
        {
            FlattenStatementsHelper flatthelper = new FlattenStatementsHelper();
            DirectGraph             dgraph      = flatthelper.BuildDirectGraph(root);
            bool res = false;
            HashSet <DirectNode>    setVisited = new HashSet <DirectNode>();
            LinkedList <DirectNode> stack      = new LinkedList <DirectNode>();
            LinkedList <Dictionary <VarVersionPair, Exprent> > stackMaps = new LinkedList <Dictionary <VarVersionPair, Exprent> >();

            stack.AddLast(dgraph.first);
            stackMaps.AddLast(new Dictionary <VarVersionPair, Exprent>());
            while (!(stack.Count == 0))
            {
                DirectNode nd = Sharpen.Collections.RemoveFirst(stack);
                Dictionary <VarVersionPair, Exprent> mapVarValues = Sharpen.Collections.RemoveFirst
                                                                        (stackMaps);
                if (setVisited.Contains(nd))
                {
                    continue;
                }
                setVisited.Add(nd);
                List <List <Exprent> > lstLists = new List <List <Exprent> >();
                if (!(nd.exprents.Count == 0))
                {
                    lstLists.Add(nd.exprents);
                }
                if (nd.succs.Count == 1)
                {
                    DirectNode ndsucc = nd.succs[0];
                    if (ndsucc.type == DirectNode.Node_Tail && !(ndsucc.exprents.Count == 0))
                    {
                        lstLists.Add(nd.succs[0].exprents);
                        nd = ndsucc;
                    }
                }
                for (int i = 0; i < lstLists.Count; i++)
                {
                    List <Exprent> lst   = lstLists[i];
                    int            index = 0;
                    while (index < lst.Count)
                    {
                        Exprent next = null;
                        if (index == lst.Count - 1)
                        {
                            if (i < lstLists.Count - 1)
                            {
                                next = lstLists[i + 1][0];
                            }
                        }
                        else
                        {
                            next = lst[index + 1];
                        }
                        int[] ret = IterateExprent(lst, index, next, mapVarValues, ssa);
                        if (ret[0] >= 0)
                        {
                            index = ret[0];
                        }
                        else
                        {
                            index++;
                        }
                        res |= (ret[1] == 1);
                    }
                }
                foreach (DirectNode ndx in nd.succs)
                {
                    stack.AddLast(ndx);
                    stackMaps.AddLast(new Dictionary <VarVersionPair, Exprent>(mapVarValues));
                }
                // make sure the 3 special exprent lists in a loop (init, condition, increment) are not empty
                // change loop type if necessary
                if ((nd.exprents.Count == 0) && (nd.type == DirectNode.Node_Init || nd.type == DirectNode
                                                 .Node_Condition || nd.type == DirectNode.Node_Increment))
                {
                    nd.exprents.Add(null);
                    if (nd.statement.type == Statement.Type_Do)
                    {
                        DoStatement loop = (DoStatement)nd.statement;
                        if (loop.GetLooptype() == DoStatement.Loop_For && loop.GetInitExprent() == null &&
                            loop.GetIncExprent() == null)
                        {
                            // "downgrade" loop to 'while'
                            loop.SetLooptype(DoStatement.Loop_While);
                        }
                    }
                }
            }
            return(res);
        }
Ejemplo n.º 3
0
        public virtual void SetVarDefinitions()
        {
            VarNamesCollector vc = varproc.GetVarNamesCollector();

            foreach (KeyValuePair <int, Statement> en in mapVarDefStatements)
            {
                Statement stat  = en.Value;
                int       index = en.Key;
                if (implDefVars.Contains(index))
                {
                    // already implicitly defined
                    continue;
                }
                varproc.SetVarName(new VarVersionPair(index, 0), vc.GetFreeName(index));
                // special case for
                if (stat.type == Statement.Type_Do)
                {
                    DoStatement dstat = (DoStatement)stat;
                    if (dstat.GetLooptype() == DoStatement.Loop_For)
                    {
                        if (dstat.GetInitExprent() != null && SetDefinition(dstat.GetInitExprent(), index
                                                                            ))
                        {
                            continue;
                        }
                        else
                        {
                            List <Exprent> lstSpecial = Sharpen.Arrays.AsList(dstat.GetConditionExprent(), dstat
                                                                              .GetIncExprent());
                            foreach (VarExprent var in GetAllVars(lstSpecial))
                            {
                                if (var.GetIndex() == index)
                                {
                                    stat = stat.GetParent();
                                    break;
                                }
                            }
                        }
                    }
                }
                Statement      first = FindFirstBlock(stat, index);
                List <Exprent> lst;
                if (first == null)
                {
                    lst = stat.GetVarDefinitions();
                }
                else if (first.GetExprents() == null)
                {
                    lst = first.GetVarDefinitions();
                }
                else
                {
                    lst = first.GetExprents();
                }
                bool defset = false;
                // search for the first assignment to var [index]
                int addindex = 0;
                foreach (Exprent expr in lst)
                {
                    if (SetDefinition(expr, index))
                    {
                        defset = true;
                        break;
                    }
                    else
                    {
                        bool foundvar = false;
                        foreach (Exprent exp in expr.GetAllExprents(true))
                        {
                            if (exp.type == Exprent.Exprent_Var && ((VarExprent)exp).GetIndex() == index)
                            {
                                foundvar = true;
                                break;
                            }
                        }
                        if (foundvar)
                        {
                            break;
                        }
                    }
                    addindex++;
                }
                if (!defset)
                {
                    VarExprent var = new VarExprent(index, varproc.GetVarType(new VarVersionPair(index
                                                                                                 , 0)), varproc);
                    var.SetDefinition(true);
                    lst.Add(addindex, var);
                }
            }
        }