AstBlock.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
  6. namespace Ryujinx.Graphics.Shader.StructuredIr
  7. {
  8. class AstBlock : AstNode, IEnumerable<IAstNode>
  9. {
  10. public AstBlockType Type { get; private set; }
  11. private IAstNode _condition;
  12. public IAstNode Condition
  13. {
  14. get
  15. {
  16. return _condition;
  17. }
  18. set
  19. {
  20. RemoveUse(_condition, this);
  21. AddUse(value, this);
  22. _condition = value;
  23. }
  24. }
  25. private LinkedList<IAstNode> _nodes;
  26. public IAstNode First => _nodes.First?.Value;
  27. public IAstNode Last => _nodes.Last?.Value;
  28. public int Count => _nodes.Count;
  29. public AstBlock(AstBlockType type, IAstNode condition = null)
  30. {
  31. Type = type;
  32. Condition = condition;
  33. _nodes = new LinkedList<IAstNode>();
  34. }
  35. public void Add(IAstNode node)
  36. {
  37. Add(node, _nodes.AddLast(node));
  38. }
  39. public void AddFirst(IAstNode node)
  40. {
  41. Add(node, _nodes.AddFirst(node));
  42. }
  43. public void AddBefore(IAstNode next, IAstNode node)
  44. {
  45. Add(node, _nodes.AddBefore(next.LLNode, node));
  46. }
  47. public void AddAfter(IAstNode prev, IAstNode node)
  48. {
  49. Add(node, _nodes.AddAfter(prev.LLNode, node));
  50. }
  51. private void Add(IAstNode node, LinkedListNode<IAstNode> newNode)
  52. {
  53. if (node.Parent != null)
  54. {
  55. throw new ArgumentException("Node already belongs to a block.");
  56. }
  57. node.Parent = this;
  58. node.LLNode = newNode;
  59. }
  60. public void Remove(IAstNode node)
  61. {
  62. _nodes.Remove(node.LLNode);
  63. node.Parent = null;
  64. node.LLNode = null;
  65. }
  66. public void AndCondition(IAstNode cond)
  67. {
  68. Condition = new AstOperation(Instruction.LogicalAnd, Condition, cond);
  69. }
  70. public void OrCondition(IAstNode cond)
  71. {
  72. Condition = new AstOperation(Instruction.LogicalOr, Condition, cond);
  73. }
  74. public void TurnIntoIf(IAstNode cond)
  75. {
  76. Condition = cond;
  77. Type = AstBlockType.If;
  78. }
  79. public void TurnIntoElseIf()
  80. {
  81. Type = AstBlockType.ElseIf;
  82. }
  83. public IEnumerator<IAstNode> GetEnumerator()
  84. {
  85. return _nodes.GetEnumerator();
  86. }
  87. IEnumerator IEnumerable.GetEnumerator()
  88. {
  89. return GetEnumerator();
  90. }
  91. }
  92. }