Browse Source

Fix short-circuiting operators when the rhs is a comparison with 0.

pull/940/merge
Daniel Grunwald 8 years ago
parent
commit
255fab7bd6
  1. 11
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.cs
  2. 60
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.il
  3. 43
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.opt.il
  4. 37
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.opt.roslyn.il
  5. 53
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.roslyn.il
  6. 3
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  7. 19
      ICSharpCode.Decompiler/IL/Instructions/IfInstruction.cs
  8. 14
      ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs

11
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.cs

@ -27,6 +27,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -27,6 +27,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
public abstract void B(bool b);
public abstract bool F(int i);
public abstract int GetInt(int i);
public abstract void M1();
public abstract void M2();
public abstract void E();
@ -91,6 +92,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -91,6 +92,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
this.E();
}
public void StmtOr4()
{
if (this.GetInt(0) != 0 || this.GetInt(1) != 0) {
this.M1();
} else {
this.M2();
}
this.E();
}
public void StmtComplex()
{
if (this.F(0) && this.F(1) && !this.F(2) && (this.F(3) || this.F(4))) {

60
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.il

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0
// Copyright (c) Microsoft Corporation. All rights reserved.
@ -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 '0fqx5k1s'
.assembly bqtfnoxz
{
.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 '0fqx5k1s.dll'
// MVID: {40923C21-ED40-40E0-8D2A-58DE2CA84852}
.module bqtfnoxz.dll
// MVID: {E41D831E-089A-4F86-AA5D-FD2CB6D7C452}
.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: 0x031A0000
// Image base: 0x03120000
// =============== CLASS MEMBERS DECLARATION ===================
@ -46,6 +46,11 @@ @@ -46,6 +46,11 @@
{
} // end of method ShortCircuit::F
.method public hidebysig newslot abstract virtual
instance int32 GetInt(int32 i) cil managed
{
} // end of method ShortCircuit::GetInt
.method public hidebysig newslot abstract virtual
instance void M1() cil managed
{
@ -348,6 +353,49 @@ @@ -348,6 +353,49 @@
IL_0040: ret
} // end of method ShortCircuit::StmtOr3
.method public hidebysig instance void
StmtOr4() cil managed
{
// Code size 56 (0x38)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.0
IL_0003: callvirt instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::GetInt(int32)
IL_0008: brtrue.s IL_0016
IL_000a: ldarg.0
IL_000b: ldc.i4.1
IL_000c: callvirt instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::GetInt(int32)
IL_0011: ldc.i4.0
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_0027
IL_001c: nop
IL_001d: ldarg.0
IL_001e: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::M1()
IL_0023: nop
IL_0024: nop
IL_0025: br.s IL_0030
IL_0027: nop
IL_0028: ldarg.0
IL_0029: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::M2()
IL_002e: nop
IL_002f: nop
IL_0030: ldarg.0
IL_0031: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::E()
IL_0036: nop
IL_0037: ret
} // end of method ShortCircuit::StmtOr4
.method public hidebysig instance void
StmtComplex() cil managed
{
@ -588,4 +636,4 @@ @@ -588,4 +636,4 @@
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************
// WARNING: Created Win32 resource file ../../Tests/TestCases/Pretty\ShortCircuit.res
// WARNING: Created Win32 resource file ../../../TestCases/Pretty\ShortCircuit.res

43
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.opt.il

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0
// Copyright (c) Microsoft Corporation. All rights reserved.
@ -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 gbge3vmw
.assembly '4yyrmwig'
{
.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 gbge3vmw.dll
// MVID: {39FE0BDC-C8CF-421C-936D-85782F68AEF4}
.module '4yyrmwig.dll'
// MVID: {A53FB590-72B6-46AB-809D-9F6F92B64475}
.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: 0x00C00000
// Image base: 0x015D0000
// =============== CLASS MEMBERS DECLARATION ===================
@ -46,6 +46,11 @@ @@ -46,6 +46,11 @@
{
} // end of method ShortCircuit::F
.method public hidebysig newslot abstract virtual
instance int32 GetInt(int32 i) cil managed
{
} // end of method ShortCircuit::GetInt
.method public hidebysig newslot abstract virtual
instance void M1() cil managed
{
@ -268,6 +273,32 @@ @@ -268,6 +273,32 @@
IL_002f: ret
} // end of method ShortCircuit::StmtOr3
.method public hidebysig instance void
StmtOr4() cil managed
{
// Code size 39 (0x27)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.0
IL_0002: callvirt instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::GetInt(int32)
IL_0007: brtrue.s IL_0012
IL_0009: ldarg.0
IL_000a: ldc.i4.1
IL_000b: callvirt instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::GetInt(int32)
IL_0010: brfalse.s IL_001a
IL_0012: ldarg.0
IL_0013: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::M1()
IL_0018: br.s IL_0020
IL_001a: ldarg.0
IL_001b: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::M2()
IL_0020: ldarg.0
IL_0021: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::E()
IL_0026: ret
} // end of method ShortCircuit::StmtOr4
.method public hidebysig instance void
StmtComplex() cil managed
{
@ -433,4 +464,4 @@ @@ -433,4 +464,4 @@
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************
// WARNING: Created Win32 resource file ../../Tests/TestCases/Pretty\ShortCircuit.opt.res
// WARNING: Created Win32 resource file ../../../TestCases/Pretty\ShortCircuit.opt.res

37
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.opt.roslyn.il

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0
// Copyright (c) Microsoft Corporation. All rights reserved.
@ -25,14 +25,14 @@ @@ -25,14 +25,14 @@
.ver 0:0:0:0
}
.module ShortCircuit.dll
// MVID: {A27E149C-542B-41C0-AB29-FA01A65D43CB}
// MVID: {FAFDF41B-AD51-4575-890D-F2652CF85283}
.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: 0x02490000
// Image base: 0x00710000
// =============== CLASS MEMBERS DECLARATION ===================
@ -50,6 +50,11 @@ @@ -50,6 +50,11 @@
{
} // end of method ShortCircuit::F
.method public hidebysig newslot abstract virtual
instance int32 GetInt(int32 i) cil managed
{
} // end of method ShortCircuit::GetInt
.method public hidebysig newslot abstract virtual
instance void M1() cil managed
{
@ -272,6 +277,32 @@ @@ -272,6 +277,32 @@
IL_002f: ret
} // end of method ShortCircuit::StmtOr3
.method public hidebysig instance void
StmtOr4() cil managed
{
// Code size 39 (0x27)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.0
IL_0002: callvirt instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::GetInt(int32)
IL_0007: brtrue.s IL_0012
IL_0009: ldarg.0
IL_000a: ldc.i4.1
IL_000b: callvirt instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::GetInt(int32)
IL_0010: brfalse.s IL_001a
IL_0012: ldarg.0
IL_0013: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::M1()
IL_0018: br.s IL_0020
IL_001a: ldarg.0
IL_001b: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::M2()
IL_0020: ldarg.0
IL_0021: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::E()
IL_0026: ret
} // end of method ShortCircuit::StmtOr4
.method public hidebysig instance void
StmtComplex() cil managed
{

53
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ShortCircuit.roslyn.il

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0
// Copyright (c) Microsoft Corporation. All rights reserved.
@ -25,14 +25,14 @@ @@ -25,14 +25,14 @@
.ver 0:0:0:0
}
.module ShortCircuit.dll
// MVID: {5FB25D31-D6C4-4D39-B72D-A6EBD832225B}
// MVID: {B0D8F5AF-A2A3-4091-846A-B19B13874A74}
.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: 0x00D60000
// Image base: 0x00AD0000
// =============== CLASS MEMBERS DECLARATION ===================
@ -50,6 +50,11 @@ @@ -50,6 +50,11 @@
{
} // end of method ShortCircuit::F
.method public hidebysig newslot abstract virtual
instance int32 GetInt(int32 i) cil managed
{
} // end of method ShortCircuit::GetInt
.method public hidebysig newslot abstract virtual
instance void M1() cil managed
{
@ -336,6 +341,48 @@ @@ -336,6 +341,48 @@
IL_003c: ret
} // end of method ShortCircuit::StmtOr3
.method public hidebysig instance void
StmtOr4() cil managed
{
// Code size 55 (0x37)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.0
IL_0003: callvirt instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::GetInt(int32)
IL_0008: brtrue.s IL_0016
IL_000a: ldarg.0
IL_000b: ldc.i4.1
IL_000c: callvirt instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::GetInt(int32)
IL_0011: ldc.i4.0
IL_0012: cgt.un
IL_0014: br.s IL_0017
IL_0016: ldc.i4.1
IL_0017: stloc.0
IL_0018: ldloc.0
IL_0019: brfalse.s IL_0026
IL_001b: nop
IL_001c: ldarg.0
IL_001d: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::M1()
IL_0022: nop
IL_0023: nop
IL_0024: br.s IL_002f
IL_0026: nop
IL_0027: ldarg.0
IL_0028: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::M2()
IL_002d: nop
IL_002e: nop
IL_002f: ldarg.0
IL_0030: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.ShortCircuit::E()
IL_0035: nop
IL_0036: ret
} // end of method ShortCircuit::StmtOr4
.method public hidebysig instance void
StmtComplex() cil managed
{

3
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -1673,7 +1673,8 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1673,7 +1673,8 @@ namespace ICSharpCode.Decompiler.CSharp
// ILAst LogicAnd/LogicOr can return a different value than 0 or 1
// if the rhs is evaluated.
// We can only correctly translate it to C# if the rhs is of type boolean:
if (op != BinaryOperatorType.Any && rhs.Type.IsKnownType(KnownTypeCode.Boolean)) {
if (op != BinaryOperatorType.Any && (rhs.Type.IsKnownType(KnownTypeCode.Boolean) || IfInstruction.IsInConditionSlot(inst))) {
rhs = rhs.ConvertToBoolean(this);
return new BinaryOperatorExpression(condition, op, rhs)
.WithILInstruction(inst)
.WithRR(new ResolveResult(compilation.FindType(KnownTypeCode.Boolean)));

19
ICSharpCode.Decompiler/IL/Instructions/IfInstruction.cs

@ -107,5 +107,24 @@ namespace ICSharpCode.Decompiler.IL @@ -107,5 +107,24 @@ namespace ICSharpCode.Decompiler.IL
falseInst.WriteTo(output, options);
}
}
/// <summary>
/// Gets whether the input instruction occurs in a context where it is being compared with 0.
/// </summary>
internal static bool IsInConditionSlot(ILInstruction inst)
{
var slot = inst.SlotInfo;
if (slot == IfInstruction.ConditionSlot)
return true;
if (slot == IfInstruction.TrueInstSlot || slot == IfInstruction.FalseInstSlot || slot == NullCoalescingInstruction.FallbackInstSlot)
return IsInConditionSlot(inst.Parent);
if (inst.Parent is Comp comp) {
if (comp.Left == inst && comp.Right.MatchLdcI4(0))
return true;
if (comp.Right == inst && comp.Left.MatchLdcI4(0))
return true;
}
return false;
}
}
}

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

@ -54,18 +54,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -54,18 +54,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// we know those were already handled previously.
}
static bool IsInConditionSlot(ILInstruction inst)
{
var slot = inst.SlotInfo;
if (slot == IfInstruction.ConditionSlot)
return true;
if (slot == IfInstruction.TrueInstSlot || slot == IfInstruction.FalseInstSlot || slot == NullCoalescingInstruction.FallbackInstSlot)
return IsInConditionSlot(inst.Parent);
if (inst.Parent.MatchLogicNot(out _))
return true;
return false;
}
protected internal override void VisitComp(Comp inst)
{
// "logic.not(arg)" is sugar for "comp(arg != ldc.i4 0)"
@ -73,7 +61,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -73,7 +61,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
VisitLogicNot(inst, arg);
return;
} else if (inst.Kind == ComparisonKind.Inequality && inst.LiftingKind == ComparisonLiftingKind.None
&& inst.Right.MatchLdcI4(0) && (IsInConditionSlot(inst) || inst.Left is Comp)
&& inst.Right.MatchLdcI4(0) && (IfInstruction.IsInConditionSlot(inst) || inst.Left is Comp)
) {
// if (comp(x != 0)) ==> if (x)
// comp(comp(...) != 0) => comp(...)

Loading…
Cancel
Save