AstBlock.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 int Count => _nodes.Count;
  28. public AstBlock(AstBlockType type, IAstNode condition = null)
  29. {
  30. Type = type;
  31. Condition = condition;
  32. _nodes = new LinkedList<IAstNode>();
  33. }
  34. public void Add(IAstNode node)
  35. {
  36. Add(node, _nodes.AddLast(node));
  37. }
  38. public void AddFirst(IAstNode node)
  39. {
  40. Add(node, _nodes.AddFirst(node));
  41. }
  42. public void AddBefore(IAstNode next, IAstNode node)
  43. {
  44. Add(node, _nodes.AddBefore(next.LLNode, node));
  45. }
  46. public void AddAfter(IAstNode prev, IAstNode node)
  47. {
  48. Add(node, _nodes.AddAfter(prev.LLNode, node));
  49. }
  50. private void Add(IAstNode node, LinkedListNode<IAstNode> newNode)
  51. {
  52. if (node.Parent != null)
  53. {
  54. throw new ArgumentException("Node already belongs to a block.");
  55. }
  56. node.Parent = this;
  57. node.LLNode = newNode;
  58. }
  59. public void Remove(IAstNode node)
  60. {
  61. _nodes.Remove(node.LLNode);
  62. node.Parent = null;
  63. node.LLNode = null;
  64. }
  65. public void AndCondition(IAstNode cond)
  66. {
  67. Condition = new AstOperation(Instruction.LogicalAnd, Condition, cond);
  68. }
  69. public void OrCondition(IAstNode cond)
  70. {
  71. Condition = new AstOperation(Instruction.LogicalOr, Condition, cond);
  72. }
  73. public void TurnIntoIf(IAstNode cond)
  74. {
  75. Condition = cond;
  76. Type = AstBlockType.If;
  77. }
  78. public void TurnIntoElseIf()
  79. {
  80. Type = AstBlockType.ElseIf;
  81. }
  82. public IEnumerator<IAstNode> GetEnumerator()
  83. {
  84. return _nodes.GetEnumerator();
  85. }
  86. IEnumerator IEnumerable.GetEnumerator()
  87. {
  88. return GetEnumerator();
  89. }
  90. }
  91. }