Browse Source

Convert integers to char literals in comparisons with char operands.

pull/847/head
Siegfried Pammer 8 years ago
parent
commit
c5012e876e
  1. 5
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs
  2. 27
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.il
  3. 21
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.il
  4. 15
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.roslyn.il
  5. 21
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.roslyn.il
  6. 13
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

5
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs

@ -95,6 +95,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -95,6 +95,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
return value;
}
public bool IsUpperCaseA(char a)
{
return a == 'A';
}
public void Int32_Local_Add(int i)
{

27
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.il

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly '140sffme'
.assembly rtuqxf3v
{
.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 '140sffme.dll'
// MVID: {4B912E9D-0D9D-4F78-B2A7-5F041A58B7CC}
.module rtuqxf3v.dll
// MVID: {0891498F-C2CF-4474-B2F7-31DC8C6C47C7}
.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: 0x00850000
// Image base: 0x00DF0000
// =============== CLASS MEMBERS DECLARATION ===================
@ -260,6 +260,23 @@ @@ -260,6 +260,23 @@
IL_0006: ret
} // end of method CompoundAssignmentTest::GetValue
.method public hidebysig instance bool
IsUpperCaseA(char a) cil managed
{
// Code size 11 (0xb)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldc.i4.s 65
IL_0004: ceq
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ret
} // end of method CompoundAssignmentTest::IsUpperCaseA
.method public hidebysig instance void
Int32_Local_Add(int32 i) cil managed
{
@ -864,4 +881,4 @@ @@ -864,4 +881,4 @@
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************
// WARNING: Created Win32 resource file ../../Tests/TestCases/Pretty\CompoundAssignmentTest.res
// WARNING: Created Win32 resource file ../../../../ICSharpCode.Decompiler.Tests/TestCases/Pretty\CompoundAssignmentTest.res

21
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.il

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly a1plok3m
.assembly mjdn04ya
{
.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 a1plok3m.dll
// MVID: {7BD8D57D-A1D7-460B-8B05-A3F6005A275C}
.module mjdn04ya.dll
// MVID: {D8AF2CB3-EE41-4B93-96CE-1FD07E5E4237}
.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: 0x01350000
// Image base: 0x01640000
// =============== CLASS MEMBERS DECLARATION ===================
@ -212,6 +212,17 @@ @@ -212,6 +212,17 @@
IL_0001: ret
} // end of method CompoundAssignmentTest::GetValue
.method public hidebysig instance bool
IsUpperCaseA(char a) cil managed
{
// Code size 6 (0x6)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldc.i4.s 65
IL_0003: ceq
IL_0005: ret
} // end of method CompoundAssignmentTest::IsUpperCaseA
.method public hidebysig instance void
Int32_Local_Add(int32 i) cil managed
{
@ -723,4 +734,4 @@ @@ -723,4 +734,4 @@
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************
// WARNING: Created Win32 resource file ../../Tests/TestCases/Pretty\CompoundAssignmentTest.opt.res
// WARNING: Created Win32 resource file ../../../../ICSharpCode.Decompiler.Tests/TestCases/Pretty\CompoundAssignmentTest.opt.res

15
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.roslyn.il

@ -25,14 +25,14 @@ @@ -25,14 +25,14 @@
.ver 0:0:0:0
}
.module CompoundAssignmentTest.dll
// MVID: {4291E75E-F92B-4A69-AD59-40A9D8C6DF95}
// MVID: {0CCA0C51-2F8D-424C-BDE8-57699BE33BC6}
.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: 0x00F40000
// Image base: 0x00900000
// =============== CLASS MEMBERS DECLARATION ===================
@ -216,6 +216,17 @@ @@ -216,6 +216,17 @@
IL_0001: ret
} // end of method CompoundAssignmentTest::GetValue
.method public hidebysig instance bool
IsUpperCaseA(char a) cil managed
{
// Code size 6 (0x6)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldc.i4.s 65
IL_0003: ceq
IL_0005: ret
} // end of method CompoundAssignmentTest::IsUpperCaseA
.method public hidebysig instance void
Int32_Local_Add(int32 i) cil managed
{

21
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.roslyn.il

@ -25,14 +25,14 @@ @@ -25,14 +25,14 @@
.ver 0:0:0:0
}
.module CompoundAssignmentTest.dll
// MVID: {FF19A157-76FD-4795-BA24-7C956CED48C1}
// MVID: {572A9208-925A-4E92-9D86-8EC36FE271FC}
.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: 0x00DA0000
// Image base: 0x00AF0000
// =============== CLASS MEMBERS DECLARATION ===================
@ -257,6 +257,23 @@ @@ -257,6 +257,23 @@
IL_0006: ret
} // end of method CompoundAssignmentTest::GetValue
.method public hidebysig instance bool
IsUpperCaseA(char a) cil managed
{
// Code size 11 (0xb)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldc.i4.s 65
IL_0004: ceq
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ret
} // end of method CompoundAssignmentTest::IsUpperCaseA
.method public hidebysig instance void
Int32_Local_Add(int32 i) cil managed
{

13
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -465,6 +465,13 @@ namespace ICSharpCode.Decompiler.CSharp @@ -465,6 +465,13 @@ namespace ICSharpCode.Decompiler.CSharp
return right;
}
}
// Special case comparisons with char literals
if (left.Type.IsKnownType(KnownTypeCode.Char) && MightBeCharLiteral(right.ResolveResult)) {
right = right.ConvertTo(left.Type, this);
} else if (right.Type.IsKnownType(KnownTypeCode.Char) && MightBeCharLiteral(left.ResolveResult)) {
left = left.ConvertTo(right.Type, this);
}
var rr = resolver.ResolveBinaryOperator(inst.Kind.ToBinaryOperatorType(), left.ResolveResult, right.ResolveResult)
as OperatorResolveResult;
@ -500,6 +507,12 @@ namespace ICSharpCode.Decompiler.CSharp @@ -500,6 +507,12 @@ namespace ICSharpCode.Decompiler.CSharp
.WithILInstruction(inst)
.WithRR(rr);
}
bool MightBeCharLiteral(ResolveResult rr)
{
return rr.IsCompileTimeConstant && rr.ConstantValue is int val
&& val >= char.MinValue && val <= char.MaxValue;
}
/// <summary>
/// Handle Comp instruction, operators other than equality/inequality.

Loading…
Cancel
Save