Operation.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. private Operand AssignDest(Operand dest)
  40. {
  41. if (dest != null && dest.Type == OperandType.LocalVariable)
  42. {
  43. dest.AsgOp = this;
  44. }
  45. return dest;
  46. }
  47. public Operand GetSource(int index)
  48. {
  49. return _sources[index];
  50. }
  51. public void SetSource(int index, Operand source)
  52. {
  53. Operand oldSrc = _sources[index];
  54. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  55. {
  56. oldSrc.UseOps.Remove(this);
  57. }
  58. if (source != null && source.Type == OperandType.LocalVariable)
  59. {
  60. source.UseOps.Add(this);
  61. }
  62. _sources[index] = source;
  63. }
  64. protected void RemoveSource(int index)
  65. {
  66. SetSource(index, null);
  67. Operand[] newSources = new Operand[_sources.Length - 1];
  68. Array.Copy(_sources, 0, newSources, 0, index);
  69. Array.Copy(_sources, index + 1, newSources, index, _sources.Length - (index + 1));
  70. _sources = newSources;
  71. }
  72. public void TurnIntoCopy(Operand source)
  73. {
  74. TurnInto(Instruction.Copy, source);
  75. }
  76. public void TurnInto(Instruction newInst, Operand source)
  77. {
  78. Inst = newInst;
  79. foreach (Operand oldSrc in _sources)
  80. {
  81. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  82. {
  83. oldSrc.UseOps.Remove(this);
  84. }
  85. }
  86. if (source.Type == OperandType.LocalVariable)
  87. {
  88. source.UseOps.Add(this);
  89. }
  90. _sources = new Operand[] { source };
  91. }
  92. }
  93. }