Operation.cs 5.1 KB

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