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.OperandHelper;
  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. // Write 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 eBytes = 1 << op.Size;
  118. Operand n = context.Copy(GetIntA32(context, op.Rn));
  119. int offset = 0;
  120. int d = op.Vd;
  121. for (int reg = 0; reg < op.Regs; reg++)
  122. {
  123. for (int elem = 0; elem < op.Elems; elem++)
  124. {
  125. int elemD = d + reg;
  126. for (int i = 0; i < count; i++)
  127. {
  128. // Write an element from a double simd register
  129. // add ebytes for each element.
  130. Operand address = context.Add(n, Const(offset));
  131. int index = ((elemD & 1) << (3 - op.Size)) + elem;
  132. if (eBytes == 8)
  133. {
  134. if (load)
  135. {
  136. EmitDVectorLoad(context, address, elemD);
  137. }
  138. else
  139. {
  140. EmitDVectorStore(context, address, elemD);
  141. }
  142. }
  143. else
  144. {
  145. if (load)
  146. {
  147. EmitLoadSimd(context, address, GetVecA32(elemD >> 1), elemD >> 1, index, op.Size);
  148. }
  149. else
  150. {
  151. EmitStoreSimd(context, address, elemD >> 1, index, op.Size);
  152. }
  153. }
  154. offset += eBytes;
  155. elemD += op.Increment;
  156. }
  157. }
  158. }
  159. if (op.WBack)
  160. {
  161. if (op.RegisterIndex)
  162. {
  163. Operand m = GetIntA32(context, op.Rm);
  164. SetIntA32(context, op.Rn, context.Add(n, m));
  165. }
  166. else
  167. {
  168. SetIntA32(context, op.Rn, context.Add(n, Const(count * 8 * op.Regs)));
  169. }
  170. }
  171. }
  172. }
  173. public static void Vldm(ArmEmitterContext context)
  174. {
  175. OpCode32SimdMemMult op = (OpCode32SimdMemMult)context.CurrOp;
  176. Operand n = context.Copy(GetIntA32(context, op.Rn));
  177. Operand baseAddress = context.Add(n, Const(op.Offset));
  178. bool writeBack = op.PostOffset != 0;
  179. if (writeBack)
  180. {
  181. SetIntA32(context, op.Rn, context.Add(n, Const(op.PostOffset)));
  182. }
  183. int range = op.RegisterRange;
  184. int sReg = (op.DoubleWidth) ? (op.Vd << 1) : op.Vd;
  185. int offset = 0;
  186. int byteSize = 4;
  187. for (int num = 0; num < range; num++, sReg++)
  188. {
  189. Operand address = context.Add(baseAddress, Const(offset));
  190. Operand vec = GetVecA32(sReg >> 2);
  191. EmitLoadSimd(context, address, vec, sReg >> 2, sReg & 3, WordSizeLog2);
  192. offset += byteSize;
  193. }
  194. }
  195. public static void Vstm(ArmEmitterContext context)
  196. {
  197. OpCode32SimdMemMult op = (OpCode32SimdMemMult)context.CurrOp;
  198. Operand n = context.Copy(GetIntA32(context, op.Rn));
  199. Operand baseAddress = context.Add(n, Const(op.Offset));
  200. bool writeBack = op.PostOffset != 0;
  201. if (writeBack)
  202. {
  203. SetIntA32(context, op.Rn, context.Add(n, Const(op.PostOffset)));
  204. }
  205. int offset = 0;
  206. int range = op.RegisterRange;
  207. int sReg = (op.DoubleWidth) ? (op.Vd << 1) : op.Vd;
  208. int byteSize = 4;
  209. for (int num = 0; num < range; num++, sReg++)
  210. {
  211. Operand address = context.Add(baseAddress, Const(offset));
  212. EmitStoreSimd(context, address, sReg >> 2, sReg & 3, WordSizeLog2);
  213. offset += byteSize;
  214. }
  215. }
  216. public static void Vldr(ArmEmitterContext context)
  217. {
  218. EmitVLoadOrStore(context, AccessType.Load);
  219. }
  220. public static void Vstr(ArmEmitterContext context)
  221. {
  222. EmitVLoadOrStore(context, AccessType.Store);
  223. }
  224. private static void EmitDVectorStore(ArmEmitterContext context, Operand address, int vecD)
  225. {
  226. int vecQ = vecD >> 1;
  227. int vecSElem = (vecD & 1) << 1;
  228. Operand lblBigEndian = Label();
  229. Operand lblEnd = Label();
  230. context.BranchIfTrue(lblBigEndian, GetFlag(PState.EFlag));
  231. EmitStoreSimd(context, address, vecQ, vecSElem, WordSizeLog2);
  232. EmitStoreSimd(context, context.Add(address, Const(4)), vecQ, vecSElem | 1, WordSizeLog2);
  233. context.Branch(lblEnd);
  234. context.MarkLabel(lblBigEndian);
  235. EmitStoreSimd(context, address, vecQ, vecSElem | 1, WordSizeLog2);
  236. EmitStoreSimd(context, context.Add(address, Const(4)), vecQ, vecSElem, WordSizeLog2);
  237. context.MarkLabel(lblEnd);
  238. }
  239. private static void EmitDVectorLoad(ArmEmitterContext context, Operand address, int vecD)
  240. {
  241. int vecQ = vecD >> 1;
  242. int vecSElem = (vecD & 1) << 1;
  243. Operand vec = GetVecA32(vecQ);
  244. Operand lblBigEndian = Label();
  245. Operand lblEnd = Label();
  246. context.BranchIfTrue(lblBigEndian, GetFlag(PState.EFlag));
  247. EmitLoadSimd(context, address, vec, vecQ, vecSElem, WordSizeLog2);
  248. EmitLoadSimd(context, context.Add(address, Const(4)), vec, vecQ, vecSElem | 1, WordSizeLog2);
  249. context.Branch(lblEnd);
  250. context.MarkLabel(lblBigEndian);
  251. EmitLoadSimd(context, address, vec, vecQ, vecSElem | 1, WordSizeLog2);
  252. EmitLoadSimd(context, context.Add(address, Const(4)), vec, vecQ, vecSElem, WordSizeLog2);
  253. context.MarkLabel(lblEnd);
  254. }
  255. private static void EmitVLoadOrStore(ArmEmitterContext context, AccessType accType)
  256. {
  257. OpCode32SimdMemImm op = (OpCode32SimdMemImm)context.CurrOp;
  258. Operand n = context.Copy(GetIntA32(context, op.Rn));
  259. Operand m = GetMemM(context, setCarry: false);
  260. Operand address = op.Add
  261. ? context.Add(n, m)
  262. : context.Subtract(n, m);
  263. int size = op.Size;
  264. if ((accType & AccessType.Load) != 0)
  265. {
  266. if (size == DWordSizeLog2)
  267. {
  268. EmitDVectorLoad(context, address, op.Vd);
  269. }
  270. else
  271. {
  272. Operand vec = GetVecA32(op.Vd >> 2);
  273. EmitLoadSimd(context, address, vec, op.Vd >> 2, (op.Vd & 3) << (2 - size), size);
  274. }
  275. }
  276. else
  277. {
  278. if (size == DWordSizeLog2)
  279. {
  280. EmitDVectorStore(context, address, op.Vd);
  281. }
  282. else
  283. {
  284. EmitStoreSimd(context, address, op.Vd >> 2, (op.Vd & 3) << (2 - size), size);
  285. }
  286. }
  287. }
  288. }
  289. }