Browse Source

Add TransformInlineAssignment and InlineAssignmentTest

pull/734/head
Siegfried Pammer 9 years ago
parent
commit
ee1b26d04d
  1. 1
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  3. 55
      ICSharpCode.Decompiler/IL/Transforms/TransformInlineAssignment.cs
  4. 2
      ICSharpCode.Decompiler/Tests/Helpers/Tester.cs
  5. 4
      ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
  6. 7
      ICSharpCode.Decompiler/Tests/PrettyTestRunner.cs
  7. 2
      ICSharpCode.Decompiler/Tests/TestCases/Pretty/HelloWorld.cs
  8. 4
      ICSharpCode.Decompiler/Tests/TestCases/Pretty/HelloWorld.il
  9. 40
      ICSharpCode.Decompiler/Tests/TestCases/Pretty/InlineAssignmentTest.cs
  10. 91
      ICSharpCode.Decompiler/Tests/TestCases/Pretty/InlineAssignmentTest.il

1
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -59,6 +59,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -59,6 +59,7 @@ namespace ICSharpCode.Decompiler.CSharp
new IntroduceExitPoints(),
new ConditionDetection(),
new ILInlining(),
new TransformInlineAssignment(),
new CopyPropagation(),
new InlineCompilerGeneratedVariables(),
new ExpressionTransforms(), // must run once before "the loop" to allow RemoveDeadVariablesInit

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -152,6 +152,7 @@ @@ -152,6 +152,7 @@
<Compile Include="IL\InstructionOutputExtensions.cs" />
<Compile Include="IL\PrimitiveType.cs" />
<Compile Include="IL\StackType.cs" />
<Compile Include="IL\Transforms\TransformInlineAssignment.cs" />
<Compile Include="NRExtensions.cs" />
<Compile Include="Output\ITextOutput.cs" />
<Compile Include="Output\PlainTextOutput.cs" />

55
ICSharpCode.Decompiler/IL/Transforms/TransformInlineAssignment.cs

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
// Copyright (c) 2015 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
namespace ICSharpCode.Decompiler.IL.Transforms
{
/// <summary>
/// Description of TransformInlineAssignment.
/// </summary>
public class TransformInlineAssignment : IILTransform
{
ILTransformContext context;
void IILTransform.Run(ILFunction function, ILTransformContext context)
{
this.context = context;
foreach (var block in function.Descendants.OfType<Block>()) {
for (int i = block.Instructions.Count - 1; i >= 0; i--) {
var inst = block.Instructions[i] as StLoc;
var nextInst = block.Instructions.ElementAtOrDefault(i + 1) as StLoc;
ILVariable localVariable;
// stloc s(value)
// stloc l(ldloc s)
// -->
// stloc s(stloc l(value))
if (inst != null && nextInst != null && nextInst.Variable.Kind != VariableKind.StackSlot && nextInst.Value.MatchLdLoc(inst.Variable)) {
block.Instructions.RemoveAt(i + 1);
var value = inst.Value.Clone();
inst.Value.ReplaceWith(new StLoc(nextInst.Variable, value));
}
}
}
}
}
}

2
ICSharpCode.Decompiler/Tests/Helpers/Tester.cs

@ -56,7 +56,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -56,7 +56,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
public static string AssembleIL(string sourceFileName, AssemblerOptions options = AssemblerOptions.UseDebug)
{
string ilasmPath = Path.Combine(Environment.GetEnvironmentVariable("windir"), @"Microsoft.NET\Framework\v4.0.30319\ilasm.exe");
string outputFile = sourceFileName + ".exe";
string outputFile = Path.GetFileNameWithoutExtension(sourceFileName) + ".exe";
string otherOptions = " ";

4
ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj

@ -135,6 +135,7 @@ @@ -135,6 +135,7 @@
<Compile Include="TestCases\Correctness\ValueTypeCall.cs" />
<Compile Include="CorrectnessTestRunner.cs" />
<Compile Include="TestCases\Pretty\HelloWorld.cs" />
<Compile Include="TestCases\Pretty\InlineAssignmentTest.cs" />
<Compile Include="Util\IntervalTests.cs" />
<Compile Include="Util\LongSetTests.cs" />
<Compile Include="CustomAttributes\CustomAttributeTests.cs" />
@ -151,6 +152,9 @@ @@ -151,6 +152,9 @@
<None Include="TestCases\Pretty\HelloWorld.il">
<DependentUpon>HelloWorld.cs</DependentUpon>
</None>
<None Include="TestCases\Pretty\InlineAssignmentTest.il">
<DependentUpon>InlineAssignmentTest.cs</DependentUpon>
</None>
<None Include="TestCases\Pretty\Readme.txt" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />

7
ICSharpCode.Decompiler/Tests/PrettyTestRunner.cs

@ -55,6 +55,13 @@ namespace ICSharpCode.Decompiler.Tests @@ -55,6 +55,13 @@ namespace ICSharpCode.Decompiler.Tests
Run("HelloWorld", AssemblerOptions.UseDebug);
}
[Test]
public void InlineAssignmentTest()
{
Run("InlineAssignmentTest");
Run("InlineAssignmentTest", AssemblerOptions.UseDebug);
}
void Run(string testName, AssemblerOptions asmOptions = AssemblerOptions.None)
{
var ilFile = Path.Combine(TestCasePath, testName + ".il");

2
ICSharpCode.Decompiler/Tests/TestCases/Pretty/HelloWorld.cs

@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
using System;
namespace Pretty.HelloWorld
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.HelloWorld
{
public class HelloWorld
{

4
ICSharpCode.Decompiler/Tests/TestCases/Pretty/HelloWorld.il

@ -30,7 +30,7 @@ @@ -30,7 +30,7 @@
// =============== CLASS MEMBERS DECLARATION ===================
.class public auto ansi beforefieldinit Pretty.HelloWorld.HelloWorld
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.HelloWorld.HelloWorld
extends [mscorlib]System.Object
{
.method public hidebysig static void Main() cil managed
@ -55,4 +55,4 @@ @@ -55,4 +55,4 @@
IL_0006: ret
} // end of method HelloWorld::.ctor
} // end of class Pretty.HelloWorld.HelloWorld
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.HelloWorld.HelloWorld

40
ICSharpCode.Decompiler/Tests/TestCases/Pretty/InlineAssignmentTest.cs

@ -0,0 +1,40 @@ @@ -0,0 +1,40 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
public class InlineAssignmentTest
{
public static void Main()
{
}
public void SimpleInlineWithLocals()
{
int V_0;
Console.WriteLine(V_0 = 5);
Console.WriteLine(V_0);
InlineAssignmentTest V_1;
Console.WriteLine((object)(V_1 = new InlineAssignmentTest()));
Console.WriteLine((object)V_1);
}
}
}

91
ICSharpCode.Decompiler/Tests/TestCases/Pretty/InlineAssignmentTest.il

@ -0,0 +1,91 @@ @@ -0,0 +1,91 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Copyright (c) Microsoft Corporation. All rights reserved.
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly '3cbicayo'
{
.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
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module '3cbicayo.exe'
// MVID: {3A96AD80-B3A9-47A7-82A9-A9BE249D0492}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x01100000
// =============== CLASS MEMBERS DECLARATION ===================
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.InlineAssignmentTest
extends [mscorlib]System.Object
{
.method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method InlineAssignmentTest::Main
.method public hidebysig instance void
SimpleInlineWithLocals() cil managed
{
// Code size 38 (0x26)
.maxstack 2
.locals init (int32 V_0,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.InlineAssignmentTest V_1)
IL_0000: nop
IL_0001: ldc.i4.5
IL_0002: dup
IL_0003: stloc.0
IL_0004: call void [mscorlib]System.Console::WriteLine(int32)
IL_0009: nop
IL_000a: ldloc.0
IL_000b: call void [mscorlib]System.Console::WriteLine(int32)
IL_0010: nop
IL_0011: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.InlineAssignmentTest::.ctor()
IL_0016: dup
IL_0017: stloc.1
IL_0018: call void [mscorlib]System.Console::WriteLine(object)
IL_001d: nop
IL_001e: ldloc.1
IL_001f: call void [mscorlib]System.Console::WriteLine(object)
IL_0024: nop
IL_0025: ret
} // end of method InlineAssignmentTest::SimpleInlineWithLocals
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method InlineAssignmentTest::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.InlineAssignmentTest
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************
// WARNING: Created Win32 resource file ../../Tests/TestCases/Pretty\InlineAssignmentTest.res
Loading…
Cancel
Save