AInstEmitMemoryEx.cs 5.9 KB

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