示例#1
0
        public void GenerateCFG(MethodDefinition method)
        {
            var methodInfo = programInfo.GetOrAdd(method);

            //GenerateIL(method);
            GenerateTAC(method);

            if (!methodInfo.Contains("CFG"))
            {
                // Control-flow
                var cfAnalysis = new ControlFlowAnalysis(method.Body);
                var cfg        = cfAnalysis.GenerateNormalControlFlow();
                //var cfg = cfAnalysis.GenerateExceptionalControlFlow();

                var domAnalysis = new DominanceAnalysis(cfg);
                domAnalysis.Analyze();
                domAnalysis.GenerateDominanceTree();

                //// Optional
                //var loopAnalysis = new NaturalLoopAnalysis(cfg);
                //loopAnalysis.Analyze();

                var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);
                domFrontierAnalysis.Analyze();

                var pdomAnalysis = new PostDominanceAnalysis(cfg);
                pdomAnalysis.Analyze();
                pdomAnalysis.GeneratePostDominanceTree();

                var pdomFrontierAnalysis = new PostDominanceFrontierAnalysis(cfg);
                pdomFrontierAnalysis.Analyze();

                var controlDependenceAnalysis = new ControlDependenceAnalysis(cfg);
                controlDependenceAnalysis.Analyze();

                var text = DGMLSerializer.Serialize(cfg);

                methodInfo.Add("CFG", cfg);
                methodInfo.Add("CFG_TEXT", text);

                text = DGMLSerializer.SerializeDominanceTree(cfg);
                methodInfo.Add("DT_TEXT", text);

                text = DGMLSerializer.SerializePostDominanceTree(cfg);
                methodInfo.Add("PDT_TEXT", text);

                text = DGMLSerializer.SerializeControlDependenceGraph(cfg);
                methodInfo.Add("CDG_TEXT", text);
            }
        }
示例#2
0
        //string _code = String.Empty;

        /// <summary>
        /// For a given methodDefinition, create a CFG and run basic analyses such as
        /// stack removal, SSA transformation, live-variables analysis, and copy-propagation.
        /// </summary>
        /// <param name="methodDefinition"></param>
        /// <returns></returns>
        private ControlFlowGraph PrepareMethod(IMethodDefinition methodDefinition)
        {
            var sw2 = new Stopwatch();

            sw2.Start();
            var disassembler = new Disassembler(mhost, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            var cfAnalysis = new ControlFlowAnalysis(methodBody);
            var cfg        = cfAnalysis.GenerateNormalControlFlow();

            var domAnalysis = new DominanceAnalysis(cfg);

            domAnalysis.Analyze();
            domAnalysis.GenerateDominanceTree();

            var loopAnalysis = new NaturalLoopAnalysis(cfg);

            loopAnalysis.Analyze();

            var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);

            domFrontierAnalysis.Analyze();

            var splitter = new WebAnalysis(cfg, methodDefinition);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var analysis = new TypeInferenceAnalysis(cfg, methodBody.MethodDefinition.Type);

            analysis.Analyze();

            var copyProgapagtion = new ForwardCopyPropagationAnalysis(cfg);

            copyProgapagtion.Analyze();
            copyProgapagtion.Transform(methodBody);

            var backwardCopyProgapagtion = new BackwardCopyPropagationAnalysis(cfg);

            backwardCopyProgapagtion.Analyze();
            backwardCopyProgapagtion.Transform(methodBody);

            var liveVariables = new LiveVariablesAnalysis(cfg);
            var resultLiveVar = liveVariables.Analyze();

            var ssa = new StaticSingleAssignment(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(liveVariables);
            methodBody.UpdateVariables();
            sw2.Stop();
            this.nonAnalysisOverhead = sw2.Elapsed;
            return(cfg);

            //var cfg = ControlFlowGraph.GenerateNormalControlFlow(methodBody);
            //ControlFlowGraph.ComputeDominators(cfg);
            //ControlFlowGraph.IdentifyLoops(cfg);

            //ControlFlowGraph.ComputeDominatorTree(cfg);
            //ControlFlowGraph.ComputeDominanceFrontiers(cfg);

            //// Uniquely rename stack variables.
            //var splitter = new WebAnalysis(cfg);
            //splitter.Analyze();
            //splitter.Transform();

            //methodBody.UpdateVariables();

            //// Infer types for stack variables.
            //var typeAnalysis = new TypeInferenceAnalysis(cfg);
            //typeAnalysis.Analyze();

            //var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);
            //backwardCopyAnalysis.Analyze();
            //backwardCopyAnalysis.Transform(methodBody);

            //var lva = new LiveVariablesAnalysis(cfg);
            //lva.Analyze();

            //var ssa = new StaticSingleAssignmentAnalysis(methodBody, cfg);
            //ssa.Transform();
            //ssa.Prune(lva);

            //methodBody.UpdateVariables();

            ////_code = methodBody.ToString();
            //return cfg;
        }
示例#3
0
        public MethodCfgAndTac AnalyzeIntraProcedural(IMethodDefinition methodDefinition)
        {
            // System.Console.WriteLine("Traversing: {0}", methodDefinition.GetName());
            if (Stubber.SuppressM(methodDefinition))
            {
                return(null);
            }
            if (methodDefinition.IsExternal)
            {
                return(null);
            }
            if (methodDefinition.IsAbstract)
            {
                return(null);
            }

            ITypeDefinition         containingDefn         = methodDefinition.ContainingTypeDefinition;
            ISourceLocationProvider sourceLocationProvider = null;

            if (containingDefn != null)
            {
                IModule mod = TypeHelper.GetDefiningUnit(containingDefn) as IModule;
                if (moduleToPdbMap.ContainsKey(mod))
                {
                    sourceLocationProvider = moduleToPdbMap[mod];
                }
                else
                {
                    if (!(mod == null || mod == Dummy.Module || mod == Dummy.Assembly))
                    {
                        sourceLocationProvider = GetPdbReader(mod.Location);
                        moduleToPdbMap[mod]    = sourceLocationProvider;
                    }
                }
            }
            var disassembler = new Disassembler(host, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            var cfAnalysis = new ControlFlowAnalysis(methodBody);
            // var cfg = cfAnalysis.GenerateNormalControlFlow();
            var cfg = cfAnalysis.GenerateExceptionalControlFlow();

            var domAnalysis = new DominanceAnalysis(cfg);

            domAnalysis.Analyze();
            domAnalysis.GenerateDominanceTree();

            var loopAnalysis = new NaturalLoopAnalysis(cfg);

            loopAnalysis.Analyze();

            var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);

            domFrontierAnalysis.Analyze();

            var splitter = new WebAnalysis(cfg, methodDefinition);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var typeAnalysis = new TypeInferenceAnalysis(cfg, methodDefinition.Type);

            typeAnalysis.Analyze();

            var forwardCopyAnalysis = new ForwardCopyPropagationAnalysis(cfg);

            forwardCopyAnalysis.Analyze();
            forwardCopyAnalysis.Transform(methodBody);

            // backwardCopyAnalysis is buggy - it says so in the source file - see notes in src/test
            // var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);
            // backwardCopyAnalysis.Analyze();
            // backwardCopyAnalysis.Transform(methodBody);

            var liveVariables = new LiveVariablesAnalysis(cfg);

            liveVariables.Analyze();

            var ssa = new StaticSingleAssignment(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(liveVariables);

            methodBody.UpdateVariables();

            MethodCfgAndTac mct = new MethodCfgAndTac(cfg, methodBody);

            foreach (IExceptionHandlerBlock ehInfo in disassembler.GetExceptionHandlers())
            {
                if (ehInfo is CatchExceptionHandler)
                {
                    mct.ehInfoList.Add(ehInfo as CatchExceptionHandler);
                }
            }
            return(mct);
        }
示例#4
0
        public static ControlFlowGraph DoAnalysisPhases(this IMethodDefinition method, IMetadataHost host, ISourceLocationProvider locationProvider,
                                                        IEnumerable <IMethodReference> methodsToTryToInline = null)
        {
            AnalysisStats.extraAnalysisOverHead.Start();
            var disassembler = new Disassembler(host, method, locationProvider);

            var methodBody = disassembler.Execute();

            MethodBodyProvider.Instance.AddBody(method, methodBody);

            if (methodsToTryToInline != null)
            {
                DoInlining(method, host, methodBody, locationProvider, methodsToTryToInline);
            }

            var cfAnalysis = new ControlFlowAnalysis(methodBody);
            //var cfg = cfAnalysis.GenerateExceptionalControlFlow();
            var cfg = cfAnalysis.GenerateNormalControlFlow();

            var domAnalysis = new DominanceAnalysis(cfg);

            domAnalysis.Analyze();
            domAnalysis.GenerateDominanceTree();

            var loopAnalysis = new NaturalLoopAnalysis(cfg);

            loopAnalysis.Analyze();

            var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);

            domFrontierAnalysis.Analyze();

            var splitter = new WebAnalysis(cfg, method);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();


            var analysis = new TypeInferenceAnalysis(cfg, methodBody.MethodDefinition.Type);

            analysis.Analyze();

            var copyProgapagtion = new ForwardCopyPropagationAnalysis(cfg);

            copyProgapagtion.Analyze();
            copyProgapagtion.Transform(methodBody);

            //var backwardCopyProgapagtion = new BackwardCopyPropagationAnalysis(cfg);
            //backwardCopyProgapagtion.Analyze();
            //backwardCopyProgapagtion.Transform(methodBody);

            var liveVariables = new LiveVariablesAnalysis(cfg);
            var resultLiveVar = liveVariables.Analyze();


            var ssa = new StaticSingleAssignment(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(liveVariables);
            methodBody.UpdateVariables();
            AnalysisStats.extraAnalysisOverHead.Stop();
            return(cfg);
        }