4

I am very curios regarding MSIL work. As far as i understand in compile time CLR translates its code to the MSIL in order to provide some optimizations which is later translated by JIT to the machine code.

For example if we have :

    using (TextWriter w = File.CreateText("log.txt"))
{
    w.WriteLine("This is line one");
}

it will translated in MSIL to the following code:

    TextWriter w = File.CreateText("log.txt");
    try
    {
        w.WriteLine("This is line one");
    }
    finally
    {
        bool flag = w == null;
        if (!flag)
        {
            w.Dispose();
        }
    }

I am not sure if it will translated exactly like that but definitely to something similar.

The point is, that i trying to investigate MSIL optimizations. Till this point i have found that MSIL translated to asm code which is not so readable for me.

Is there any tool that shows me translated MSIL in C# code or even in pseudo code ?

Highly appreciate references and related links.

P.S.

I wrote using we see above:

    using (TextWriter w = File.CreateText("log.txt"))
{
    w.WriteLine("This is line one");
}

and compiled it. Then i used Reflector to see the code and i saw the same code i wrote instead to see some optimizations.

The i used LINQPad with the same code. I have found that MSIL output was :

    IL_0001:  ldstr       "log.txt"
IL_0006:  call        System.IO.File.CreateText
IL_000B:  stloc.0     // w
IL_000C:  nop         
IL_000D:  ldloc.0     // w
IL_000E:  ldstr       "This is line one"
IL_0013:  callvirt    System.IO.TextWriter.WriteLine
IL_0018:  nop         
IL_0019:  nop         
IL_001A:  leave.s     IL_002C
IL_001C:  ldloc.0     // w
IL_001D:  ldnull      
IL_001E:  ceq         
IL_0020:  stloc.1     // CS$4$0000
IL_0021:  ldloc.1     // CS$4$0000
IL_0022:  brtrue.s    IL_002B
IL_0024:  ldloc.0     // w
IL_0025:  callvirt    System.IDisposable.Dispose
IL_002A:  nop         
IL_002B:  endfinally  

From this code we see System.IDisposable.Dispose which looks like part of optimization.My intent is to see somehow C# code or pseudo code.

Jviaches
  • 826
  • 3
  • 14
  • 30
  • Not clear what you are looking for - Reflector/ILSpy will decompile IL to C#, but not sure how it would help understanding of IL itself. – Alexei Levenkov Jan 11 '13 at 19:26
  • Reflector showed me same code i wrote without any optimizations. i wrote: using (TextWriter w = File.CreateText("log.txt")) { w.WriteLine("This is line one"); and this is exactly what he showed me } – Jviaches Jan 11 '13 at 19:34
  • A lot of tools mentioned below actually will hide the fact of optimization. They know how to recognize a particular optimization and revert decompiled C# to an original form. If you really want to learn how C# compiler optimizes IL, learn IL. It is not that hard. Just look online about each command one-by-one and in a couple of hours you will get enough understanding to be able to read it. You also could go further and look at x86 assembler code. – Dennis Jan 11 '13 at 20:12

4 Answers4

2

I would recommend Telerik JustDecompile. It'll be "Free forever".

Historically for a long time there was "The Reflector" by Lutz Roeder. But that is now owned by RedGate. You can still obtain it for a hefty price.

Mr. Young
  • 2,364
  • 3
  • 25
  • 41
1

Reflector, ILSpy, dotPeek, and ildasm.

Reflexil is pretty nice plugin for Reflector. But as was wrote Reflector now is paid.

About optimizations:

Is there something to optimize? Did you checked "Optimize code" in project properties? Also JIT compiler does some compilation optimizations. You can get machine code of your method using SOS extentions:

Retrieve JIT output

How do I see what the JIT compiled, anyway?

Debugging with SOS, SOSEX and Dumps in Visual Studio

Community
  • 1
  • 1
Alexander Balte
  • 900
  • 5
  • 11
0

I recommend IlSpy, it's free and reliable

http://ilspy.net/

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
0

ILSpy (http://ilspy.net/) is good and free, and you can check the source code if you want too. Reflector is good but not free any more (avec expensive).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Stephane Halimi
  • 408
  • 3
  • 5