InstEmitMemoryHelper.cs 15 KB

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