Operation.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. if (dests != null)
  53. {
  54. // The array may be modified externally, so we store a copy.
  55. _dests = (Operand[])dests.Clone();
  56. for (int dstIndex = 0; dstIndex < dests.Length; dstIndex++)
  57. {
  58. Operand dest = dests[dstIndex];
  59. if (dest != null && dest.Type == OperandType.LocalVariable)
  60. {
  61. dest.AsgOp = this;
  62. }
  63. }
  64. }
  65. else
  66. {
  67. _dests = Array.Empty<Operand>();
  68. }
  69. }
  70. public Operation(Instruction inst, Operand dest, params Operand[] sources) : this(sources)
  71. {
  72. Inst = inst;
  73. if (dest != null)
  74. {
  75. dest.AsgOp = this;
  76. _dests = new[] { dest };
  77. }
  78. else
  79. {
  80. _dests = Array.Empty<Operand>();
  81. }
  82. }
  83. public Operation(Instruction inst, int index, Operand dest, params Operand[] sources) : this(inst, dest, sources)
  84. {
  85. Index = index;
  86. }
  87. public void AppendDests(Operand[] operands)
  88. {
  89. int startIndex = _dests.Length;
  90. Array.Resize(ref _dests, startIndex + operands.Length);
  91. for (int index = 0; index < operands.Length; index++)
  92. {
  93. Operand dest = operands[index];
  94. if (dest != null && dest.Type == OperandType.LocalVariable)
  95. {
  96. Debug.Assert(dest.AsgOp == null);
  97. dest.AsgOp = this;
  98. }
  99. _dests[startIndex + index] = dest;
  100. }
  101. }
  102. public void AppendSources(Operand[] operands)
  103. {
  104. int startIndex = _sources.Length;
  105. Array.Resize(ref _sources, startIndex + operands.Length);
  106. for (int index = 0; index < operands.Length; index++)
  107. {
  108. Operand source = operands[index];
  109. if (source.Type == OperandType.LocalVariable)
  110. {
  111. source.UseOps.Add(this);
  112. }
  113. _sources[startIndex + index] = source;
  114. }
  115. }
  116. public Operand GetDest(int index)
  117. {
  118. return _dests[index];
  119. }
  120. public Operand GetSource(int index)
  121. {
  122. return _sources[index];
  123. }
  124. public void SetDest(int index, Operand dest)
  125. {
  126. Operand oldDest = _dests[index];
  127. if (oldDest != null && oldDest.Type == OperandType.LocalVariable)
  128. {
  129. oldDest.AsgOp = null;
  130. }
  131. if (dest != null && dest.Type == OperandType.LocalVariable)
  132. {
  133. dest.AsgOp = this;
  134. }
  135. _dests[index] = dest;
  136. }
  137. public void SetSource(int index, Operand source)
  138. {
  139. Operand oldSrc = _sources[index];
  140. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  141. {
  142. oldSrc.UseOps.Remove(this);
  143. }
  144. if (source != null && source.Type == OperandType.LocalVariable)
  145. {
  146. source.UseOps.Add(this);
  147. }
  148. _sources[index] = source;
  149. }
  150. public void InsertSource(int index, Operand source)
  151. {
  152. Operand[] newSources = new Operand[_sources.Length + 1];
  153. Array.Copy(_sources, 0, newSources, 0, index);
  154. Array.Copy(_sources, index, newSources, index + 1, _sources.Length - index);
  155. newSources[index] = source;
  156. _sources = newSources;
  157. }
  158. protected void RemoveSource(int index)
  159. {
  160. SetSource(index, null);
  161. Operand[] newSources = new Operand[_sources.Length - 1];
  162. Array.Copy(_sources, 0, newSources, 0, index);
  163. Array.Copy(_sources, index + 1, newSources, index, _sources.Length - (index + 1));
  164. _sources = newSources;
  165. }
  166. public void TurnIntoCopy(Operand source)
  167. {
  168. TurnInto(Instruction.Copy, source);
  169. }
  170. public void TurnInto(Instruction newInst, Operand source)
  171. {
  172. Inst = newInst;
  173. foreach (Operand oldSrc in _sources)
  174. {
  175. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  176. {
  177. oldSrc.UseOps.Remove(this);
  178. }
  179. }
  180. if (source.Type == OperandType.LocalVariable)
  181. {
  182. source.UseOps.Add(this);
  183. }
  184. _sources = new Operand[] { source };
  185. }
  186. }
  187. }