Node.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ARMeilleure.IntermediateRepresentation
  4. {
  5. class Node : IIntrusiveListNode<Node>
  6. {
  7. public Node ListPrevious { get; set; }
  8. public Node ListNext { get; set; }
  9. public Operand Destination
  10. {
  11. get => _destinations.Count != 0 ? GetDestination(0) : null;
  12. set => SetDestination(value);
  13. }
  14. private readonly List<Operand> _destinations;
  15. private readonly List<Operand> _sources;
  16. private bool _clearedDest;
  17. public int DestinationsCount => _destinations.Count;
  18. public int SourcesCount => _sources.Count;
  19. private void Resize(List<Operand> list, int size)
  20. {
  21. if (list.Count > size)
  22. {
  23. list.RemoveRange(size, list.Count - size);
  24. }
  25. else
  26. {
  27. while (list.Count < size)
  28. {
  29. list.Add(null);
  30. }
  31. }
  32. }
  33. public Node()
  34. {
  35. _destinations = new List<Operand>();
  36. _sources = new List<Operand>();
  37. }
  38. public Node(Operand destination, int sourcesCount) : this()
  39. {
  40. Destination = destination;
  41. Resize(_sources, sourcesCount);
  42. }
  43. private void Reset(int sourcesCount)
  44. {
  45. _clearedDest = true;
  46. _sources.Clear();
  47. ListPrevious = null;
  48. ListNext = null;
  49. Resize(_sources, sourcesCount);
  50. }
  51. public Node With(Operand destination, int sourcesCount)
  52. {
  53. Reset(sourcesCount);
  54. Destination = destination;
  55. return this;
  56. }
  57. public Node With(Operand[] destinations, int sourcesCount)
  58. {
  59. Reset(sourcesCount);
  60. SetDestinations(destinations ?? throw new ArgumentNullException(nameof(destinations)));
  61. return this;
  62. }
  63. public Operand GetDestination(int index)
  64. {
  65. return _destinations[index];
  66. }
  67. public Operand GetSource(int index)
  68. {
  69. return _sources[index];
  70. }
  71. public void SetDestination(int index, Operand destination)
  72. {
  73. if (!_clearedDest)
  74. {
  75. RemoveAssignment(_destinations[index]);
  76. }
  77. AddAssignment(destination);
  78. _clearedDest = false;
  79. _destinations[index] = destination;
  80. }
  81. public void SetSource(int index, Operand source)
  82. {
  83. RemoveUse(_sources[index]);
  84. AddUse(source);
  85. _sources[index] = source;
  86. }
  87. private void RemoveOldDestinations()
  88. {
  89. if (!_clearedDest)
  90. {
  91. for (int index = 0; index < _destinations.Count; index++)
  92. {
  93. RemoveAssignment(_destinations[index]);
  94. }
  95. }
  96. _clearedDest = false;
  97. }
  98. public void SetDestination(Operand destination)
  99. {
  100. RemoveOldDestinations();
  101. if (destination == null)
  102. {
  103. _destinations.Clear();
  104. _clearedDest = true;
  105. }
  106. else
  107. {
  108. Resize(_destinations, 1);
  109. _destinations[0] = destination;
  110. AddAssignment(destination);
  111. }
  112. }
  113. public void SetDestinations(Operand[] destinations)
  114. {
  115. RemoveOldDestinations();
  116. Resize(_destinations, destinations.Length);
  117. for (int index = 0; index < destinations.Length; index++)
  118. {
  119. Operand newOp = destinations[index];
  120. _destinations[index] = newOp;
  121. AddAssignment(newOp);
  122. }
  123. }
  124. private void RemoveOldSources()
  125. {
  126. for (int index = 0; index < _sources.Count; index++)
  127. {
  128. RemoveUse(_sources[index]);
  129. }
  130. }
  131. public void SetSource(Operand source)
  132. {
  133. RemoveOldSources();
  134. if (source == null)
  135. {
  136. _sources.Clear();
  137. }
  138. else
  139. {
  140. Resize(_sources, 1);
  141. _sources[0] = source;
  142. AddUse(source);
  143. }
  144. }
  145. public void SetSources(Operand[] sources)
  146. {
  147. RemoveOldSources();
  148. Resize(_sources, sources.Length);
  149. for (int index = 0; index < sources.Length; index++)
  150. {
  151. Operand newOp = sources[index];
  152. _sources[index] = newOp;
  153. AddUse(newOp);
  154. }
  155. }
  156. private void AddAssignment(Operand op)
  157. {
  158. if (op == null)
  159. {
  160. return;
  161. }
  162. if (op.Kind == OperandKind.LocalVariable)
  163. {
  164. op.Assignments.Add(this);
  165. }
  166. else if (op.Kind == OperandKind.Memory)
  167. {
  168. MemoryOperand memOp = (MemoryOperand)op;
  169. if (memOp.BaseAddress != null)
  170. {
  171. memOp.BaseAddress.Assignments.Add(this);
  172. }
  173. if (memOp.Index != null)
  174. {
  175. memOp.Index.Assignments.Add(this);
  176. }
  177. }
  178. }
  179. private void RemoveAssignment(Operand op)
  180. {
  181. if (op == null)
  182. {
  183. return;
  184. }
  185. if (op.Kind == OperandKind.LocalVariable)
  186. {
  187. op.Assignments.Remove(this);
  188. }
  189. else if (op.Kind == OperandKind.Memory)
  190. {
  191. MemoryOperand memOp = (MemoryOperand)op;
  192. if (memOp.BaseAddress != null)
  193. {
  194. memOp.BaseAddress.Assignments.Remove(this);
  195. }
  196. if (memOp.Index != null)
  197. {
  198. memOp.Index.Assignments.Remove(this);
  199. }
  200. }
  201. }
  202. private void AddUse(Operand op)
  203. {
  204. if (op == null)
  205. {
  206. return;
  207. }
  208. if (op.Kind == OperandKind.LocalVariable)
  209. {
  210. op.Uses.Add(this);
  211. }
  212. else if (op.Kind == OperandKind.Memory)
  213. {
  214. MemoryOperand memOp = (MemoryOperand)op;
  215. if (memOp.BaseAddress != null)
  216. {
  217. memOp.BaseAddress.Uses.Add(this);
  218. }
  219. if (memOp.Index != null)
  220. {
  221. memOp.Index.Uses.Add(this);
  222. }
  223. }
  224. }
  225. private void RemoveUse(Operand op)
  226. {
  227. if (op == null)
  228. {
  229. return;
  230. }
  231. if (op.Kind == OperandKind.LocalVariable)
  232. {
  233. op.Uses.Remove(this);
  234. }
  235. else if (op.Kind == OperandKind.Memory)
  236. {
  237. MemoryOperand memOp = (MemoryOperand)op;
  238. if (memOp.BaseAddress != null)
  239. {
  240. memOp.BaseAddress.Uses.Remove(this);
  241. }
  242. if (memOp.Index != null)
  243. {
  244. memOp.Index.Uses.Remove(this);
  245. }
  246. }
  247. }
  248. }
  249. }