InstEmitMemory32.cs 7.6 KB

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