Exemplo n.º 1
0
 private static bool CollapseIfElse(IfHelper.IfNode rtnode)
 {
     if (rtnode.edgetypes[0] == 0)
     {
         IfHelper.IfNode ifbranch = rtnode.succs[0];
         if (ifbranch.succs.Count == 2)
         {
             // if-else branch
             if (ifbranch.succs[0].value == rtnode.succs[1].value)
             {
                 IfStatement ifparent = (IfStatement)rtnode.value;
                 IfStatement ifchild  = (IfStatement)ifbranch.value;
                 if ((ifchild.GetFirst().GetExprents().Count == 0))
                 {
                     ifparent.GetFirst().RemoveSuccessor(ifparent.GetIfEdge());
                     ifchild.GetFirst().RemoveSuccessor(ifchild.GetIfEdge());
                     ifparent.GetStats().RemoveWithKey(ifchild.id);
                     if (ifbranch.edgetypes[1] == 1 && ifbranch.edgetypes[0] == 1)
                     {
                         // target null
                         ifparent.SetIfstat(null);
                         StatEdge ifedge = ifchild.GetAllSuccessorEdges()[0];
                         ifchild.RemoveSuccessor(ifedge);
                         ifedge.SetSource(ifparent.GetFirst());
                         ifparent.GetFirst().AddSuccessor(ifedge);
                         ifparent.SetIfEdge(ifedge);
                     }
                     else
                     {
                         throw new Exception("inconsistent if structure!");
                     }
                     // merge if conditions
                     IfExprent      statexpr    = ifparent.GetHeadexprent();
                     List <Exprent> lstOperands = new List <Exprent>();
                     lstOperands.Add(statexpr.GetCondition());
                     lstOperands.Add(new FunctionExprent(FunctionExprent.Function_Bool_Not, ifchild.GetHeadexprent
                                                             ().GetCondition(), null));
                     statexpr.SetCondition(new FunctionExprent(FunctionExprent.Function_Cadd, lstOperands
                                                               , null));
                     statexpr.AddBytecodeOffsets(ifchild.GetHeadexprent().bytecode);
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Exemplo n.º 2
0
 public virtual void AddChild(IfHelper.IfNode child, int type)
 {
     succs.Add(child);
     edgetypes.Add(type);
 }
Exemplo n.º 3
0
        public static bool MergeIfs(Statement statement, HashSet <int> setReorderedIfs
                                    )
        {
            if (statement.type != Statement.Type_If && statement.type != Statement.Type_Sequence)
            {
                return(false);
            }
            bool res = false;

            while (true)
            {
                bool             updated = false;
                List <Statement> lst     = new List <Statement>();
                if (statement.type == Statement.Type_If)
                {
                    lst.Add(statement);
                }
                else
                {
                    Sharpen.Collections.AddAll(lst, statement.GetStats());
                }
                bool stsingle = (lst.Count == 1);
                foreach (Statement stat in lst)
                {
                    if (stat.type == Statement.Type_If)
                    {
                        IfHelper.IfNode rtnode = BuildGraph((IfStatement)stat, stsingle);
                        if (rtnode == null)
                        {
                            continue;
                        }
                        if (updated = CollapseIfIf(rtnode))
                        {
                            break;
                        }
                        if (!setReorderedIfs.Contains(stat.id))
                        {
                            if (updated = CollapseIfElse(rtnode))
                            {
                                break;
                            }
                            if (updated = CollapseElse(rtnode))
                            {
                                break;
                            }
                        }
                        if (updated = ReorderIf((IfStatement)stat))
                        {
                            setReorderedIfs.Add(stat.id);
                            break;
                        }
                    }
                }
                if (!updated)
                {
                    break;
                }
                res |= true;
            }
            return(res);
        }
Exemplo n.º 4
0
        private static IfHelper.IfNode BuildGraph(IfStatement stat, bool stsingle)
        {
            if (stat.iftype == IfStatement.Iftype_Ifelse)
            {
                return(null);
            }
            IfHelper.IfNode res = new IfHelper.IfNode(stat);
            // if branch
            Statement ifchild = stat.GetIfstat();

            if (ifchild == null)
            {
                StatEdge edge = stat.GetIfEdge();
                res.AddChild(new IfHelper.IfNode(edge.GetDestination()), 1);
            }
            else
            {
                IfHelper.IfNode ifnode = new IfHelper.IfNode(ifchild);
                res.AddChild(ifnode, 0);
                if (ifchild.type == Statement.Type_If && ((IfStatement)ifchild).iftype == IfStatement
                    .Iftype_If)
                {
                    IfStatement stat2    = (IfStatement)ifchild;
                    Statement   ifchild2 = stat2.GetIfstat();
                    if (ifchild2 == null)
                    {
                        StatEdge edge = stat2.GetIfEdge();
                        ifnode.AddChild(new IfHelper.IfNode(edge.GetDestination()), 1);
                    }
                    else
                    {
                        ifnode.AddChild(new IfHelper.IfNode(ifchild2), 0);
                    }
                }
                if (!(ifchild.GetAllSuccessorEdges().Count == 0))
                {
                    ifnode.AddChild(new IfHelper.IfNode(ifchild.GetAllSuccessorEdges()[0].GetDestination
                                                            ()), 1);
                }
            }
            // else branch
            StatEdge  edge_1    = stat.GetAllSuccessorEdges()[0];
            Statement elsechild = edge_1.GetDestination();

            IfHelper.IfNode elsenode = new IfHelper.IfNode(elsechild);
            if (stsingle || edge_1.GetType() != StatEdge.Type_Regular)
            {
                res.AddChild(elsenode, 1);
            }
            else
            {
                res.AddChild(elsenode, 0);
                if (elsechild.type == Statement.Type_If && ((IfStatement)elsechild).iftype == IfStatement
                    .Iftype_If)
                {
                    IfStatement stat2    = (IfStatement)elsechild;
                    Statement   ifchild2 = stat2.GetIfstat();
                    if (ifchild2 == null)
                    {
                        elsenode.AddChild(new IfHelper.IfNode(stat2.GetIfEdge().GetDestination()), 1);
                    }
                    else
                    {
                        elsenode.AddChild(new IfHelper.IfNode(ifchild2), 0);
                    }
                }
                if (!(elsechild.GetAllSuccessorEdges().Count == 0))
                {
                    elsenode.AddChild(new IfHelper.IfNode(elsechild.GetAllSuccessorEdges()[0].GetDestination
                                                              ()), 1);
                }
            }
            return(res);
        }
Exemplo n.º 5
0
 private static bool CollapseElse(IfHelper.IfNode rtnode)
 {
     if (rtnode.edgetypes[1] == 0)
     {
         IfHelper.IfNode elsebranch = rtnode.succs[1];
         if (elsebranch.succs.Count == 2)
         {
             // else-if or else-else branch
             int path = elsebranch.succs[1].value == rtnode.succs[0].value ? 2 : (elsebranch.succs
                                                                                  [0].value == rtnode.succs[0].value ? 1 : 0);
             if (path > 0)
             {
                 IfStatement firstif  = (IfStatement)rtnode.value;
                 IfStatement secondif = (IfStatement)elsebranch.value;
                 Statement   parent   = firstif.GetParent();
                 if ((secondif.GetFirst().GetExprents().Count == 0))
                 {
                     firstif.GetFirst().RemoveSuccessor(firstif.GetIfEdge());
                     // remove first if
                     firstif.RemoveAllSuccessors(secondif);
                     foreach (StatEdge edge in firstif.GetAllPredecessorEdges())
                     {
                         if (!firstif.ContainsStatementStrict(edge.GetSource()))
                         {
                             firstif.RemovePredecessor(edge);
                             edge.GetSource().ChangeEdgeNode(Statement.Direction_Forward, edge, secondif);
                             secondif.AddPredecessor(edge);
                         }
                     }
                     parent.GetStats().RemoveWithKey(firstif.id);
                     if (parent.GetFirst() == firstif)
                     {
                         parent.SetFirst(secondif);
                     }
                     // merge if conditions
                     IfExprent      statexpr    = secondif.GetHeadexprent();
                     List <Exprent> lstOperands = new List <Exprent>();
                     lstOperands.Add(firstif.GetHeadexprent().GetCondition());
                     if (path == 2)
                     {
                         lstOperands[0] = new FunctionExprent(FunctionExprent.Function_Bool_Not, lstOperands
                                                              [0], null);
                     }
                     lstOperands.Add(statexpr.GetCondition());
                     statexpr.SetCondition(new FunctionExprent(path == 1 ? FunctionExprent.Function_Cor
                                                          : FunctionExprent.Function_Cadd, lstOperands, null));
                     if ((secondif.GetFirst().GetExprents().Count == 0) && !(firstif.GetFirst().GetExprents
                                                                                 ().Count == 0))
                     {
                         secondif.ReplaceStatement(secondif.GetFirst(), firstif.GetFirst());
                     }
                     return(true);
                 }
             }
         }
         else if (elsebranch.succs.Count == 1)
         {
             if (elsebranch.succs[0].value == rtnode.succs[0].value)
             {
                 IfStatement firstif = (IfStatement)rtnode.value;
                 Statement   second  = elsebranch.value;
                 firstif.RemoveAllSuccessors(second);
                 foreach (StatEdge edge in second.GetAllSuccessorEdges())
                 {
                     second.RemoveSuccessor(edge);
                     edge.SetSource(firstif);
                     firstif.AddSuccessor(edge);
                 }
                 StatEdge ifedge = firstif.GetIfEdge();
                 firstif.GetFirst().RemoveSuccessor(ifedge);
                 second.AddSuccessor(new StatEdge(ifedge.GetType(), second, ifedge.GetDestination(
                                                      ), ifedge.closure));
                 StatEdge newifedge = new StatEdge(StatEdge.Type_Regular, firstif.GetFirst(), second
                                                   );
                 firstif.GetFirst().AddSuccessor(newifedge);
                 firstif.SetIfstat(second);
                 firstif.GetStats().AddWithKey(second, second.id);
                 second.SetParent(firstif);
                 firstif.GetParent().GetStats().RemoveWithKey(second.id);
                 // negate the if condition
                 IfExprent statexpr = firstif.GetHeadexprent();
                 statexpr.SetCondition(new FunctionExprent(FunctionExprent.Function_Bool_Not, statexpr
                                                           .GetCondition(), null));
                 return(true);
             }
         }
     }
     return(false);
 }
Exemplo n.º 6
0
 private static bool CollapseIfIf(IfHelper.IfNode rtnode)
 {
     if (rtnode.edgetypes[0] == 0)
     {
         IfHelper.IfNode ifbranch = rtnode.succs[0];
         if (ifbranch.succs.Count == 2)
         {
             // if-if branch
             if (ifbranch.succs[1].value == rtnode.succs[1].value)
             {
                 IfStatement ifparent = (IfStatement)rtnode.value;
                 IfStatement ifchild  = (IfStatement)ifbranch.value;
                 Statement   ifinner  = ifbranch.succs[0].value;
                 if ((ifchild.GetFirst().GetExprents().Count == 0))
                 {
                     ifparent.GetFirst().RemoveSuccessor(ifparent.GetIfEdge());
                     ifchild.RemoveSuccessor(ifchild.GetAllSuccessorEdges()[0]);
                     ifparent.GetStats().RemoveWithKey(ifchild.id);
                     if (ifbranch.edgetypes[0] == 1)
                     {
                         // target null
                         ifparent.SetIfstat(null);
                         StatEdge ifedge = ifchild.GetIfEdge();
                         ifchild.GetFirst().RemoveSuccessor(ifedge);
                         ifedge.SetSource(ifparent.GetFirst());
                         if (ifedge.closure == ifchild)
                         {
                             ifedge.closure = null;
                         }
                         ifparent.GetFirst().AddSuccessor(ifedge);
                         ifparent.SetIfEdge(ifedge);
                     }
                     else
                     {
                         ifchild.GetFirst().RemoveSuccessor(ifchild.GetIfEdge());
                         StatEdge ifedge = new StatEdge(StatEdge.Type_Regular, ifparent.GetFirst(), ifinner
                                                        );
                         ifparent.GetFirst().AddSuccessor(ifedge);
                         ifparent.SetIfEdge(ifedge);
                         ifparent.SetIfstat(ifinner);
                         ifparent.GetStats().AddWithKey(ifinner, ifinner.id);
                         ifinner.SetParent(ifparent);
                         if (!(ifinner.GetAllSuccessorEdges().Count == 0))
                         {
                             StatEdge edge = ifinner.GetAllSuccessorEdges()[0];
                             if (edge.closure == ifchild)
                             {
                                 edge.closure = null;
                             }
                         }
                     }
                     // merge if conditions
                     IfExprent      statexpr    = ifparent.GetHeadexprent();
                     List <Exprent> lstOperands = new List <Exprent>();
                     lstOperands.Add(statexpr.GetCondition());
                     lstOperands.Add(ifchild.GetHeadexprent().GetCondition());
                     statexpr.SetCondition(new FunctionExprent(FunctionExprent.Function_Cadd, lstOperands
                                                               , null));
                     statexpr.AddBytecodeOffsets(ifchild.GetHeadexprent().bytecode);
                     return(true);
                 }
             }
         }
     }
     return(false);
 }