BasicBlock.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using ChocolArm64.State;
  2. using System;
  3. using System.Collections.Generic;
  4. using static ChocolArm64.State.RegisterConsts;
  5. namespace ChocolArm64.IntermediateRepresentation
  6. {
  7. class BasicBlock
  8. {
  9. public int Index { get; set; }
  10. public RegisterMask RegInputs { get; private set; }
  11. public RegisterMask RegOutputs { get; private set; }
  12. public bool HasStateLoad { get; private set; }
  13. private List<Operation> _operations;
  14. public int Count => _operations.Count;
  15. private BasicBlock _next;
  16. private BasicBlock _branch;
  17. public BasicBlock Next
  18. {
  19. get => _next;
  20. set => _next = AddSuccessor(_next, value);
  21. }
  22. public BasicBlock Branch
  23. {
  24. get => _branch;
  25. set => _branch = AddSuccessor(_branch, value);
  26. }
  27. public List<BasicBlock> Predecessors { get; }
  28. public BasicBlock(int index = 0)
  29. {
  30. Index = index;
  31. _operations = new List<Operation>();
  32. Predecessors = new List<BasicBlock>();
  33. }
  34. private BasicBlock AddSuccessor(BasicBlock oldBlock, BasicBlock newBlock)
  35. {
  36. oldBlock?.Predecessors.Remove(this);
  37. newBlock?.Predecessors.Add(this);
  38. return newBlock;
  39. }
  40. public void Add(Operation operation)
  41. {
  42. if (operation.Type == OperationType.LoadLocal ||
  43. operation.Type == OperationType.StoreLocal)
  44. {
  45. int index = operation.GetArg<int>(0);
  46. if (IsRegIndex(index))
  47. {
  48. long intMask = 0;
  49. long vecMask = 0;
  50. switch (operation.GetArg<RegisterType>(1))
  51. {
  52. case RegisterType.Flag: intMask = (1L << RegsCount) << index; break;
  53. case RegisterType.Int: intMask = 1L << index; break;
  54. case RegisterType.Vector: vecMask = 1L << index; break;
  55. }
  56. RegisterMask mask = new RegisterMask(intMask, vecMask);
  57. if (operation.Type == OperationType.LoadLocal)
  58. {
  59. RegInputs |= mask & ~RegOutputs;
  60. }
  61. else
  62. {
  63. RegOutputs |= mask;
  64. }
  65. }
  66. }
  67. else if (operation.Type == OperationType.LoadContext)
  68. {
  69. HasStateLoad = true;
  70. }
  71. operation.Parent = this;
  72. _operations.Add(operation);
  73. }
  74. public static bool IsRegIndex(int index)
  75. {
  76. return (uint)index < RegsCount;
  77. }
  78. public Operation GetOperation(int index)
  79. {
  80. if ((uint)index >= _operations.Count)
  81. {
  82. throw new ArgumentOutOfRangeException(nameof(index));
  83. }
  84. return _operations[index];
  85. }
  86. public Operation GetLastOp()
  87. {
  88. if (Count == 0)
  89. {
  90. return null;
  91. }
  92. return _operations[Count - 1];
  93. }
  94. }
  95. }