InstEmitMemory.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.Translation;
  3. using System.Reflection.Emit;
  4. using static ChocolArm64.Instructions.InstEmitMemoryHelper;
  5. namespace ChocolArm64.Instructions
  6. {
  7. static partial class InstEmit
  8. {
  9. public static void Adr(ILEmitterCtx context)
  10. {
  11. OpCodeAdr64 op = (OpCodeAdr64)context.CurrOp;
  12. context.EmitLdc_I(op.Position + op.Imm);
  13. context.EmitStintzr(op.Rd);
  14. }
  15. public static void Adrp(ILEmitterCtx context)
  16. {
  17. OpCodeAdr64 op = (OpCodeAdr64)context.CurrOp;
  18. context.EmitLdc_I((op.Position & ~0xfffL) + (op.Imm << 12));
  19. context.EmitStintzr(op.Rd);
  20. }
  21. public static void Ldr(ILEmitterCtx context) => EmitLdr(context, false);
  22. public static void Ldrs(ILEmitterCtx context) => EmitLdr(context, true);
  23. private static void EmitLdr(ILEmitterCtx context, bool signed)
  24. {
  25. OpCodeMem64 op = (OpCodeMem64)context.CurrOp;
  26. EmitLoadAddress(context);
  27. if (signed && op.Extend64)
  28. {
  29. EmitReadSx64Call(context, op.Size);
  30. }
  31. else if (signed)
  32. {
  33. EmitReadSx32Call(context, op.Size);
  34. }
  35. else
  36. {
  37. EmitReadZxCall(context, op.Size);
  38. }
  39. if (op is IOpCodeSimd64)
  40. {
  41. context.EmitStvec(op.Rt);
  42. }
  43. else
  44. {
  45. context.EmitStintzr(op.Rt);
  46. }
  47. EmitWBackIfNeeded(context);
  48. }
  49. public static void Ldr_Literal(ILEmitterCtx context)
  50. {
  51. IOpCodeLit64 op = (IOpCodeLit64)context.CurrOp;
  52. if (op.Prefetch)
  53. {
  54. return;
  55. }
  56. context.EmitLdc_I8(op.Imm);
  57. if (op.Signed)
  58. {
  59. EmitReadSx64Call(context, op.Size);
  60. }
  61. else
  62. {
  63. EmitReadZxCall(context, op.Size);
  64. }
  65. if (op is IOpCodeSimd64)
  66. {
  67. context.EmitStvec(op.Rt);
  68. }
  69. else
  70. {
  71. context.EmitStint(op.Rt);
  72. }
  73. }
  74. public static void Ldp(ILEmitterCtx context)
  75. {
  76. OpCodeMemPair64 op = (OpCodeMemPair64)context.CurrOp;
  77. void EmitReadAndStore(int rt)
  78. {
  79. if (op.Extend64)
  80. {
  81. EmitReadSx64Call(context, op.Size);
  82. }
  83. else
  84. {
  85. EmitReadZxCall(context, op.Size);
  86. }
  87. if (op is IOpCodeSimd64)
  88. {
  89. context.EmitStvec(rt);
  90. }
  91. else
  92. {
  93. context.EmitStintzr(rt);
  94. }
  95. }
  96. EmitLoadAddress(context);
  97. EmitReadAndStore(op.Rt);
  98. context.EmitLdtmp();
  99. context.EmitLdc_I8(1 << op.Size);
  100. context.Emit(OpCodes.Add);
  101. EmitReadAndStore(op.Rt2);
  102. EmitWBackIfNeeded(context);
  103. }
  104. public static void Str(ILEmitterCtx context)
  105. {
  106. OpCodeMem64 op = (OpCodeMem64)context.CurrOp;
  107. EmitLoadAddress(context);
  108. if (op is IOpCodeSimd64)
  109. {
  110. context.EmitLdvec(op.Rt);
  111. }
  112. else
  113. {
  114. context.EmitLdintzr(op.Rt);
  115. }
  116. EmitWriteCall(context, op.Size);
  117. EmitWBackIfNeeded(context);
  118. }
  119. public static void Stp(ILEmitterCtx context)
  120. {
  121. OpCodeMemPair64 op = (OpCodeMemPair64)context.CurrOp;
  122. EmitLoadAddress(context);
  123. if (op is IOpCodeSimd64)
  124. {
  125. context.EmitLdvec(op.Rt);
  126. }
  127. else
  128. {
  129. context.EmitLdintzr(op.Rt);
  130. }
  131. EmitWriteCall(context, op.Size);
  132. context.EmitLdtmp();
  133. context.EmitLdc_I8(1 << op.Size);
  134. context.Emit(OpCodes.Add);
  135. if (op is IOpCodeSimd64)
  136. {
  137. context.EmitLdvec(op.Rt2);
  138. }
  139. else
  140. {
  141. context.EmitLdintzr(op.Rt2);
  142. }
  143. EmitWriteCall(context, op.Size);
  144. EmitWBackIfNeeded(context);
  145. }
  146. private static void EmitLoadAddress(ILEmitterCtx context)
  147. {
  148. switch (context.CurrOp)
  149. {
  150. case OpCodeMemImm64 op:
  151. context.EmitLdint(op.Rn);
  152. if (!op.PostIdx)
  153. {
  154. // Pre-indexing.
  155. context.EmitLdc_I(op.Imm);
  156. context.Emit(OpCodes.Add);
  157. }
  158. break;
  159. case OpCodeMemReg64 op:
  160. context.EmitLdint(op.Rn);
  161. context.EmitLdintzr(op.Rm);
  162. context.EmitCast(op.IntType);
  163. if (op.Shift)
  164. {
  165. context.EmitLsl(op.Size);
  166. }
  167. context.Emit(OpCodes.Add);
  168. break;
  169. }
  170. // Save address to Scratch var since the register value may change.
  171. context.Emit(OpCodes.Dup);
  172. context.EmitSttmp();
  173. }
  174. private static void EmitWBackIfNeeded(ILEmitterCtx context)
  175. {
  176. // Check whenever the current OpCode has post-indexed write back, if so write it.
  177. // Note: AOpCodeMemPair inherits from AOpCodeMemImm, so this works for both.
  178. if (context.CurrOp is OpCodeMemImm64 op && op.WBack)
  179. {
  180. context.EmitLdtmp();
  181. if (op.PostIdx)
  182. {
  183. context.EmitLdc_I(op.Imm);
  184. context.Emit(OpCodes.Add);
  185. }
  186. context.EmitStint(op.Rn);
  187. }
  188. }
  189. }
  190. }