InstEmitMemoryEx.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using System.Diagnostics;
  6. using static ARMeilleure.Instructions.InstEmitHelper;
  7. using static ARMeilleure.Instructions.InstEmitMemoryExHelper;
  8. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static partial class InstEmit
  12. {
  13. [Flags]
  14. private enum AccessType
  15. {
  16. None = 0,
  17. Ordered = 1,
  18. Exclusive = 2,
  19. OrderedEx = Ordered | Exclusive
  20. }
  21. public static void Clrex(ArmEmitterContext context)
  22. {
  23. EmitClearExclusive(context);
  24. }
  25. public static void Csdb(ArmEmitterContext context)
  26. {
  27. // Execute as no-op.
  28. }
  29. public static void Dmb(ArmEmitterContext context) => EmitBarrier(context);
  30. public static void Dsb(ArmEmitterContext context) => EmitBarrier(context);
  31. public static void Ldar(ArmEmitterContext context) => EmitLdr(context, AccessType.Ordered);
  32. public static void Ldaxr(ArmEmitterContext context) => EmitLdr(context, AccessType.OrderedEx);
  33. public static void Ldxr(ArmEmitterContext context) => EmitLdr(context, AccessType.Exclusive);
  34. public static void Ldxp(ArmEmitterContext context) => EmitLdp(context, AccessType.Exclusive);
  35. public static void Ldaxp(ArmEmitterContext context) => EmitLdp(context, AccessType.OrderedEx);
  36. private static void EmitLdr(ArmEmitterContext context, AccessType accType)
  37. {
  38. EmitLoadEx(context, accType, pair: false);
  39. }
  40. private static void EmitLdp(ArmEmitterContext context, AccessType accType)
  41. {
  42. EmitLoadEx(context, accType, pair: true);
  43. }
  44. private static void EmitLoadEx(ArmEmitterContext context, AccessType accType, bool pair)
  45. {
  46. OpCodeMemEx op = (OpCodeMemEx)context.CurrOp;
  47. bool ordered = (accType & AccessType.Ordered) != 0;
  48. bool exclusive = (accType & AccessType.Exclusive) != 0;
  49. if (ordered)
  50. {
  51. EmitBarrier(context);
  52. }
  53. Operand address = context.Copy(GetIntOrSP(context, op.Rn));
  54. if (pair)
  55. {
  56. // Exclusive loads should be atomic. For pairwise loads, we need to
  57. // read all the data at once. For a 32-bits pairwise load, we do a
  58. // simple 64-bits load, for a 128-bits load, we need to call a special
  59. // method to read 128-bits atomically.
  60. if (op.Size == 2)
  61. {
  62. Operand value = EmitLoadExclusive(context, address, exclusive, 3);
  63. Operand valueLow = context.ConvertI64ToI32(value);
  64. valueLow = context.ZeroExtend32(OperandType.I64, valueLow);
  65. Operand valueHigh = context.ShiftRightUI(value, Const(32));
  66. SetIntOrZR(context, op.Rt, valueLow);
  67. SetIntOrZR(context, op.Rt2, valueHigh);
  68. }
  69. else if (op.Size == 3)
  70. {
  71. Operand value = EmitLoadExclusive(context, address, exclusive, 4);
  72. Operand valueLow = context.VectorExtract(OperandType.I64, value, 0);
  73. Operand valueHigh = context.VectorExtract(OperandType.I64, value, 1);
  74. SetIntOrZR(context, op.Rt, valueLow);
  75. SetIntOrZR(context, op.Rt2, valueHigh);
  76. }
  77. else
  78. {
  79. throw new InvalidOperationException($"Invalid load size of {1 << op.Size} bytes.");
  80. }
  81. }
  82. else
  83. {
  84. // 8, 16, 32 or 64-bits (non-pairwise) load.
  85. Operand value = EmitLoadExclusive(context, address, exclusive, op.Size);
  86. SetIntOrZR(context, op.Rt, value);
  87. }
  88. }
  89. public static void Prfm(ArmEmitterContext context)
  90. {
  91. // Memory Prefetch, execute as no-op.
  92. }
  93. public static void Stlr(ArmEmitterContext context) => EmitStr(context, AccessType.Ordered);
  94. public static void Stlxr(ArmEmitterContext context) => EmitStr(context, AccessType.OrderedEx);
  95. public static void Stxr(ArmEmitterContext context) => EmitStr(context, AccessType.Exclusive);
  96. public static void Stxp(ArmEmitterContext context) => EmitStp(context, AccessType.Exclusive);
  97. public static void Stlxp(ArmEmitterContext context) => EmitStp(context, AccessType.OrderedEx);
  98. private static void EmitStr(ArmEmitterContext context, AccessType accType)
  99. {
  100. EmitStoreEx(context, accType, pair: false);
  101. }
  102. private static void EmitStp(ArmEmitterContext context, AccessType accType)
  103. {
  104. EmitStoreEx(context, accType, pair: true);
  105. }
  106. private static void EmitStoreEx(ArmEmitterContext context, AccessType accType, bool pair)
  107. {
  108. OpCodeMemEx op = (OpCodeMemEx)context.CurrOp;
  109. bool ordered = (accType & AccessType.Ordered) != 0;
  110. bool exclusive = (accType & AccessType.Exclusive) != 0;
  111. Operand address = context.Copy(GetIntOrSP(context, op.Rn));
  112. Operand t = GetIntOrZR(context, op.Rt);
  113. if (pair)
  114. {
  115. Debug.Assert(op.Size == 2 || op.Size == 3, "Invalid size for pairwise store.");
  116. Operand t2 = GetIntOrZR(context, op.Rt2);
  117. Operand value;
  118. if (op.Size == 2)
  119. {
  120. value = context.BitwiseOr(t, context.ShiftLeft(t2, Const(32)));
  121. }
  122. else /* if (op.Size == 3) */
  123. {
  124. value = context.VectorInsert(context.VectorZero(), t, 0);
  125. value = context.VectorInsert(value, t2, 1);
  126. }
  127. EmitStoreExclusive(context, address, value, exclusive, op.Size + 1, op.Rs, a32: false);
  128. }
  129. else
  130. {
  131. EmitStoreExclusive(context, address, t, exclusive, op.Size, op.Rs, a32: false);
  132. }
  133. if (ordered)
  134. {
  135. EmitBarrier(context);
  136. }
  137. }
  138. private static void EmitBarrier(ArmEmitterContext context)
  139. {
  140. context.MemoryBarrier();
  141. }
  142. }
  143. }