InstEmitMemoryHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using ChocolArm64.Translation;
  5. using System;
  6. using System.Reflection.Emit;
  7. using System.Runtime.Intrinsics.X86;
  8. namespace ChocolArm64.Instructions
  9. {
  10. static class InstEmitMemoryHelper
  11. {
  12. private static int _tempIntAddress = ILEmitterCtx.GetIntTempIndex();
  13. private static int _tempIntValue = ILEmitterCtx.GetIntTempIndex();
  14. private static int _tempIntPtAddr = ILEmitterCtx.GetIntTempIndex();
  15. private static int _tempVecValue = ILEmitterCtx.GetVecTempIndex();
  16. private enum Extension
  17. {
  18. Zx,
  19. Sx32,
  20. Sx64
  21. }
  22. public static void EmitReadZxCall(ILEmitterCtx context, int size)
  23. {
  24. EmitReadCall(context, Extension.Zx, size);
  25. }
  26. public static void EmitReadSx32Call(ILEmitterCtx context, int size)
  27. {
  28. EmitReadCall(context, Extension.Sx32, size);
  29. }
  30. public static void EmitReadSx64Call(ILEmitterCtx context, int size)
  31. {
  32. EmitReadCall(context, Extension.Sx64, size);
  33. }
  34. private static void EmitReadCall(ILEmitterCtx context, Extension ext, int size)
  35. {
  36. //Save the address into a temp.
  37. context.EmitStint(_tempIntAddress);
  38. bool isSimd = IsSimd(context);
  39. if (size < 0 || size > (isSimd ? 4 : 3))
  40. {
  41. throw new ArgumentOutOfRangeException(nameof(size));
  42. }
  43. if (isSimd)
  44. {
  45. if (context.Tier == TranslationTier.Tier0 || !Sse2.IsSupported || size < 2)
  46. {
  47. EmitReadVectorFallback(context, size);
  48. }
  49. else
  50. {
  51. EmitReadVector(context, size);
  52. }
  53. }
  54. else
  55. {
  56. if (context.Tier == TranslationTier.Tier0)
  57. {
  58. EmitReadIntFallback(context, size);
  59. }
  60. else
  61. {
  62. EmitReadInt(context, size);
  63. }
  64. }
  65. if (!isSimd)
  66. {
  67. if (ext == Extension.Sx32 ||
  68. ext == Extension.Sx64)
  69. {
  70. switch (size)
  71. {
  72. case 0: context.Emit(OpCodes.Conv_I1); break;
  73. case 1: context.Emit(OpCodes.Conv_I2); break;
  74. case 2: context.Emit(OpCodes.Conv_I4); break;
  75. }
  76. }
  77. if (size < 3)
  78. {
  79. context.Emit(ext == Extension.Sx64
  80. ? OpCodes.Conv_I8
  81. : OpCodes.Conv_U8);
  82. }
  83. }
  84. }
  85. public static void EmitWriteCall(ILEmitterCtx context, int size)
  86. {
  87. bool isSimd = IsSimd(context);
  88. //Save the value into a temp.
  89. if (isSimd)
  90. {
  91. context.EmitStvec(_tempVecValue);
  92. }
  93. else
  94. {
  95. context.EmitStint(_tempIntValue);
  96. }
  97. //Save the address into a temp.
  98. context.EmitStint(_tempIntAddress);
  99. if (size < 0 || size > (isSimd ? 4 : 3))
  100. {
  101. throw new ArgumentOutOfRangeException(nameof(size));
  102. }
  103. if (isSimd)
  104. {
  105. if (context.Tier == TranslationTier.Tier0 || !Sse2.IsSupported || size < 2)
  106. {
  107. EmitWriteVectorFallback(context, size);
  108. }
  109. else
  110. {
  111. EmitWriteVector(context, size);
  112. }
  113. }
  114. else
  115. {
  116. if (context.Tier == TranslationTier.Tier0)
  117. {
  118. EmitWriteIntFallback(context, size);
  119. }
  120. else
  121. {
  122. EmitWriteInt(context, size);
  123. }
  124. }
  125. }
  126. private static bool IsSimd(ILEmitterCtx context)
  127. {
  128. return context.CurrOp is IOpCodeSimd64 &&
  129. !(context.CurrOp is OpCodeSimdMemMs64 ||
  130. context.CurrOp is OpCodeSimdMemSs64);
  131. }
  132. private static void EmitReadInt(ILEmitterCtx context, int size)
  133. {
  134. EmitAddressCheck(context, size);
  135. ILLabel lblFastPath = new ILLabel();
  136. ILLabel lblSlowPath = new ILLabel();
  137. ILLabel lblEnd = new ILLabel();
  138. context.Emit(OpCodes.Brfalse_S, lblFastPath);
  139. context.MarkLabel(lblSlowPath);
  140. EmitReadIntFallback(context, size);
  141. context.Emit(OpCodes.Br, lblEnd);
  142. context.MarkLabel(lblFastPath);
  143. EmitPtPointerLoad(context, lblSlowPath);
  144. switch (size)
  145. {
  146. case 0: context.Emit(OpCodes.Ldind_U1); break;
  147. case 1: context.Emit(OpCodes.Ldind_U2); break;
  148. case 2: context.Emit(OpCodes.Ldind_U4); break;
  149. case 3: context.Emit(OpCodes.Ldind_I8); break;
  150. }
  151. context.MarkLabel(lblEnd);
  152. }
  153. private static void EmitReadVector(ILEmitterCtx context, int size)
  154. {
  155. EmitAddressCheck(context, size);
  156. ILLabel lblFastPath = new ILLabel();
  157. ILLabel lblSlowPath = new ILLabel();
  158. ILLabel lblEnd = new ILLabel();
  159. context.Emit(OpCodes.Brfalse_S, lblFastPath);
  160. context.MarkLabel(lblSlowPath);
  161. EmitReadVectorFallback(context, size);
  162. context.Emit(OpCodes.Br, lblEnd);
  163. context.MarkLabel(lblFastPath);
  164. EmitPtPointerLoad(context, lblSlowPath);
  165. switch (size)
  166. {
  167. case 2: context.EmitCall(typeof(Sse), nameof(Sse.LoadScalarVector128)); break;
  168. case 3:
  169. {
  170. Type[] types = new Type[] { typeof(double*) };
  171. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.LoadScalarVector128), types));
  172. break;
  173. }
  174. case 4: context.EmitCall(typeof(Sse), nameof(Sse.LoadAlignedVector128)); break;
  175. throw new InvalidOperationException($"Invalid vector load size of {1 << size} bytes.");
  176. }
  177. context.MarkLabel(lblEnd);
  178. }
  179. private static void EmitWriteInt(ILEmitterCtx context, int size)
  180. {
  181. EmitAddressCheck(context, size);
  182. ILLabel lblFastPath = new ILLabel();
  183. ILLabel lblSlowPath = new ILLabel();
  184. ILLabel lblEnd = new ILLabel();
  185. context.Emit(OpCodes.Brfalse_S, lblFastPath);
  186. context.MarkLabel(lblSlowPath);
  187. EmitWriteIntFallback(context, size);
  188. context.Emit(OpCodes.Br, lblEnd);
  189. context.MarkLabel(lblFastPath);
  190. EmitPtPointerLoad(context, lblSlowPath);
  191. context.EmitLdint(_tempIntValue);
  192. if (size < 3)
  193. {
  194. context.Emit(OpCodes.Conv_U4);
  195. }
  196. switch (size)
  197. {
  198. case 0: context.Emit(OpCodes.Stind_I1); break;
  199. case 1: context.Emit(OpCodes.Stind_I2); break;
  200. case 2: context.Emit(OpCodes.Stind_I4); break;
  201. case 3: context.Emit(OpCodes.Stind_I8); break;
  202. }
  203. context.MarkLabel(lblEnd);
  204. }
  205. private static void EmitWriteVector(ILEmitterCtx context, int size)
  206. {
  207. EmitAddressCheck(context, size);
  208. ILLabel lblFastPath = new ILLabel();
  209. ILLabel lblSlowPath = new ILLabel();
  210. ILLabel lblEnd = new ILLabel();
  211. context.Emit(OpCodes.Brfalse_S, lblFastPath);
  212. context.MarkLabel(lblSlowPath);
  213. EmitWriteVectorFallback(context, size);
  214. context.Emit(OpCodes.Br, lblEnd);
  215. context.MarkLabel(lblFastPath);
  216. EmitPtPointerLoad(context, lblSlowPath);
  217. context.EmitLdvec(_tempVecValue);
  218. switch (size)
  219. {
  220. case 2: context.EmitCall(typeof(Sse), nameof(Sse.StoreScalar)); break;
  221. case 3: context.EmitCall(typeof(Sse2), nameof(Sse2.StoreScalar)); break;
  222. case 4: context.EmitCall(typeof(Sse), nameof(Sse.StoreAligned)); break;
  223. default: throw new InvalidOperationException($"Invalid vector store size of {1 << size} bytes.");
  224. }
  225. context.MarkLabel(lblEnd);
  226. }
  227. private static void EmitAddressCheck(ILEmitterCtx context, int size)
  228. {
  229. long addressCheckMask = ~(context.Memory.AddressSpaceSize - 1);
  230. addressCheckMask |= (1u << size) - 1;
  231. context.EmitLdint(_tempIntAddress);
  232. context.EmitLdc_I(addressCheckMask);
  233. context.Emit(OpCodes.And);
  234. }
  235. private static void EmitPtPointerLoad(ILEmitterCtx context, ILLabel lblFallbackPath)
  236. {
  237. context.EmitLdc_I8(context.Memory.PageTable.ToInt64());
  238. context.Emit(OpCodes.Conv_I);
  239. int bit = MemoryManager.PageBits;
  240. do
  241. {
  242. context.EmitLdint(_tempIntAddress);
  243. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  244. {
  245. context.Emit(OpCodes.Conv_U8);
  246. }
  247. context.EmitLsr(bit);
  248. bit += context.Memory.PtLevelBits;
  249. if (bit < context.Memory.AddressSpaceBits)
  250. {
  251. context.EmitLdc_I8(context.Memory.PtLevelMask);
  252. context.Emit(OpCodes.And);
  253. }
  254. context.EmitLdc_I8(IntPtr.Size);
  255. context.Emit(OpCodes.Mul);
  256. context.Emit(OpCodes.Conv_I);
  257. context.Emit(OpCodes.Add);
  258. context.Emit(OpCodes.Ldind_I);
  259. }
  260. while (bit < context.Memory.AddressSpaceBits);
  261. if (!context.Memory.HasWriteWatchSupport)
  262. {
  263. context.Emit(OpCodes.Conv_U8);
  264. context.EmitStint(_tempIntPtAddr);
  265. context.EmitLdint(_tempIntPtAddr);
  266. context.EmitLdc_I8(MemoryManager.PteFlagsMask);
  267. context.Emit(OpCodes.And);
  268. context.Emit(OpCodes.Brtrue, lblFallbackPath);
  269. context.EmitLdint(_tempIntPtAddr);
  270. context.Emit(OpCodes.Conv_I);
  271. }
  272. context.EmitLdint(_tempIntAddress);
  273. context.EmitLdc_I(MemoryManager.PageMask);
  274. context.Emit(OpCodes.And);
  275. context.Emit(OpCodes.Conv_I);
  276. context.Emit(OpCodes.Add);
  277. }
  278. private static void EmitReadIntFallback(ILEmitterCtx context, int size)
  279. {
  280. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  281. context.EmitLdint(_tempIntAddress);
  282. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  283. {
  284. context.Emit(OpCodes.Conv_U8);
  285. }
  286. string fallbackMethodName = null;
  287. switch (size)
  288. {
  289. case 0: fallbackMethodName = nameof(MemoryManager.ReadByte); break;
  290. case 1: fallbackMethodName = nameof(MemoryManager.ReadUInt16); break;
  291. case 2: fallbackMethodName = nameof(MemoryManager.ReadUInt32); break;
  292. case 3: fallbackMethodName = nameof(MemoryManager.ReadUInt64); break;
  293. }
  294. context.EmitCall(typeof(MemoryManager), fallbackMethodName);
  295. }
  296. private static void EmitReadVectorFallback(ILEmitterCtx context, int size)
  297. {
  298. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  299. context.EmitLdint(_tempIntAddress);
  300. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  301. {
  302. context.Emit(OpCodes.Conv_U8);
  303. }
  304. string fallbackMethodName = null;
  305. switch (size)
  306. {
  307. case 0: fallbackMethodName = nameof(MemoryManager.ReadVector8); break;
  308. case 1: fallbackMethodName = nameof(MemoryManager.ReadVector16); break;
  309. case 2: fallbackMethodName = nameof(MemoryManager.ReadVector32); break;
  310. case 3: fallbackMethodName = nameof(MemoryManager.ReadVector64); break;
  311. case 4: fallbackMethodName = nameof(MemoryManager.ReadVector128); break;
  312. }
  313. context.EmitCall(typeof(MemoryManager), fallbackMethodName);
  314. }
  315. private static void EmitWriteIntFallback(ILEmitterCtx context, int size)
  316. {
  317. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  318. context.EmitLdint(_tempIntAddress);
  319. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  320. {
  321. context.Emit(OpCodes.Conv_U8);
  322. }
  323. context.EmitLdint(_tempIntValue);
  324. if (size < 3)
  325. {
  326. context.Emit(OpCodes.Conv_U4);
  327. }
  328. string fallbackMethodName = null;
  329. switch (size)
  330. {
  331. case 0: fallbackMethodName = nameof(MemoryManager.WriteByte); break;
  332. case 1: fallbackMethodName = nameof(MemoryManager.WriteUInt16); break;
  333. case 2: fallbackMethodName = nameof(MemoryManager.WriteUInt32); break;
  334. case 3: fallbackMethodName = nameof(MemoryManager.WriteUInt64); break;
  335. }
  336. context.EmitCall(typeof(MemoryManager), fallbackMethodName);
  337. }
  338. private static void EmitWriteVectorFallback(ILEmitterCtx context, int size)
  339. {
  340. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  341. context.EmitLdint(_tempIntAddress);
  342. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  343. {
  344. context.Emit(OpCodes.Conv_U8);
  345. }
  346. context.EmitLdvec(_tempVecValue);
  347. string fallbackMethodName = null;
  348. switch (size)
  349. {
  350. case 0: fallbackMethodName = nameof(MemoryManager.WriteVector8); break;
  351. case 1: fallbackMethodName = nameof(MemoryManager.WriteVector16); break;
  352. case 2: fallbackMethodName = nameof(MemoryManager.WriteVector32); break;
  353. case 3: fallbackMethodName = nameof(MemoryManager.WriteVector64); break;
  354. case 4: fallbackMethodName = nameof(MemoryManager.WriteVector128); break;
  355. }
  356. context.EmitCall(typeof(MemoryManager), fallbackMethodName);
  357. }
  358. }
  359. }