Пример #1
0
 public static IBlockStatement GetRidOfStack(SourceMethodBody methodBody, IBlockStatement block)
 {
     var me = new Unstacker(methodBody);
     var result = me.Rewrite(block);
     var stmts = new List<IStatement>();
     foreach (var loc in me.createdLocals.Values)
     {
         var decl = new LocalDeclarationStatement()
         {
             InitialValue = null,
             LocalVariable = loc,
         };
         stmts.Add(decl);
     }
     stmts.AddRange(result.Statements);
     var newBlock = new BlockStatement()
     {
         Statements = stmts,
         Locations = new List<ILocation>(result.Locations),
     };
     return newBlock;
 }
Пример #2
0
        public static IBlockStatement GetRidOfStack(SourceMethodBody methodBody, IBlockStatement block)
        {
            var me     = new Unstacker(methodBody);
            var result = me.Rewrite(block);
            var stmts  = new List <IStatement>();

            foreach (var loc in me.createdLocals.Values)
            {
                var decl = new LocalDeclarationStatement()
                {
                    InitialValue  = null,
                    LocalVariable = loc,
                };
                stmts.Add(decl);
            }
            stmts.AddRange(result.Statements);
            var newBlock = new BlockStatement()
            {
                Statements = stmts,
                Locations  = new List <ILocation>(result.Locations),
            };

            return(newBlock);
        }
Пример #3
0
        /// <summary>
        /// Decompile the IL operations of this method body into a block of statements.
        /// </summary>
        protected override IBlockStatement GetBlock()
        {
            var block = new DecompiledBlock(0, this.ilMethodBody.Size, new Sublist <BasicBlock <Instruction> >(this.cdfg.AllBlocks, 0, this.cdfg.AllBlocks.Count), isLexicalScope: true);

            this.CreateExceptionBlocks(block);
            bool addDeclarations = true;

            if (this.localScopeProvider != null)
            {
                var scopes = new List <ILocalScope>(this.localScopeProvider.GetLocalScopes(this.ilMethodBody));
                if (scopes.Count > 0)
                {
                    this.CreateLexicalScopes(block, new Sublist <ILocalScope>(scopes, 0, scopes.Count));
                    addDeclarations = false;
                }
            }
            if (addDeclarations)
            {
                int counter = 0;
                foreach (var local in this.ilMethodBody.LocalVariables)
                {
                    Contract.Assume(local != null);
                    Contract.Assume(counter <= block.Statements.Count);
                    block.Statements.Insert(counter++, new LocalDeclarationStatement()
                    {
                        LocalVariable = this.GetLocalWithSourceName(local)
                    });
                }
            }
            new InstructionParser(this).Traverse(block);
            new SwitchReplacer(this).Traverse(block);
            DeleteNops(block);
            DeleteLocalAssignedLocal(block);
            new PatternReplacer(this, block).Traverse(block);
            new TryCatchReplacer(this, block).Traverse(block);
            new RemoveNonLexicalBlocks().Traverse(block);
            new DeclarationUnifier(this).Traverse(block);
            new ResourceUseReplacer(this).Traverse(block);
            new IfThenElseReplacer(this).Traverse(block);
            if ((this.options & DecompilerOptions.Loops) != 0)
            {
                new WhileLoopReplacer(this).Traverse(block);
                new ForLoopReplacer(this).Traverse(block);
                new ForEachLoopReplacer(this).Traverse(block);
            }
            new UnreferencedLabelRemover(this).Traverse(block);
            new LockReplacer(this).Traverse(block);
            new BlockFlattener().Traverse(block);
            this.RemoveRedundantFinalReturn(block);
            var result = new CompilationArtifactRemover(this).Rewrite(block);

            if ((this.options & DecompilerOptions.AnonymousDelegates) != 0)
            {
                bool didNothing;
                result = new AnonymousDelegateInserter(this).InsertAnonymousDelegates(result, out didNothing);
                if (!didNothing)
                {
                    new DeclarationUnifier(this).Traverse(result);
                }
            }
            this.AddBackFirstNop(result as BlockStatement);
            if ((this.options & DecompilerOptions.Unstack) != 0)
            {
                result = Unstacker.GetRidOfStack(this, result);
            }
            return(result);
        }