Esempio n. 1
0
        private bool performPatternReplacing()
        {
            bool result = false;

            foreach (BasicBlock block in blockList)
            {
                NodeArray body = block.Body;

                bool flag = true;
                while (flag)
                {
                    flag = false;
                    for (int i = 0; i < body.Count - 1 && !flag; i++)
                    {
                        Node n1 = body[i];

                        if (n1 is Branch && (n1 as Branch).Next == (n1 as Branch).Alt)
                        {
                            result = flag = true;
                            Node n = newRemoveStackTop(block);
                            body[i] = n;
                            n1.ReplaceByNode(n);
                            n.Next = n1.Next;
                            n1.RemoveFromGraph();
                        }
                        else
                        {
                            Node n2 = body[i + 1];

                            if (n1 is StoreVar && n2 is LoadVar &&
                                (n1 as ManageVar).Var == (n2 as ManageVar).Var)
                            {
                                result = flag = true;
                                Node n = newDuplicateStackTop(block);
                                body[i]     = n;
                                body[i + 1] = n1;
                                n1.ReplaceByNode(n);
                                n.Next  = n1;
                                n1.Next = n2.Next;
                                n2.RemoveFromGraph();
                            }
                            else if (detectedDoubleLoading(n1, n2))
                            {
                                result = flag = true;
                                Node n = newDuplicateStackTop(block);
                                body[i + 1] = n;
                                n2.ReplaceByNode(n);
                                n.Next = n2.Next;
                                n2.RemoveFromGraph();
                            }
                            else if (detectedDoubleCasting(n1, n2))
                            {
                                result = flag = true;
                                body.RemoveAt(i + 1);
                                n1.Next = n2.Next;
                                n2.RemoveFromGraph();
                            }
                            else if (n1 is StoreVar && n2 is Leave &&
                                     n2.Parent is MethodBodyBlock)
                            {
                                result = flag = true;
                                Node n = newRemoveStackTop(block);
                                body[i] = n;
                                n1.ReplaceByNode(n);
                                n1.RemoveFromGraph();
                                n.Next = n2;
                            }
                            else if (detectedReplacementByPop(n1, n2))
                            {
                                result = flag = true;
                                Node n = newRemoveStackTop(block);
                                body.RemoveAt(i + 1);
                                body[i] = n;
                                n1.ReplaceByNode(n);
                                n1.RemoveFromGraph();
                                n.Next = n2.Next;
                                n2.RemoveFromGraph();
                            }
                            else if (detectedReplacementByDoublePop(n1, n2))
                            {
                                result = flag = true;
                                Node new1 = newRemoveStackTop(block),
                                     new2 = newRemoveStackTop(block);
                                body[i]     = new1;
                                body[i + 1] = new2;
                                n1.ReplaceByNode(new1);
                                n1.RemoveFromGraph();
                                new1.Next = new2;
                                new2.Next = n2.Next;
                                n2.RemoveFromGraph();
                            }
                            else if (detectedRemoval(n1, n2))
                            {
                                result = flag = true;
                                body.RemoveAt(i + 1);
                                body.RemoveAt(i);
                                n1.ReplaceByNode(n2.Next);
                                n1.RemoveFromGraph();
                                n2.RemoveFromGraph();
                            }
                            else if (detectedInitValueReplacement(n1, n2))
                            {
                                result = flag = true;
                                Variable var  = (n1 as LoadVarAddr).Var;
                                Type     type = (n2 as InitValue).Type;

                                object constant = null;
                                if (type == typeof(Int64))
                                {
                                    constant = (Int64)0;
                                }
                                else if (type == typeof(Single) || type == typeof(Double))
                                {
                                    constant = (Double)0;
                                }
                                else
                                {
                                    constant = (Int32)0;
                                }

                                Node new1 = new LoadConst(constant),
                                     new2 = new StoreVar(var);
                                new1.Options[BasicBlock.BASIC_BLOCK_OPTION] = block;
                                new2.Options[BasicBlock.BASIC_BLOCK_OPTION] = block;

                                body[i]     = new1;
                                body[i + 1] = new2;
                                n1.ReplaceByNode(new1);
                                n1.RemoveFromGraph();
                                new1.Next = new2;
                                new2.Next = n2.Next;
                                n2.RemoveFromGraph();
                            }
                            else if (i < body.Count - 2)
                            {
                                Node n3 = body[i + 2];

                                if (i < body.Count - 3)
                                {
                                    Node n4 = body[i + 3];

                                    if (detectedOperandExchange(n1, n2, n3, n4))
                                    {
                                        result = flag = true;
                                        Node n = newDuplicateStackTop(block);
                                        body[i]     = n;
                                        body[i + 1] = n1;
                                        body[i + 2] = n2;
                                        body[i + 3] = n4;
                                        n3.RemoveFromGraph();
                                        n1.ReplaceByNode(n);
                                        n.Next  = n1;
                                        n2.Next = n4;
                                    }
                                    else if (detectedQuadruple1(n1, n2, n3, n4))
                                    {
                                        result = flag = true;
                                        Node n = newDuplicateStackTop(block);
                                        body[i]     = n;
                                        body[i + 1] = n1;
                                        body[i + 2] = n2;
                                        body[i + 3] = n3;
                                        n1.ReplaceByNode(n);
                                        n.Next  = n1;
                                        n3.Next = n4.Next;
                                        n4.RemoveFromGraph();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }