Operation.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 _dest;
  8. public Operand Dest
  9. {
  10. get => _dest;
  11. set => _dest = AssignDest(value);
  12. }
  13. private Operand[] _sources;
  14. public int SourcesCount => _sources.Length;
  15. public int Index { get; }
  16. public Operation(Instruction inst, Operand dest, params Operand[] sources)
  17. {
  18. Inst = inst;
  19. Dest = dest;
  20. // The array may be modified externally, so we store a copy.
  21. _sources = (Operand[])sources.Clone();
  22. for (int index = 0; index < _sources.Length; index++)
  23. {
  24. Operand source = _sources[index];
  25. if (source.Type == OperandType.LocalVariable)
  26. {
  27. source.UseOps.Add(this);
  28. }
  29. }
  30. }
  31. public Operation(
  32. Instruction inst,
  33. int index,
  34. Operand dest,
  35. params Operand[] sources) : this(inst, dest, sources)
  36. {
  37. Index = index;
  38. }
  39. public void AppendOperands(params Operand[] operands)
  40. {
  41. int startIndex = _sources.Length;
  42. Array.Resize(ref _sources, startIndex + operands.Length);
  43. for (int index = 0; index < operands.Length; index++)
  44. {
  45. Operand source = operands[index];
  46. if (source.Type == OperandType.LocalVariable)
  47. {
  48. source.UseOps.Add(this);
  49. }
  50. _sources[startIndex + index] = source;
  51. }
  52. }
  53. private Operand AssignDest(Operand dest)
  54. {
  55. if (dest != null && dest.Type == OperandType.LocalVariable)
  56. {
  57. dest.AsgOp = this;
  58. }
  59. return dest;
  60. }
  61. public Operand GetSource(int index)
  62. {
  63. return _sources[index];
  64. }
  65. public void SetSource(int index, Operand source)
  66. {
  67. Operand oldSrc = _sources[index];
  68. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  69. {
  70. oldSrc.UseOps.Remove(this);
  71. }
  72. if (source != null && source.Type == OperandType.LocalVariable)
  73. {
  74. source.UseOps.Add(this);
  75. }
  76. _sources[index] = source;
  77. }
  78. protected void RemoveSource(int index)
  79. {
  80. SetSource(index, null);
  81. Operand[] newSources = new Operand[_sources.Length - 1];
  82. Array.Copy(_sources, 0, newSources, 0, index);
  83. Array.Copy(_sources, index + 1, newSources, index, _sources.Length - (index + 1));
  84. _sources = newSources;
  85. }
  86. public void TurnIntoCopy(Operand source)
  87. {
  88. TurnInto(Instruction.Copy, source);
  89. }
  90. public void TurnInto(Instruction newInst, Operand source)
  91. {
  92. Inst = newInst;
  93. foreach (Operand oldSrc in _sources)
  94. {
  95. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  96. {
  97. oldSrc.UseOps.Remove(this);
  98. }
  99. }
  100. if (source.Type == OperandType.LocalVariable)
  101. {
  102. source.UseOps.Add(this);
  103. }
  104. _sources = new Operand[] { source };
  105. }
  106. }
  107. }