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.

142 lines
5.9 KiB

  1. // Copyright (c) 2019 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. using System.Linq;
  19. namespace ICSharpCode.Decompiler.IL.Transforms
  20. {
  21. public class DynamicIsEventAssignmentTransform : IStatementTransform
  22. {
  23. /// stloc V_1(dynamic.isevent (target))
  24. /// if (logic.not(ldloc V_1)) Block IL_004a {
  25. /// stloc V_2(dynamic.getmember B(target))
  26. /// }
  27. /// [stloc copyOfValue(value)]
  28. /// if (logic.not(ldloc V_1)) Block IL_0149 {
  29. /// dynamic.setmember.compound B(target, dynamic.binary.operator AddAssign(ldloc V_2, value))
  30. /// } else Block IL_0151 {
  31. /// dynamic.invokemember.invokespecial.discard add_B(target, value)
  32. /// }
  33. /// =>
  34. /// if (logic.not(dynamic.isevent (target))) Block IL_0149 {
  35. /// dynamic.setmember.compound B(target, dynamic.binary.operator AddAssign(dynamic.getmember B(target), value))
  36. /// } else Block IL_0151 {
  37. /// dynamic.invokemember.invokespecial.discard add_B(target, value)
  38. /// }
  39. public void Run(Block block, int pos, StatementTransformContext context)
  40. {
  41. if (!(pos + 3 < block.Instructions.Count && block.Instructions[pos].MatchStLoc(out var flagVar, out var inst) && inst is DynamicIsEventInstruction isEvent))
  42. return;
  43. if (!(flagVar.IsSingleDefinition && flagVar.LoadCount == 2))
  44. return;
  45. if (!MatchLhsCacheIfInstruction(block.Instructions[pos + 1], flagVar, out var dynamicGetMemberStore))
  46. return;
  47. if (!(dynamicGetMemberStore.MatchStLoc(out var getMemberVar, out inst) && inst is DynamicGetMemberInstruction getMemberInst))
  48. return;
  49. int offset = 2;
  50. if (block.Instructions[pos + offset].MatchStLoc(out var valueVariable)
  51. && pos + 4 < block.Instructions.Count && valueVariable.IsSingleDefinition && valueVariable.LoadCount == 2
  52. && valueVariable.LoadInstructions.All(ld => ld.Parent is DynamicInstruction))
  53. {
  54. offset++;
  55. }
  56. foreach (var descendant in block.Instructions[pos + offset].Descendants)
  57. {
  58. if (!MatchIsEventAssignmentIfInstruction(descendant, isEvent, flagVar, getMemberVar, out var setMemberInst, out var getMemberVarUse, out var isEventConditionUse))
  59. continue;
  60. context.Step("DynamicIsEventAssignmentTransform", block.Instructions[pos]);
  61. // Collapse duplicate condition
  62. getMemberVarUse.ReplaceWith(getMemberInst);
  63. isEventConditionUse.ReplaceWith(isEvent);
  64. block.Instructions.RemoveRange(pos, 2);
  65. // Reuse ExpressionTransforms
  66. ExpressionTransforms.TransformDynamicSetMemberInstruction(setMemberInst, context);
  67. context.RequestRerun();
  68. break;
  69. }
  70. }
  71. /// <summary>
  72. /// if (logic.not(ldloc V_1)) Block IL_0149 {
  73. /// dynamic.setmember.compound B(target, dynamic.binary.operator AddAssign(ldloc V_2, value))
  74. /// } else Block IL_0151 {
  75. /// dynamic.invokemember.invokespecial.discard add_B(target, value)
  76. /// }
  77. /// </summary>
  78. static bool MatchIsEventAssignmentIfInstruction(ILInstruction ifInst, DynamicIsEventInstruction isEvent, ILVariable flagVar, ILVariable getMemberVar,
  79. out DynamicSetMemberInstruction setMemberInst, out ILInstruction getMemberVarUse, out ILInstruction isEventConditionUse)
  80. {
  81. setMemberInst = null;
  82. getMemberVarUse = null;
  83. isEventConditionUse = null;
  84. if (!ifInst.MatchIfInstruction(out var condition, out var trueInst, out var falseInst))
  85. return false;
  86. if (MatchFlagEqualsZero(condition, flagVar))
  87. {
  88. if (!condition.MatchCompEquals(out var left, out _))
  89. return false;
  90. isEventConditionUse = left;
  91. }
  92. else if (condition.MatchLdLoc(flagVar))
  93. {
  94. var tmp = trueInst;
  95. trueInst = falseInst;
  96. falseInst = tmp;
  97. isEventConditionUse = condition;
  98. }
  99. else
  100. return false;
  101. setMemberInst = Block.Unwrap(trueInst) as DynamicSetMemberInstruction;
  102. if (setMemberInst == null)
  103. return false;
  104. if (!isEvent.Argument.Match(setMemberInst.Target).Success)
  105. return false;
  106. if (!(Block.Unwrap(falseInst) is DynamicInvokeMemberInstruction invokeMemberInst && invokeMemberInst.Arguments.Count == 2))
  107. return false;
  108. if (!isEvent.Argument.Match(invokeMemberInst.Arguments[0]).Success)
  109. return false;
  110. if (!(setMemberInst.Value is DynamicBinaryOperatorInstruction binOp && binOp.Left.MatchLdLoc(getMemberVar)))
  111. return false;
  112. getMemberVarUse = binOp.Left;
  113. return true;
  114. }
  115. /// <summary>
  116. /// if (logic.not(ldloc V_1)) Block IL_004a {
  117. /// stloc V_2(dynamic.getmember B(target))
  118. /// }
  119. /// </summary>
  120. static bool MatchLhsCacheIfInstruction(ILInstruction ifInst, ILVariable flagVar, out StLoc cacheStore)
  121. {
  122. cacheStore = null;
  123. if (!ifInst.MatchIfInstruction(out var condition, out var trueInst))
  124. return false;
  125. if (!MatchFlagEqualsZero(condition, flagVar))
  126. return false;
  127. cacheStore = Block.Unwrap(trueInst) as StLoc;
  128. return cacheStore != null;
  129. }
  130. static bool MatchFlagEqualsZero(ILInstruction condition, ILVariable flagVar)
  131. {
  132. return condition.MatchCompEquals(out var left, out var right)
  133. && left.MatchLdLoc(flagVar)
  134. && right.MatchLdcI4(0);
  135. }
  136. }
  137. }