Operation.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Diagnostics;
  3. namespace Ryujinx.Graphics.Shader.IntermediateRepresentation
  4. {
  5. class Operation : INode
  6. {
  7. public Instruction Inst { get; private set; }
  8. private Operand[] _dests;
  9. public Operand Dest
  10. {
  11. get
  12. {
  13. return _dests.Length != 0 ? _dests[0] : null;
  14. }
  15. set
  16. {
  17. if (value != null && value.Type == OperandType.LocalVariable)
  18. {
  19. value.AsgOp = this;
  20. }
  21. if (value != null)
  22. {
  23. _dests = new[] { value };
  24. }
  25. else
  26. {
  27. _dests = Array.Empty<Operand>();
  28. }
  29. }
  30. }
  31. public int DestsCount => _dests.Length;
  32. private Operand[] _sources;
  33. public int SourcesCount => _sources.Length;
  34. public int Index { get; }
  35. private Operation(Operand[] sources)
  36. {
  37. // The array may be modified externally, so we store a copy.
  38. _sources = (Operand[])sources.Clone();
  39. for (int index = 0; index < _sources.Length; index++)
  40. {
  41. Operand source = _sources[index];
  42. if (source.Type == OperandType.LocalVariable)
  43. {
  44. source.UseOps.Add(this);
  45. }
  46. }
  47. }
  48. public Operation(Instruction inst, int index, Operand[] dests, Operand[] sources) : this(sources)
  49. {
  50. Inst = inst;
  51. Index = index;
  52. // The array may be modified externally, so we store a copy.
  53. _dests = (Operand[])dests.Clone();
  54. for (int dstIndex = 0; dstIndex < dests.Length; dstIndex++)
  55. {
  56. Operand dest = dests[dstIndex];
  57. if (dest != null && dest.Type == OperandType.LocalVariable)
  58. {
  59. dest.AsgOp = this;
  60. }
  61. }
  62. }
  63. public Operation(Instruction inst, Operand dest, params Operand[] sources) : this(sources)
  64. {
  65. Inst = inst;
  66. if (dest != null)
  67. {
  68. dest.AsgOp = this;
  69. _dests = new[] { dest };
  70. }
  71. else
  72. {
  73. _dests = Array.Empty<Operand>();
  74. }
  75. }
  76. public Operation(Instruction inst, int index, Operand dest, params Operand[] sources) : this(inst, dest, sources)
  77. {
  78. Index = index;
  79. }
  80. public void AppendDests(Operand[] operands)
  81. {
  82. int startIndex = _dests.Length;
  83. Array.Resize(ref _dests, startIndex + operands.Length);
  84. for (int index = 0; index < operands.Length; index++)
  85. {
  86. Operand dest = operands[index];
  87. if (dest != null && dest.Type == OperandType.LocalVariable)
  88. {
  89. Debug.Assert(dest.AsgOp == null);
  90. dest.AsgOp = this;
  91. }
  92. _dests[startIndex + index] = dest;
  93. }
  94. }
  95. public void AppendSources(Operand[] operands)
  96. {
  97. int startIndex = _sources.Length;
  98. Array.Resize(ref _sources, startIndex + operands.Length);
  99. for (int index = 0; index < operands.Length; index++)
  100. {
  101. Operand source = operands[index];
  102. if (source.Type == OperandType.LocalVariable)
  103. {
  104. source.UseOps.Add(this);
  105. }
  106. _sources[startIndex + index] = source;
  107. }
  108. }
  109. public Operand GetDest(int index)
  110. {
  111. return _dests[index];
  112. }
  113. public Operand GetSource(int index)
  114. {
  115. return _sources[index];
  116. }
  117. public void SetDest(int index, Operand dest)
  118. {
  119. Operand oldDest = _dests[index];
  120. if (oldDest != null && oldDest.Type == OperandType.LocalVariable)
  121. {
  122. oldDest.AsgOp = null;
  123. }
  124. if (dest != null && dest.Type == OperandType.LocalVariable)
  125. {
  126. dest.AsgOp = this;
  127. }
  128. _dests[index] = dest;
  129. }
  130. public void SetSource(int index, Operand source)
  131. {
  132. Operand oldSrc = _sources[index];
  133. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  134. {
  135. oldSrc.UseOps.Remove(this);
  136. }
  137. if (source != null && source.Type == OperandType.LocalVariable)
  138. {
  139. source.UseOps.Add(this);
  140. }
  141. _sources[index] = source;
  142. }
  143. public void InsertSource(int index, Operand source)
  144. {
  145. Operand[] newSources = new Operand[_sources.Length + 1];
  146. Array.Copy(_sources, 0, newSources, 0, index);
  147. Array.Copy(_sources, index, newSources, index + 1, _sources.Length - index);
  148. newSources[index] = source;
  149. _sources = newSources;
  150. }
  151. protected void RemoveSource(int index)
  152. {
  153. SetSource(index, null);
  154. Operand[] newSources = new Operand[_sources.Length - 1];
  155. Array.Copy(_sources, 0, newSources, 0, index);
  156. Array.Copy(_sources, index + 1, newSources, index, _sources.Length - (index + 1));
  157. _sources = newSources;
  158. }
  159. public void TurnIntoCopy(Operand source)
  160. {
  161. TurnInto(Instruction.Copy, source);
  162. }
  163. public void TurnInto(Instruction newInst, Operand source)
  164. {
  165. Inst = newInst;
  166. foreach (Operand oldSrc in _sources)
  167. {
  168. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  169. {
  170. oldSrc.UseOps.Remove(this);
  171. }
  172. }
  173. if (source.Type == OperandType.LocalVariable)
  174. {
  175. source.UseOps.Add(this);
  176. }
  177. _sources = new Operand[] { source };
  178. }
  179. }
  180. }