OperandHelper.cs 3.2 KB

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