SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

[SOLVED] Don't want to remove dead stores

Last post 09-20-2016 2:54 PM by indexless. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 09-09-2016 2:31 PM

    • indexless
    • Not Ranked
    • Joined on 02-05-2015
    • Russia Moscow reg., Dubna
    • Posts 2

    [SOLVED] Don't want to remove dead stores

    Hi there,

    I'd like the decompiled C# code emitted by ILSpy to be as close to the original one as possible. In particular, I don't want any code to be deleted. For example, I want the code like

    internal sealed class Abc
    {
            public static void Main()
            {
                    var abc = "string1";
            }
    }

    to be fully restored. As it said in the Architecture Overview, all the optimization is encoded in ILAstOptimization.cs so is performed over the IL AST. I tried to disable it (just commented out the following code):

                // ILAstOptimizer bodyGraph = new ILAstOptimizer();
                // bodyGraph.Optimize(context, ilMethod);
                // context.CancellationToken.ThrowIfCancellationRequested();

    in ICSharpCode.Decompiler.Ast.AstMethodBodyBuilder.CreateMethodBody(), but unfortunately ended up with invalid C#:

        public static void Main()
        {
             arg_06_0 = "string1";
            string abc = arg_06_0;
            return;
        }

    I also found in the Overview mentioned above something about restriction of optimization which might be helpful:

    "Dead store removal. If a variable is stored and nobody is there to read it, then was it really written?
    Originally we removed all such dead stores; but after some users complained about 'missing code', we restricted this optimization to apply only to stack locations. Dead stores to stack locations occur mainly after the removal of 'pop' instructions."

    So could that restriction be unapplied, making the unused automatic variables also appearing in the emitted C# code?

    Thank you!

  • 09-20-2016 2:54 PM In reply to

    • indexless
    • Not Ranked
    • Joined on 02-05-2015
    • Russia Moscow reg., Dubna
    • Posts 2

    Re: Don't want to remove dead stores

    Probably I've found a solution: the dead stores can be preserved in ICSharpCode.Decompiler.ILAst.ILInlining.InlineOneIfPossible() method, by replcaing the lines (under '// Remove completely' comment):

        AnalyzeNode(body[pos], -1);
        body.RemoveAt(pos);
        return true;

    with just

        return false;

Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.