InstEmitMemoryHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Memory;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using static ARMeilleure.Instructions.InstEmitHelper;
  7. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  8. namespace ARMeilleure.Instructions
  9. {
  10. static class InstEmitMemoryHelper
  11. {
  12. private enum Extension
  13. {
  14. Zx,
  15. Sx32,
  16. Sx64
  17. }
  18. public static void EmitLoadZx(ArmEmitterContext context, Operand address, int rt, int size)
  19. {
  20. EmitLoad(context, address, Extension.Zx, rt, size);
  21. }
  22. public static void EmitLoadSx32(ArmEmitterContext context, Operand address, int rt, int size)
  23. {
  24. EmitLoad(context, address, Extension.Sx32, rt, size);
  25. }
  26. public static void EmitLoadSx64(ArmEmitterContext context, Operand address, int rt, int size)
  27. {
  28. EmitLoad(context, address, Extension.Sx64, rt, size);
  29. }
  30. private static void EmitLoad(ArmEmitterContext context, Operand address, Extension ext, int rt, int size)
  31. {
  32. bool isSimd = IsSimd(context);
  33. if ((uint)size > (isSimd ? 4 : 3))
  34. {
  35. throw new ArgumentOutOfRangeException(nameof(size));
  36. }
  37. if (isSimd)
  38. {
  39. EmitReadVector(context, address, context.VectorZero(), rt, 0, size);
  40. }
  41. else
  42. {
  43. EmitReadInt(context, address, rt, size);
  44. }
  45. if (!isSimd)
  46. {
  47. Operand value = GetIntOrZR(context, rt);
  48. if (ext == Extension.Sx32 || ext == Extension.Sx64)
  49. {
  50. OperandType destType = ext == Extension.Sx64 ? OperandType.I64 : OperandType.I32;
  51. switch (size)
  52. {
  53. case 0: value = context.SignExtend8 (destType, value); break;
  54. case 1: value = context.SignExtend16(destType, value); break;
  55. case 2: value = context.SignExtend32(destType, value); break;
  56. }
  57. }
  58. SetIntOrZR(context, rt, value);
  59. }
  60. }
  61. public static void EmitLoadSimd(
  62. ArmEmitterContext context,
  63. Operand address,
  64. Operand vector,
  65. int rt,
  66. int elem,
  67. int size)
  68. {
  69. EmitReadVector(context, address, vector, rt, elem, size);
  70. }
  71. public static void EmitStore(ArmEmitterContext context, Operand address, int rt, int size)
  72. {
  73. bool isSimd = IsSimd(context);
  74. if ((uint)size > (isSimd ? 4 : 3))
  75. {
  76. throw new ArgumentOutOfRangeException(nameof(size));
  77. }
  78. if (isSimd)
  79. {
  80. EmitWriteVector(context, address, rt, 0, size);
  81. }
  82. else
  83. {
  84. EmitWriteInt(context, address, rt, size);
  85. }
  86. }
  87. public static void EmitStoreSimd(
  88. ArmEmitterContext context,
  89. Operand address,
  90. int rt,
  91. int elem,
  92. int size)
  93. {
  94. EmitWriteVector(context, address, rt, elem, size);
  95. }
  96. private static bool IsSimd(ArmEmitterContext context)
  97. {
  98. return context.CurrOp is IOpCodeSimd &&
  99. !(context.CurrOp is OpCodeSimdMemMs ||
  100. context.CurrOp is OpCodeSimdMemSs);
  101. }
  102. private static void EmitReadInt(ArmEmitterContext context, Operand address, int rt, int size)
  103. {
  104. Operand isUnalignedAddr = EmitAddressCheck(context, address, size);
  105. Operand lblFastPath = Label();
  106. Operand lblSlowPath = Label();
  107. Operand lblEnd = Label();
  108. context.BranchIfFalse(lblFastPath, isUnalignedAddr);
  109. context.MarkLabel(lblSlowPath);
  110. EmitReadIntFallback(context, address, rt, size);
  111. context.Branch(lblEnd);
  112. context.MarkLabel(lblFastPath);
  113. Operand physAddr = EmitPtPointerLoad(context, address, lblSlowPath);
  114. Operand value = null;
  115. switch (size)
  116. {
  117. case 0:
  118. value = context.Load8(physAddr);
  119. break;
  120. case 1:
  121. value = context.Load16(physAddr);
  122. break;
  123. case 2:
  124. value = context.Load(OperandType.I32, physAddr);
  125. break;
  126. case 3:
  127. value = context.Load(OperandType.I64, physAddr);
  128. break;
  129. }
  130. SetInt(context, rt, value);
  131. context.MarkLabel(lblEnd);
  132. }
  133. private static void EmitReadVector(
  134. ArmEmitterContext context,
  135. Operand address,
  136. Operand vector,
  137. int rt,
  138. int elem,
  139. int size)
  140. {
  141. Operand isUnalignedAddr = EmitAddressCheck(context, address, size);
  142. Operand lblFastPath = Label();
  143. Operand lblSlowPath = Label();
  144. Operand lblEnd = Label();
  145. context.BranchIfFalse(lblFastPath, isUnalignedAddr);
  146. context.MarkLabel(lblSlowPath);
  147. EmitReadVectorFallback(context, address, vector, rt, elem, size);
  148. context.Branch(lblEnd);
  149. context.MarkLabel(lblFastPath);
  150. Operand physAddr = EmitPtPointerLoad(context, address, lblSlowPath);
  151. Operand value = null;
  152. switch (size)
  153. {
  154. case 0:
  155. value = context.VectorInsert8(vector, context.Load8(physAddr), elem);
  156. break;
  157. case 1:
  158. value = context.VectorInsert16(vector, context.Load16(physAddr), elem);
  159. break;
  160. case 2:
  161. value = context.VectorInsert(vector, context.Load(OperandType.I32, physAddr), elem);
  162. break;
  163. case 3:
  164. value = context.VectorInsert(vector, context.Load(OperandType.I64, physAddr), elem);
  165. break;
  166. case 4:
  167. value = context.Load(OperandType.V128, physAddr);
  168. break;
  169. }
  170. context.Copy(GetVec(rt), value);
  171. context.MarkLabel(lblEnd);
  172. }
  173. private static Operand VectorCreate(ArmEmitterContext context, Operand value)
  174. {
  175. return context.VectorInsert(context.VectorZero(), value, 0);
  176. }
  177. private static void EmitWriteInt(ArmEmitterContext context, Operand address, int rt, int size)
  178. {
  179. Operand isUnalignedAddr = EmitAddressCheck(context, address, size);
  180. Operand lblFastPath = Label();
  181. Operand lblSlowPath = Label();
  182. Operand lblEnd = Label();
  183. context.BranchIfFalse(lblFastPath, isUnalignedAddr);
  184. context.MarkLabel(lblSlowPath);
  185. EmitWriteIntFallback(context, address, rt, size);
  186. context.Branch(lblEnd);
  187. context.MarkLabel(lblFastPath);
  188. Operand physAddr = EmitPtPointerLoad(context, address, lblSlowPath);
  189. Operand value = GetInt(context, rt);
  190. if (size < 3 && value.Type == OperandType.I64)
  191. {
  192. value = context.ConvertI64ToI32(value);
  193. }
  194. switch (size)
  195. {
  196. case 0: context.Store8 (physAddr, value); break;
  197. case 1: context.Store16(physAddr, value); break;
  198. case 2: context.Store (physAddr, value); break;
  199. case 3: context.Store (physAddr, value); break;
  200. }
  201. context.MarkLabel(lblEnd);
  202. }
  203. private static void EmitWriteVector(
  204. ArmEmitterContext context,
  205. Operand address,
  206. int rt,
  207. int elem,
  208. int size)
  209. {
  210. Operand isUnalignedAddr = EmitAddressCheck(context, address, size);
  211. Operand lblFastPath = Label();
  212. Operand lblSlowPath = Label();
  213. Operand lblEnd = Label();
  214. context.BranchIfFalse(lblFastPath, isUnalignedAddr);
  215. context.MarkLabel(lblSlowPath);
  216. EmitWriteVectorFallback(context, address, rt, elem, size);
  217. context.Branch(lblEnd);
  218. context.MarkLabel(lblFastPath);
  219. Operand physAddr = EmitPtPointerLoad(context, address, lblSlowPath);
  220. Operand value = GetVec(rt);
  221. switch (size)
  222. {
  223. case 0:
  224. context.Store8(physAddr, context.VectorExtract8(value, elem));
  225. break;
  226. case 1:
  227. context.Store16(physAddr, context.VectorExtract16(value, elem));
  228. break;
  229. case 2:
  230. context.Store(physAddr, context.VectorExtract(OperandType.FP32, value, elem));
  231. break;
  232. case 3:
  233. context.Store(physAddr, context.VectorExtract(OperandType.FP64, value, elem));
  234. break;
  235. case 4:
  236. context.Store(physAddr, value);
  237. break;
  238. }
  239. context.MarkLabel(lblEnd);
  240. }
  241. private static Operand EmitAddressCheck(ArmEmitterContext context, Operand address, int size)
  242. {
  243. long addressCheckMask = ~(context.Memory.AddressSpaceSize - 1);
  244. addressCheckMask |= (1u << size) - 1;
  245. return context.BitwiseAnd(address, Const(address.Type, addressCheckMask));
  246. }
  247. private static Operand EmitPtPointerLoad(ArmEmitterContext context, Operand address, Operand lblFallbackPath)
  248. {
  249. Operand pte = Const(context.Memory.PageTable.ToInt64());
  250. int bit = MemoryManager.PageBits;
  251. do
  252. {
  253. Operand addrPart = context.ShiftRightUI(address, Const(bit));
  254. bit += context.Memory.PtLevelBits;
  255. if (bit < context.Memory.AddressSpaceBits)
  256. {
  257. addrPart = context.BitwiseAnd(addrPart, Const(addrPart.Type, context.Memory.PtLevelMask));
  258. }
  259. Operand pteOffset = context.ShiftLeft(addrPart, Const(3));
  260. if (pteOffset.Type == OperandType.I32)
  261. {
  262. pteOffset = context.ZeroExtend32(OperandType.I64, pteOffset);
  263. }
  264. Operand pteAddress = context.Add(pte, pteOffset);
  265. pte = context.Load(OperandType.I64, pteAddress);
  266. }
  267. while (bit < context.Memory.AddressSpaceBits);
  268. Operand hasFlagSet = context.BitwiseAnd(pte, Const((long)MemoryManager.PteFlagsMask));
  269. context.BranchIfTrue(lblFallbackPath, hasFlagSet);
  270. Operand pageOffset = context.BitwiseAnd(address, Const(address.Type, MemoryManager.PageMask));
  271. if (pageOffset.Type == OperandType.I32)
  272. {
  273. pageOffset = context.ZeroExtend32(OperandType.I64, pageOffset);
  274. }
  275. Operand physAddr = context.Add(pte, pageOffset);
  276. return physAddr;
  277. }
  278. private static void EmitReadIntFallback(ArmEmitterContext context, Operand address, int rt, int size)
  279. {
  280. Delegate fallbackMethodDlg = null;
  281. switch (size)
  282. {
  283. case 0: fallbackMethodDlg = new _U8_U64 (NativeInterface.ReadByte); break;
  284. case 1: fallbackMethodDlg = new _U16_U64(NativeInterface.ReadUInt16); break;
  285. case 2: fallbackMethodDlg = new _U32_U64(NativeInterface.ReadUInt32); break;
  286. case 3: fallbackMethodDlg = new _U64_U64(NativeInterface.ReadUInt64); break;
  287. }
  288. SetInt(context, rt, context.Call(fallbackMethodDlg, address));
  289. }
  290. private static void EmitReadVectorFallback(
  291. ArmEmitterContext context,
  292. Operand address,
  293. Operand vector,
  294. int rt,
  295. int elem,
  296. int size)
  297. {
  298. Delegate fallbackMethodDlg = null;
  299. switch (size)
  300. {
  301. case 0: fallbackMethodDlg = new _U8_U64 (NativeInterface.ReadByte); break;
  302. case 1: fallbackMethodDlg = new _U16_U64 (NativeInterface.ReadUInt16); break;
  303. case 2: fallbackMethodDlg = new _U32_U64 (NativeInterface.ReadUInt32); break;
  304. case 3: fallbackMethodDlg = new _U64_U64 (NativeInterface.ReadUInt64); break;
  305. case 4: fallbackMethodDlg = new _V128_U64(NativeInterface.ReadVector128); break;
  306. }
  307. Operand value = context.Call(fallbackMethodDlg, address);
  308. switch (size)
  309. {
  310. case 0: value = context.VectorInsert8 (vector, value, elem); break;
  311. case 1: value = context.VectorInsert16(vector, value, elem); break;
  312. case 2: value = context.VectorInsert (vector, value, elem); break;
  313. case 3: value = context.VectorInsert (vector, value, elem); break;
  314. }
  315. context.Copy(GetVec(rt), value);
  316. }
  317. private static void EmitWriteIntFallback(ArmEmitterContext context, Operand address, int rt, int size)
  318. {
  319. Delegate fallbackMethodDlg = null;
  320. switch (size)
  321. {
  322. case 0: fallbackMethodDlg = new _Void_U64_U8 (NativeInterface.WriteByte); break;
  323. case 1: fallbackMethodDlg = new _Void_U64_U16(NativeInterface.WriteUInt16); break;
  324. case 2: fallbackMethodDlg = new _Void_U64_U32(NativeInterface.WriteUInt32); break;
  325. case 3: fallbackMethodDlg = new _Void_U64_U64(NativeInterface.WriteUInt64); break;
  326. }
  327. Operand value = GetInt(context, rt);
  328. if (size < 3 && value.Type == OperandType.I64)
  329. {
  330. value = context.ConvertI64ToI32(value);
  331. }
  332. context.Call(fallbackMethodDlg, address, value);
  333. }
  334. private static void EmitWriteVectorFallback(
  335. ArmEmitterContext context,
  336. Operand address,
  337. int rt,
  338. int elem,
  339. int size)
  340. {
  341. Delegate fallbackMethodDlg = null;
  342. switch (size)
  343. {
  344. case 0: fallbackMethodDlg = new _Void_U64_U8 (NativeInterface.WriteByte); break;
  345. case 1: fallbackMethodDlg = new _Void_U64_U16 (NativeInterface.WriteUInt16); break;
  346. case 2: fallbackMethodDlg = new _Void_U64_U32 (NativeInterface.WriteUInt32); break;
  347. case 3: fallbackMethodDlg = new _Void_U64_U64 (NativeInterface.WriteUInt64); break;
  348. case 4: fallbackMethodDlg = new _Void_U64_V128(NativeInterface.WriteVector128); break;
  349. }
  350. Operand value = null;
  351. if (size < 4)
  352. {
  353. switch (size)
  354. {
  355. case 0:
  356. value = context.VectorExtract8(GetVec(rt), elem);
  357. break;
  358. case 1:
  359. value = context.VectorExtract16(GetVec(rt), elem);
  360. break;
  361. case 2:
  362. value = context.VectorExtract(OperandType.I32, GetVec(rt), elem);
  363. break;
  364. case 3:
  365. value = context.VectorExtract(OperandType.I64, GetVec(rt), elem);
  366. break;
  367. }
  368. }
  369. else
  370. {
  371. value = GetVec(rt);
  372. }
  373. context.Call(fallbackMethodDlg, address, value);
  374. }
  375. private static Operand GetInt(ArmEmitterContext context, int rt)
  376. {
  377. return context.CurrOp is OpCode32 ? GetIntA32(context, rt) : GetIntOrZR(context, rt);
  378. }
  379. private static void SetInt(ArmEmitterContext context, int rt, Operand value)
  380. {
  381. if (context.CurrOp is OpCode32)
  382. {
  383. SetIntA32(context, rt, value);
  384. }
  385. else
  386. {
  387. SetIntOrZR(context, rt, value);
  388. }
  389. }
  390. }
  391. }