Node.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. namespace ARMeilleure.IntermediateRepresentation
  3. {
  4. class Node : IIntrusiveListNode<Node>
  5. {
  6. public Node ListPrevious { get; set; }
  7. public Node ListNext { get; set; }
  8. public Operand Destination
  9. {
  10. get
  11. {
  12. return _destinations.Length != 0 ? GetDestination(0) : null;
  13. }
  14. set
  15. {
  16. if (value != null)
  17. {
  18. SetDestinations(new Operand[] { value });
  19. }
  20. else
  21. {
  22. SetDestinations(new Operand[0]);
  23. }
  24. }
  25. }
  26. private Operand[] _destinations;
  27. private Operand[] _sources;
  28. public int DestinationsCount => _destinations.Length;
  29. public int SourcesCount => _sources.Length;
  30. public Node(Operand destination, int sourcesCount)
  31. {
  32. Destination = destination;
  33. _sources = new Operand[sourcesCount];
  34. }
  35. public Node(Operand[] destinations, int sourcesCount)
  36. {
  37. SetDestinations(destinations ?? throw new ArgumentNullException(nameof(destinations)));
  38. _sources = new Operand[sourcesCount];
  39. }
  40. public Operand GetDestination(int index)
  41. {
  42. return _destinations[index];
  43. }
  44. public Operand GetSource(int index)
  45. {
  46. return _sources[index];
  47. }
  48. public void SetDestination(int index, Operand destination)
  49. {
  50. RemoveAssignment(_destinations[index]);
  51. AddAssignment(destination);
  52. _destinations[index] = destination;
  53. }
  54. public void SetSource(int index, Operand source)
  55. {
  56. RemoveUse(_sources[index]);
  57. AddUse(source);
  58. _sources[index] = source;
  59. }
  60. public void SetDestinations(Operand[] destinations)
  61. {
  62. if (_destinations != null)
  63. {
  64. for (int index = 0; index < _destinations.Length; index++)
  65. {
  66. RemoveAssignment(_destinations[index]);
  67. }
  68. _destinations = destinations;
  69. }
  70. else
  71. {
  72. _destinations = new Operand[destinations.Length];
  73. }
  74. for (int index = 0; index < destinations.Length; index++)
  75. {
  76. Operand newOp = destinations[index];
  77. _destinations[index] = newOp;
  78. AddAssignment(newOp);
  79. }
  80. }
  81. public void SetSources(Operand[] sources)
  82. {
  83. for (int index = 0; index < _sources.Length; index++)
  84. {
  85. RemoveUse(_sources[index]);
  86. }
  87. _sources = new Operand[sources.Length];
  88. for (int index = 0; index < sources.Length; index++)
  89. {
  90. Operand newOp = sources[index];
  91. _sources[index] = newOp;
  92. AddUse(newOp);
  93. }
  94. }
  95. private void AddAssignment(Operand op)
  96. {
  97. if (op == null)
  98. {
  99. return;
  100. }
  101. if (op.Kind == OperandKind.LocalVariable)
  102. {
  103. op.Assignments.Add(this);
  104. }
  105. else if (op.Kind == OperandKind.Memory)
  106. {
  107. MemoryOperand memOp = (MemoryOperand)op;
  108. if (memOp.BaseAddress != null)
  109. {
  110. memOp.BaseAddress.Assignments.Add(this);
  111. }
  112. if (memOp.Index != null)
  113. {
  114. memOp.Index.Assignments.Add(this);
  115. }
  116. }
  117. }
  118. private void RemoveAssignment(Operand op)
  119. {
  120. if (op == null)
  121. {
  122. return;
  123. }
  124. if (op.Kind == OperandKind.LocalVariable)
  125. {
  126. op.Assignments.Remove(this);
  127. }
  128. else if (op.Kind == OperandKind.Memory)
  129. {
  130. MemoryOperand memOp = (MemoryOperand)op;
  131. if (memOp.BaseAddress != null)
  132. {
  133. memOp.BaseAddress.Assignments.Remove(this);
  134. }
  135. if (memOp.Index != null)
  136. {
  137. memOp.Index.Assignments.Remove(this);
  138. }
  139. }
  140. }
  141. private void AddUse(Operand op)
  142. {
  143. if (op == null)
  144. {
  145. return;
  146. }
  147. if (op.Kind == OperandKind.LocalVariable)
  148. {
  149. op.Uses.Add(this);
  150. }
  151. else if (op.Kind == OperandKind.Memory)
  152. {
  153. MemoryOperand memOp = (MemoryOperand)op;
  154. if (memOp.BaseAddress != null)
  155. {
  156. memOp.BaseAddress.Uses.Add(this);
  157. }
  158. if (memOp.Index != null)
  159. {
  160. memOp.Index.Uses.Add(this);
  161. }
  162. }
  163. }
  164. private void RemoveUse(Operand op)
  165. {
  166. if (op == null)
  167. {
  168. return;
  169. }
  170. if (op.Kind == OperandKind.LocalVariable)
  171. {
  172. op.Uses.Remove(this);
  173. }
  174. else if (op.Kind == OperandKind.Memory)
  175. {
  176. MemoryOperand memOp = (MemoryOperand)op;
  177. if (memOp.BaseAddress != null)
  178. {
  179. memOp.BaseAddress.Uses.Remove(this);
  180. }
  181. if (memOp.Index != null)
  182. {
  183. memOp.Index.Uses.Remove(this);
  184. }
  185. }
  186. }
  187. }
  188. }