Ejemplo n.º 1
0
        public FlowGraphNode Split(long labelValue, long prevLabelValue)
        {
            if (nodes.ContainsKey(labelValue))
            {
                return(nodes[labelValue]);
            }
            FlowGraphNode n1 = Find(labelValue);

            if (n1 == null)
            {
                return(null);
            }
            FlowGraphNode n2 = NewNode(labelValue);

            //断开n1与它的后继并将之添加为n2的后继
            FlowGraphNode[] n1s = new FlowGraphNode[n1.NextNodes.Count];
            n1.NextNodes.CopyTo(n1s, 0);
            foreach (FlowGraphNode n in n1s)
            {
                n1.DelNext(n);
                n2.AddNext(n);
            }
            //将n2添加为n1的后继
            n1.AddNext(n2);
            //设置n2的结束标签为n1的结束标签,若n1的结束标签不大于n2的开始标签则不调整n2的结束标签
            if (n1.EndLabelValue > n2.LabelValue)
            {
                n2.EndLabelValue = n1.EndLabelValue;
            }
            //设置n1的结束标签为分离标签的前一个标签
            n1.EndLabelValue = prevLabelValue;

            return(n2);
        }
Ejemplo n.º 2
0
        private void AdjustFlowGraph()
        {
            flowGraph.Reset();
            for (; flowGraph.CurNode != null; flowGraph.Next())
            {
                FlowGraphNode node = flowGraph.CurNode;
                int           i    = ilLines.Keys.IndexOf(node.LabelValue);
                for (; i < ilLines.Values.Count; i++)
                {
                    IlIns il = ilLines.Values[i];
                    if (il.LabelValue == node.LabelValue)
                    {
                        BeginGraphNode(node);
                    }
                    else if (il.LabelValue != node.LabelValue && flowGraph.IsNode(il.LabelValue))
                    {
                        //行进到块的开始语句才发现新开始块是异常构造块或跳转目标倒致的块
                        EndGraphNode(node, false);
                        break;
                    }
                    IlOpcode op0 = IlOpcodes.GetEH(il.Opcode);
                    if (op0 != IlOpcode.Prefixref)
                    {
                        EvaluateEH(op0, il, false);
                    }
                    else
                    {
                        OpCode op   = IlOpcodes.Get(il.Opcode);
                        int    type = EvaluateIl(op, il, false);
                        switch (type)
                        {
                        case -2:    //永不成立的条件跳转,当前结点与分支目标块断开
                        {
                            long target = IlIns.LabelToValue(il.Value);
                            if (flowGraph.IsNode(target))
                            {
                                FlowGraphNode n = flowGraph[target];
                                node.DelNext(n);
                            }
                            EndGraphNode(node, false);
                        }
                        break;

                        case -1:    //永成立的条件跳转,当前结点与下一条指令开始的块断开
                        {
                            if (i + 1 < ilLines.Values.Count)
                            {
                                if (flowGraph.IsNode(ilLines.Values[i + 1].LabelValue))
                                {
                                    FlowGraphNode n = flowGraph[ilLines.Values[i + 1].LabelValue];
                                    node.DelNext(n);
                                }
                            }
                            EndGraphNode(node, false);
                        }
                        break;

                        case 0:    //正常指令流
                        {
                        }
                        break;

                        case 1:    //无条件跳转
                        case 2:    //条件跳转
                        case 3:    //switch分支
                        {
                            EndGraphNode(node, false);
                        }
                        break;

                        case 4:    //结束类语句
                        {
                            EndGraphNode(node, false);
                        }
                        break;
                        }
                        if (type != 0)
                        {
                            break;
                        }
                    }
                }
            }
            //合并无分支的顺序块
            flowGraph.Reset();
            for (; flowGraph.CurNode != null; flowGraph.Next())
            {
                FlowGraphNode node = flowGraph.CurNode;
                while (node.NextNodes.Count == 1 && node.NextNodes[0].PrevNodes.Count == 1)
                {
                    FlowGraphNode n = node.NextNodes[0];
                    int           i = ilLines.Keys.IndexOf(node.EndLabelValue);
                    int           j = ilLines.Keys.IndexOf(n.LabelValue);
                    if (i + 1 == j)
                    {
                        flowGraph.Combine(node, n);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public void Combine(FlowGraphNode n1, FlowGraphNode n2)
 {
     if (n1 == null || n2 == null)
     {
         return;
     }
     if (n2.PrevNodes.Count == 1 && n2.PrevNodes[0] == n1 && n1.NextNodes.Count == 1 && n1.NextNodes[0] == n2)//合并条件:n2必须是n1的唯一后继,n1必须是n2的唯一前导
     {
         //断开n1与n2的关联
         n1.DelNext(n2);
         //断开n2与其后继的关联,并将其后继添加为n1的后继
         FlowGraphNode[] n2s = new FlowGraphNode[n2.NextNodes.Count];
         n2.NextNodes.CopyTo(n2s, 0);
         foreach (FlowGraphNode n in n2s)
         {
             n2.DelNext(n);
             n1.AddNext(n);
         }
         //n1的结束标签改为n2的结束标签
         n1.EndLabelValue = n2.EndLabelValue;
         //删除流图对n2的记忆
         nodes.Remove(n2.LabelValue);
         accessedNodes.Remove(n2);
         unaccessedNodes.Remove(n2);
         bool inStack = false;
         foreach (FlowGraphNode n in iterateStack)
         {
             if (n == n2)
             {
                 inStack = true;
                 break;
             }
         }
         if (inStack)
         {
             FlowGraphNode[] ns = iterateStack.ToArray();
             iterateStack.Clear();
             for (int i = ns.Length - 1; i >= 0; i--)
             {
                 if (ns[i] != n2)
                 {
                     iterateStack.Push(ns[i]);
                 }
             }
         }
         if (curNode == n2)
         {
             curNode = n1;
             //设置n1为已访问
             AccessNode(n1);
             //删除迭代器栈里的n1
             inStack = false;
             foreach (FlowGraphNode n in iterateStack)
             {
                 if (n == n1)
                 {
                     inStack = true;
                     break;
                 }
             }
             if (inStack)
             {
                 FlowGraphNode[] ns = iterateStack.ToArray();
                 iterateStack.Clear();
                 for (int i = ns.Length - 1; i >= 0; i--)
                 {
                     if (ns[i] != n1)
                     {
                         iterateStack.Push(ns[i]);
                     }
                 }
             }
         }
     }
 }