You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.7 KiB

  1. // Copyright (c) 2021 Siegfried Pammer
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. #nullable enable
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using ICSharpCode.Decompiler.TypeSystem;
  24. namespace ICSharpCode.Decompiler.IL.Transforms
  25. {
  26. // stloc s(ldloca v)
  27. // stobj System.DateTime(ldloc s, default.value T)
  28. // where s.LoadCount > 1
  29. // which is: ldloca; dup; initobj in IL (generated by Roslyn >= 2)
  30. // =>
  31. // stloc v(default.value T)
  32. // stloc s(ldloca v)
  33. // which is: ldloca; ldloca; initobj in IL (generated by legacy csc)
  34. //
  35. // The second pattern allows inlining in the subsequent uses.
  36. class LdLocaDupInitObjTransform : IILTransform
  37. {
  38. void IILTransform.Run(ILFunction function, ILTransformContext context)
  39. {
  40. foreach (var block in function.Descendants.OfType<Block>())
  41. {
  42. for (int i = 0; i < block.Instructions.Count; i++)
  43. {
  44. TryTransform(block, i, context);
  45. }
  46. }
  47. }
  48. private bool TryTransform(Block block, int i, ILTransformContext context)
  49. {
  50. if (block.Instructions[i] is not StLoc { Variable: var s, Value: LdLoca { Variable: var v } } inst1)
  51. {
  52. return false;
  53. }
  54. if (block.Instructions.ElementAtOrDefault(i + 1) is not StObj inst2)
  55. {
  56. return false;
  57. }
  58. if (!(inst2.Target.MatchLdLoc(s)
  59. && TypeUtils.IsCompatibleTypeForMemoryAccess(v.Type, inst2.Type)
  60. && inst2.UnalignedPrefix == 0
  61. && !inst2.IsVolatile
  62. && inst2.Value is DefaultValue))
  63. {
  64. return false;
  65. }
  66. context.Step("LdLocaDupInitObjTransform", inst1);
  67. block.Instructions[i] = new StLoc(v, inst2.Value).WithILRange(inst2);
  68. block.Instructions[i + 1] = inst1;
  69. return true;
  70. }
  71. }
  72. }