Skip to content

Commit

Permalink
Fix icsharpcode#2231: Add support for ldloca; dup; initobj pattern us…
Browse files Browse the repository at this point in the history
…ed by Roslyn.
  • Loading branch information
ElektroKill committed Aug 11, 2021
1 parent d836a04 commit 0425eb6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,15 @@ public static void Issue1959(int a, int b, int? c)
Console.WriteLine("{}, {}", a < b, a > b);
Console.WriteLine("{}, {}", a < Environment.GetLogicalDrives().Length, a > (c ?? b));
}

public static Type Issue2231<T>()
{
return default(T).GetType();
}

public static string Issue2231b<T>()
{
return default(T).ToString();
}
}
}
10 changes: 10 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/ValueTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,15 @@ public static string CallOnIntegerConstant()
{
return ulong.MaxValue.ToString();
}

public static void M4()
{
Test(default(DateTime).GetType());
Test(default(DateTime).ToString());
}

public static void Test(object x)
{
}
}
}
1 change: 1 addition & 0 deletions ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static List<IILTransform> GetILTransforms()
new AsyncAwaitDecompiler(), // must run after inlining but before loop detection
new DetectCatchWhenConditionBlocks(), // must run after inlining but before loop detection
new DetectExitPoints(canIntroduceExitForReturn: false),
new LdLocaDupInitObjTransform(),
new EarlyExpressionTransforms(),
// RemoveDeadVariableInit must run after EarlyExpressionTransforms so that stobj(ldloca V, ...)
// is already collapsed into stloc(V, ...).
Expand Down
1 change: 1 addition & 0 deletions ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="CSharp\TranslatedStatement.cs" />
<Compile Include="IL\ControlFlow\AwaitInFinallyTransform.cs" />
<Compile Include="IL\Transforms\IntroduceNativeIntTypeOnLocals.cs" />
<Compile Include="IL\Transforms\LdLocaDupInitObjTransform.cs" />
<Compile Include="Semantics\OutVarResolveResult.cs" />
<Compile Include="CSharp\Syntax\AstNode.cs" />
<Compile Include="CSharp\Syntax\AstNodeCollection.cs" />
Expand Down
77 changes: 77 additions & 0 deletions ICSharpCode.Decompiler/IL/Transforms/LdLocaDupInitObjTransform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2021 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.

#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ICSharpCode.Decompiler.TypeSystem;

namespace ICSharpCode.Decompiler.IL.Transforms
{
// stloc s(ldloca v)
// stobj System.DateTime(ldloc s, default.value T)
// where s.LoadCount > 1
// which is: ldloca; dup; initobj in IL (generated by Roslyn >= 2)
// =>
// stloc v(default.value T)
// stloc s(ldloca v)
// which is: ldloca; ldloca; initobj in IL (generated by legacy csc)
//
// The second pattern allows inlining in the subsequent uses.
class LdLocaDupInitObjTransform : IILTransform
{
void IILTransform.Run(ILFunction function, ILTransformContext context)
{
foreach (var block in function.Descendants.OfType<Block>())
{
for (int i = 0; i < block.Instructions.Count; i++)
{
TryTransform(block, i, context);
}
}
}

private bool TryTransform(Block block, int i, ILTransformContext context)
{
if (block.Instructions[i] is not StLoc { Variable: var s, Value: LdLoca { Variable: var v } } inst1)
{
return false;
}
if (block.Instructions.ElementAtOrDefault(i + 1) is not StObj inst2)
{
return false;
}
if (!(inst2.Target.MatchLdLoc(s)
&& TypeUtils.IsCompatibleTypeForMemoryAccess(v.Type, inst2.Type)
&& inst2.UnalignedPrefix == 0
&& !inst2.IsVolatile
&& inst2.Value is DefaultValue))
{
return false;
}
context.Step("LdLocaDupInitObjTransform", inst1);
block.Instructions[i] = new StLoc(v, inst2.Value).WithILRange(inst2);
block.Instructions[i + 1] = inst1;
return true;
}
}
}

0 comments on commit 0425eb6

Please # to comment.