InstEmitSimdMemory32.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using static ARMeilleure.Instructions.InstEmitHelper;
  6. using static ARMeilleure.Instructions.InstEmitMemoryHelper;
  7. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  8. namespace ARMeilleure.Instructions
  9. {
  10. static partial class InstEmit32
  11. {
  12. public static void Vld1(ArmEmitterContext context)
  13. {
  14. EmitVStoreOrLoadN(context, 1, true);
  15. }
  16. public static void Vld2(ArmEmitterContext context)
  17. {
  18. EmitVStoreOrLoadN(context, 2, true);
  19. }
  20. public static void Vld3(ArmEmitterContext context)
  21. {
  22. EmitVStoreOrLoadN(context, 3, true);
  23. }
  24. public static void Vld4(ArmEmitterContext context)
  25. {
  26. EmitVStoreOrLoadN(context, 4, true);
  27. }
  28. public static void Vst1(ArmEmitterContext context)
  29. {
  30. EmitVStoreOrLoadN(context, 1, false);
  31. }
  32. public static void Vst2(ArmEmitterContext context)
  33. {
  34. EmitVStoreOrLoadN(context, 2, false);
  35. }
  36. public static void Vst3(ArmEmitterContext context)
  37. {
  38. EmitVStoreOrLoadN(context, 3, false);
  39. }
  40. public static void Vst4(ArmEmitterContext context)
  41. {
  42. EmitVStoreOrLoadN(context, 4, false);
  43. }
  44. public static void EmitVStoreOrLoadN(ArmEmitterContext context, int count, bool load)
  45. {
  46. if (context.CurrOp is OpCode32SimdMemSingle)
  47. {
  48. OpCode32SimdMemSingle op = (OpCode32SimdMemSingle)context.CurrOp;
  49. int eBytes = 1 << op.Size;
  50. Operand n = context.Copy(GetIntA32(context, op.Rn));
  51. // TODO: Check alignment.
  52. int offset = 0;
  53. int d = op.Vd;
  54. for (int i = 0; i < count; i++)
  55. {
  56. // Accesses an element from a double simd register.
  57. Operand address = context.Add(n, Const(offset));
  58. if (eBytes == 8)
  59. {
  60. if (load)
  61. {
  62. EmitDVectorLoad(context, address, d);
  63. }
  64. else
  65. {
  66. EmitDVectorStore(context, address, d);
  67. }
  68. }
  69. else
  70. {
  71. int index = ((d & 1) << (3 - op.Size)) + op.Index;
  72. if (load)
  73. {
  74. if (op.Replicate)
  75. {
  76. var regs = (count > 1) ? 1 : op.Increment;
  77. for (int reg = 0; reg < regs; reg++)
  78. {
  79. int dreg = reg + d;
  80. int rIndex = ((dreg & 1) << (3 - op.Size));
  81. int limit = rIndex + (1 << (3 - op.Size));
  82. while (rIndex < limit)
  83. {
  84. EmitLoadSimd(context, address, GetVecA32(dreg >> 1), dreg >> 1, rIndex++, op.Size);
  85. }
  86. }
  87. }
  88. else
  89. {
  90. EmitLoadSimd(context, address, GetVecA32(d >> 1), d >> 1, index, op.Size);
  91. }
  92. }
  93. else
  94. {
  95. EmitStoreSimd(context, address, d >> 1, index, op.Size);
  96. }
  97. }
  98. offset += eBytes;
  99. d += op.Increment;
  100. }
  101. if (op.WBack)
  102. {
  103. if (op.RegisterIndex)
  104. {
  105. Operand m = GetIntA32(context, op.Rm);
  106. SetIntA32(context, op.Rn, context.Add(n, m));
  107. }
  108. else
  109. {
  110. SetIntA32(context, op.Rn, context.Add(n, Const(count * eBytes)));
  111. }
  112. }
  113. }
  114. else
  115. {
  116. OpCode32SimdMemPair op = (OpCode32SimdMemPair)context.CurrOp;
  117. int increment = count > 1 ? op.Increment : 1;
  118. int eBytes = 1 << op.Size;
  119. Operand n = context.Copy(GetIntA32(context, op.Rn));
  120. int offset = 0;
  121. int d = op.Vd;
  122. for (int reg = 0; reg < op.Regs; reg++)
  123. {
  124. for (int elem = 0; elem < op.Elems; elem++)
  125. {
  126. int elemD = d + reg;
  127. for (int i = 0; i < count; i++)
  128. {
  129. // Accesses an element from a double simd register,
  130. // add ebytes for each element.
  131. Operand address = context.Add(n, Const(offset));
  132. int index = ((elemD & 1) << (3 - op.Size)) + elem;
  133. if (eBytes == 8)
  134. {
  135. if (load)
  136. {
  137. EmitDVectorLoad(context, address, elemD);
  138. }
  139. else
  140. {
  141. EmitDVectorStore(context, address, elemD);
  142. }
  143. }
  144. else
  145. {
  146. if (load)
  147. {
  148. EmitLoadSimd(context, address, GetVecA32(elemD >> 1), elemD >> 1, index, op.Size);
  149. }
  150. else
  151. {
  152. EmitStoreSimd(context, address, elemD >> 1, index, op.Size);
  153. }
  154. }
  155. offset += eBytes;
  156. elemD += increment;
  157. }
  158. }
  159. }
  160. if (op.WBack)
  161. {
  162. if (op.RegisterIndex)
  163. {
  164. Operand m = GetIntA32(context, op.Rm);
  165. SetIntA32(context, op.Rn, context.Add(n, m));
  166. }
  167. else
  168. {
  169. SetIntA32(context, op.Rn, context.Add(n, Const(count * 8 * op.Regs)));
  170. }
  171. }
  172. }
  173. }
  174. public static void Vldm(ArmEmitterContext context)
  175. {
  176. OpCode32SimdMemMult op = (OpCode32SimdMemMult)context.CurrOp;
  177. Operand n = context.Copy(GetIntA32(context, op.Rn));
  178. Operand baseAddress = context.Add(n, Const(op.Offset));
  179. bool writeBack = op.PostOffset != 0;
  180. if (writeBack)
  181. {
  182. SetIntA32(context, op.Rn, context.Add(n, Const(op.PostOffset)));
  183. }
  184. int range = op.RegisterRange;
  185. int sReg = (op.DoubleWidth) ? (op.Vd << 1) : op.Vd;
  186. int offset = 0;
  187. int byteSize = 4;
  188. for (int num = 0; num < range; num++, sReg++)
  189. {
  190. Operand address = context.Add(baseAddress, Const(offset));
  191. Operand vec = GetVecA32(sReg >> 2);
  192. EmitLoadSimd(context, address, vec, sReg >> 2, sReg & 3, WordSizeLog2);
  193. offset += byteSize;
  194. }
  195. }
  196. public static void Vstm(ArmEmitterContext context)
  197. {
  198. OpCode32SimdMemMult op = (OpCode32SimdMemMult)context.CurrOp;
  199. Operand n = context.Copy(GetIntA32(context, op.Rn));
  200. Operand baseAddress = context.Add(n, Const(op.Offset));
  201. bool writeBack = op.PostOffset != 0;
  202. if (writeBack)
  203. {
  204. SetIntA32(context, op.Rn, context.Add(n, Const(op.PostOffset)));
  205. }
  206. int offset = 0;
  207. int range = op.RegisterRange;
  208. int sReg = (op.DoubleWidth) ? (op.Vd << 1) : op.Vd;
  209. int byteSize = 4;
  210. for (int num = 0; num < range; num++, sReg++)
  211. {
  212. Operand address = context.Add(baseAddress, Const(offset));
  213. EmitStoreSimd(context, address, sReg >> 2, sReg & 3, WordSizeLog2);
  214. offset += byteSize;
  215. }
  216. }
  217. public static void Vldr(ArmEmitterContext context)
  218. {
  219. EmitVLoadOrStore(context, AccessType.Load);
  220. }
  221. public static void Vstr(ArmEmitterContext context)
  222. {
  223. EmitVLoadOrStore(context, AccessType.Store);
  224. }
  225. private static void EmitDVectorStore(ArmEmitterContext context, Operand address, int vecD)
  226. {
  227. int vecQ = vecD >> 1;
  228. int vecSElem = (vecD & 1) << 1;
  229. Operand lblBigEndian = Label();
  230. Operand lblEnd = Label();
  231. context.BranchIfTrue(lblBigEndian, GetFlag(PState.EFlag));
  232. EmitStoreSimd(context, address, vecQ, vecSElem, WordSizeLog2);
  233. EmitStoreSimd(context, context.Add(address, Const(4)), vecQ, vecSElem | 1, WordSizeLog2);
  234. context.Branch(lblEnd);
  235. context.MarkLabel(lblBigEndian);
  236. EmitStoreSimd(context, address, vecQ, vecSElem | 1, WordSizeLog2);
  237. EmitStoreSimd(context, context.Add(address, Const(4)), vecQ, vecSElem, WordSizeLog2);
  238. context.MarkLabel(lblEnd);
  239. }
  240. private static void EmitDVectorLoad(ArmEmitterContext context, Operand address, int vecD)
  241. {
  242. int vecQ = vecD >> 1;
  243. int vecSElem = (vecD & 1) << 1;
  244. Operand vec = GetVecA32(vecQ);
  245. Operand lblBigEndian = Label();
  246. Operand lblEnd = Label();
  247. context.BranchIfTrue(lblBigEndian, GetFlag(PState.EFlag));
  248. EmitLoadSimd(context, address, vec, vecQ, vecSElem, WordSizeLog2);
  249. EmitLoadSimd(context, context.Add(address, Const(4)), vec, vecQ, vecSElem | 1, WordSizeLog2);
  250. context.Branch(lblEnd);
  251. context.MarkLabel(lblBigEndian);
  252. EmitLoadSimd(context, address, vec, vecQ, vecSElem | 1, WordSizeLog2);
  253. EmitLoadSimd(context, context.Add(address, Const(4)), vec, vecQ, vecSElem, WordSizeLog2);
  254. context.MarkLabel(lblEnd);
  255. }
  256. private static void EmitVLoadOrStore(ArmEmitterContext context, AccessType accType)
  257. {
  258. OpCode32SimdMemImm op = (OpCode32SimdMemImm)context.CurrOp;
  259. Operand n = context.Copy(GetIntA32(context, op.Rn));
  260. Operand m = GetMemM(context, setCarry: false);
  261. Operand address = op.Add
  262. ? context.Add(n, m)
  263. : context.Subtract(n, m);
  264. int size = op.Size;
  265. if ((accType & AccessType.Load) != 0)
  266. {
  267. if (size == DWordSizeLog2)
  268. {
  269. EmitDVectorLoad(context, address, op.Vd);
  270. }
  271. else
  272. {
  273. Operand vec = GetVecA32(op.Vd >> 2);
  274. EmitLoadSimd(context, address, vec, op.Vd >> 2, (op.Vd & 3) << (2 - size), size);
  275. }
  276. }
  277. else
  278. {
  279. if (size == DWordSizeLog2)
  280. {
  281. EmitDVectorStore(context, address, op.Vd);
  282. }
  283. else
  284. {
  285. EmitStoreSimd(context, address, op.Vd >> 2, (op.Vd & 3) << (2 - size), size);
  286. }
  287. }
  288. }
  289. }
  290. }