OperandHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using ARMeilleure.Common;
  2. using ARMeilleure.Translation.PTC;
  3. using System.Runtime.CompilerServices;
  4. namespace ARMeilleure.IntermediateRepresentation
  5. {
  6. static class OperandHelper
  7. {
  8. public static Operand Const(OperandType type, long value)
  9. {
  10. return type == OperandType.I32 ? Operand().With((int)value) : Operand().With(value);
  11. }
  12. public static Operand Const(bool value)
  13. {
  14. return Operand().With(value ? 1 : 0);
  15. }
  16. public static Operand Const(int value)
  17. {
  18. return Operand().With(value);
  19. }
  20. public static Operand Const(uint value)
  21. {
  22. return Operand().With(value);
  23. }
  24. public static Operand Const(long value)
  25. {
  26. return Operand().With(value);
  27. }
  28. public static Operand Const(long value, Symbol symbol)
  29. {
  30. return Operand().With(value, symbol);
  31. }
  32. public static Operand Const(ulong value)
  33. {
  34. return Operand().With(value);
  35. }
  36. public static unsafe Operand Const<T>(ref T reference, Symbol symbol = default)
  37. {
  38. return Operand().With((long)Unsafe.AsPointer(ref reference), symbol);
  39. }
  40. public static Operand ConstF(float value)
  41. {
  42. return Operand().With(value);
  43. }
  44. public static Operand ConstF(double value)
  45. {
  46. return Operand().With(value);
  47. }
  48. public static Operand Label()
  49. {
  50. return Operand().With(OperandKind.Label);
  51. }
  52. public static Operand Local(OperandType type)
  53. {
  54. return Operand().With(OperandKind.LocalVariable, type);
  55. }
  56. public static Operand Register(int index, RegisterType regType, OperandType type)
  57. {
  58. return Operand().With(index, regType, type);
  59. }
  60. public static Operand Undef()
  61. {
  62. return Operand().With(OperandKind.Undefined);
  63. }
  64. public static MemoryOperand MemoryOp(
  65. OperandType type,
  66. Operand baseAddress,
  67. Operand index = null,
  68. Multiplier scale = Multiplier.x1,
  69. int displacement = 0)
  70. {
  71. return MemoryOperand().With(type, baseAddress, index, scale, displacement);
  72. }
  73. #region "ThreadStaticPool"
  74. public static void PrepareOperandPool(int groupId = 0)
  75. {
  76. ThreadStaticPool<Operand>.PreparePool(groupId, ChunkSizeLimit.Large);
  77. ThreadStaticPool<MemoryOperand>.PreparePool(groupId, ChunkSizeLimit.Small);
  78. }
  79. private static Operand Operand()
  80. {
  81. return ThreadStaticPool<Operand>.Instance.Allocate();
  82. }
  83. private static MemoryOperand MemoryOperand()
  84. {
  85. return ThreadStaticPool<MemoryOperand>.Instance.Allocate();
  86. }
  87. public static void ResetOperandPool(int groupId = 0)
  88. {
  89. ThreadStaticPool<MemoryOperand>.ResetPool(groupId);
  90. ThreadStaticPool<Operand>.ResetPool(groupId);
  91. }
  92. public static void DisposeOperandPools()
  93. {
  94. ThreadStaticPool<Operand>.DisposePools();
  95. ThreadStaticPool<MemoryOperand>.DisposePools();
  96. }
  97. #endregion
  98. }
  99. }