Example #1
0
        private BlockStatement DecompileMethod(MethodBody body, out MethodSpecificContext methodContext)
        {
            methodContext = null;

            BlockStatement block;

            try
            {
                DecompilationContext decompilationContext =
                    new DecompilationContext(new MethodSpecificContext(body), this.typeContext ?? new TypeSpecificContext(body.Method.DeclaringType));

                DecompilationPipeline pipeline = this.language.CreatePipeline(body.Method, decompilationContext);

                methodContext = pipeline.Run(body).MethodContext;

                block = pipeline.Body;
            }
            catch (Exception ex)
            {
                this.ExceptionsWhileDecompiling.Add(body.Method);

                methodContext = new MethodSpecificContext(body);

                block = new BlockStatement();
                block.AddStatement(new ExceptionStatement(ex, body.Method));

                OnExceptionThrown(ex);
            }

            return(block);
        }
Example #2
0
 internal static BlockStatement DecompileLambda(this MethodBody body, ILanguage language, DecompilationContext context)
 {
     MethodDefinition method = null;
     if (body != null)
     {
         method = body.Method;
     }
     DecompilationPipeline pipeline = language.CreateLambdaPipeline(context);
     return RunPipeline(pipeline, language, body, out context);
 }
        /// <summary>
        /// Decompiles <paramref name="body"/>, using the preset <paramref name="context"/>.
        /// </summary>
        /// <param name="body">The body of the method, to be decompiled.</param>
        /// <param name="language">The language to which is decompiled.</param>
        /// <param name="context">The context for the decompilation.</param>
        /// <returns>Returns the expression-statement tree, representing the decompiled method.</returns>
        public static BlockStatement Decompile(this MethodBody body, ILanguage language, DecompilationContext context)
        {
            MethodDefinition method = null;

            if (body != null)
            {
                method = body.Method;
            }
            DecompilationPipeline pipeline = language.CreatePipeline(method, context);

            return(RunPipeline(pipeline, language, body, out context));
        }
        private bool CheckMethodAndDecompile(MethodDefinition methodDef, out BlockStatement methodBody)
        {
            if (!methodDef.HasParameters ||
                methodDef.Parameters.Count != 1)
            {
                methodBody = null;
                return(false);
            }

            DecompilationPipeline pipeline = BaseLanguage.IntermediateRepresenationPipeline;

            pipeline.Run(methodDef.Body);
            methodBody = pipeline.Body;
            return(true);
        }
Example #5
0
        private BlockStatement DecompileMethodPartially(MethodBody body, out DecompilationContext context, bool needDecompiledMember = false)
        {
            context = null;

            if (this.IsCachingEnabled && this.cacheService.IsDecompiledMemberInCache(body.Method, this.language, this.renameInvalidMembers))
            {
                CachedDecompiledMember cachedDecompiledMember = this.cacheService.GetDecompiledMemberFromCache(body.Method, this.language, this.renameInvalidMembers);
                return(cachedDecompiledMember.Member.Statement as BlockStatement);
            }

            //Performance improvement
            ControlFlowGraph cfg = new ControlFlowGraphBuilder(body.Method).CreateGraph();

            if (cfg.Blocks.Length > 2)
            {
                return(null);
            }

            BlockStatement block;

            try
            {
                DecompilationPipeline pipeline;
                DecompilationContext  decompilationContext =
                    new DecompilationContext(new MethodSpecificContext(body), this.typeContext ?? new TypeSpecificContext(body.Method.DeclaringType));
                if (!needDecompiledMember)
                {
                    decompilationContext.MethodContext.EnableEventAnalysis = false;
                }

                pipeline = new DecompilationPipeline(BaseLanguage.IntermediateRepresenationPipeline.Steps, decompilationContext);

                context = pipeline.Run(body);

                block = pipeline.Body;
            }
            catch (Exception ex)
            {
                this.ExceptionsWhileDecompiling.Add(body.Method);

                block = new BlockStatement();
                block.AddStatement(new ExceptionStatement(ex, body.Method));

                OnExceptionThrown(ex);
            }

            return(block);
        }
Example #6
0
        private static BlockStatement DecompileStateMachine(this MethodBody body, DecompilationContext enclosingContext,
            BaseStateMachineRemoverStep removeStateMachineStep, Func<DecompilationContext, IStateMachineData> stateMachineDataSelector,
            out DecompilationContext decompilationContext)
        {
            DecompilationPipeline thePipeline = GetStateMachineRemovalPipeline(removeStateMachineStep, stateMachineDataSelector);
            decompilationContext = thePipeline.Run(body, enclosingContext.Language);

            enclosingContext.MethodContext.Variables.AddRange(decompilationContext.MethodContext.Variables);
            enclosingContext.MethodContext.VariableDefinitionToNameMap.AddRange(decompilationContext.MethodContext.VariableDefinitionToNameMap);
            enclosingContext.MethodContext.AddInnerMethodParametersToContext(decompilationContext.MethodContext);
            enclosingContext.MethodContext.VariableAssignmentData.AddRange(decompilationContext.MethodContext.VariableAssignmentData);
            enclosingContext.MethodContext.GotoLabels.AddRange(decompilationContext.MethodContext.GotoLabels);
            enclosingContext.MethodContext.GotoStatements.AddRange(decompilationContext.MethodContext.GotoStatements);
            BlockStatement theBlockStatement = thePipeline.Body;
            return theBlockStatement;
        }
        private static DecompilationPipeline GetStateMachineRemovalPipeline(IStateMachineRemoverStep removeStateMachineStep,
                                                                            Func <DecompilationContext, IStateMachineData> stateMachineDataSelector)
        {
            DecompilationPipeline     intermediatePipeline = BaseLanguage.IntermediateRepresenationPipeline;
            List <IDecompilationStep> newSteps             = new List <IDecompilationStep>();

            newSteps.Add(removeStateMachineStep);
            foreach (IDecompilationStep step in intermediatePipeline.Steps)
            {
                newSteps.Add(step);
                if (step is VariableAssignmentAnalysisStep)
                {
                    newSteps.Add(new FieldAssignmentAnalysisStep(stateMachineDataSelector));
                }
            }

            return(new DecompilationPipeline(newSteps));
        }
Example #8
0
        private BlockStatement GetStatements(MethodBody body)
        {
            //Performance improvement
            ControlFlowGraph cfg = new ControlFlowGraphBuilder(body.Method).CreateGraph();

            if (cfg.Blocks.Length > 2)
            {
                return(null);
            }

            DecompilationPipeline pipeline = new DecompilationPipeline(BaseLanguage.IntermediateRepresenationPipeline.Steps,
                                                                       new DecompilationContext(new MethodSpecificContext(body)
            {
                EnableEventAnalysis = false
            }, new TypeSpecificContext(body.Method.DeclaringType)));

            pipeline.Run(body);
            return(pipeline.Body);
        }
Example #9
0
 private BlockStatement DecompileMethodPartially(MethodBody body, out DecompilationContext context, bool needDecompiledMember = false)
 {
     context = null;
     if (this.get_IsCachingEnabled() && this.cacheService.IsDecompiledMemberInCache(body.get_Method(), this.language, this.renameInvalidMembers))
     {
         return(this.cacheService.GetDecompiledMemberFromCache(body.get_Method(), this.language, this.renameInvalidMembers).get_Member().get_Statement() as BlockStatement);
     }
     if ((int)(new ControlFlowGraphBuilder(body.get_Method())).CreateGraph().get_Blocks().Length > 2)
     {
         return(null);
     }
     try
     {
         stackVariable13 = new MethodSpecificContext(body);
         stackVariable15 = this.typeContext;
         if (stackVariable15 == null)
         {
             dummyVar0       = stackVariable15;
             stackVariable15 = new TypeSpecificContext(body.get_Method().get_DeclaringType());
         }
         V_2 = new DecompilationContext(stackVariable13, stackVariable15, this.language);
         if (!needDecompiledMember)
         {
             V_2.get_MethodContext().set_EnableEventAnalysis(false);
         }
         V_1     = new DecompilationPipeline(BaseLanguage.get_IntermediateRepresenationPipeline().get_Steps(), V_2);
         context = V_1.Run(body, this.language);
         V_0     = V_1.get_Body();
     }
     catch (Exception exception_0)
     {
         V_3 = exception_0;
         this.get_ExceptionsWhileDecompiling().Add(body.get_Method());
         V_0 = new BlockStatement();
         V_0.AddStatement(new ExceptionStatement(V_3, body.get_Method()));
         this.OnExceptionThrown(V_3);
     }
     return(V_0);
 }
        private static BlockStatement DecompileStateMachine(this MethodBody body, MethodSpecificContext enclosingMethodContext,
                                                            IStateMachineRemoverStep removeStateMachineStep,
                                                            Func <DecompilationContext, IStateMachineData> stateMachineDataSelector,
                                                            out DecompilationContext decompilationContext)
        {
            ILanguage language = CSharp.GetLanguage(CSharpVersion.V4);

            removeStateMachineStep.Language = language;

            DecompilationPipeline thePipeline = GetStateMachineRemovalPipeline(removeStateMachineStep, stateMachineDataSelector);

            decompilationContext = thePipeline.Run(body, language);

            enclosingMethodContext.Variables.AddRange(decompilationContext.MethodContext.Variables);
            enclosingMethodContext.VariableDefinitionToNameMap.AddRange(decompilationContext.MethodContext.VariableDefinitionToNameMap);
            enclosingMethodContext.AddInnerMethodParametersToContext(decompilationContext.MethodContext);
            enclosingMethodContext.VariableAssignmentData.AddRange(decompilationContext.MethodContext.VariableAssignmentData);
            enclosingMethodContext.GotoLabels.AddRange(decompilationContext.MethodContext.GotoLabels);
            enclosingMethodContext.GotoStatements.AddRange(decompilationContext.MethodContext.GotoStatements);
            BlockStatement theBlockStatement = thePipeline.Body;

            return(theBlockStatement);
        }
 static BlockStatement RunPipeline(DecompilationPipeline pipeline, ILanguage language, MethodBody body, out DecompilationContext context)
 {
     context = pipeline.Run(body, language);
     return(pipeline.Body);
 }
 public virtual DecompilationPipeline CreatePipeline(MethodDefinition method, DecompilationContext context)
 {
     DecompilationPipeline result = new DecompilationPipeline(IntermediateRepresenationPipeline.Steps, context);
     return result;
 }
 public virtual DecompilationPipeline CreatePipeline(MethodDefinition method)
 {
     DecompilationPipeline result = new DecompilationPipeline();
     result.AddSteps(IntermediateRepresenationPipeline.Steps);
     return result;
 }
 private DecompilationPipeline CreatePipelineInternal(DecompilationContext context, bool inlineAggressively)
 {
     DecompilationPipeline result = new DecompilationPipeline(BaseLanguage.IntermediateRepresenationPipeline.Steps, context);
     result.AddSteps(LanguageDecompilationSteps(inlineAggressively));
     return result;
 }
        private BlockStatement DecompileMethodPartially(MethodBody body, out DecompilationContext context, bool needDecompiledMember = false)
        {
            context = null;

            if (this.IsCachingEnabled && this.cacheService.IsDecompiledMemberInCache(body.Method, this.language, this.renameInvalidMembers))
            {
                CachedDecompiledMember cachedDecompiledMember = this.cacheService.GetDecompiledMemberFromCache(body.Method, this.language, this.renameInvalidMembers);
                return cachedDecompiledMember.Member.Statement as BlockStatement;
            }

            //Performance improvement
            ControlFlowGraph cfg = new ControlFlowGraphBuilder(body.Method).CreateGraph();
            if (cfg.Blocks.Length > 2)
            {
                return null;
            }

            BlockStatement block;
            try
            {
                DecompilationPipeline pipeline;
                DecompilationContext decompilationContext =
                    new DecompilationContext(new MethodSpecificContext(body), this.typeContext ?? new TypeSpecificContext(body.Method.DeclaringType), this.language);
                if (!needDecompiledMember)
                {
                    decompilationContext.MethodContext.EnableEventAnalysis = false;
                }

                pipeline = new DecompilationPipeline(BaseLanguage.IntermediateRepresenationPipeline.Steps, decompilationContext);

                context = pipeline.Run(body, this.language);

                block = pipeline.Body;
            }
            catch (Exception ex)
            {
                this.ExceptionsWhileDecompiling.Add(body.Method);

                block = new BlockStatement();
                block.AddStatement(new ExceptionStatement(ex, body.Method));

                OnExceptionThrown(ex);
            }

            return block;
        }
        private BlockStatement GetStatements(MethodBody body)
        {
            //Performance improvement
            ControlFlowGraph cfg = new ControlFlowGraphBuilder(body.Method).CreateGraph();
            if (cfg.Blocks.Length > 2)
            {
                return null;
            }

            DecompilationPipeline pipeline = new DecompilationPipeline(BaseLanguage.IntermediateRepresenationPipeline.Steps,
                new DecompilationContext(new MethodSpecificContext(body) { EnableEventAnalysis = false }, new TypeSpecificContext(body.Method.DeclaringType))) ;

            pipeline.Run(body);
            return pipeline.Body;
        }
 public virtual DecompilationPipeline CreatePipeline()
 {
     DecompilationPipeline result = new DecompilationPipeline();
     result.AddSteps(BaseLanguage.IntermediateRepresenationPipeline.Steps);
     result.AddSteps(LanguageDecompilationSteps(false));
     return result;
 }