BasicBlock.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections.Generic;
  2. namespace ARMeilleure.IntermediateRepresentation
  3. {
  4. class BasicBlock
  5. {
  6. public int Index { get; set; }
  7. public LinkedListNode<BasicBlock> Node { get; set; }
  8. public LinkedList<Node> Operations { get; }
  9. private BasicBlock _next;
  10. private BasicBlock _branch;
  11. public BasicBlock Next
  12. {
  13. get => _next;
  14. set => _next = AddSuccessor(_next, value);
  15. }
  16. public BasicBlock Branch
  17. {
  18. get => _branch;
  19. set => _branch = AddSuccessor(_branch, value);
  20. }
  21. public List<BasicBlock> Predecessors { get; }
  22. public HashSet<BasicBlock> DominanceFrontiers { get; }
  23. public BasicBlock ImmediateDominator { get; set; }
  24. public BasicBlock()
  25. {
  26. Operations = new LinkedList<Node>();
  27. Predecessors = new List<BasicBlock>();
  28. DominanceFrontiers = new HashSet<BasicBlock>();
  29. Index = -1;
  30. }
  31. public BasicBlock(int index) : this()
  32. {
  33. Index = index;
  34. }
  35. private BasicBlock AddSuccessor(BasicBlock oldBlock, BasicBlock newBlock)
  36. {
  37. oldBlock?.Predecessors.Remove(this);
  38. newBlock?.Predecessors.Add(this);
  39. return newBlock;
  40. }
  41. public void Append(Node node)
  42. {
  43. // If the branch block is not null, then the list of operations
  44. // should end with a branch instruction. We insert the new operation
  45. // before this branch.
  46. if (_branch != null || (Operations.Last != null && IsLeafBlock()))
  47. {
  48. Operations.AddBefore(Operations.Last, node);
  49. }
  50. else
  51. {
  52. Operations.AddLast(node);
  53. }
  54. }
  55. private bool IsLeafBlock()
  56. {
  57. return _branch == null && _next == null;
  58. }
  59. public Node GetLastOp()
  60. {
  61. return Operations.Last?.Value;
  62. }
  63. }
  64. }