AInstEmitMemoryEx.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using ChocolArm64.Translation;
  5. using System;
  6. using System.Reflection.Emit;
  7. using System.Threading;
  8. using static ChocolArm64.Instruction.AInstEmitMemoryHelper;
  9. namespace ChocolArm64.Instruction
  10. {
  11. static partial class AInstEmit
  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(AILEmitterCtx Context)
  22. {
  23. EmitMemoryCall(Context, nameof(AMemory.ClearExclusive));
  24. }
  25. public static void Dmb(AILEmitterCtx Context) => EmitBarrier(Context);
  26. public static void Dsb(AILEmitterCtx Context) => EmitBarrier(Context);
  27. public static void Ldar(AILEmitterCtx Context) => EmitLdr(Context, AccessType.Ordered);
  28. public static void Ldaxr(AILEmitterCtx Context) => EmitLdr(Context, AccessType.OrderedEx);
  29. public static void Ldxr(AILEmitterCtx Context) => EmitLdr(Context, AccessType.Exclusive);
  30. public static void Ldxp(AILEmitterCtx Context) => EmitLdp(Context, AccessType.Exclusive);
  31. public static void Ldaxp(AILEmitterCtx Context) => EmitLdp(Context, AccessType.OrderedEx);
  32. private static void EmitLdr(AILEmitterCtx Context, AccessType AccType)
  33. {
  34. EmitLoad(Context, AccType, false);
  35. }
  36. private static void EmitLdp(AILEmitterCtx Context, AccessType AccType)
  37. {
  38. EmitLoad(Context, AccType, true);
  39. }
  40. private static void EmitLoad(AILEmitterCtx Context, AccessType AccType, bool Pair)
  41. {
  42. AOpCodeMemEx Op = (AOpCodeMemEx)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. if (Exclusive)
  50. {
  51. EmitMemoryCall(Context, nameof(AMemory.SetExclusive), Op.Rn);
  52. }
  53. Context.EmitLdint(Op.Rn);
  54. Context.EmitSttmp();
  55. Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
  56. Context.EmitLdtmp();
  57. EmitReadZxCall(Context, Op.Size);
  58. Context.EmitStintzr(Op.Rt);
  59. if (Pair)
  60. {
  61. Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
  62. Context.EmitLdtmp();
  63. Context.EmitLdc_I8(1 << Op.Size);
  64. Context.Emit(OpCodes.Add);
  65. EmitReadZxCall(Context, Op.Size);
  66. Context.EmitStintzr(Op.Rt2);
  67. }
  68. }
  69. public static void Pfrm(AILEmitterCtx Context)
  70. {
  71. //Memory Prefetch, execute as no-op.
  72. }
  73. public static void Stlr(AILEmitterCtx Context) => EmitStr(Context, AccessType.Ordered);
  74. public static void Stlxr(AILEmitterCtx Context) => EmitStr(Context, AccessType.OrderedEx);
  75. public static void Stxr(AILEmitterCtx Context) => EmitStr(Context, AccessType.Exclusive);
  76. public static void Stxp(AILEmitterCtx Context) => EmitStp(Context, AccessType.Exclusive);
  77. public static void Stlxp(AILEmitterCtx Context) => EmitStp(Context, AccessType.OrderedEx);
  78. private static void EmitStr(AILEmitterCtx Context, AccessType AccType)
  79. {
  80. EmitStore(Context, AccType, false);
  81. }
  82. private static void EmitStp(AILEmitterCtx Context, AccessType AccType)
  83. {
  84. EmitStore(Context, AccType, true);
  85. }
  86. private static void EmitStore(AILEmitterCtx Context, AccessType AccType, bool Pair)
  87. {
  88. AOpCodeMemEx Op = (AOpCodeMemEx)Context.CurrOp;
  89. bool Ordered = (AccType & AccessType.Ordered) != 0;
  90. bool Exclusive = (AccType & AccessType.Exclusive) != 0;
  91. if (Ordered)
  92. {
  93. EmitBarrier(Context);
  94. }
  95. AILLabel LblEx = new AILLabel();
  96. AILLabel LblEnd = new AILLabel();
  97. if (Exclusive)
  98. {
  99. EmitMemoryCall(Context, nameof(AMemory.TestExclusive), Op.Rn);
  100. Context.Emit(OpCodes.Brtrue_S, LblEx);
  101. Context.EmitLdc_I8(1);
  102. Context.EmitStintzr(Op.Rs);
  103. Context.Emit(OpCodes.Br_S, LblEnd);
  104. }
  105. Context.MarkLabel(LblEx);
  106. Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
  107. Context.EmitLdint(Op.Rn);
  108. Context.EmitLdintzr(Op.Rt);
  109. EmitWriteCall(Context, Op.Size);
  110. if (Pair)
  111. {
  112. Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
  113. Context.EmitLdint(Op.Rn);
  114. Context.EmitLdc_I8(1 << Op.Size);
  115. Context.Emit(OpCodes.Add);
  116. Context.EmitLdintzr(Op.Rt2);
  117. EmitWriteCall(Context, Op.Size);
  118. }
  119. if (Exclusive)
  120. {
  121. Context.EmitLdc_I8(0);
  122. Context.EmitStintzr(Op.Rs);
  123. EmitMemoryCall(Context, nameof(AMemory.ClearExclusiveForStore));
  124. }
  125. Context.MarkLabel(LblEnd);
  126. }
  127. private static void EmitMemoryCall(AILEmitterCtx Context, string Name, int Rn = -1)
  128. {
  129. Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
  130. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  131. Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Core));
  132. if (Rn != -1)
  133. {
  134. Context.EmitLdint(Rn);
  135. }
  136. Context.EmitCall(typeof(AMemory), Name);
  137. }
  138. private static void EmitBarrier(AILEmitterCtx Context)
  139. {
  140. //Note: This barrier is most likely not necessary, and probably
  141. //doesn't make any difference since we need to do a ton of stuff
  142. //(software MMU emulation) to read or write anything anyway.
  143. Context.EmitCall(typeof(Thread), nameof(Thread.MemoryBarrier));
  144. }
  145. }
  146. }