I am very interested in the work of MSIL. As I understand it, at compile time, the CLR translates its code into MSIL to provide some optimizations that JIT later translates into machine code.
For example, if we have:
using (TextWriter w = File.CreateText("log.txt")) { w.WriteLine("This is line one"); }
it will be converted to MSIL with 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 that it will be translated exactly like that, but definitely to something like that.
The thing is, I'm trying to research the MSIL optimization. Until that moment, I found that MSIL was translated into asm code, which is not readable for me.
Is there any tool that shows that I have translated MSIL into C # code or even pseudo code?
Appreciate links and links to them.
PS
I wrote using the 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 that I wrote instead to see some optimizations.
I used LINQPad with the same code. I found that the output of MSIL is:
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 the optimization. My intention is to somehow see the C # code or pseudocode.