Beispiel #1
0
        public MethodBody Execute()
        {
            MethodBody bytecodeBody = new MethodBody(MethodBodyKind.Bytecode);

            bytecodeBody.Parameters.AddRange(_tacBody.Parameters);
            bytecodeBody.LocalVariables.AddRange(_tacBody.LocalVariables);
            //bytecodeBody.ExceptionInformation.AddRange(_tacBody.ExceptionInformation);
            bytecodeBody.MaxStack = 0;

            if (_tacBody.Instructions.Count > 0)
            {
                InstructionConverter instructionConverter = new InstructionConverter();
                ControlFlowAnalysis  cfanalysis           = new ControlFlowAnalysis(_tacBody);
                // exceptions disabled for now
                ControlFlowGraph cfg = cfanalysis.GenerateNormalControlFlow();

                //FillExceptionHandlersStart();
                foreach (CFGNode node in cfg.ForwardOrder)
                {
                    ProcessBasicBlock(bytecodeBody, node, instructionConverter);
                }

                bytecodeBody.Instructions.AddRange(instructionConverter.Result);
            }

            return(bytecodeBody);
        }
Beispiel #2
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);
            }
        }
Beispiel #3
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;
        }
Beispiel #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);
        }