Browse Source

Fix #861: Failure to decompile for loop with conditional early return

pull/987/head
Siegfried Pammer 8 years ago
parent
commit
69f2609bff
  1. 21
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs
  2. 89
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.il
  3. 73
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.il
  4. 85
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.roslyn.il
  5. 103
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.roslyn.il
  6. 11
      ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs
  7. 30
      ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs

21
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs

@ -264,8 +264,18 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -264,8 +264,18 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
public class Item
{
}
private IEnumerable<string> alternatives;
private void TryGetItem(int id, out Item item)
{
item = null;
}
private static void Operation(ref int item)
{
}
@ -654,5 +664,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -654,5 +664,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Environment.GetCommandLineArgs();
}
public void ForLoopWithEarlyReturn(int[] ids)
{
for (int i = 0; i < ids.Length; i++) {
Item item = null;
this.TryGetItem(ids[i], out item);
if (item == null) {
break;
}
}
}
}
}

89
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.il

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly fawxdzyv
.assembly pk2dgaaq
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
@ -20,15 +20,15 @@ @@ -20,15 +20,15 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module fawxdzyv.dll
// MVID: {8B0E086E-9CF4-420F-8491-C1950C43B6C0}
.module pk2dgaaq.dll
// MVID: {85B97923-960B-4CE8-8A96-24E1123E3B2C}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x03590000
// Image base: 0x00C10000
// =============== CLASS MEMBERS DECLARATION ===================
@ -633,6 +633,21 @@ @@ -633,6 +633,21 @@
} // end of property DataItem::Property
} // end of class DataItem
.class auto ansi nested public beforefieldinit Item
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Item::.ctor
} // end of class Item
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass1'
extends [mscorlib]System.Object
{
@ -668,6 +683,19 @@ @@ -668,6 +683,19 @@
} // end of class '<>c__DisplayClass1'
.field private class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives
.method private hidebysig instance void
TryGetItem(int32 id,
[out] class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item& item) cil managed
{
// Code size 5 (0x5)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.2
IL_0002: ldnull
IL_0003: stind.ref
IL_0004: ret
} // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed
{
// Code size 2 (0x2)
@ -2267,6 +2295,59 @@ @@ -2267,6 +2295,59 @@
IL_0042: ret
} // end of method Loops::ReturnFromDoWhileInTryFinally
.method public hidebysig instance void
ForLoopWithEarlyReturn(int32[] ids) cil managed
{
// Code size 50 (0x32)
.maxstack 3
.locals init (int32 V_0,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item V_1,
bool V_2)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: br.s IL_0027
IL_0005: nop
IL_0006: ldnull
IL_0007: stloc.1
IL_0008: ldarg.0
IL_0009: ldarg.1
IL_000a: ldloc.0
IL_000b: ldelem.i4
IL_000c: ldloca.s V_1
IL_000e: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::TryGetItem(int32,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item&)
IL_0013: nop
IL_0014: ldloc.1
IL_0015: ldnull
IL_0016: ceq
IL_0018: ldc.i4.0
IL_0019: ceq
IL_001b: stloc.2
IL_001c: ldloc.2
IL_001d: brtrue.s IL_0022
IL_001f: nop
IL_0020: br.s IL_0031
IL_0022: nop
IL_0023: ldloc.0
IL_0024: ldc.i4.1
IL_0025: add
IL_0026: stloc.0
IL_0027: ldloc.0
IL_0028: ldarg.1
IL_0029: ldlen
IL_002a: conv.i4
IL_002b: clt
IL_002d: stloc.2
IL_002e: ldloc.2
IL_002f: brtrue.s IL_0005
IL_0031: ret
} // end of method Loops::ForLoopWithEarlyReturn
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

73
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.il

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly w2cet5qb
.assembly nk23jmlw
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
@ -20,15 +20,15 @@ @@ -20,15 +20,15 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module w2cet5qb.dll
// MVID: {F3696C2D-E180-4496-B78F-ACC7FD27833A}
.module nk23jmlw.dll
// MVID: {AA004973-46E8-4F60-871C-9F7A919FE9EF}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x04C90000
// Image base: 0x01190000
// =============== CLASS MEMBERS DECLARATION ===================
@ -549,6 +549,21 @@ @@ -549,6 +549,21 @@
} // end of property DataItem::Property
} // end of class DataItem
.class auto ansi nested public beforefieldinit Item
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Item::.ctor
} // end of class Item
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass1'
extends [mscorlib]System.Object
{
@ -579,6 +594,18 @@ @@ -579,6 +594,18 @@
} // end of class '<>c__DisplayClass1'
.field private class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives
.method private hidebysig instance void
TryGetItem(int32 id,
[out] class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item& item) cil managed
{
// Code size 4 (0x4)
.maxstack 8
IL_0000: ldarg.2
IL_0001: ldnull
IL_0002: stind.ref
IL_0003: ret
} // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed
{
// Code size 1 (0x1)
@ -1754,6 +1781,44 @@ @@ -1754,6 +1781,44 @@
IL_0031: ret
} // end of method Loops::ReturnFromDoWhileInTryFinally
.method public hidebysig instance void
ForLoopWithEarlyReturn(int32[] ids) cil managed
{
// Code size 32 (0x20)
.maxstack 3
.locals init (int32 V_0,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item V_1)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: br.s IL_0019
IL_0004: ldnull
IL_0005: stloc.1
IL_0006: ldarg.0
IL_0007: ldarg.1
IL_0008: ldloc.0
IL_0009: ldelem.i4
IL_000a: ldloca.s V_1
IL_000c: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::TryGetItem(int32,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item&)
IL_0011: ldloc.1
IL_0012: brtrue.s IL_0015
IL_0014: ret
IL_0015: ldloc.0
IL_0016: ldc.i4.1
IL_0017: add
IL_0018: stloc.0
IL_0019: ldloc.0
IL_001a: ldarg.1
IL_001b: ldlen
IL_001c: conv.i4
IL_001d: blt.s IL_0004
IL_001f: ret
} // end of method Loops::ForLoopWithEarlyReturn
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

85
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.roslyn.il

@ -25,14 +25,14 @@ @@ -25,14 +25,14 @@
.ver 0:0:0:0
}
.module Loops.dll
// MVID: {54789F7A-02C9-433E-A0D4-8F5763DBAB55}
// MVID: {C5E69070-3D7F-4B1F-B9FD-172AC1104EF4}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x034F0000
// Image base: 0x00D80000
// =============== CLASS MEMBERS DECLARATION ===================
@ -553,7 +553,22 @@ @@ -553,7 +553,22 @@
} // end of property DataItem::Property
} // end of class DataItem
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass26_0'
.class auto ansi nested public beforefieldinit Item
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Item::.ctor
} // end of class Item
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass28_0'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
@ -566,7 +581,7 @@ @@ -566,7 +581,7 @@
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method '<>c__DisplayClass26_0'::.ctor
} // end of method '<>c__DisplayClass28_0'::.ctor
.method assembly hidebysig instance bool
'<ForeachWithCapturedVariable>b__0'() cil managed
@ -574,15 +589,27 @@ @@ -574,15 +589,27 @@
// Code size 10 (0xa)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass26_0'::c
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass28_0'::c
IL_0006: ldc.i4.5
IL_0007: ceq
IL_0009: ret
} // end of method '<>c__DisplayClass26_0'::'<ForeachWithCapturedVariable>b__0'
} // end of method '<>c__DisplayClass28_0'::'<ForeachWithCapturedVariable>b__0'
} // end of class '<>c__DisplayClass26_0'
} // end of class '<>c__DisplayClass28_0'
.field private class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives
.method private hidebysig instance void
TryGetItem(int32 id,
[out] class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item& item) cil managed
{
// Code size 4 (0x4)
.maxstack 8
IL_0000: ldarg.2
IL_0001: ldnull
IL_0002: stind.ref
IL_0003: ret
} // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed
{
// Code size 1 (0x1)
@ -1144,11 +1171,11 @@ @@ -1144,11 +1171,11 @@
IL_0009: ldloca.s V_0
IL_000b: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::get_Current()
IL_0010: stloc.1
IL_0011: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass26_0'::.ctor()
IL_0011: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass28_0'::.ctor()
IL_0016: dup
IL_0017: ldloc.1
IL_0018: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass26_0'::c
IL_001d: ldftn instance bool ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass26_0'::'<ForeachWithCapturedVariable>b__0'()
IL_0018: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass28_0'::c
IL_001d: ldftn instance bool ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass28_0'::'<ForeachWithCapturedVariable>b__0'()
IL_0023: newobj instance void class [mscorlib]System.Func`1<bool>::.ctor(object,
native int)
IL_0028: call void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::Operation(class [mscorlib]System.Func`1<bool>)
@ -1707,6 +1734,44 @@ @@ -1707,6 +1734,44 @@
IL_0031: ret
} // end of method Loops::ReturnFromDoWhileInTryFinally
.method public hidebysig instance void
ForLoopWithEarlyReturn(int32[] ids) cil managed
{
// Code size 32 (0x20)
.maxstack 3
.locals init (int32 V_0,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item V_1)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: br.s IL_0019
IL_0004: ldnull
IL_0005: stloc.1
IL_0006: ldarg.0
IL_0007: ldarg.1
IL_0008: ldloc.0
IL_0009: ldelem.i4
IL_000a: ldloca.s V_1
IL_000c: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::TryGetItem(int32,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item&)
IL_0011: ldloc.1
IL_0012: brtrue.s IL_0015
IL_0014: ret
IL_0015: ldloc.0
IL_0016: ldc.i4.1
IL_0017: add
IL_0018: stloc.0
IL_0019: ldloc.0
IL_001a: ldarg.1
IL_001b: ldlen
IL_001c: conv.i4
IL_001d: blt.s IL_0004
IL_001f: ret
} // end of method Loops::ForLoopWithEarlyReturn
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

103
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.roslyn.il

@ -25,14 +25,14 @@ @@ -25,14 +25,14 @@
.ver 0:0:0:0
}
.module Loops.dll
// MVID: {AF6A735F-62A4-480B-8A6B-50E8BB64AD30}
// MVID: {3E422539-E52E-4CE4-9C67-E7DBCEF4A91A}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x05400000
// Image base: 0x03640000
// =============== CLASS MEMBERS DECLARATION ===================
@ -637,7 +637,23 @@ @@ -637,7 +637,23 @@
} // end of property DataItem::Property
} // end of class DataItem
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass26_0'
.class auto ansi nested public beforefieldinit Item
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method Item::.ctor
} // end of class Item
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass28_0'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
@ -651,7 +667,7 @@ @@ -651,7 +667,7 @@
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method '<>c__DisplayClass26_0'::.ctor
} // end of method '<>c__DisplayClass28_0'::.ctor
.method assembly hidebysig instance bool
'<ForeachWithCapturedVariable>b__0'() cil managed
@ -659,15 +675,28 @@ @@ -659,15 +675,28 @@
// Code size 10 (0xa)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass26_0'::c
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass28_0'::c
IL_0006: ldc.i4.5
IL_0007: ceq
IL_0009: ret
} // end of method '<>c__DisplayClass26_0'::'<ForeachWithCapturedVariable>b__0'
} // end of method '<>c__DisplayClass28_0'::'<ForeachWithCapturedVariable>b__0'
} // end of class '<>c__DisplayClass26_0'
} // end of class '<>c__DisplayClass28_0'
.field private class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives
.method private hidebysig instance void
TryGetItem(int32 id,
[out] class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item& item) cil managed
{
// Code size 5 (0x5)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.2
IL_0002: ldnull
IL_0003: stind.ref
IL_0004: ret
} // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed
{
// Code size 2 (0x2)
@ -1352,7 +1381,7 @@ @@ -1352,7 +1381,7 @@
.maxstack 2
.locals init (valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32> V_0,
int32 V_1,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass26_0' V_2)
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass28_0' V_2)
IL_0000: nop
IL_0001: nop
IL_0002: ldarg.0
@ -1365,14 +1394,14 @@ @@ -1365,14 +1394,14 @@
IL_000b: ldloca.s V_0
IL_000d: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::get_Current()
IL_0012: stloc.1
IL_0013: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass26_0'::.ctor()
IL_0013: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass28_0'::.ctor()
IL_0018: stloc.2
IL_0019: nop
IL_001a: ldloc.2
IL_001b: ldloc.1
IL_001c: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass26_0'::c
IL_001c: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass28_0'::c
IL_0021: ldloc.2
IL_0022: ldftn instance bool ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass26_0'::'<ForeachWithCapturedVariable>b__0'()
IL_0022: ldftn instance bool ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass28_0'::'<ForeachWithCapturedVariable>b__0'()
IL_0028: newobj instance void class [mscorlib]System.Func`1<bool>::.ctor(object,
native int)
IL_002d: call void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::Operation(class [mscorlib]System.Func`1<bool>)
@ -2132,6 +2161,58 @@ @@ -2132,6 +2161,58 @@
IL_003d: ret
} // end of method Loops::ReturnFromDoWhileInTryFinally
.method public hidebysig instance void
ForLoopWithEarlyReturn(int32[] ids) cil managed
{
// Code size 47 (0x2f)
.maxstack 3
.locals init (int32 V_0,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item V_1,
bool V_2,
bool V_3)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: br.s IL_0024
IL_0005: nop
IL_0006: ldnull
IL_0007: stloc.1
IL_0008: ldarg.0
IL_0009: ldarg.1
IL_000a: ldloc.0
IL_000b: ldelem.i4
IL_000c: ldloca.s V_1
IL_000e: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::TryGetItem(int32,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/Item&)
IL_0013: nop
IL_0014: ldloc.1
IL_0015: ldnull
IL_0016: ceq
IL_0018: stloc.2
IL_0019: ldloc.2
IL_001a: brfalse.s IL_001f
IL_001c: nop
IL_001d: br.s IL_002e
IL_001f: nop
IL_0020: ldloc.0
IL_0021: ldc.i4.1
IL_0022: add
IL_0023: stloc.0
IL_0024: ldloc.0
IL_0025: ldarg.1
IL_0026: ldlen
IL_0027: conv.i4
IL_0028: clt
IL_002a: stloc.3
IL_002b: ldloc.3
IL_002c: brtrue.s IL_0005
IL_002e: ret
} // end of method Loops::ForLoopWithEarlyReturn
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

11
ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs

@ -34,7 +34,16 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -34,7 +34,16 @@ namespace ICSharpCode.Decompiler.IL.Transforms
public class ExpressionTransforms : ILVisitor, IStatementTransform
{
internal StatementTransformContext context;
public static void RunOnSingleStatment(ILInstruction statement, ILTransformContext context)
{
if (statement == null)
throw new ArgumentNullException(nameof(statement));
if (!(statement.Parent is Block parent))
throw new ArgumentException("ILInstruction must be a statement, i.e., direct child of a block.");
new ExpressionTransforms().Run(parent, statement.ChildIndex, new StatementTransformContext(new BlockTransformContext(context)));
}
public void Run(Block block, int pos, StatementTransformContext context)
{
this.context = context;

30
ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs

@ -90,6 +90,25 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -90,6 +90,25 @@ namespace ICSharpCode.Decompiler.IL.Transforms
ifInstruction.FalseInst = loop.EntryPoint.Instructions[1];
loop.EntryPoint.Instructions.RemoveAt(1);
loop.Kind = ContainerKind.While;
// Invert condition and unwrap nested block, if loop ends in a break or return statement preceeded by an IfInstruction.
while (loopBody.Instructions.Last() is Leave leave && loopBody.Instructions.SecondToLastOrDefault() is IfInstruction nestedIf && nestedIf.FalseInst.MatchNop()) {
switch (nestedIf.TrueInst) {
case Block nestedBlock:
loopBody.Instructions.RemoveAt(leave.ChildIndex);
loopBody.Instructions.AddRange(nestedBlock.Instructions);
break;
case Branch br:
leave.ReplaceWith(nestedIf.TrueInst);
break;
default:
return true;
}
nestedIf.Condition = Comp.LogicNot(nestedIf.Condition);
nestedIf.TrueInst = leave;
ExpressionTransforms.RunOnSingleStatment(nestedIf, context);
if (!loopBody.HasFlag(InstructionFlags.EndPointUnreachable))
loopBody.Instructions.Add(new Leave(loop));
}
return true;
}
@ -130,7 +149,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -130,7 +149,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
returnCondition.Condition = Comp.LogicNot(returnCondition.Condition);
returnCondition.TrueInst = leaveFunction;
// simplify the condition:
new ExpressionTransforms().Run(topLevelBlock, returnCondition.ChildIndex, new StatementTransformContext(new BlockTransformContext(context)));
ExpressionTransforms.RunOnSingleStatment(returnCondition, context);
topLevelBlock.Instructions.RemoveAt(returnCondition.ChildIndex + 1);
topLevelBlock.Instructions.AddRange(originalBlock.Instructions);
originalBlock = topLevelBlock;
@ -174,7 +193,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -174,7 +193,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// insert the combined conditions into the condition block:
conditionBlock.Instructions.Add(condition);
// simplify the condition:
new ExpressionTransforms().Run(conditionBlock, condition.ChildIndex, new StatementTransformContext(new BlockTransformContext(context)));
ExpressionTransforms.RunOnSingleStatment(condition, context);
// transform complete
loop.Kind = ContainerKind.DoWhile;
return true;
@ -344,10 +363,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -344,10 +363,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms
context.Step("Transform to for loop", loop);
// split condition block:
whileCondition.ReplaceWith(forCondition);
new ExpressionTransforms().Run(loop.EntryPoint, forCondition.ChildIndex, new StatementTransformContext(new BlockTransformContext(context)));
ExpressionTransforms.RunOnSingleStatment(forCondition, context);
for (int i = conditions.Count - 1; i >= numberOfConditions; i--) {
whileLoopBody.Instructions.Insert(0, new IfInstruction(Comp.LogicNot(conditions[i]), new Leave(loop)));
new ExpressionTransforms().Run(whileLoopBody, 0, new StatementTransformContext(new BlockTransformContext(context)));
IfInstruction inst;
whileLoopBody.Instructions.Insert(0, inst = new IfInstruction(Comp.LogicNot(conditions[i]), new Leave(loop)));
ExpressionTransforms.RunOnSingleStatment(inst, context);
}
// create a new increment block and add it at the end:
int secondToLastIndex = secondToLast.ChildIndex;

Loading…
Cancel
Save