InstEmitMemory32.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using static ARMeilleure.Instructions.InstEmitHelper;
  7. using static ARMeilleure.Instructions.InstEmitMemoryHelper;
  8. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static partial class InstEmit32
  12. {
  13. private const int ByteSizeLog2 = 0;
  14. private const int HWordSizeLog2 = 1;
  15. private const int WordSizeLog2 = 2;
  16. private const int DWordSizeLog2 = 3;
  17. [Flags]
  18. enum AccessType
  19. {
  20. Store = 0,
  21. Signed = 1,
  22. Load = 2,
  23. Ordered = 4,
  24. Exclusive = 8,
  25. LoadZx = Load,
  26. LoadSx = Load | Signed,
  27. }
  28. public static void Ldm(ArmEmitterContext context)
  29. {
  30. IOpCode32MemMult op = (IOpCode32MemMult)context.CurrOp;
  31. Operand n = GetIntA32(context, op.Rn);
  32. Operand baseAddress = context.Add(n, Const(op.Offset));
  33. bool writesToPc = (op.RegisterMask & (1 << RegisterAlias.Aarch32Pc)) != 0;
  34. bool writeBack = op.PostOffset != 0 && (op.Rn != RegisterAlias.Aarch32Pc || !writesToPc);
  35. if (writeBack)
  36. {
  37. SetIntA32(context, op.Rn, context.Add(n, Const(op.PostOffset)));
  38. }
  39. int mask = op.RegisterMask;
  40. int offset = 0;
  41. for (int register = 0; mask != 0; mask >>= 1, register++)
  42. {
  43. if ((mask & 1) != 0)
  44. {
  45. Operand address = context.Add(baseAddress, Const(offset));
  46. EmitLoadZx(context, address, register, WordSizeLog2);
  47. offset += 4;
  48. }
  49. }
  50. }
  51. public static void Ldr(ArmEmitterContext context)
  52. {
  53. EmitLoadOrStore(context, WordSizeLog2, AccessType.LoadZx);
  54. }
  55. public static void Ldrb(ArmEmitterContext context)
  56. {
  57. EmitLoadOrStore(context, ByteSizeLog2, AccessType.LoadZx);
  58. }
  59. public static void Ldrd(ArmEmitterContext context)
  60. {
  61. EmitLoadOrStore(context, DWordSizeLog2, AccessType.LoadZx);
  62. }
  63. public static void Ldrh(ArmEmitterContext context)
  64. {
  65. EmitLoadOrStore(context, HWordSizeLog2, AccessType.LoadZx);
  66. }
  67. public static void Ldrsb(ArmEmitterContext context)
  68. {
  69. EmitLoadOrStore(context, ByteSizeLog2, AccessType.LoadSx);
  70. }
  71. public static void Ldrsh(ArmEmitterContext context)
  72. {
  73. EmitLoadOrStore(context, HWordSizeLog2, AccessType.LoadSx);
  74. }
  75. public static void Stm(ArmEmitterContext context)
  76. {
  77. IOpCode32MemMult op = (IOpCode32MemMult)context.CurrOp;
  78. Operand n = context.Copy(GetIntA32(context, op.Rn));
  79. Operand baseAddress = context.Add(n, Const(op.Offset));
  80. int mask = op.RegisterMask;
  81. int offset = 0;
  82. for (int register = 0; mask != 0; mask >>= 1, register++)
  83. {
  84. if ((mask & 1) != 0)
  85. {
  86. Operand address = context.Add(baseAddress, Const(offset));
  87. EmitStore(context, address, register, WordSizeLog2);
  88. // Note: If Rn is also specified on the register list,
  89. // and Rn is the first register on this list, then the
  90. // value that is written to memory is the unmodified value,
  91. // before the write back. If it is on the list, but it's
  92. // not the first one, then the value written to memory
  93. // varies between CPUs.
  94. if (offset == 0 && op.PostOffset != 0)
  95. {
  96. // Emit write back after the first write.
  97. SetIntA32(context, op.Rn, context.Add(n, Const(op.PostOffset)));
  98. }
  99. offset += 4;
  100. }
  101. }
  102. }
  103. public static void Str(ArmEmitterContext context)
  104. {
  105. EmitLoadOrStore(context, WordSizeLog2, AccessType.Store);
  106. }
  107. public static void Strb(ArmEmitterContext context)
  108. {
  109. EmitLoadOrStore(context, ByteSizeLog2, AccessType.Store);
  110. }
  111. public static void Strd(ArmEmitterContext context)
  112. {
  113. EmitLoadOrStore(context, DWordSizeLog2, AccessType.Store);
  114. }
  115. public static void Strh(ArmEmitterContext context)
  116. {
  117. EmitLoadOrStore(context, HWordSizeLog2, AccessType.Store);
  118. }
  119. private static void EmitLoadOrStore(ArmEmitterContext context, int size, AccessType accType)
  120. {
  121. IOpCode32Mem op = (IOpCode32Mem)context.CurrOp;
  122. Operand n = context.Copy(GetIntA32AlignedPC(context, op.Rn));
  123. Operand m = GetMemM(context, setCarry: false);
  124. Operand temp = default;
  125. if (op.Index || op.WBack)
  126. {
  127. temp = op.Add
  128. ? context.Add (n, m)
  129. : context.Subtract(n, m);
  130. }
  131. if (op.WBack)
  132. {
  133. SetIntA32(context, op.Rn, temp);
  134. }
  135. Operand address;
  136. if (op.Index)
  137. {
  138. address = temp;
  139. }
  140. else
  141. {
  142. address = n;
  143. }
  144. if ((accType & AccessType.Load) != 0)
  145. {
  146. void Load(int rt, int offs, int loadSize)
  147. {
  148. Operand addr = context.Add(address, Const(offs));
  149. if ((accType & AccessType.Signed) != 0)
  150. {
  151. EmitLoadSx32(context, addr, rt, loadSize);
  152. }
  153. else
  154. {
  155. EmitLoadZx(context, addr, rt, loadSize);
  156. }
  157. }
  158. if (size == DWordSizeLog2)
  159. {
  160. Operand lblBigEndian = Label();
  161. Operand lblEnd = Label();
  162. context.BranchIfTrue(lblBigEndian, GetFlag(PState.EFlag));
  163. Load(op.Rt, 0, WordSizeLog2);
  164. Load(op.Rt2, 4, WordSizeLog2);
  165. context.Branch(lblEnd);
  166. context.MarkLabel(lblBigEndian);
  167. Load(op.Rt2, 0, WordSizeLog2);
  168. Load(op.Rt, 4, WordSizeLog2);
  169. context.MarkLabel(lblEnd);
  170. }
  171. else
  172. {
  173. Load(op.Rt, 0, size);
  174. }
  175. }
  176. else
  177. {
  178. void Store(int rt, int offs, int storeSize)
  179. {
  180. Operand addr = context.Add(address, Const(offs));
  181. EmitStore(context, addr, rt, storeSize);
  182. }
  183. if (size == DWordSizeLog2)
  184. {
  185. Operand lblBigEndian = Label();
  186. Operand lblEnd = Label();
  187. context.BranchIfTrue(lblBigEndian, GetFlag(PState.EFlag));
  188. Store(op.Rt, 0, WordSizeLog2);
  189. Store(op.Rt2, 4, WordSizeLog2);
  190. context.Branch(lblEnd);
  191. context.MarkLabel(lblBigEndian);
  192. Store(op.Rt2, 0, WordSizeLog2);
  193. Store(op.Rt, 4, WordSizeLog2);
  194. context.MarkLabel(lblEnd);
  195. }
  196. else
  197. {
  198. Store(op.Rt, 0, size);
  199. }
  200. }
  201. }
  202. public static void Adr(ArmEmitterContext context)
  203. {
  204. IOpCode32Adr op = (IOpCode32Adr)context.CurrOp;
  205. SetIntA32(context, op.Rd, Const(op.Immediate));
  206. }
  207. }
  208. }