Node.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ARMeilleure.IntermediateRepresentation
  4. {
  5. class Node
  6. {
  7. public Operand Destination
  8. {
  9. get
  10. {
  11. return _destinations.Length != 0 ? GetDestination(0) : null;
  12. }
  13. set
  14. {
  15. if (value != null)
  16. {
  17. SetDestinations(new Operand[] { value });
  18. }
  19. else
  20. {
  21. SetDestinations(new Operand[0]);
  22. }
  23. }
  24. }
  25. private Operand[] _destinations;
  26. private Operand[] _sources;
  27. private LinkedListNode<Node>[] _asgUseNodes;
  28. private LinkedListNode<Node>[] _srcUseNodes;
  29. public int DestinationsCount => _destinations.Length;
  30. public int SourcesCount => _sources.Length;
  31. public Node(Operand destination, int sourcesCount)
  32. {
  33. Destination = destination;
  34. _sources = new Operand[sourcesCount];
  35. _srcUseNodes = new LinkedListNode<Node>[sourcesCount];
  36. }
  37. public Node(Operand[] destinations, int sourcesCount)
  38. {
  39. SetDestinations(destinations ?? throw new ArgumentNullException(nameof(destinations)));
  40. _sources = new Operand[sourcesCount];
  41. _srcUseNodes = new LinkedListNode<Node>[sourcesCount];
  42. }
  43. public Operand GetDestination(int index)
  44. {
  45. return _destinations[index];
  46. }
  47. public Operand GetSource(int index)
  48. {
  49. return _sources[index];
  50. }
  51. public void SetDestination(int index, Operand destination)
  52. {
  53. Operand oldOp = _destinations[index];
  54. if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
  55. {
  56. oldOp.Assignments.Remove(_asgUseNodes[index]);
  57. }
  58. if (destination != null && destination.Kind == OperandKind.LocalVariable)
  59. {
  60. _asgUseNodes[index] = destination.Assignments.AddLast(this);
  61. }
  62. _destinations[index] = destination;
  63. }
  64. public void SetSource(int index, Operand source)
  65. {
  66. Operand oldOp = _sources[index];
  67. if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
  68. {
  69. oldOp.Uses.Remove(_srcUseNodes[index]);
  70. }
  71. if (source != null && source.Kind == OperandKind.LocalVariable)
  72. {
  73. _srcUseNodes[index] = source.Uses.AddLast(this);
  74. }
  75. _sources[index] = source;
  76. }
  77. public void SetDestinations(Operand[] destinations)
  78. {
  79. if (_destinations != null)
  80. {
  81. for (int index = 0; index < _destinations.Length; index++)
  82. {
  83. Operand oldOp = _destinations[index];
  84. if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
  85. {
  86. oldOp.Assignments.Remove(_asgUseNodes[index]);
  87. }
  88. }
  89. _destinations = destinations;
  90. }
  91. else
  92. {
  93. _destinations = new Operand[destinations.Length];
  94. }
  95. _asgUseNodes = new LinkedListNode<Node>[destinations.Length];
  96. for (int index = 0; index < destinations.Length; index++)
  97. {
  98. Operand newOp = destinations[index];
  99. _destinations[index] = newOp;
  100. if (newOp.Kind == OperandKind.LocalVariable)
  101. {
  102. _asgUseNodes[index] = newOp.Assignments.AddLast(this);
  103. }
  104. }
  105. }
  106. public void SetSources(Operand[] sources)
  107. {
  108. for (int index = 0; index < _sources.Length; index++)
  109. {
  110. Operand oldOp = _sources[index];
  111. if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
  112. {
  113. oldOp.Uses.Remove(_srcUseNodes[index]);
  114. }
  115. }
  116. _sources = new Operand[sources.Length];
  117. _srcUseNodes = new LinkedListNode<Node>[sources.Length];
  118. for (int index = 0; index < sources.Length; index++)
  119. {
  120. Operand newOp = sources[index];
  121. _sources[index] = newOp;
  122. if (newOp.Kind == OperandKind.LocalVariable)
  123. {
  124. _srcUseNodes[index] = newOp.Uses.AddLast(this);
  125. }
  126. }
  127. }
  128. }
  129. }