Node.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. Operand oldOp = _destinations[index];
  51. if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
  52. {
  53. oldOp.Assignments.Remove(this);
  54. }
  55. if (destination != null && destination.Kind == OperandKind.LocalVariable)
  56. {
  57. destination.Assignments.Add(this);
  58. }
  59. _destinations[index] = destination;
  60. }
  61. public void SetSource(int index, Operand source)
  62. {
  63. Operand oldOp = _sources[index];
  64. if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
  65. {
  66. oldOp.Uses.Remove(this);
  67. }
  68. if (source != null && source.Kind == OperandKind.LocalVariable)
  69. {
  70. source.Uses.Add(this);
  71. }
  72. _sources[index] = source;
  73. }
  74. public void SetDestinations(Operand[] destinations)
  75. {
  76. if (_destinations != null)
  77. {
  78. for (int index = 0; index < _destinations.Length; index++)
  79. {
  80. Operand oldOp = _destinations[index];
  81. if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
  82. {
  83. oldOp.Assignments.Remove(this);
  84. }
  85. }
  86. _destinations = destinations;
  87. }
  88. else
  89. {
  90. _destinations = new Operand[destinations.Length];
  91. }
  92. for (int index = 0; index < destinations.Length; index++)
  93. {
  94. Operand newOp = destinations[index];
  95. _destinations[index] = newOp;
  96. if (newOp.Kind == OperandKind.LocalVariable)
  97. {
  98. newOp.Assignments.Add(this);
  99. }
  100. }
  101. }
  102. public void SetSources(Operand[] sources)
  103. {
  104. for (int index = 0; index < _sources.Length; index++)
  105. {
  106. Operand oldOp = _sources[index];
  107. if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
  108. {
  109. oldOp.Uses.Remove(this);
  110. }
  111. }
  112. _sources = new Operand[sources.Length];
  113. for (int index = 0; index < sources.Length; index++)
  114. {
  115. Operand newOp = sources[index];
  116. _sources[index] = newOp;
  117. if (newOp.Kind == OperandKind.LocalVariable)
  118. {
  119. newOp.Uses.Add(this);
  120. }
  121. }
  122. }
  123. }
  124. }