SourceToIL is unable to decompile loops?

Dec 4, 2009 at 2:57 PM

I'm using the very latest version of Ast+Metadata at the moment of writing these lines (namely: rev 35368). While playing with PeToText sample I've stumbled across an issue - it seems to be unable to decompile a simple loop, namely:

for (var i = 0; i < 5; ++i)

What I get instead of a "for" statement is basically a couple of expressions glued with gotos:

public void Test3()
            int i = 0;
            goto IL_0012;
            i = (i + 1);
            if ((i < 5))
                goto IL_0005;


    IL_0000 ... IL_001b {
        bool local_1
    IL_0001 ... IL_0019 {
        int i

    Playground.cs(35:9)-(35:10): {
    IL_0000: Nop

    Playground.cs(36:18)-(36:28): var i = 0;
    IL_0001: Ldc_I4_0
    IL_0002: Stloc_0 i

    IL_0003: Br_S IL_0012

    Playground.cs(37:13)-(37:14): {
    IL_0005: Nop

    Playground.cs(38:17)-(38:34): Console.Write(i);
    IL_0006: Ldloc_0 i
    IL_0007: Call System.Void System.Console.Write(System.Int32)
    IL_000c: Nop

    Playground.cs(39:13)-(39:14): }
    IL_000d: Nop

    Playground.cs(36:36)-(36:39): ++i
    IL_000e: Ldloc_0 i
    IL_000f: Ldc_I4_1
    IL_0010: Add
    IL_0011: Stloc_0 i

    Playground.cs(36:29)-(36:34): i < 5
    IL_0012: Ldloc_0 i
    IL_0013: Ldc_I4_5
    IL_0014: Clt
    IL_0016: Stloc_1 local_1

    IL_0017: Ldloc_1 local_1
    IL_0018: Brtrue_S IL_0005

    Playground.cs(40:9)-(40:10): }
    IL_001a: Ret

It seems strange to me since I've spotted the IForStatement in Microsoft.Cci.CodeModel, so it's not like CCI isn't supporting loops by design. Now I'm digging into SourceMethodBody sources in the quest to find out more about how it works, but maybe I'm overlooking something simple. Please, comment on this issue.

Dec 4, 2009 at 3:33 PM

CCI is a work in progress and full decompilation has quite a way to go. At the moment our focus is to get IL-CodeModel-IL working, in order to facilitate IL rewriting at the CodeModel level.

Any help from decompilation experts will be much appreciated.

Apr 5, 2011 at 8:33 AM

Has there been any progress in this area since 2009?  I've also discovered that when I disassemble code my for loops aren't reconstructed.  For my application I need to reconstuct more of the original algorithm structure, rather than working at the level of "goto spaghetti".  But I don't want to reinvent the wheel.  So are there utility methods lurking in CCI that will attempt to reconstitute IForStatement instances, for example, and I just need to switch on this processing? Or has someone already extended the library to do this? Or does the library still require me to attempt this myself?


Apr 5, 2011 at 2:22 PM

I'm afraid that there has been very little progress. There is nothing lurking in the the library itself and I am not aware of other projects that have done this.

Nor should you expect the current set of developers to address this issue any time soon.