InstEmitMemoryHelper.cs 19 KB

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