Browse Source

Improve UseCSharpSwitch to reduce over-aggressive use of switch producing poor quality code.

pull/1258/head
Chicken-Bones 7 years ago
parent
commit
62b2ad4f8d
  1. 200
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs
  2. 704
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.il
  3. 526
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.il
  4. 545
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.roslyn.il
  5. 782
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.roslyn.il
  6. 12
      ICSharpCode.Decompiler/IL/ControlFlow/SwitchAnalysis.cs
  7. 190
      ICSharpCode.Decompiler/IL/ControlFlow/SwitchDetection.cs
  8. 2
      ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

200
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs

@ -123,6 +123,22 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -123,6 +123,22 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
public static bool SparseIntegerSwitch3(int i)
{
switch (i) {
case 0:
case 10:
case 11:
case 12:
case 100:
case 101:
case 200:
return true;
default:
return false;
}
}
public static string SwitchOverNullableInt(int? i)
{
switch (i) {
@ -240,6 +256,25 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -240,6 +256,25 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
// SwitchDetection.UseCSharpSwitch requires more complex heuristic to identify this when compiled with Roslyn
public static void CompactSwitchOverInt(int i)
{
switch (i) {
case 0:
case 1:
case 2:
Console.WriteLine("012");
break;
case 3:
Console.WriteLine("3");
break;
default:
Console.WriteLine("default");
break;
}
Console.WriteLine("end");
}
public static string ShortSwitchOverString(string text)
{
Console.WriteLine("ShortSwitchOverString: " + text);
@ -387,6 +422,45 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -387,6 +422,45 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine("End of method");
}
// Needs to be long enough to generate a hashtable
public static void SwitchWithGotoString(string s)
{
Console.WriteLine("SwitchWithGotoString: " + s);
switch (s) {
case "1":
Console.WriteLine("one");
goto default;
case "2":
Console.WriteLine("two");
goto case "3";
case "3":
Console.WriteLine("three");
break;
case "4":
Console.WriteLine("four");
return;
case "5":
Console.WriteLine("five");
return;
case "6":
Console.WriteLine("six");
return;
case "7":
Console.WriteLine("seven");
return;
case "8":
Console.WriteLine("eight");
return;
case "9":
Console.WriteLine("nine");
return;
default:
Console.WriteLine("default");
break;
}
Console.WriteLine("End of method");
}
private static SetProperty[] GetProperties()
{
return new SetProperty[0];
@ -465,5 +539,131 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -465,5 +539,131 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
Console.WriteLine("end");
}
// These decompile poorly into switch statements and should be left as is
#region Overagressive Switch Use
#if ROSLYN || OPT
public static void SingleIf1(int i, bool a)
{
if (i == 1 || (i == 2 && a)) {
Console.WriteLine(1);
}
Console.WriteLine(2);
}
#endif
public static void SingleIf2(int i, bool a, bool b)
{
if (i == 1 || (i == 2 && a) || (i == 3 && b)) {
Console.WriteLine(1);
}
Console.WriteLine(2);
}
public static void SingleIf3(int i, bool a, bool b)
{
if (a || i == 1 || (i == 2 && b)) {
Console.WriteLine(1);
}
Console.WriteLine(2);
}
public static void SingleIf4(int i, bool a)
{
if (i == 1 || i == 2 || (i != 3 && a) || i != 4) {
Console.WriteLine(1);
}
Console.WriteLine(2);
}
public static void NestedIf(int i)
{
if (i != 1) {
if (i == 2) {
Console.WriteLine(2);
}
Console.WriteLine("default");
}
Console.WriteLine();
}
public static void IfChainWithCondition(int i)
{
if (i == 0) {
Console.WriteLine(0);
} else if (i == 1) {
Console.WriteLine(1);
} else if (i == 2) {
Console.WriteLine(2);
} else if (i == 3) {
Console.WriteLine(3);
} else if (i == 4) {
Console.WriteLine(4);
} else if (i == 5 && Console.CapsLock) {
Console.WriteLine("5A");
} else {
Console.WriteLine("default");
}
Console.WriteLine();
}
#endregion
// Ensure correctness of SwitchDetection.FindBreakTarget
public static void SwitchWithReturnAndBreak(int i, bool b)
{
switch (i) {
case 0:
if (b) {
return;
}
break;
case 1:
if (!b) {
return;
}
break;
}
Console.WriteLine();
}
public static int SwitchWithReturnAndBreak2(int i, bool b)
{
switch (i) {
case 4:
case 33:
Console.WriteLine();
return 1;
case 334:
if (b) {
return 2;
}
break;
case 395:
case 410:
case 455:
Console.WriteLine();
break;
}
Console.WriteLine();
return 0;
}
// Ensure correctness of SwitchDetection.FindBreakTarget (default case has no associated block)
public static void SwitchWithReturnAndBreak3(int i)
{
switch (i) {
default:
return;
case 0:
Console.WriteLine(0);
break;
case 1:
Console.WriteLine(1);
break;
}
Console.WriteLine();
}
}
}

704
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.il

@ -356,6 +356,56 @@ @@ -356,6 +356,56 @@
IL_005d: ret
} // end of method Switch::SparseIntegerSwitch2
.method public hidebysig static bool SparseIntegerSwitch3(int32 i) cil managed
{
// Code size 72 (0x48)
.maxstack 2
.locals init (bool V_0,
int32 V_1)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.1
IL_0003: ldloc.1
IL_0004: ldc.i4.s 12
IL_0006: bgt.s IL_0023
IL_0008: ldloc.1
IL_0009: ldc.i4.0
IL_000a: beq.s IL_003e
IL_000c: ldloc.1
IL_000d: ldc.i4.s 10
IL_000f: sub
IL_0010: switch (
IL_003e,
IL_003e,
IL_003e)
IL_0021: br.s IL_0042
IL_0023: ldloc.1
IL_0024: ldc.i4.s 100
IL_0026: sub
IL_0027: switch (
IL_003e,
IL_003e)
IL_0034: ldloc.1
IL_0035: ldc.i4 0xc8
IL_003a: beq.s IL_003e
IL_003c: br.s IL_0042
IL_003e: ldc.i4.1
IL_003f: stloc.0
IL_0040: br.s IL_0046
IL_0042: ldc.i4.0
IL_0043: stloc.0
IL_0044: br.s IL_0046
IL_0046: ldloc.0
IL_0047: ret
} // end of method Switch::SparseIntegerSwitch3
.method public hidebysig static string
SwitchOverNullableInt(valuetype [mscorlib]System.Nullable`1<int32> i) cil managed
{
@ -806,6 +856,43 @@ @@ -806,6 +856,43 @@
IL_008f: ret
} // end of method Switch::SwitchOverInt
.method public hidebysig static void CompactSwitchOverInt(int32 i) cil managed
{
// Code size 78 (0x4e)
.maxstack 1
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: switch (
IL_001b,
IL_001b,
IL_001b,
IL_0028)
IL_0019: br.s IL_0035
IL_001b: ldstr "012"
IL_0020: call void [mscorlib]System.Console::WriteLine(string)
IL_0025: nop
IL_0026: br.s IL_0042
IL_0028: ldstr "3"
IL_002d: call void [mscorlib]System.Console::WriteLine(string)
IL_0032: nop
IL_0033: br.s IL_0042
IL_0035: ldstr "default"
IL_003a: call void [mscorlib]System.Console::WriteLine(string)
IL_003f: nop
IL_0040: br.s IL_0042
IL_0042: ldstr "end"
IL_0047: call void [mscorlib]System.Console::WriteLine(string)
IL_004c: nop
IL_004d: ret
} // end of method Switch::CompactSwitchOverInt
.method public hidebysig static string
ShortSwitchOverString(string text) cil managed
{
@ -939,7 +1026,7 @@ @@ -939,7 +1026,7 @@
IL_0015: brfalse IL_00e9
IL_001a: volatile.
IL_001c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000e-1'
IL_001c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000010-1'
IL_0021: brtrue.s IL_0084
IL_0023: ldc.i4.7
@ -980,9 +1067,9 @@ @@ -980,9 +1067,9 @@
IL_0078: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_007d: volatile.
IL_007f: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000e-1'
IL_007f: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000010-1'
IL_0084: volatile.
IL_0086: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000e-1'
IL_0086: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000010-1'
IL_008b: ldloc.1
IL_008c: ldloca.s V_2
IL_008e: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::TryGetValue(!0,
@ -1054,7 +1141,7 @@ @@ -1054,7 +1141,7 @@
IL_0013: brfalse IL_0158
IL_0018: volatile.
IL_001a: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000f-1'
IL_001a: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000011-1'
IL_001f: brtrue IL_00b8
IL_0024: ldc.i4.s 11
@ -1115,9 +1202,9 @@ @@ -1115,9 +1202,9 @@
IL_00ac: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_00b1: volatile.
IL_00b3: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000f-1'
IL_00b3: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000011-1'
IL_00b8: volatile.
IL_00ba: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000f-1'
IL_00ba: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000011-1'
IL_00bf: ldloc.1
IL_00c0: ldloca.s V_2
IL_00c2: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::TryGetValue(!0,
@ -1350,6 +1437,154 @@ @@ -1350,6 +1437,154 @@
IL_007f: ret
} // end of method Switch::SwitchWithGoto
.method public hidebysig static void SwitchWithGotoString(string s) cil managed
{
// Code size 363 (0x16b)
.maxstack 4
.locals init (string V_0,
int32 V_1)
IL_0000: nop
IL_0001: ldstr "SwitchWithGotoString: "
IL_0006: ldarg.0
IL_0007: call string [mscorlib]System.String::Concat(string,
string)
IL_000c: call void [mscorlib]System.Console::WriteLine(string)
IL_0011: nop
IL_0012: ldarg.0
IL_0013: stloc.0
IL_0014: ldloc.0
IL_0015: brfalse IL_0152
IL_001a: volatile.
IL_001c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000015-1'
IL_0021: brtrue.s IL_009d
IL_0023: ldc.i4.s 9
IL_0025: newobj instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::.ctor(int32)
IL_002a: dup
IL_002b: ldstr "1"
IL_0030: ldc.i4.0
IL_0031: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0036: dup
IL_0037: ldstr "2"
IL_003c: ldc.i4.1
IL_003d: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0042: dup
IL_0043: ldstr "3"
IL_0048: ldc.i4.2
IL_0049: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_004e: dup
IL_004f: ldstr "4"
IL_0054: ldc.i4.3
IL_0055: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_005a: dup
IL_005b: ldstr "5"
IL_0060: ldc.i4.4
IL_0061: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0066: dup
IL_0067: ldstr "6"
IL_006c: ldc.i4.5
IL_006d: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0072: dup
IL_0073: ldstr "7"
IL_0078: ldc.i4.6
IL_0079: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_007e: dup
IL_007f: ldstr "8"
IL_0084: ldc.i4.7
IL_0085: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_008a: dup
IL_008b: ldstr "9"
IL_0090: ldc.i4.8
IL_0091: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0096: volatile.
IL_0098: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000015-1'
IL_009d: volatile.
IL_009f: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000015-1'
IL_00a4: ldloc.0
IL_00a5: ldloca.s V_1
IL_00a7: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::TryGetValue(!0,
!1&)
IL_00ac: brfalse IL_0152
IL_00b1: ldloc.1
IL_00b2: switch (
IL_00dd,
IL_00ea,
IL_00f7,
IL_0104,
IL_0111,
IL_011e,
IL_012b,
IL_0138,
IL_0145)
IL_00db: br.s IL_0152
IL_00dd: ldstr "one"
IL_00e2: call void [mscorlib]System.Console::WriteLine(string)
IL_00e7: nop
IL_00e8: br.s IL_0152
IL_00ea: ldstr "two"
IL_00ef: call void [mscorlib]System.Console::WriteLine(string)
IL_00f4: nop
IL_00f5: br.s IL_00f7
IL_00f7: ldstr "three"
IL_00fc: call void [mscorlib]System.Console::WriteLine(string)
IL_0101: nop
IL_0102: br.s IL_015f
IL_0104: ldstr "four"
IL_0109: call void [mscorlib]System.Console::WriteLine(string)
IL_010e: nop
IL_010f: br.s IL_016a
IL_0111: ldstr "five"
IL_0116: call void [mscorlib]System.Console::WriteLine(string)
IL_011b: nop
IL_011c: br.s IL_016a
IL_011e: ldstr "six"
IL_0123: call void [mscorlib]System.Console::WriteLine(string)
IL_0128: nop
IL_0129: br.s IL_016a
IL_012b: ldstr "seven"
IL_0130: call void [mscorlib]System.Console::WriteLine(string)
IL_0135: nop
IL_0136: br.s IL_016a
IL_0138: ldstr "eight"
IL_013d: call void [mscorlib]System.Console::WriteLine(string)
IL_0142: nop
IL_0143: br.s IL_016a
IL_0145: ldstr "nine"
IL_014a: call void [mscorlib]System.Console::WriteLine(string)
IL_014f: nop
IL_0150: br.s IL_016a
IL_0152: ldstr "default"
IL_0157: call void [mscorlib]System.Console::WriteLine(string)
IL_015c: nop
IL_015d: br.s IL_015f
IL_015f: ldstr "End of method"
IL_0164: call void [mscorlib]System.Console::WriteLine(string)
IL_0169: nop
IL_016a: ret
} // end of method Switch::SwitchWithGotoString
.method private hidebysig static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch/SetProperty[]
GetProperties() cil managed
{
@ -1405,7 +1640,7 @@ @@ -1405,7 +1640,7 @@
IL_003b: brfalse IL_012c
IL_0040: volatile.
IL_0042: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000014-1'
IL_0042: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000017-1'
IL_0047: brtrue.s IL_009e
IL_0049: ldc.i4.6
@ -1441,9 +1676,9 @@ @@ -1441,9 +1676,9 @@
IL_0092: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0097: volatile.
IL_0099: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000014-1'
IL_0099: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000017-1'
IL_009e: volatile.
IL_00a0: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000014-1'
IL_00a0: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000017-1'
IL_00a5: ldloc.s V_5
IL_00a7: ldloca.s V_6
IL_00a9: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::TryGetValue(!0,
@ -1668,15 +1903,460 @@ @@ -1668,15 +1903,460 @@
IL_007d: ret
} // end of method Switch::SwitchWithArray
.method public hidebysig static void SingleIf2(int32 i,
bool a,
bool b) cil managed
{
// Code size 49 (0x31)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.1
IL_0003: beq.s IL_001a
IL_0005: ldarg.0
IL_0006: ldc.i4.2
IL_0007: bne.un.s IL_000c
IL_0009: ldarg.1
IL_000a: brtrue.s IL_001a
IL_000c: ldarg.0
IL_000d: ldc.i4.3
IL_000e: bne.un.s IL_0016
IL_0010: ldarg.2
IL_0011: ldc.i4.0
IL_0012: ceq
IL_0014: br.s IL_0017
IL_0016: ldc.i4.1
IL_0017: nop
IL_0018: br.s IL_001b
IL_001a: ldc.i4.0
IL_001b: nop
IL_001c: stloc.0
IL_001d: ldloc.0
IL_001e: brtrue.s IL_0029
IL_0020: nop
IL_0021: ldc.i4.1
IL_0022: call void [mscorlib]System.Console::WriteLine(int32)
IL_0027: nop
IL_0028: nop
IL_0029: ldc.i4.2
IL_002a: call void [mscorlib]System.Console::WriteLine(int32)
IL_002f: nop
IL_0030: ret
} // end of method Switch::SingleIf2
.method public hidebysig static void SingleIf3(int32 i,
bool a,
bool b) cil managed
{
// Code size 45 (0x2d)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: brtrue.s IL_0016
IL_0004: ldarg.0
IL_0005: ldc.i4.1
IL_0006: beq.s IL_0016
IL_0008: ldarg.0
IL_0009: ldc.i4.2
IL_000a: bne.un.s IL_0012
IL_000c: ldarg.2
IL_000d: ldc.i4.0
IL_000e: ceq
IL_0010: br.s IL_0013
IL_0012: ldc.i4.1
IL_0013: nop
IL_0014: br.s IL_0017
IL_0016: ldc.i4.0
IL_0017: nop
IL_0018: stloc.0
IL_0019: ldloc.0
IL_001a: brtrue.s IL_0025
IL_001c: nop
IL_001d: ldc.i4.1
IL_001e: call void [mscorlib]System.Console::WriteLine(int32)
IL_0023: nop
IL_0024: nop
IL_0025: ldc.i4.2
IL_0026: call void [mscorlib]System.Console::WriteLine(int32)
IL_002b: nop
IL_002c: ret
} // end of method Switch::SingleIf3
.method public hidebysig static void SingleIf4(int32 i,
bool a) cil managed
{
// Code size 45 (0x2d)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.1
IL_0003: beq.s IL_0016
IL_0005: ldarg.0
IL_0006: ldc.i4.2
IL_0007: beq.s IL_0016
IL_0009: ldarg.0
IL_000a: ldc.i4.3
IL_000b: beq.s IL_0010
IL_000d: ldarg.1
IL_000e: brtrue.s IL_0016
IL_0010: ldarg.0
IL_0011: ldc.i4.4
IL_0012: ceq
IL_0014: br.s IL_0017
IL_0016: ldc.i4.0
IL_0017: nop
IL_0018: stloc.0
IL_0019: ldloc.0
IL_001a: brtrue.s IL_0025
IL_001c: nop
IL_001d: ldc.i4.1
IL_001e: call void [mscorlib]System.Console::WriteLine(int32)
IL_0023: nop
IL_0024: nop
IL_0025: ldc.i4.2
IL_0026: call void [mscorlib]System.Console::WriteLine(int32)
IL_002b: nop
IL_002c: ret
} // end of method Switch::SingleIf4
.method public hidebysig static void NestedIf(int32 i) cil managed
{
// Code size 49 (0x31)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.1
IL_0003: ceq
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: brtrue.s IL_002a
IL_0009: nop
IL_000a: ldarg.0
IL_000b: ldc.i4.2
IL_000c: ceq
IL_000e: ldc.i4.0
IL_000f: ceq
IL_0011: stloc.0
IL_0012: ldloc.0
IL_0013: brtrue.s IL_001e
IL_0015: nop
IL_0016: ldc.i4.2
IL_0017: call void [mscorlib]System.Console::WriteLine(int32)
IL_001c: nop
IL_001d: nop
IL_001e: ldstr "default"
IL_0023: call void [mscorlib]System.Console::WriteLine(string)
IL_0028: nop
IL_0029: nop
IL_002a: call void [mscorlib]System.Console::WriteLine()
IL_002f: nop
IL_0030: ret
} // end of method Switch::NestedIf
.method public hidebysig static void IfChainWithCondition(int32 i) cil managed
{
// Code size 169 (0xa9)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.0
IL_0003: ceq
IL_0005: ldc.i4.0
IL_0006: ceq
IL_0008: stloc.0
IL_0009: ldloc.0
IL_000a: brtrue.s IL_001a
IL_000c: nop
IL_000d: ldc.i4.0
IL_000e: call void [mscorlib]System.Console::WriteLine(int32)
IL_0013: nop
IL_0014: nop
IL_0015: br IL_00a2
IL_001a: ldarg.0
IL_001b: ldc.i4.1
IL_001c: ceq
IL_001e: ldc.i4.0
IL_001f: ceq
IL_0021: stloc.0
IL_0022: ldloc.0
IL_0023: brtrue.s IL_0030
IL_0025: nop
IL_0026: ldc.i4.1
IL_0027: call void [mscorlib]System.Console::WriteLine(int32)
IL_002c: nop
IL_002d: nop
IL_002e: br.s IL_00a2
IL_0030: ldarg.0
IL_0031: ldc.i4.2
IL_0032: ceq
IL_0034: ldc.i4.0
IL_0035: ceq
IL_0037: stloc.0
IL_0038: ldloc.0
IL_0039: brtrue.s IL_0046
IL_003b: nop
IL_003c: ldc.i4.2
IL_003d: call void [mscorlib]System.Console::WriteLine(int32)
IL_0042: nop
IL_0043: nop
IL_0044: br.s IL_00a2
IL_0046: ldarg.0
IL_0047: ldc.i4.3
IL_0048: ceq
IL_004a: ldc.i4.0
IL_004b: ceq
IL_004d: stloc.0
IL_004e: ldloc.0
IL_004f: brtrue.s IL_005c
IL_0051: nop
IL_0052: ldc.i4.3
IL_0053: call void [mscorlib]System.Console::WriteLine(int32)
IL_0058: nop
IL_0059: nop
IL_005a: br.s IL_00a2
IL_005c: ldarg.0
IL_005d: ldc.i4.4
IL_005e: ceq
IL_0060: ldc.i4.0
IL_0061: ceq
IL_0063: stloc.0
IL_0064: ldloc.0
IL_0065: brtrue.s IL_0072
IL_0067: nop
IL_0068: ldc.i4.4
IL_0069: call void [mscorlib]System.Console::WriteLine(int32)
IL_006e: nop
IL_006f: nop
IL_0070: br.s IL_00a2
IL_0072: ldarg.0
IL_0073: ldc.i4.5
IL_0074: bne.un.s IL_0080
IL_0076: call bool [mscorlib]System.Console::get_CapsLock()
IL_007b: ldc.i4.0
IL_007c: ceq
IL_007e: br.s IL_0081
IL_0080: ldc.i4.1
IL_0081: nop
IL_0082: stloc.0
IL_0083: ldloc.0
IL_0084: brtrue.s IL_0095
IL_0086: nop
IL_0087: ldstr "5A"
IL_008c: call void [mscorlib]System.Console::WriteLine(string)
IL_0091: nop
IL_0092: nop
IL_0093: br.s IL_00a2
IL_0095: nop
IL_0096: ldstr "default"
IL_009b: call void [mscorlib]System.Console::WriteLine(string)
IL_00a0: nop
IL_00a1: nop
IL_00a2: call void [mscorlib]System.Console::WriteLine()
IL_00a7: nop
IL_00a8: ret
} // end of method Switch::IfChainWithCondition
.method public hidebysig static void SwitchWithReturnAndBreak(int32 i,
bool b) cil managed
{
// Code size 49 (0x31)
.maxstack 2
.locals init (int32 V_0,
bool V_1)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: switch (
IL_0013,
IL_0020)
IL_0011: br.s IL_002a
IL_0013: ldarg.1
IL_0014: ldc.i4.0
IL_0015: ceq
IL_0017: stloc.1
IL_0018: ldloc.1
IL_0019: brtrue.s IL_001e
IL_001b: nop
IL_001c: br.s IL_0030
IL_001e: br.s IL_002a
IL_0020: ldarg.1
IL_0021: stloc.1
IL_0022: ldloc.1
IL_0023: brtrue.s IL_0028
IL_0025: nop
IL_0026: br.s IL_0030
IL_0028: br.s IL_002a
IL_002a: call void [mscorlib]System.Console::WriteLine()
IL_002f: nop
IL_0030: ret
} // end of method Switch::SwitchWithReturnAndBreak
.method public hidebysig static int32 SwitchWithReturnAndBreak2(int32 i,
bool b) cil managed
{
// Code size 101 (0x65)
.maxstack 2
.locals init (int32 V_0,
int32 V_1,
bool V_2)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.1
IL_0003: ldloc.1
IL_0004: ldc.i4 0x14e
IL_0009: bgt.s IL_001e
IL_000b: ldloc.1
IL_000c: ldc.i4.4
IL_000d: beq.s IL_0038
IL_000f: ldloc.1
IL_0010: ldc.i4.s 33
IL_0012: beq.s IL_0038
IL_0014: ldloc.1
IL_0015: ldc.i4 0x14e
IL_001a: beq.s IL_0042
IL_001c: br.s IL_0059
IL_001e: ldloc.1
IL_001f: ldc.i4 0x18b
IL_0024: beq.s IL_0051
IL_0026: ldloc.1
IL_0027: ldc.i4 0x19a
IL_002c: beq.s IL_0051
IL_002e: ldloc.1
IL_002f: ldc.i4 0x1c7
IL_0034: beq.s IL_0051
IL_0036: br.s IL_0059
IL_0038: call void [mscorlib]System.Console::WriteLine()
IL_003d: nop
IL_003e: ldc.i4.1
IL_003f: stloc.0
IL_0040: br.s IL_0063
IL_0042: ldarg.1
IL_0043: ldc.i4.0
IL_0044: ceq
IL_0046: stloc.2
IL_0047: ldloc.2
IL_0048: brtrue.s IL_004f
IL_004a: nop
IL_004b: ldc.i4.2
IL_004c: stloc.0
IL_004d: br.s IL_0063
IL_004f: br.s IL_0059
IL_0051: call void [mscorlib]System.Console::WriteLine()
IL_0056: nop
IL_0057: br.s IL_0059
IL_0059: call void [mscorlib]System.Console::WriteLine()
IL_005e: nop
IL_005f: ldc.i4.0
IL_0060: stloc.0
IL_0061: br.s IL_0063
IL_0063: ldloc.0
IL_0064: ret
} // end of method Switch::SwitchWithReturnAndBreak2
.method public hidebysig static void SwitchWithReturnAndBreak3(int32 i) cil managed
{
// Code size 46 (0x2e)
.maxstack 1
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: switch (
IL_0015,
IL_001e)
IL_0011: br.s IL_0013
IL_0013: br.s IL_002d
IL_0015: ldc.i4.0
IL_0016: call void [mscorlib]System.Console::WriteLine(int32)
IL_001b: nop
IL_001c: br.s IL_0027
IL_001e: ldc.i4.1
IL_001f: call void [mscorlib]System.Console::WriteLine(int32)
IL_0024: nop
IL_0025: br.s IL_0027
IL_0027: call void [mscorlib]System.Console::WriteLine()
IL_002c: nop
IL_002d: ret
} // end of method Switch::SwitchWithReturnAndBreak3
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch
.class private auto ansi '<PrivateImplementationDetails>'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x600000e-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x600000f-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000014-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000010-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000011-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000015-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000017-1'
} // end of class '<PrivateImplementationDetails>'

526
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.il

@ -310,6 +310,47 @@ @@ -310,6 +310,47 @@
IL_0056: ret
} // end of method Switch::SparseIntegerSwitch2
.method public hidebysig static bool SparseIntegerSwitch3(int32 i) cil managed
{
// Code size 63 (0x3f)
.maxstack 2
.locals init (int32 V_0)
IL_0000: ldarg.0
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: ldc.i4.s 12
IL_0005: bgt.s IL_0022
IL_0007: ldloc.0
IL_0008: ldc.i4.0
IL_0009: beq.s IL_003b
IL_000b: ldloc.0
IL_000c: ldc.i4.s 10
IL_000e: sub
IL_000f: switch (
IL_003b,
IL_003b,
IL_003b)
IL_0020: br.s IL_003d
IL_0022: ldloc.0
IL_0023: ldc.i4.s 100
IL_0025: sub
IL_0026: switch (
IL_003b,
IL_003b)
IL_0033: ldloc.0
IL_0034: ldc.i4 0xc8
IL_0039: bne.un.s IL_003d
IL_003b: ldc.i4.1
IL_003c: ret
IL_003d: ldc.i4.0
IL_003e: ret
} // end of method Switch::SparseIntegerSwitch3
.method public hidebysig static string
SwitchOverNullableInt(valuetype [mscorlib]System.Nullable`1<int32> i) cil managed
{
@ -694,6 +735,36 @@ @@ -694,6 +735,36 @@
IL_007c: ret
} // end of method Switch::SwitchOverInt
.method public hidebysig static void CompactSwitchOverInt(int32 i) cil managed
{
// Code size 71 (0x47)
.maxstack 1
.locals init (int32 V_0)
IL_0000: ldarg.0
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: switch (
IL_001a,
IL_001a,
IL_001a,
IL_0026)
IL_0018: br.s IL_0032
IL_001a: ldstr "012"
IL_001f: call void [mscorlib]System.Console::WriteLine(string)
IL_0024: br.s IL_003c
IL_0026: ldstr "3"
IL_002b: call void [mscorlib]System.Console::WriteLine(string)
IL_0030: br.s IL_003c
IL_0032: ldstr "default"
IL_0037: call void [mscorlib]System.Console::WriteLine(string)
IL_003c: ldstr "end"
IL_0041: call void [mscorlib]System.Console::WriteLine(string)
IL_0046: ret
} // end of method Switch::CompactSwitchOverInt
.method public hidebysig static string
ShortSwitchOverString(string text) cil managed
{
@ -804,7 +875,7 @@ @@ -804,7 +875,7 @@
IL_0013: brfalse IL_00db
IL_0018: volatile.
IL_001a: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000e-1'
IL_001a: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000010-1'
IL_001f: brtrue.s IL_0082
IL_0021: ldc.i4.7
@ -845,9 +916,9 @@ @@ -845,9 +916,9 @@
IL_0076: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_007b: volatile.
IL_007d: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000e-1'
IL_007d: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000010-1'
IL_0082: volatile.
IL_0084: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000e-1'
IL_0084: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000010-1'
IL_0089: ldloc.0
IL_008a: ldloca.s V_1
IL_008c: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::TryGetValue(!0,
@ -905,7 +976,7 @@ @@ -905,7 +976,7 @@
IL_0011: brfalse IL_013d
IL_0016: volatile.
IL_0018: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000f-1'
IL_0018: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000011-1'
IL_001d: brtrue IL_00b6
IL_0022: ldc.i4.s 11
@ -966,9 +1037,9 @@ @@ -966,9 +1037,9 @@
IL_00aa: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_00af: volatile.
IL_00b1: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000f-1'
IL_00b1: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000011-1'
IL_00b6: volatile.
IL_00b8: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x600000f-1'
IL_00b8: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000011-1'
IL_00bd: ldloc.0
IL_00be: ldloca.s V_1
IL_00c0: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::TryGetValue(!0,
@ -1149,6 +1220,137 @@ @@ -1149,6 +1220,137 @@
IL_0072: ret
} // end of method Switch::SwitchWithGoto
.method public hidebysig static void SwitchWithGotoString(string s) cil managed
{
// Code size 340 (0x154)
.maxstack 4
.locals init (string V_0,
int32 V_1)
IL_0000: ldstr "SwitchWithGotoString: "
IL_0005: ldarg.0
IL_0006: call string [mscorlib]System.String::Concat(string,
string)
IL_000b: call void [mscorlib]System.Console::WriteLine(string)
IL_0010: ldarg.0
IL_0011: dup
IL_0012: stloc.0
IL_0013: brfalse IL_013f
IL_0018: volatile.
IL_001a: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000015-1'
IL_001f: brtrue.s IL_009b
IL_0021: ldc.i4.s 9
IL_0023: newobj instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::.ctor(int32)
IL_0028: dup
IL_0029: ldstr "1"
IL_002e: ldc.i4.0
IL_002f: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0034: dup
IL_0035: ldstr "2"
IL_003a: ldc.i4.1
IL_003b: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0040: dup
IL_0041: ldstr "3"
IL_0046: ldc.i4.2
IL_0047: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_004c: dup
IL_004d: ldstr "4"
IL_0052: ldc.i4.3
IL_0053: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0058: dup
IL_0059: ldstr "5"
IL_005e: ldc.i4.4
IL_005f: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0064: dup
IL_0065: ldstr "6"
IL_006a: ldc.i4.5
IL_006b: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0070: dup
IL_0071: ldstr "7"
IL_0076: ldc.i4.6
IL_0077: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_007c: dup
IL_007d: ldstr "8"
IL_0082: ldc.i4.7
IL_0083: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0088: dup
IL_0089: ldstr "9"
IL_008e: ldc.i4.8
IL_008f: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0094: volatile.
IL_0096: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000015-1'
IL_009b: volatile.
IL_009d: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000015-1'
IL_00a2: ldloc.0
IL_00a3: ldloca.s V_1
IL_00a5: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::TryGetValue(!0,
!1&)
IL_00aa: brfalse IL_013f
IL_00af: ldloc.1
IL_00b0: switch (
IL_00db,
IL_00e7,
IL_00f1,
IL_00fd,
IL_0108,
IL_0113,
IL_011e,
IL_0129,
IL_0134)
IL_00d9: br.s IL_013f
IL_00db: ldstr "one"
IL_00e0: call void [mscorlib]System.Console::WriteLine(string)
IL_00e5: br.s IL_013f
IL_00e7: ldstr "two"
IL_00ec: call void [mscorlib]System.Console::WriteLine(string)
IL_00f1: ldstr "three"
IL_00f6: call void [mscorlib]System.Console::WriteLine(string)
IL_00fb: br.s IL_0149
IL_00fd: ldstr "four"
IL_0102: call void [mscorlib]System.Console::WriteLine(string)
IL_0107: ret
IL_0108: ldstr "five"
IL_010d: call void [mscorlib]System.Console::WriteLine(string)
IL_0112: ret
IL_0113: ldstr "six"
IL_0118: call void [mscorlib]System.Console::WriteLine(string)
IL_011d: ret
IL_011e: ldstr "seven"
IL_0123: call void [mscorlib]System.Console::WriteLine(string)
IL_0128: ret
IL_0129: ldstr "eight"
IL_012e: call void [mscorlib]System.Console::WriteLine(string)
IL_0133: ret
IL_0134: ldstr "nine"
IL_0139: call void [mscorlib]System.Console::WriteLine(string)
IL_013e: ret
IL_013f: ldstr "default"
IL_0144: call void [mscorlib]System.Console::WriteLine(string)
IL_0149: ldstr "End of method"
IL_014e: call void [mscorlib]System.Console::WriteLine(string)
IL_0153: ret
} // end of method Switch::SwitchWithGotoString
.method private hidebysig static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch/SetProperty[]
GetProperties() cil managed
{
@ -1194,7 +1396,7 @@ @@ -1194,7 +1396,7 @@
IL_0037: brfalse IL_011f
IL_003c: volatile.
IL_003e: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000014-1'
IL_003e: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000017-1'
IL_0043: brtrue.s IL_009a
IL_0045: ldc.i4.6
@ -1230,9 +1432,9 @@ @@ -1230,9 +1432,9 @@
IL_008e: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::Add(!0,
!1)
IL_0093: volatile.
IL_0095: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000014-1'
IL_0095: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000017-1'
IL_009a: volatile.
IL_009c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000014-1'
IL_009c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '<PrivateImplementationDetails>'::'$$method0x6000017-1'
IL_00a1: ldloc.s V_5
IL_00a3: ldloca.s V_6
IL_00a5: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32>::TryGetValue(!0,
@ -1424,15 +1626,315 @@ @@ -1424,15 +1626,315 @@
IL_0075: ret
} // end of method Switch::SwitchWithArray
.method public hidebysig static void SingleIf1(int32 i,
bool a) cil managed
{
// Code size 24 (0x18)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: beq.s IL_000b
IL_0004: ldarg.0
IL_0005: ldc.i4.2
IL_0006: bne.un.s IL_0011
IL_0008: ldarg.1
IL_0009: brfalse.s IL_0011
IL_000b: ldc.i4.1
IL_000c: call void [mscorlib]System.Console::WriteLine(int32)
IL_0011: ldc.i4.2
IL_0012: call void [mscorlib]System.Console::WriteLine(int32)
IL_0017: ret
} // end of method Switch::SingleIf1
.method public hidebysig static void SingleIf2(int32 i,
bool a,
bool b) cil managed
{
// Code size 31 (0x1f)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: beq.s IL_0012
IL_0004: ldarg.0
IL_0005: ldc.i4.2
IL_0006: bne.un.s IL_000b
IL_0008: ldarg.1
IL_0009: brtrue.s IL_0012
IL_000b: ldarg.0
IL_000c: ldc.i4.3
IL_000d: bne.un.s IL_0018
IL_000f: ldarg.2
IL_0010: brfalse.s IL_0018
IL_0012: ldc.i4.1
IL_0013: call void [mscorlib]System.Console::WriteLine(int32)
IL_0018: ldc.i4.2
IL_0019: call void [mscorlib]System.Console::WriteLine(int32)
IL_001e: ret
} // end of method Switch::SingleIf2
.method public hidebysig static void SingleIf3(int32 i,
bool a,
bool b) cil managed
{
// Code size 27 (0x1b)
.maxstack 8
IL_0000: ldarg.1
IL_0001: brtrue.s IL_000e
IL_0003: ldarg.0
IL_0004: ldc.i4.1
IL_0005: beq.s IL_000e
IL_0007: ldarg.0
IL_0008: ldc.i4.2
IL_0009: bne.un.s IL_0014
IL_000b: ldarg.2
IL_000c: brfalse.s IL_0014
IL_000e: ldc.i4.1
IL_000f: call void [mscorlib]System.Console::WriteLine(int32)
IL_0014: ldc.i4.2
IL_0015: call void [mscorlib]System.Console::WriteLine(int32)
IL_001a: ret
} // end of method Switch::SingleIf3
.method public hidebysig static void SingleIf4(int32 i,
bool a) cil managed
{
// Code size 32 (0x20)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: beq.s IL_0013
IL_0004: ldarg.0
IL_0005: ldc.i4.2
IL_0006: beq.s IL_0013
IL_0008: ldarg.0
IL_0009: ldc.i4.3
IL_000a: beq.s IL_000f
IL_000c: ldarg.1
IL_000d: brtrue.s IL_0013
IL_000f: ldarg.0
IL_0010: ldc.i4.4
IL_0011: beq.s IL_0019
IL_0013: ldc.i4.1
IL_0014: call void [mscorlib]System.Console::WriteLine(int32)
IL_0019: ldc.i4.2
IL_001a: call void [mscorlib]System.Console::WriteLine(int32)
IL_001f: ret
} // end of method Switch::SingleIf4
.method public hidebysig static void NestedIf(int32 i) cil managed
{
// Code size 30 (0x1e)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: beq.s IL_0018
IL_0004: ldarg.0
IL_0005: ldc.i4.2
IL_0006: bne.un.s IL_000e
IL_0008: ldc.i4.2
IL_0009: call void [mscorlib]System.Console::WriteLine(int32)
IL_000e: ldstr "default"
IL_0013: call void [mscorlib]System.Console::WriteLine(string)
IL_0018: call void [mscorlib]System.Console::WriteLine()
IL_001d: ret
} // end of method Switch::NestedIf
.method public hidebysig static void IfChainWithCondition(int32 i) cil managed
{
// Code size 98 (0x62)
.maxstack 2
IL_0000: ldarg.0
IL_0001: brtrue.s IL_000b
IL_0003: ldc.i4.0
IL_0004: call void [mscorlib]System.Console::WriteLine(int32)
IL_0009: br.s IL_005c
IL_000b: ldarg.0
IL_000c: ldc.i4.1
IL_000d: bne.un.s IL_0017
IL_000f: ldc.i4.1
IL_0010: call void [mscorlib]System.Console::WriteLine(int32)
IL_0015: br.s IL_005c
IL_0017: ldarg.0
IL_0018: ldc.i4.2
IL_0019: bne.un.s IL_0023
IL_001b: ldc.i4.2
IL_001c: call void [mscorlib]System.Console::WriteLine(int32)
IL_0021: br.s IL_005c
IL_0023: ldarg.0
IL_0024: ldc.i4.3
IL_0025: bne.un.s IL_002f
IL_0027: ldc.i4.3
IL_0028: call void [mscorlib]System.Console::WriteLine(int32)
IL_002d: br.s IL_005c
IL_002f: ldarg.0
IL_0030: ldc.i4.4
IL_0031: bne.un.s IL_003b
IL_0033: ldc.i4.4
IL_0034: call void [mscorlib]System.Console::WriteLine(int32)
IL_0039: br.s IL_005c
IL_003b: ldarg.0
IL_003c: ldc.i4.5
IL_003d: bne.un.s IL_0052
IL_003f: call bool [mscorlib]System.Console::get_CapsLock()
IL_0044: brfalse.s IL_0052
IL_0046: ldstr "5A"
IL_004b: call void [mscorlib]System.Console::WriteLine(string)
IL_0050: br.s IL_005c
IL_0052: ldstr "default"
IL_0057: call void [mscorlib]System.Console::WriteLine(string)
IL_005c: call void [mscorlib]System.Console::WriteLine()
IL_0061: ret
} // end of method Switch::IfChainWithCondition
.method public hidebysig static void SwitchWithReturnAndBreak(int32 i,
bool b) cil managed
{
// Code size 32 (0x20)
.maxstack 1
.locals init (int32 V_0)
IL_0000: ldarg.0
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: switch (
IL_0012,
IL_0016)
IL_0010: br.s IL_001a
IL_0012: ldarg.1
IL_0013: brfalse.s IL_001a
IL_0015: ret
IL_0016: ldarg.1
IL_0017: brtrue.s IL_001a
IL_0019: ret
IL_001a: call void [mscorlib]System.Console::WriteLine()
IL_001f: ret
} // end of method Switch::SwitchWithReturnAndBreak
.method public hidebysig static int32 SwitchWithReturnAndBreak2(int32 i,
bool b) cil managed
{
// Code size 79 (0x4f)
.maxstack 2
.locals init (int32 V_0)
IL_0000: ldarg.0
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: ldc.i4 0x14e
IL_0008: bgt.s IL_001d
IL_000a: ldloc.0
IL_000b: ldc.i4.4
IL_000c: beq.s IL_0037
IL_000e: ldloc.0
IL_000f: ldc.i4.s 33
IL_0011: beq.s IL_0037
IL_0013: ldloc.0
IL_0014: ldc.i4 0x14e
IL_0019: beq.s IL_003e
IL_001b: br.s IL_0048
IL_001d: ldloc.0
IL_001e: ldc.i4 0x18b
IL_0023: beq.s IL_0043
IL_0025: ldloc.0
IL_0026: ldc.i4 0x19a
IL_002b: beq.s IL_0043
IL_002d: ldloc.0
IL_002e: ldc.i4 0x1c7
IL_0033: beq.s IL_0043
IL_0035: br.s IL_0048
IL_0037: call void [mscorlib]System.Console::WriteLine()
IL_003c: ldc.i4.1
IL_003d: ret
IL_003e: ldarg.1
IL_003f: brfalse.s IL_0048
IL_0041: ldc.i4.2
IL_0042: ret
IL_0043: call void [mscorlib]System.Console::WriteLine()
IL_0048: call void [mscorlib]System.Console::WriteLine()
IL_004d: ldc.i4.0
IL_004e: ret
} // end of method Switch::SwitchWithReturnAndBreak2
.method public hidebysig static void SwitchWithReturnAndBreak3(int32 i) cil managed
{
// Code size 37 (0x25)
.maxstack 1
.locals init (int32 V_0)
IL_0000: ldarg.0
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: switch (
IL_0011,
IL_0019)
IL_0010: ret
IL_0011: ldc.i4.0
IL_0012: call void [mscorlib]System.Console::WriteLine(int32)
IL_0017: br.s IL_001f
IL_0019: ldc.i4.1
IL_001a: call void [mscorlib]System.Console::WriteLine(int32)
IL_001f: call void [mscorlib]System.Console::WriteLine()
IL_0024: ret
} // end of method Switch::SwitchWithReturnAndBreak3
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch
.class private auto ansi '<PrivateImplementationDetails>'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x600000e-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x600000f-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000014-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000010-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000011-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000015-1'
.field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2<string,int32> '$$method0x6000017-1'
} // end of class '<PrivateImplementationDetails>'

545
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.roslyn.il

@ -331,6 +331,42 @@ @@ -331,6 +331,42 @@
IL_0042: ret
} // end of method Switch::SparseIntegerSwitch2
.method public hidebysig static bool SparseIntegerSwitch3(int32 i) cil managed
{
// Code size 36 (0x24)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.s 12
IL_0003: bgt.s IL_0011
IL_0005: ldarg.0
IL_0006: brfalse.s IL_0020
IL_0008: ldarg.0
IL_0009: ldc.i4.s 10
IL_000b: sub
IL_000c: ldc.i4.2
IL_000d: ble.un.s IL_0020
IL_000f: br.s IL_0022
IL_0011: ldarg.0
IL_0012: ldc.i4.s 100
IL_0014: sub
IL_0015: ldc.i4.1
IL_0016: ble.un.s IL_0020
IL_0018: ldarg.0
IL_0019: ldc.i4 0xc8
IL_001e: bne.un.s IL_0022
IL_0020: ldc.i4.1
IL_0021: ret
IL_0022: ldc.i4.0
IL_0023: ret
} // end of method Switch::SparseIntegerSwitch3
.method public hidebysig static string
SwitchOverNullableInt(valuetype [mscorlib]System.Nullable`1<int32> i) cil managed
{
@ -714,6 +750,35 @@ @@ -714,6 +750,35 @@
IL_0079: ret
} // end of method Switch::SwitchOverInt
.method public hidebysig static void CompactSwitchOverInt(int32 i) cil managed
{
// Code size 55 (0x37)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.2
IL_0002: ble.un.s IL_000a
IL_0004: ldarg.0
IL_0005: ldc.i4.3
IL_0006: beq.s IL_0016
IL_0008: br.s IL_0022
IL_000a: ldstr "012"
IL_000f: call void [mscorlib]System.Console::WriteLine(string)
IL_0014: br.s IL_002c
IL_0016: ldstr "3"
IL_001b: call void [mscorlib]System.Console::WriteLine(string)
IL_0020: br.s IL_002c
IL_0022: ldstr "default"
IL_0027: call void [mscorlib]System.Console::WriteLine(string)
IL_002c: ldstr "end"
IL_0031: call void [mscorlib]System.Console::WriteLine(string)
IL_0036: ret
} // end of method Switch::CompactSwitchOverInt
.method public hidebysig static string
ShortSwitchOverString(string text) cil managed
{
@ -1265,6 +1330,188 @@ @@ -1265,6 +1330,188 @@
IL_0070: ret
} // end of method Switch::SwitchWithGoto
.method public hidebysig static void SwitchWithGotoString(string s) cil managed
{
// Code size 443 (0x1bb)
.maxstack 2
.locals init (uint32 V_0)
IL_0000: ldstr "SwitchWithGotoString: "
IL_0005: ldarg.0
IL_0006: call string [mscorlib]System.String::Concat(string,
string)
IL_000b: call void [mscorlib]System.Console::WriteLine(string)
IL_0010: ldarg.0
IL_0011: call uint32 '<PrivateImplementationDetails>'::ComputeStringHash(string)
IL_0016: stloc.0
IL_0017: ldloc.0
IL_0018: ldc.i4 0x330ca589
IL_001d: bgt.un.s IL_005d
IL_001f: ldloc.0
IL_0020: ldc.i4 0x310ca263
IL_0025: bgt.un.s IL_0042
IL_0027: ldloc.0
IL_0028: ldc.i4 0x300ca0d0
IL_002d: beq IL_00ee
IL_0032: ldloc.0
IL_0033: ldc.i4 0x310ca263
IL_0038: beq IL_00dc
IL_003d: br IL_01a6
IL_0042: ldloc.0
IL_0043: ldc.i4 0x320ca3f6
IL_0048: beq IL_0112
IL_004d: ldloc.0
IL_004e: ldc.i4 0x330ca589
IL_0053: beq IL_0100
IL_0058: br IL_01a6
IL_005d: ldloc.0
IL_005e: ldc.i4 0x360caa42
IL_0063: bgt.un.s IL_007a
IL_0065: ldloc.0
IL_0066: ldc.i4 0x340ca71c
IL_006b: beq.s IL_009d
IL_006d: ldloc.0
IL_006e: ldc.i4 0x360caa42
IL_0073: beq.s IL_00c7
IL_0075: br IL_01a6
IL_007a: ldloc.0
IL_007b: ldc.i4 0x370cabd5
IL_0080: beq.s IL_00b2
IL_0082: ldloc.0
IL_0083: ldc.i4 0x3c0cb3b4
IL_0088: beq IL_0133
IL_008d: ldloc.0
IL_008e: ldc.i4 0x3d0cb547
IL_0093: beq IL_0124
IL_0098: br IL_01a6
IL_009d: ldarg.0
IL_009e: ldstr "1"
IL_00a3: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_00a8: brtrue IL_0142
IL_00ad: br IL_01a6
IL_00b2: ldarg.0
IL_00b3: ldstr "2"
IL_00b8: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_00bd: brtrue IL_014e
IL_00c2: br IL_01a6
IL_00c7: ldarg.0
IL_00c8: ldstr "3"
IL_00cd: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_00d2: brtrue IL_0158
IL_00d7: br IL_01a6
IL_00dc: ldarg.0
IL_00dd: ldstr "4"
IL_00e2: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_00e7: brtrue.s IL_0164
IL_00e9: br IL_01a6
IL_00ee: ldarg.0
IL_00ef: ldstr "5"
IL_00f4: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_00f9: brtrue.s IL_016f
IL_00fb: br IL_01a6
IL_0100: ldarg.0
IL_0101: ldstr "6"
IL_0106: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_010b: brtrue.s IL_017a
IL_010d: br IL_01a6
IL_0112: ldarg.0
IL_0113: ldstr "7"
IL_0118: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_011d: brtrue.s IL_0185
IL_011f: br IL_01a6
IL_0124: ldarg.0
IL_0125: ldstr "8"
IL_012a: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_012f: brtrue.s IL_0190
IL_0131: br.s IL_01a6
IL_0133: ldarg.0
IL_0134: ldstr "9"
IL_0139: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_013e: brtrue.s IL_019b
IL_0140: br.s IL_01a6
IL_0142: ldstr "one"
IL_0147: call void [mscorlib]System.Console::WriteLine(string)
IL_014c: br.s IL_01a6
IL_014e: ldstr "two"
IL_0153: call void [mscorlib]System.Console::WriteLine(string)
IL_0158: ldstr "three"
IL_015d: call void [mscorlib]System.Console::WriteLine(string)
IL_0162: br.s IL_01b0
IL_0164: ldstr "four"
IL_0169: call void [mscorlib]System.Console::WriteLine(string)
IL_016e: ret
IL_016f: ldstr "five"
IL_0174: call void [mscorlib]System.Console::WriteLine(string)
IL_0179: ret
IL_017a: ldstr "six"
IL_017f: call void [mscorlib]System.Console::WriteLine(string)
IL_0184: ret
IL_0185: ldstr "seven"
IL_018a: call void [mscorlib]System.Console::WriteLine(string)
IL_018f: ret
IL_0190: ldstr "eight"
IL_0195: call void [mscorlib]System.Console::WriteLine(string)
IL_019a: ret
IL_019b: ldstr "nine"
IL_01a0: call void [mscorlib]System.Console::WriteLine(string)
IL_01a5: ret
IL_01a6: ldstr "default"
IL_01ab: call void [mscorlib]System.Console::WriteLine(string)
IL_01b0: ldstr "End of method"
IL_01b5: call void [mscorlib]System.Console::WriteLine(string)
IL_01ba: ret
} // end of method Switch::SwitchWithGotoString
.method private hidebysig static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch/SetProperty[]
GetProperties() cil managed
{
@ -1511,6 +1758,304 @@ @@ -1511,6 +1758,304 @@
IL_0072: ret
} // end of method Switch::SwitchWithArray
.method public hidebysig static void SingleIf1(int32 i,
bool a) cil managed
{
// Code size 25 (0x19)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: beq.s IL_000c
IL_0004: ldarg.0
IL_0005: ldc.i4.2
IL_0006: ceq
IL_0008: ldarg.1
IL_0009: and
IL_000a: brfalse.s IL_0012
IL_000c: ldc.i4.1
IL_000d: call void [mscorlib]System.Console::WriteLine(int32)
IL_0012: ldc.i4.2
IL_0013: call void [mscorlib]System.Console::WriteLine(int32)
IL_0018: ret
} // end of method Switch::SingleIf1
.method public hidebysig static void SingleIf2(int32 i,
bool a,
bool b) cil managed
{
// Code size 33 (0x21)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: beq.s IL_0014
IL_0004: ldarg.0
IL_0005: ldc.i4.2
IL_0006: ceq
IL_0008: ldarg.1
IL_0009: and
IL_000a: brtrue.s IL_0014
IL_000c: ldarg.0
IL_000d: ldc.i4.3
IL_000e: ceq
IL_0010: ldarg.2
IL_0011: and
IL_0012: brfalse.s IL_001a
IL_0014: ldc.i4.1
IL_0015: call void [mscorlib]System.Console::WriteLine(int32)
IL_001a: ldc.i4.2
IL_001b: call void [mscorlib]System.Console::WriteLine(int32)
IL_0020: ret
} // end of method Switch::SingleIf2
.method public hidebysig static void SingleIf3(int32 i,
bool a,
bool b) cil managed
{
// Code size 28 (0x1c)
.maxstack 8
IL_0000: ldarg.1
IL_0001: brtrue.s IL_000f
IL_0003: ldarg.0
IL_0004: ldc.i4.1
IL_0005: beq.s IL_000f
IL_0007: ldarg.0
IL_0008: ldc.i4.2
IL_0009: ceq
IL_000b: ldarg.2
IL_000c: and
IL_000d: brfalse.s IL_0015
IL_000f: ldc.i4.1
IL_0010: call void [mscorlib]System.Console::WriteLine(int32)
IL_0015: ldc.i4.2
IL_0016: call void [mscorlib]System.Console::WriteLine(int32)
IL_001b: ret
} // end of method Switch::SingleIf3
.method public hidebysig static void SingleIf4(int32 i,
bool a) cil managed
{
// Code size 36 (0x24)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: beq.s IL_0017
IL_0004: ldarg.0
IL_0005: ldc.i4.2
IL_0006: beq.s IL_0017
IL_0008: ldarg.0
IL_0009: ldc.i4.3
IL_000a: ceq
IL_000c: ldc.i4.0
IL_000d: ceq
IL_000f: ldarg.1
IL_0010: and
IL_0011: brtrue.s IL_0017
IL_0013: ldarg.0
IL_0014: ldc.i4.4
IL_0015: beq.s IL_001d
IL_0017: ldc.i4.1
IL_0018: call void [mscorlib]System.Console::WriteLine(int32)
IL_001d: ldc.i4.2
IL_001e: call void [mscorlib]System.Console::WriteLine(int32)
IL_0023: ret
} // end of method Switch::SingleIf4
.method public hidebysig static void NestedIf(int32 i) cil managed
{
// Code size 30 (0x1e)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: beq.s IL_0018
IL_0004: ldarg.0
IL_0005: ldc.i4.2
IL_0006: bne.un.s IL_000e
IL_0008: ldc.i4.2
IL_0009: call void [mscorlib]System.Console::WriteLine(int32)
IL_000e: ldstr "default"
IL_0013: call void [mscorlib]System.Console::WriteLine(string)
IL_0018: call void [mscorlib]System.Console::WriteLine()
IL_001d: ret
} // end of method Switch::NestedIf
.method public hidebysig static void IfChainWithCondition(int32 i) cil managed
{
// Code size 98 (0x62)
.maxstack 2
IL_0000: ldarg.0
IL_0001: brtrue.s IL_000b
IL_0003: ldc.i4.0
IL_0004: call void [mscorlib]System.Console::WriteLine(int32)
IL_0009: br.s IL_005c
IL_000b: ldarg.0
IL_000c: ldc.i4.1
IL_000d: bne.un.s IL_0017
IL_000f: ldc.i4.1
IL_0010: call void [mscorlib]System.Console::WriteLine(int32)
IL_0015: br.s IL_005c
IL_0017: ldarg.0
IL_0018: ldc.i4.2
IL_0019: bne.un.s IL_0023
IL_001b: ldc.i4.2
IL_001c: call void [mscorlib]System.Console::WriteLine(int32)
IL_0021: br.s IL_005c
IL_0023: ldarg.0
IL_0024: ldc.i4.3
IL_0025: bne.un.s IL_002f
IL_0027: ldc.i4.3
IL_0028: call void [mscorlib]System.Console::WriteLine(int32)
IL_002d: br.s IL_005c
IL_002f: ldarg.0
IL_0030: ldc.i4.4
IL_0031: bne.un.s IL_003b
IL_0033: ldc.i4.4
IL_0034: call void [mscorlib]System.Console::WriteLine(int32)
IL_0039: br.s IL_005c
IL_003b: ldarg.0
IL_003c: ldc.i4.5
IL_003d: bne.un.s IL_0052
IL_003f: call bool [mscorlib]System.Console::get_CapsLock()
IL_0044: brfalse.s IL_0052
IL_0046: ldstr "5A"
IL_004b: call void [mscorlib]System.Console::WriteLine(string)
IL_0050: br.s IL_005c
IL_0052: ldstr "default"
IL_0057: call void [mscorlib]System.Console::WriteLine(string)
IL_005c: call void [mscorlib]System.Console::WriteLine()
IL_0061: ret
} // end of method Switch::IfChainWithCondition
.method public hidebysig static void SwitchWithReturnAndBreak(int32 i,
bool b) cil managed
{
// Code size 23 (0x17)
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_0009
IL_0003: ldarg.0
IL_0004: ldc.i4.1
IL_0005: beq.s IL_000d
IL_0007: br.s IL_0011
IL_0009: ldarg.1
IL_000a: brfalse.s IL_0011
IL_000c: ret
IL_000d: ldarg.1
IL_000e: brtrue.s IL_0011
IL_0010: ret
IL_0011: call void [mscorlib]System.Console::WriteLine()
IL_0016: ret
} // end of method Switch::SwitchWithReturnAndBreak
.method public hidebysig static int32 SwitchWithReturnAndBreak2(int32 i,
bool b) cil managed
{
// Code size 77 (0x4d)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldc.i4 0x14e
IL_0006: bgt.s IL_001b
IL_0008: ldarg.0
IL_0009: ldc.i4.4
IL_000a: beq.s IL_0035
IL_000c: ldarg.0
IL_000d: ldc.i4.s 33
IL_000f: beq.s IL_0035
IL_0011: ldarg.0
IL_0012: ldc.i4 0x14e
IL_0017: beq.s IL_003c
IL_0019: br.s IL_0046
IL_001b: ldarg.0
IL_001c: ldc.i4 0x18b
IL_0021: beq.s IL_0041
IL_0023: ldarg.0
IL_0024: ldc.i4 0x19a
IL_0029: beq.s IL_0041
IL_002b: ldarg.0
IL_002c: ldc.i4 0x1c7
IL_0031: beq.s IL_0041
IL_0033: br.s IL_0046
IL_0035: call void [mscorlib]System.Console::WriteLine()
IL_003a: ldc.i4.1
IL_003b: ret
IL_003c: ldarg.1
IL_003d: brfalse.s IL_0046
IL_003f: ldc.i4.2
IL_0040: ret
IL_0041: call void [mscorlib]System.Console::WriteLine()
IL_0046: call void [mscorlib]System.Console::WriteLine()
IL_004b: ldc.i4.0
IL_004c: ret
} // end of method Switch::SwitchWithReturnAndBreak2
.method public hidebysig static void SwitchWithReturnAndBreak3(int32 i) cil managed
{
// Code size 28 (0x1c)
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_0008
IL_0003: ldarg.0
IL_0004: ldc.i4.1
IL_0005: beq.s IL_0010
IL_0007: ret
IL_0008: ldc.i4.0
IL_0009: call void [mscorlib]System.Console::WriteLine(int32)
IL_000e: br.s IL_0016
IL_0010: ldc.i4.1
IL_0011: call void [mscorlib]System.Console::WriteLine(int32)
IL_0016: call void [mscorlib]System.Console::WriteLine()
IL_001b: ret
} // end of method Switch::SwitchWithReturnAndBreak3
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch
.class private auto ansi sealed '<PrivateImplementationDetails>'

782
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.roslyn.il

@ -404,6 +404,58 @@ @@ -404,6 +404,58 @@
IL_0055: ret
} // end of method Switch::SparseIntegerSwitch2
.method public hidebysig static bool SparseIntegerSwitch3(int32 i) cil managed
{
// Code size 51 (0x33)
.maxstack 2
.locals init (int32 V_0,
bool V_1)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: ldc.i4.s 12
IL_0006: bgt.s IL_0016
IL_0008: ldloc.0
IL_0009: brfalse.s IL_0029
IL_000b: br.s IL_000d
IL_000d: ldloc.0
IL_000e: ldc.i4.s 10
IL_0010: sub
IL_0011: ldc.i4.2
IL_0012: ble.un.s IL_0029
IL_0014: br.s IL_002d
IL_0016: ldloc.0
IL_0017: ldc.i4.s 100
IL_0019: sub
IL_001a: ldc.i4.1
IL_001b: ble.un.s IL_0029
IL_001d: br.s IL_001f
IL_001f: ldloc.0
IL_0020: ldc.i4 0xc8
IL_0025: beq.s IL_0029
IL_0027: br.s IL_002d
IL_0029: ldc.i4.1
IL_002a: stloc.1
IL_002b: br.s IL_0031
IL_002d: ldc.i4.0
IL_002e: stloc.1
IL_002f: br.s IL_0031
IL_0031: ldloc.1
IL_0032: ret
} // end of method Switch::SparseIntegerSwitch3
.method public hidebysig static string
SwitchOverNullableInt(valuetype [mscorlib]System.Nullable`1<int32> i) cil managed
{
@ -906,6 +958,47 @@ @@ -906,6 +958,47 @@
IL_0096: ret
} // end of method Switch::SwitchOverInt
.method public hidebysig static void CompactSwitchOverInt(int32 i) cil managed
{
// Code size 66 (0x42)
.maxstack 2
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: ldc.i4.2
IL_0005: ble.un.s IL_000f
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ldc.i4.3
IL_000b: beq.s IL_001c
IL_000d: br.s IL_0029
IL_000f: ldstr "012"
IL_0014: call void [mscorlib]System.Console::WriteLine(string)
IL_0019: nop
IL_001a: br.s IL_0036
IL_001c: ldstr "3"
IL_0021: call void [mscorlib]System.Console::WriteLine(string)
IL_0026: nop
IL_0027: br.s IL_0036
IL_0029: ldstr "default"
IL_002e: call void [mscorlib]System.Console::WriteLine(string)
IL_0033: nop
IL_0034: br.s IL_0036
IL_0036: ldstr "end"
IL_003b: call void [mscorlib]System.Console::WriteLine(string)
IL_0040: nop
IL_0041: ret
} // end of method Switch::CompactSwitchOverInt
.method public hidebysig static string
ShortSwitchOverString(string text) cil managed
{
@ -1590,6 +1683,218 @@ @@ -1590,6 +1683,218 @@
IL_007f: ret
} // end of method Switch::SwitchWithGoto
.method public hidebysig static void SwitchWithGotoString(string s) cil managed
{
// Code size 484 (0x1e4)
.maxstack 2
.locals init (string V_0,
uint32 V_1)
IL_0000: nop
IL_0001: ldstr "SwitchWithGotoString: "
IL_0006: ldarg.0
IL_0007: call string [mscorlib]System.String::Concat(string,
string)
IL_000c: call void [mscorlib]System.Console::WriteLine(string)
IL_0011: nop
IL_0012: ldarg.0
IL_0013: stloc.0
IL_0014: ldloc.0
IL_0015: call uint32 '<PrivateImplementationDetails>'::ComputeStringHash(string)
IL_001a: stloc.1
IL_001b: ldloc.1
IL_001c: ldc.i4 0x330ca589
IL_0021: bgt.un.s IL_0065
IL_0023: ldloc.1
IL_0024: ldc.i4 0x310ca263
IL_0029: bgt.un.s IL_0048
IL_002b: ldloc.1
IL_002c: ldc.i4 0x300ca0d0
IL_0031: beq IL_00ff
IL_0036: br.s IL_0038
IL_0038: ldloc.1
IL_0039: ldc.i4 0x310ca263
IL_003e: beq IL_00ea
IL_0043: br IL_01cb
IL_0048: ldloc.1
IL_0049: ldc.i4 0x320ca3f6
IL_004e: beq IL_0123
IL_0053: br.s IL_0055
IL_0055: ldloc.1
IL_0056: ldc.i4 0x330ca589
IL_005b: beq IL_0111
IL_0060: br IL_01cb
IL_0065: ldloc.1
IL_0066: ldc.i4 0x360caa42
IL_006b: bgt.un.s IL_0084
IL_006d: ldloc.1
IL_006e: ldc.i4 0x340ca71c
IL_0073: beq.s IL_00ab
IL_0075: br.s IL_0077
IL_0077: ldloc.1
IL_0078: ldc.i4 0x360caa42
IL_007d: beq.s IL_00d5
IL_007f: br IL_01cb
IL_0084: ldloc.1
IL_0085: ldc.i4 0x370cabd5
IL_008a: beq.s IL_00c0
IL_008c: br.s IL_008e
IL_008e: ldloc.1
IL_008f: ldc.i4 0x3c0cb3b4
IL_0094: beq IL_0147
IL_0099: br.s IL_009b
IL_009b: ldloc.1
IL_009c: ldc.i4 0x3d0cb547
IL_00a1: beq IL_0135
IL_00a6: br IL_01cb
IL_00ab: ldloc.0
IL_00ac: ldstr "1"
IL_00b1: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_00b6: brtrue IL_0156
IL_00bb: br IL_01cb
IL_00c0: ldloc.0
IL_00c1: ldstr "2"
IL_00c6: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_00cb: brtrue IL_0163
IL_00d0: br IL_01cb
IL_00d5: ldloc.0
IL_00d6: ldstr "3"
IL_00db: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_00e0: brtrue IL_0170
IL_00e5: br IL_01cb
IL_00ea: ldloc.0
IL_00eb: ldstr "4"
IL_00f0: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_00f5: brtrue IL_017d
IL_00fa: br IL_01cb
IL_00ff: ldloc.0
IL_0100: ldstr "5"
IL_0105: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_010a: brtrue.s IL_018a
IL_010c: br IL_01cb
IL_0111: ldloc.0
IL_0112: ldstr "6"
IL_0117: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_011c: brtrue.s IL_0197
IL_011e: br IL_01cb
IL_0123: ldloc.0
IL_0124: ldstr "7"
IL_0129: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_012e: brtrue.s IL_01a4
IL_0130: br IL_01cb
IL_0135: ldloc.0
IL_0136: ldstr "8"
IL_013b: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_0140: brtrue.s IL_01b1
IL_0142: br IL_01cb
IL_0147: ldloc.0
IL_0148: ldstr "9"
IL_014d: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_0152: brtrue.s IL_01be
IL_0154: br.s IL_01cb
IL_0156: ldstr "one"
IL_015b: call void [mscorlib]System.Console::WriteLine(string)
IL_0160: nop
IL_0161: br.s IL_01cb
IL_0163: ldstr "two"
IL_0168: call void [mscorlib]System.Console::WriteLine(string)
IL_016d: nop
IL_016e: br.s IL_0170
IL_0170: ldstr "three"
IL_0175: call void [mscorlib]System.Console::WriteLine(string)
IL_017a: nop
IL_017b: br.s IL_01d8
IL_017d: ldstr "four"
IL_0182: call void [mscorlib]System.Console::WriteLine(string)
IL_0187: nop
IL_0188: br.s IL_01e3
IL_018a: ldstr "five"
IL_018f: call void [mscorlib]System.Console::WriteLine(string)
IL_0194: nop
IL_0195: br.s IL_01e3
IL_0197: ldstr "six"
IL_019c: call void [mscorlib]System.Console::WriteLine(string)
IL_01a1: nop
IL_01a2: br.s IL_01e3
IL_01a4: ldstr "seven"
IL_01a9: call void [mscorlib]System.Console::WriteLine(string)
IL_01ae: nop
IL_01af: br.s IL_01e3
IL_01b1: ldstr "eight"
IL_01b6: call void [mscorlib]System.Console::WriteLine(string)
IL_01bb: nop
IL_01bc: br.s IL_01e3
IL_01be: ldstr "nine"
IL_01c3: call void [mscorlib]System.Console::WriteLine(string)
IL_01c8: nop
IL_01c9: br.s IL_01e3
IL_01cb: ldstr "default"
IL_01d0: call void [mscorlib]System.Console::WriteLine(string)
IL_01d5: nop
IL_01d6: br.s IL_01d8
IL_01d8: ldstr "End of method"
IL_01dd: call void [mscorlib]System.Console::WriteLine(string)
IL_01e2: nop
IL_01e3: ret
} // end of method Switch::SwitchWithGotoString
.method private hidebysig static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch/SetProperty[]
GetProperties() cil managed
{
@ -1878,6 +2183,483 @@ @@ -1878,6 +2183,483 @@
IL_007a: ret
} // end of method Switch::SwitchWithArray
.method public hidebysig static void SingleIf1(int32 i,
bool a) cil managed
{
// Code size 35 (0x23)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.1
IL_0003: beq.s IL_000d
IL_0005: ldarg.0
IL_0006: ldc.i4.2
IL_0007: ceq
IL_0009: ldarg.1
IL_000a: and
IL_000b: br.s IL_000e
IL_000d: ldc.i4.1
IL_000e: stloc.0
IL_000f: ldloc.0
IL_0010: brfalse.s IL_001b
IL_0012: nop
IL_0013: ldc.i4.1
IL_0014: call void [mscorlib]System.Console::WriteLine(int32)
IL_0019: nop
IL_001a: nop
IL_001b: ldc.i4.2
IL_001c: call void [mscorlib]System.Console::WriteLine(int32)
IL_0021: nop
IL_0022: ret
} // end of method Switch::SingleIf1
.method public hidebysig static void SingleIf2(int32 i,
bool a,
bool b) cil managed
{
// Code size 43 (0x2b)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.1
IL_0003: beq.s IL_0015
IL_0005: ldarg.0
IL_0006: ldc.i4.2
IL_0007: ceq
IL_0009: ldarg.1
IL_000a: and
IL_000b: brtrue.s IL_0015
IL_000d: ldarg.0
IL_000e: ldc.i4.3
IL_000f: ceq
IL_0011: ldarg.2
IL_0012: and
IL_0013: br.s IL_0016
IL_0015: ldc.i4.1
IL_0016: stloc.0
IL_0017: ldloc.0
IL_0018: brfalse.s IL_0023
IL_001a: nop
IL_001b: ldc.i4.1
IL_001c: call void [mscorlib]System.Console::WriteLine(int32)
IL_0021: nop
IL_0022: nop
IL_0023: ldc.i4.2
IL_0024: call void [mscorlib]System.Console::WriteLine(int32)
IL_0029: nop
IL_002a: ret
} // end of method Switch::SingleIf2
.method public hidebysig static void SingleIf3(int32 i,
bool a,
bool b) cil managed
{
// Code size 38 (0x26)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: brtrue.s IL_0010
IL_0004: ldarg.0
IL_0005: ldc.i4.1
IL_0006: beq.s IL_0010
IL_0008: ldarg.0
IL_0009: ldc.i4.2
IL_000a: ceq
IL_000c: ldarg.2
IL_000d: and
IL_000e: br.s IL_0011
IL_0010: ldc.i4.1
IL_0011: stloc.0
IL_0012: ldloc.0
IL_0013: brfalse.s IL_001e
IL_0015: nop
IL_0016: ldc.i4.1
IL_0017: call void [mscorlib]System.Console::WriteLine(int32)
IL_001c: nop
IL_001d: nop
IL_001e: ldc.i4.2
IL_001f: call void [mscorlib]System.Console::WriteLine(int32)
IL_0024: nop
IL_0025: ret
} // end of method Switch::SingleIf3
.method public hidebysig static void SingleIf4(int32 i,
bool a) cil managed
{
// Code size 51 (0x33)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.1
IL_0003: beq.s IL_001d
IL_0005: ldarg.0
IL_0006: ldc.i4.2
IL_0007: beq.s IL_001d
IL_0009: ldarg.0
IL_000a: ldc.i4.3
IL_000b: ceq
IL_000d: ldc.i4.0
IL_000e: ceq
IL_0010: ldarg.1
IL_0011: and
IL_0012: brtrue.s IL_001d
IL_0014: ldarg.0
IL_0015: ldc.i4.4
IL_0016: ceq
IL_0018: ldc.i4.0
IL_0019: ceq
IL_001b: br.s IL_001e
IL_001d: ldc.i4.1
IL_001e: stloc.0
IL_001f: ldloc.0
IL_0020: brfalse.s IL_002b
IL_0022: nop
IL_0023: ldc.i4.1
IL_0024: call void [mscorlib]System.Console::WriteLine(int32)
IL_0029: nop
IL_002a: nop
IL_002b: ldc.i4.2
IL_002c: call void [mscorlib]System.Console::WriteLine(int32)
IL_0031: nop
IL_0032: ret
} // end of method Switch::SingleIf4
.method public hidebysig static void NestedIf(int32 i) cil managed
{
// Code size 49 (0x31)
.maxstack 2
.locals init (bool V_0,
bool V_1)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.1
IL_0003: ceq
IL_0005: ldc.i4.0
IL_0006: ceq
IL_0008: stloc.0
IL_0009: ldloc.0
IL_000a: brfalse.s IL_002a
IL_000c: nop
IL_000d: ldarg.0
IL_000e: ldc.i4.2
IL_000f: ceq
IL_0011: stloc.1
IL_0012: ldloc.1
IL_0013: brfalse.s IL_001e
IL_0015: nop
IL_0016: ldc.i4.2
IL_0017: call void [mscorlib]System.Console::WriteLine(int32)
IL_001c: nop
IL_001d: nop
IL_001e: ldstr "default"
IL_0023: call void [mscorlib]System.Console::WriteLine(string)
IL_0028: nop
IL_0029: nop
IL_002a: call void [mscorlib]System.Console::WriteLine()
IL_002f: nop
IL_0030: ret
} // end of method Switch::NestedIf
.method public hidebysig static void IfChainWithCondition(int32 i) cil managed
{
// Code size 151 (0x97)
.maxstack 2
.locals init (bool V_0,
bool V_1,
bool V_2,
bool V_3,
bool V_4,
bool V_5)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.0
IL_0003: ceq
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: brfalse.s IL_0014
IL_0009: nop
IL_000a: ldc.i4.0
IL_000b: call void [mscorlib]System.Console::WriteLine(int32)
IL_0010: nop
IL_0011: nop
IL_0012: br.s IL_0090
IL_0014: ldarg.0
IL_0015: ldc.i4.1
IL_0016: ceq
IL_0018: stloc.1
IL_0019: ldloc.1
IL_001a: brfalse.s IL_0027
IL_001c: nop
IL_001d: ldc.i4.1
IL_001e: call void [mscorlib]System.Console::WriteLine(int32)
IL_0023: nop
IL_0024: nop
IL_0025: br.s IL_0090
IL_0027: ldarg.0
IL_0028: ldc.i4.2
IL_0029: ceq
IL_002b: stloc.2
IL_002c: ldloc.2
IL_002d: brfalse.s IL_003a
IL_002f: nop
IL_0030: ldc.i4.2
IL_0031: call void [mscorlib]System.Console::WriteLine(int32)
IL_0036: nop
IL_0037: nop
IL_0038: br.s IL_0090
IL_003a: ldarg.0
IL_003b: ldc.i4.3
IL_003c: ceq
IL_003e: stloc.3
IL_003f: ldloc.3
IL_0040: brfalse.s IL_004d
IL_0042: nop
IL_0043: ldc.i4.3
IL_0044: call void [mscorlib]System.Console::WriteLine(int32)
IL_0049: nop
IL_004a: nop
IL_004b: br.s IL_0090
IL_004d: ldarg.0
IL_004e: ldc.i4.4
IL_004f: ceq
IL_0051: stloc.s V_4
IL_0053: ldloc.s V_4
IL_0055: brfalse.s IL_0062
IL_0057: nop
IL_0058: ldc.i4.4
IL_0059: call void [mscorlib]System.Console::WriteLine(int32)
IL_005e: nop
IL_005f: nop
IL_0060: br.s IL_0090
IL_0062: ldarg.0
IL_0063: ldc.i4.5
IL_0064: bne.un.s IL_006d
IL_0066: call bool [mscorlib]System.Console::get_CapsLock()
IL_006b: br.s IL_006e
IL_006d: ldc.i4.0
IL_006e: stloc.s V_5
IL_0070: ldloc.s V_5
IL_0072: brfalse.s IL_0083
IL_0074: nop
IL_0075: ldstr "5A"
IL_007a: call void [mscorlib]System.Console::WriteLine(string)
IL_007f: nop
IL_0080: nop
IL_0081: br.s IL_0090
IL_0083: nop
IL_0084: ldstr "default"
IL_0089: call void [mscorlib]System.Console::WriteLine(string)
IL_008e: nop
IL_008f: nop
IL_0090: call void [mscorlib]System.Console::WriteLine()
IL_0095: nop
IL_0096: ret
} // end of method Switch::IfChainWithCondition
.method public hidebysig static void SwitchWithReturnAndBreak(int32 i,
bool b) cil managed
{
// Code size 44 (0x2c)
.maxstack 2
.locals init (int32 V_0,
bool V_1,
bool V_2)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: brfalse.s IL_000e
IL_0006: br.s IL_0008
IL_0008: ldloc.0
IL_0009: ldc.i4.1
IL_000a: beq.s IL_0018
IL_000c: br.s IL_0025
IL_000e: ldarg.1
IL_000f: stloc.1
IL_0010: ldloc.1
IL_0011: brfalse.s IL_0016
IL_0013: nop
IL_0014: br.s IL_002b
IL_0016: br.s IL_0025
IL_0018: ldarg.1
IL_0019: ldc.i4.0
IL_001a: ceq
IL_001c: stloc.2
IL_001d: ldloc.2
IL_001e: brfalse.s IL_0023
IL_0020: nop
IL_0021: br.s IL_002b
IL_0023: br.s IL_0025
IL_0025: call void [mscorlib]System.Console::WriteLine()
IL_002a: nop
IL_002b: ret
} // end of method Switch::SwitchWithReturnAndBreak
.method public hidebysig static int32 SwitchWithReturnAndBreak2(int32 i,
bool b) cil managed
{
// Code size 106 (0x6a)
.maxstack 2
.locals init (int32 V_0,
int32 V_1,
bool V_2)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: ldc.i4 0x14e
IL_0009: bgt.s IL_0022
IL_000b: ldloc.0
IL_000c: ldc.i4.4
IL_000d: beq.s IL_0040
IL_000f: br.s IL_0011
IL_0011: ldloc.0
IL_0012: ldc.i4.s 33
IL_0014: beq.s IL_0040
IL_0016: br.s IL_0018
IL_0018: ldloc.0
IL_0019: ldc.i4 0x14e
IL_001e: beq.s IL_004a
IL_0020: br.s IL_005e
IL_0022: ldloc.0
IL_0023: ldc.i4 0x18b
IL_0028: beq.s IL_0056
IL_002a: br.s IL_002c
IL_002c: ldloc.0
IL_002d: ldc.i4 0x19a
IL_0032: beq.s IL_0056
IL_0034: br.s IL_0036
IL_0036: ldloc.0
IL_0037: ldc.i4 0x1c7
IL_003c: beq.s IL_0056
IL_003e: br.s IL_005e
IL_0040: call void [mscorlib]System.Console::WriteLine()
IL_0045: nop
IL_0046: ldc.i4.1
IL_0047: stloc.1
IL_0048: br.s IL_0068
IL_004a: ldarg.1
IL_004b: stloc.2
IL_004c: ldloc.2
IL_004d: brfalse.s IL_0054
IL_004f: nop
IL_0050: ldc.i4.2
IL_0051: stloc.1
IL_0052: br.s IL_0068
IL_0054: br.s IL_005e
IL_0056: call void [mscorlib]System.Console::WriteLine()
IL_005b: nop
IL_005c: br.s IL_005e
IL_005e: call void [mscorlib]System.Console::WriteLine()
IL_0063: nop
IL_0064: ldc.i4.0
IL_0065: stloc.1
IL_0066: br.s IL_0068
IL_0068: ldloc.1
IL_0069: ret
} // end of method Switch::SwitchWithReturnAndBreak2
.method public hidebysig static void SwitchWithReturnAndBreak3(int32 i) cil managed
{
// Code size 41 (0x29)
.maxstack 2
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: brfalse.s IL_0010
IL_0006: br.s IL_0008
IL_0008: ldloc.0
IL_0009: ldc.i4.1
IL_000a: beq.s IL_0019
IL_000c: br.s IL_000e
IL_000e: br.s IL_0028
IL_0010: ldc.i4.0
IL_0011: call void [mscorlib]System.Console::WriteLine(int32)
IL_0016: nop
IL_0017: br.s IL_0022
IL_0019: ldc.i4.1
IL_001a: call void [mscorlib]System.Console::WriteLine(int32)
IL_001f: nop
IL_0020: br.s IL_0022
IL_0022: call void [mscorlib]System.Console::WriteLine()
IL_0027: nop
IL_0028: ret
} // end of method Switch::SwitchWithReturnAndBreak3
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch
.class private auto ansi sealed '<PrivateImplementationDetails>'

12
ICSharpCode.Decompiler/IL/ControlFlow/SwitchAnalysis.cs

@ -59,8 +59,8 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -59,8 +59,8 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
/// Blocks that can be deleted if the tail of the initial block is replaced with a switch instruction.
/// </summary>
public readonly List<Block> InnerBlocks = new List<Block>();
Block rootBlock;
public Block RootBlock { get; private set; }
/// <summary>
/// Analyze the last two statements in the block and see if they can be turned into a
@ -70,7 +70,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -70,7 +70,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
public bool AnalyzeBlock(Block block)
{
switchVar = null;
rootBlock = block;
RootBlock = block;
targetBlockToSectionIndex.Clear();
targetContainerToSectionIndex.Clear();
Sections.Clear();
@ -100,12 +100,12 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -100,12 +100,12 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
return false;
}
if (tailOnly) {
Debug.Assert(block == rootBlock);
Debug.Assert(block == RootBlock);
} else {
Debug.Assert(switchVar != null); // switchVar should always be determined by the top-level call
if (block.IncomingEdgeCount != 1 || block == rootBlock)
if (block.IncomingEdgeCount != 1 || block == RootBlock)
return false; // for now, let's only consider if-structures that form a tree
if (block.Parent != rootBlock.Parent)
if (block.Parent != RootBlock.Parent)
return false; // all blocks should belong to the same container
}
LongSet trueValues;

190
ICSharpCode.Decompiler/IL/ControlFlow/SwitchDetection.cs

@ -21,6 +21,7 @@ using System; @@ -21,6 +21,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using ICSharpCode.Decompiler.FlowAnalysis;
using ICSharpCode.Decompiler.Util;
using ICSharpCode.Decompiler.TypeSystem;
@ -35,11 +36,20 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -35,11 +36,20 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
/// </summary>
class SwitchDetection : IILTransform
{
private ILTransformContext context;
private BlockContainer currentContainer;
private ControlFlowGraph controlFlowGraph;
SwitchAnalysis analysis = new SwitchAnalysis();
public void Run(ILFunction function, ILTransformContext context)
{
this.context = context;
foreach (var container in function.Descendants.OfType<BlockContainer>()) {
currentContainer = container;
controlFlowGraph = null;
bool blockContainerNeedsCleanup = false;
foreach (var block in container.Blocks) {
context.CancellationToken.ThrowIfCancellationRequested();
@ -56,7 +66,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -56,7 +66,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
{
bool analysisSuccess = analysis.AnalyzeBlock(block);
KeyValuePair<LongSet, ILInstruction> defaultSection;
if (analysisSuccess && UseCSharpSwitch(analysis, out defaultSection)) {
if (analysisSuccess && UseCSharpSwitch(out defaultSection)) {
// complex multi-block switch that can be combined into a single SwitchInstruction
ILInstruction switchValue = new LdLoc(analysis.SwitchVariable);
if (switchValue.ResultType == StackType.Unknown) {
@ -85,6 +95,8 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -85,6 +95,8 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
Debug.Assert(innerBlock != ((BlockContainer)block.Parent).EntryPoint);
innerBlock.Instructions.Clear();
}
controlFlowGraph = null; // control flow graph is no-longer valid
blockContainerNeedsCleanup = true;
SortSwitchSections(sw);
} else {
@ -156,7 +168,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -156,7 +168,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
/// <summary>
/// Tests whether we should prefer a switch statement over an if statement.
/// </summary>
static bool UseCSharpSwitch(SwitchAnalysis analysis, out KeyValuePair<LongSet, ILInstruction> defaultSection)
private bool UseCSharpSwitch(out KeyValuePair<LongSet, ILInstruction> defaultSection)
{
if (!analysis.InnerBlocks.Any()) {
defaultSection = default(KeyValuePair<LongSet, ILInstruction>);
@ -168,24 +180,174 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -168,24 +180,174 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
// This should never happen, as we'd need 2^64/MaxValuesPerSection sections to hit this case...
return false;
}
ulong valuePerSectionLimit = MaxValuesPerSection;
if (!analysis.ContainsILSwitch) {
// If there's no IL switch involved, limit the number of keys per section
// much more drastically to avoid generating switches where an if condition
// would be shorter.
valuePerSectionLimit = Math.Min(
valuePerSectionLimit,
(ulong)analysis.InnerBlocks.Count);
}
var defaultSectionKey = defaultSection.Key;
if (analysis.Sections.Any(s => !s.Key.SetEquals(defaultSectionKey)
&& s.Key.Count() > valuePerSectionLimit)) {
if (analysis.Sections.Any(s => !s.Key.SetEquals(defaultSectionKey) && s.Key.Count() > MaxValuesPerSection)) {
// Only the default section is allowed to have tons of keys.
// C# doesn't support "case 1 to 100000000", and we don't want to generate
// gigabytes of case labels.
return false;
}
return true;
// good enough indicator that the surrounding code also forms a switch statement
if (analysis.ContainsILSwitch || MatchRoslynSwitchOnString())
return true;
int ifCount = analysis.InnerBlocks.Count + 1;
int labelCount = analysis.Sections.Where(s => !s.Key.SetEquals(defaultSectionKey)).Sum(s => s.Key.Intervals.Length);
// heuristic to determine if a block would be better represented as an if statement rather than a case statement
if (ifCount < labelCount)
return false;
// if there is no ILSwitch, there's still many control flow patterns that
// match a switch statement but were originally just regular if statements,
// and converting them to switches results in poor quality code with goto statements
//
// If a single break target cannot be identified, then the equivalent switch statement would require goto statements.
// These goto statements may be "goto case x" or "goto default", but these are a hint that the original code was not a switch,
// and that the switch statement may be very poor quality.
// Thus the rule of thumb is no goto statements if the original code didn't include them
return !SwitchRequiresGoto();
}
/// <summary>
/// stloc switchValueVar(call ComputeStringHash(switchValue))
///
/// Previously, the roslyn case block heads were added to the flowBlocks for case control flow analysis.
/// This forbade goto case statements (as is the purpose of ValidatePotentialSwitchFlow)
/// Identifying the roslyn string switch head is a better indicator for UseCSharpSwitch
/// </summary>
private bool MatchRoslynSwitchOnString()
{
var insns = analysis.RootBlock.Instructions;
return insns.Count >= 3 && SwitchOnStringTransform.MatchComputeStringHashCall(insns[insns.Count - 3], analysis.SwitchVariable, out var switchLdLoc);
}
/// <summary>
/// Determines if the analysed switch can be constructed without any gotos
/// </summary>
private bool SwitchRequiresGoto()
{
if (controlFlowGraph == null)
controlFlowGraph = new ControlFlowGraph(currentContainer, context.CancellationToken);
// grab the control flow nodes for blocks targetted by each section
var caseNodes = new List<ControlFlowNode>();
ControlFlowNode defaultNode = null;
foreach (var s in analysis.Sections) {
// leave sections not considered
if (s.Value.MatchBranch(out var block) && !IsContinue(block)) {
if (s.Key.Count() > MaxValuesPerSection)
defaultNode = controlFlowGraph.GetNode(block);
else
caseNodes.Add(controlFlowGraph.GetNode(block));
}
}
var flowBlocks = analysis.InnerBlocks.Concat(new[] {analysis.RootBlock}).ToHashSet();
AddNullCase(flowBlocks, caseNodes);
// all predecessors of case blocks must be part of the switch logic (to avoid gotos)
foreach (var caseNode in caseNodes)
if (caseNode.Predecessors.Any(n => !flowBlocks.Contains(n.UserData)))
return true;
// determine if the switch would have a single exit point (break target)
return !FindBreakTarget(caseNodes, defaultNode, out var _);
}
/// <summary>
/// Does some of the analysis of SwitchOnNullableTransform to add the null case control flow
/// to the results of SwitchAnaylsis
/// </summary>
private void AddNullCase(HashSet<Block> flowBlocks, List<ControlFlowNode> caseNodes)
{
if (analysis.RootBlock.IncomingEdgeCount != 1)
return;
// if (comp(logic.not(call get_HasValue(ldloca nullableVar))) br NullCase
// br RootBlock
var nullableBlock = (Block)controlFlowGraph.GetNode(analysis.RootBlock).Predecessors.SingleOrDefault()?.UserData;
if (nullableBlock == null ||
nullableBlock.Instructions.Count < 2 ||
!nullableBlock.Instructions.Last().MatchBranch(analysis.RootBlock) ||
!nullableBlock.Instructions.SecondToLastOrDefault().MatchIfInstruction(out var cond, out var trueInst) ||
!cond.MatchLogicNot(out var getHasValue) ||
!NullableLiftingTransform.MatchHasValueCall(getHasValue, out ILInstruction nullableInst))
return;
// could check that nullableInst is ldloc or ldloca and that the switch variable matches a GetValueOrDefault
// but the effect of adding an incorrect block to the flowBlock list would only be disasterous if it branched directly
// to a candidate case block
// must branch to a case label, otherwise we can proceed fine and let SwitchOnNullableTransform do all the work
if (!trueInst.MatchBranch(out var nullBlock) || !caseNodes.Exists(n => n.UserData == nullBlock))
return;
//add the null case logic to the incoming flow blocks
flowBlocks.Add(nullableBlock);
}
internal static bool IsContinue(Block block) => false;
/// <summary>
/// Enumerates all nodes via which a domination tree can be exited. (Not distinct)
/// That is, all nodes with at least one dominated predecessor which are not dominated themselves
///
/// Note that while this construction appears less efficient than a recursive solution,
/// a recursive solution would require the use of the Visted flag to avoid loops in the dominator tree
/// </summary>
internal static IEnumerable<ControlFlowNode> GetDominatorTreeExits(ControlFlowNode dominator) =>
dominator.DominatorTreeChildren.Concat(new[] {dominator})
.SelectMany(n => n.Successors)
.Where(n => !dominator.Dominates(n));
/// <summary>
/// Lists all potential targets for break; statements from a domination tree,
/// assuming the domination tree must be exited via either break; or continue;
/// </summary>
internal static IEnumerable<ControlFlowNode> GetBreakTargets(ControlFlowNode dominator) =>
GetDominatorTreeExits(dominator).Where(n => !IsContinue((Block)n.UserData));
/// <summary>
/// Finds the target node of all branches leaving the dominator tree of the cases of a switch block.
/// Returns true if a single target was found, or if no break statements are required (breakTarget = null)
/// If the defaultNode is the breakTarget, then it should not be inlined.
///
/// Note that branches to case labels ("goto case x") are considered break targets, and will fail this method
///
/// defaultNode may be null if there is no control block associated with the default case.
/// </summary>
/// <returns></returns>
internal static bool FindBreakTarget(List<ControlFlowNode> caseNodes, ControlFlowNode defaultNode, out ControlFlowNode breakTarget)
{
breakTarget = null;
var breakTargets = caseNodes.SelectMany(GetBreakTargets).ToHashSet();
// no cases require break statements, inlining of default case doesn't matter
if (breakTargets.Count == 0)
return true;
if (defaultNode != null) {
// default case is break target, don't inline
if (breakTargets.Count == 1 && breakTargets.Single() == defaultNode) {
breakTarget = defaultNode;
return true;
}
// default case requires inlining, ensure break target matches that of case statements
breakTargets.AddRange(GetBreakTargets(defaultNode));
}
// single break target found
if (breakTargets.Count == 1) {
breakTarget = breakTargets.Single();
return true;
}
// more than 1 break target found
return false;
}
}
}

2
ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

@ -839,7 +839,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -839,7 +839,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
/// <summary>
/// Matches 'stloc(targetVar, call ComputeStringHash(ldloc switchValue))'
/// </summary>
bool MatchComputeStringHashCall(ILInstruction inst, ILVariable targetVar, out LdLoc switchValue)
internal static bool MatchComputeStringHashCall(ILInstruction inst, ILVariable targetVar, out LdLoc switchValue)
{
switchValue = null;
if (!inst.MatchStLoc(targetVar, out var value))

Loading…
Cancel
Save