InstEmitMemoryEx.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.OperandHelper;
  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 Dmb(ArmEmitterContext context) => EmitBarrier(context);
  26. public static void Dsb(ArmEmitterContext context) => EmitBarrier(context);
  27. public static void Ldar(ArmEmitterContext context) => EmitLdr(context, AccessType.Ordered);
  28. public static void Ldaxr(ArmEmitterContext context) => EmitLdr(context, AccessType.OrderedEx);
  29. public static void Ldxr(ArmEmitterContext context) => EmitLdr(context, AccessType.Exclusive);
  30. public static void Ldxp(ArmEmitterContext context) => EmitLdp(context, AccessType.Exclusive);
  31. public static void Ldaxp(ArmEmitterContext context) => EmitLdp(context, AccessType.OrderedEx);
  32. private static void EmitLdr(ArmEmitterContext context, AccessType accType)
  33. {
  34. EmitLoadEx(context, accType, pair: false);
  35. }
  36. private static void EmitLdp(ArmEmitterContext context, AccessType accType)
  37. {
  38. EmitLoadEx(context, accType, pair: true);
  39. }
  40. private static void EmitLoadEx(ArmEmitterContext context, AccessType accType, bool pair)
  41. {
  42. OpCodeMemEx op = (OpCodeMemEx)context.CurrOp;
  43. bool ordered = (accType & AccessType.Ordered) != 0;
  44. bool exclusive = (accType & AccessType.Exclusive) != 0;
  45. if (ordered)
  46. {
  47. EmitBarrier(context);
  48. }
  49. Operand address = context.Copy(GetIntOrSP(context, op.Rn));
  50. if (pair)
  51. {
  52. // Exclusive loads should be atomic. For pairwise loads, we need to
  53. // read all the data at once. For a 32-bits pairwise load, we do a
  54. // simple 64-bits load, for a 128-bits load, we need to call a special
  55. // method to read 128-bits atomically.
  56. if (op.Size == 2)
  57. {
  58. Operand value = EmitLoadExclusive(context, address, exclusive, 3);
  59. Operand valueLow = context.ConvertI64ToI32(value);
  60. valueLow = context.ZeroExtend32(OperandType.I64, valueLow);
  61. Operand valueHigh = context.ShiftRightUI(value, Const(32));
  62. SetIntOrZR(context, op.Rt, valueLow);
  63. SetIntOrZR(context, op.Rt2, valueHigh);
  64. }
  65. else if (op.Size == 3)
  66. {
  67. Operand value = EmitLoadExclusive(context, address, exclusive, 4);
  68. Operand valueLow = context.VectorExtract(OperandType.I64, value, 0);
  69. Operand valueHigh = context.VectorExtract(OperandType.I64, value, 1);
  70. SetIntOrZR(context, op.Rt, valueLow);
  71. SetIntOrZR(context, op.Rt2, valueHigh);
  72. }
  73. else
  74. {
  75. throw new InvalidOperationException($"Invalid load size of {1 << op.Size} bytes.");
  76. }
  77. }
  78. else
  79. {
  80. // 8, 16, 32 or 64-bits (non-pairwise) load.
  81. Operand value = EmitLoadExclusive(context, address, exclusive, op.Size);
  82. SetIntOrZR(context, op.Rt, value);
  83. }
  84. }
  85. public static void Prfm(ArmEmitterContext context)
  86. {
  87. // Memory Prefetch, execute as no-op.
  88. }
  89. public static void Stlr(ArmEmitterContext context) => EmitStr(context, AccessType.Ordered);
  90. public static void Stlxr(ArmEmitterContext context) => EmitStr(context, AccessType.OrderedEx);
  91. public static void Stxr(ArmEmitterContext context) => EmitStr(context, AccessType.Exclusive);
  92. public static void Stxp(ArmEmitterContext context) => EmitStp(context, AccessType.Exclusive);
  93. public static void Stlxp(ArmEmitterContext context) => EmitStp(context, AccessType.OrderedEx);
  94. private static void EmitStr(ArmEmitterContext context, AccessType accType)
  95. {
  96. EmitStoreEx(context, accType, pair: false);
  97. }
  98. private static void EmitStp(ArmEmitterContext context, AccessType accType)
  99. {
  100. EmitStoreEx(context, accType, pair: true);
  101. }
  102. private static void EmitStoreEx(ArmEmitterContext context, AccessType accType, bool pair)
  103. {
  104. OpCodeMemEx op = (OpCodeMemEx)context.CurrOp;
  105. bool ordered = (accType & AccessType.Ordered) != 0;
  106. bool exclusive = (accType & AccessType.Exclusive) != 0;
  107. if (ordered)
  108. {
  109. EmitBarrier(context);
  110. }
  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. }
  134. private static void EmitBarrier(ArmEmitterContext context)
  135. {
  136. // Note: This barrier is most likely not necessary, and probably
  137. // doesn't make any difference since we need to do a ton of stuff
  138. // (software MMU emulation) to read or write anything anyway.
  139. }
  140. }
  141. }