Browse Source

Fix #1007: post-increment transform incorrect due to variable splitting.

pull/1012/head
Daniel Grunwald 8 years ago
parent
commit
0a1921ff67
  1. 7
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs
  2. 40
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.il
  3. 39
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.il
  4. 33
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.roslyn.il
  5. 34
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.roslyn.il
  6. 19
      ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs
  7. 10
      ICSharpCode.Decompiler/IL/Transforms/TransformAssignment.cs

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

@ -519,5 +519,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -519,5 +519,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
this.ushortDict.Add(this.ushortField++, val);
}
private void Issue1007(TimeSpan[] items, int startIndex, TimeSpan item)
{
int num = startIndex;
items[num++] = item;
items[num++] = item;
}
}
}

40
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 c4ghupf5
.assembly '2caluqpm'
{
.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 c4ghupf5.dll
// MVID: {A420A635-A261-460E-9F30-F603256DEDB0}
.module '2caluqpm.dll'
// MVID: {7839A86C-BE29-4CFE-BC36-763505CDFB63}
.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: 0x02FD0000
// Image base: 0x03060000
// =============== CLASS MEMBERS DECLARATION ===================
@ -1983,6 +1983,38 @@ @@ -1983,6 +1983,38 @@
IL_0020: ret
} // end of method CompoundAssignmentTest::Issue588
.method private hidebysig instance void
Issue1007(valuetype [mscorlib]System.TimeSpan[] items,
int32 startIndex,
valuetype [mscorlib]System.TimeSpan item) cil managed
{
// Code size 38 (0x26)
.maxstack 4
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldarg.2
IL_0002: stloc.0
IL_0003: ldarg.1
IL_0004: ldloc.0
IL_0005: dup
IL_0006: ldc.i4.1
IL_0007: add
IL_0008: stloc.0
IL_0009: ldelema [mscorlib]System.TimeSpan
IL_000e: ldarg.3
IL_000f: stobj [mscorlib]System.TimeSpan
IL_0014: ldarg.1
IL_0015: ldloc.0
IL_0016: dup
IL_0017: ldc.i4.1
IL_0018: add
IL_0019: stloc.0
IL_001a: ldelema [mscorlib]System.TimeSpan
IL_001f: ldarg.3
IL_0020: stobj [mscorlib]System.TimeSpan
IL_0025: ret
} // end of method CompoundAssignmentTest::Issue1007
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

39
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 '0ffxad1m'
.assembly eoamkle2
{
.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 '0ffxad1m.dll'
// MVID: {C53B3CFA-3FF4-4724-8DBE-8B180FA612BA}
.module eoamkle2.dll
// MVID: {61ABCA72-03C4-4AE8-9DF9-464B253BB877}
.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: 0x028A0000
// Image base: 0x030A0000
// =============== CLASS MEMBERS DECLARATION ===================
@ -1636,6 +1636,37 @@ @@ -1636,6 +1636,37 @@
IL_001e: ret
} // end of method CompoundAssignmentTest::Issue588
.method private hidebysig instance void
Issue1007(valuetype [mscorlib]System.TimeSpan[] items,
int32 startIndex,
valuetype [mscorlib]System.TimeSpan item) cil managed
{
// Code size 37 (0x25)
.maxstack 4
.locals init (int32 V_0)
IL_0000: ldarg.2
IL_0001: stloc.0
IL_0002: ldarg.1
IL_0003: ldloc.0
IL_0004: dup
IL_0005: ldc.i4.1
IL_0006: add
IL_0007: stloc.0
IL_0008: ldelema [mscorlib]System.TimeSpan
IL_000d: ldarg.3
IL_000e: stobj [mscorlib]System.TimeSpan
IL_0013: ldarg.1
IL_0014: ldloc.0
IL_0015: dup
IL_0016: ldc.i4.1
IL_0017: add
IL_0018: stloc.0
IL_0019: ldelema [mscorlib]System.TimeSpan
IL_001e: ldarg.3
IL_001f: stobj [mscorlib]System.TimeSpan
IL_0024: ret
} // end of method CompoundAssignmentTest::Issue1007
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

33
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: {EE880025-0C8C-414F-BD89-E5DB86D9B5FA}
// MVID: {A41AE390-D37A-4425-A0E0-8C87CC28D8D9}
.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: 0x016C0000
// Image base: 0x04BC0000
// =============== CLASS MEMBERS DECLARATION ===================
@ -1641,6 +1641,35 @@ @@ -1641,6 +1641,35 @@
IL_001e: ret
} // end of method CompoundAssignmentTest::Issue588
.method private hidebysig instance void
Issue1007(valuetype [mscorlib]System.TimeSpan[] items,
int32 startIndex,
valuetype [mscorlib]System.TimeSpan item) cil managed
{
// Code size 27 (0x1b)
.maxstack 4
.locals init (int32 V_0)
IL_0000: ldarg.2
IL_0001: stloc.0
IL_0002: ldarg.1
IL_0003: ldloc.0
IL_0004: dup
IL_0005: ldc.i4.1
IL_0006: add
IL_0007: stloc.0
IL_0008: ldarg.3
IL_0009: stelem [mscorlib]System.TimeSpan
IL_000e: ldarg.1
IL_000f: ldloc.0
IL_0010: dup
IL_0011: ldc.i4.1
IL_0012: add
IL_0013: stloc.0
IL_0014: ldarg.3
IL_0015: stelem [mscorlib]System.TimeSpan
IL_001a: ret
} // end of method CompoundAssignmentTest::Issue1007
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

34
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: {367B213F-CE98-4EEA-B458-88A41927D3BC}
// MVID: {D0C2FA90-A1F6-4313-9C43-6BF03D13D957}
.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: 0x006A0000
// Image base: 0x02B80000
// =============== CLASS MEMBERS DECLARATION ===================
@ -1978,6 +1978,36 @@ @@ -1978,6 +1978,36 @@
IL_0020: ret
} // end of method CompoundAssignmentTest::Issue588
.method private hidebysig instance void
Issue1007(valuetype [mscorlib]System.TimeSpan[] items,
int32 startIndex,
valuetype [mscorlib]System.TimeSpan item) cil managed
{
// Code size 28 (0x1c)
.maxstack 4
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldarg.2
IL_0002: stloc.0
IL_0003: ldarg.1
IL_0004: ldloc.0
IL_0005: dup
IL_0006: ldc.i4.1
IL_0007: add
IL_0008: stloc.0
IL_0009: ldarg.3
IL_000a: stelem [mscorlib]System.TimeSpan
IL_000f: ldarg.1
IL_0010: ldloc.0
IL_0011: dup
IL_0012: ldc.i4.1
IL_0013: add
IL_0014: stloc.0
IL_0015: ldarg.3
IL_0016: stelem [mscorlib]System.TimeSpan
IL_001b: ret
} // end of method CompoundAssignmentTest::Issue1007
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

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

@ -243,5 +243,24 @@ namespace ICSharpCode.Decompiler.IL @@ -243,5 +243,24 @@ namespace ICSharpCode.Decompiler.IL
Variables.Add(variable);
return variable;
}
/// <summary>
/// Recombine split variables by replacing all occurrences of variable2 with variable1.
/// </summary>
internal void RecombineVariables(ILVariable variable1, ILVariable variable2)
{
Debug.Assert(ILVariableEqualityComparer.Instance.Equals(variable1, variable2));
foreach (var ldloc in variable2.LoadInstructions.ToArray()) {
ldloc.Variable = variable1;
}
foreach (var store in variable2.StoreInstructions.ToArray()) {
store.Variable = variable1;
}
foreach (var ldloca in variable2.AddressInstructions.ToArray()) {
ldloca.Variable = variable1;
}
bool ok = Variables.Remove(variable2);
Debug.Assert(ok);
}
}
}

10
ICSharpCode.Decompiler/IL/Transforms/TransformAssignment.cs

@ -384,7 +384,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -384,7 +384,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
{
var inst = block.Instructions[pos] as StLoc;
var nextInst = block.Instructions.ElementAtOrDefault(pos + 1) as StLoc;
if (inst == null || nextInst == null || !inst.Value.MatchLdLoc(out var l) || !ILVariableEqualityComparer.Instance.Equals(l, nextInst.Variable))
if (inst == null || nextInst == null || !inst.Value.MatchLdLoc(out var loadVar) || !ILVariableEqualityComparer.Instance.Equals(loadVar, nextInst.Variable))
return false;
var binary = nextInst.Value as BinaryNumericInstruction;
if (inst.Variable.Kind != VariableKind.StackSlot || nextInst.Variable.Kind == VariableKind.StackSlot || binary == null)
@ -394,10 +394,14 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -394,10 +394,14 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if ((binary.Operator != BinaryNumericOperator.Add && binary.Operator != BinaryNumericOperator.Sub) || !binary.Left.MatchLdLoc(inst.Variable) || !binary.Right.MatchLdcI4(1))
return false;
context.Step($"TransformPostIncDecOperatorLocal", inst);
if (loadVar != nextInst.Variable) {
// load and store are two different variables, that were split from the same variable
context.Function.RecombineVariables(loadVar, nextInst.Variable);
}
var tempStore = context.Function.RegisterVariable(VariableKind.StackSlot, inst.Variable.Type);
var assignment = new Block(BlockKind.PostfixOperator);
assignment.Instructions.Add(new StLoc(tempStore, new LdLoc(nextInst.Variable)));
assignment.Instructions.Add(new StLoc(nextInst.Variable, new BinaryNumericInstruction(binary.Operator, new LdLoc(tempStore), new LdcI4(1), binary.CheckForOverflow, binary.Sign)));
assignment.Instructions.Add(new StLoc(tempStore, new LdLoc(loadVar)));
assignment.Instructions.Add(new StLoc(loadVar, new BinaryNumericInstruction(binary.Operator, new LdLoc(tempStore), new LdcI4(1), binary.CheckForOverflow, binary.Sign)));
assignment.FinalInstruction = new LdLoc(tempStore);
inst.Value = assignment;
block.Instructions.RemoveAt(pos + 1); // remove nextInst

Loading…
Cancel
Save