InstEmitFlowHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using ARMeilleure.Translation.Cache;
  6. using ARMeilleure.Translation.PTC;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static class InstEmitFlowHelper
  12. {
  13. public static void EmitCondBranch(ArmEmitterContext context, Operand target, Condition cond)
  14. {
  15. if (cond != Condition.Al)
  16. {
  17. context.BranchIfTrue(target, GetCondTrue(context, cond));
  18. }
  19. else
  20. {
  21. context.Branch(target);
  22. }
  23. }
  24. public static Operand GetCondTrue(ArmEmitterContext context, Condition condition)
  25. {
  26. Operand cmpResult = context.TryGetComparisonResult(condition);
  27. if (cmpResult != null)
  28. {
  29. return cmpResult;
  30. }
  31. Operand value = Const(1);
  32. Operand Inverse(Operand val)
  33. {
  34. return context.BitwiseExclusiveOr(val, Const(1));
  35. }
  36. switch (condition)
  37. {
  38. case Condition.Eq:
  39. value = GetFlag(PState.ZFlag);
  40. break;
  41. case Condition.Ne:
  42. value = Inverse(GetFlag(PState.ZFlag));
  43. break;
  44. case Condition.GeUn:
  45. value = GetFlag(PState.CFlag);
  46. break;
  47. case Condition.LtUn:
  48. value = Inverse(GetFlag(PState.CFlag));
  49. break;
  50. case Condition.Mi:
  51. value = GetFlag(PState.NFlag);
  52. break;
  53. case Condition.Pl:
  54. value = Inverse(GetFlag(PState.NFlag));
  55. break;
  56. case Condition.Vs:
  57. value = GetFlag(PState.VFlag);
  58. break;
  59. case Condition.Vc:
  60. value = Inverse(GetFlag(PState.VFlag));
  61. break;
  62. case Condition.GtUn:
  63. {
  64. Operand c = GetFlag(PState.CFlag);
  65. Operand z = GetFlag(PState.ZFlag);
  66. value = context.BitwiseAnd(c, Inverse(z));
  67. break;
  68. }
  69. case Condition.LeUn:
  70. {
  71. Operand c = GetFlag(PState.CFlag);
  72. Operand z = GetFlag(PState.ZFlag);
  73. value = context.BitwiseOr(Inverse(c), z);
  74. break;
  75. }
  76. case Condition.Ge:
  77. {
  78. Operand n = GetFlag(PState.NFlag);
  79. Operand v = GetFlag(PState.VFlag);
  80. value = context.ICompareEqual(n, v);
  81. break;
  82. }
  83. case Condition.Lt:
  84. {
  85. Operand n = GetFlag(PState.NFlag);
  86. Operand v = GetFlag(PState.VFlag);
  87. value = context.ICompareNotEqual(n, v);
  88. break;
  89. }
  90. case Condition.Gt:
  91. {
  92. Operand n = GetFlag(PState.NFlag);
  93. Operand z = GetFlag(PState.ZFlag);
  94. Operand v = GetFlag(PState.VFlag);
  95. value = context.BitwiseAnd(Inverse(z), context.ICompareEqual(n, v));
  96. break;
  97. }
  98. case Condition.Le:
  99. {
  100. Operand n = GetFlag(PState.NFlag);
  101. Operand z = GetFlag(PState.ZFlag);
  102. Operand v = GetFlag(PState.VFlag);
  103. value = context.BitwiseOr(z, context.ICompareNotEqual(n, v));
  104. break;
  105. }
  106. }
  107. return value;
  108. }
  109. public static void EmitCall(ArmEmitterContext context, ulong immediate)
  110. {
  111. bool isRecursive = immediate == context.EntryAddress;
  112. EmitJumpTableBranch(context, Const(immediate), isRecursive);
  113. }
  114. private static void EmitNativeCall(ArmEmitterContext context, Operand nativeContextPtr, Operand funcAddr, bool isJump = false)
  115. {
  116. context.StoreToContext();
  117. if (isJump)
  118. {
  119. context.Tailcall(funcAddr, nativeContextPtr);
  120. }
  121. else
  122. {
  123. OpCode op = context.CurrOp;
  124. Operand returnAddress = context.Call(funcAddr, OperandType.I64, nativeContextPtr);
  125. context.LoadFromContext();
  126. // Note: The return value of a translated function is always an Int64 with the address execution has
  127. // returned to. We expect this address to be immediately after the current instruction, if it isn't we
  128. // keep returning until we reach the dispatcher.
  129. Operand nextAddr = Const((long)op.Address + op.OpCodeSizeInBytes);
  130. // Try to continue within this block.
  131. // If the return address isn't to our next instruction, we need to return so the JIT can figure out
  132. // what to do.
  133. Operand lblContinue = context.GetLabel(nextAddr.Value);
  134. // We need to clear out the call flag for the return address before comparing it.
  135. context.BranchIf(lblContinue, returnAddress, nextAddr, Comparison.Equal, BasicBlockFrequency.Cold);
  136. context.Return(returnAddress);
  137. }
  138. }
  139. private static void EmitNativeCall(ArmEmitterContext context, Operand funcAddr, bool isJump = false)
  140. {
  141. EmitNativeCall(context, context.LoadArgument(OperandType.I64, 0), funcAddr, isJump);
  142. }
  143. public static void EmitVirtualCall(ArmEmitterContext context, Operand target)
  144. {
  145. EmitVirtualCallOrJump(context, target, isJump: false);
  146. }
  147. public static void EmitVirtualJump(ArmEmitterContext context, Operand target, bool isReturn)
  148. {
  149. EmitVirtualCallOrJump(context, target, isJump: true, isReturn: isReturn);
  150. }
  151. private static void EmitVirtualCallOrJump(ArmEmitterContext context, Operand target, bool isJump, bool isReturn = false)
  152. {
  153. if (isReturn)
  154. {
  155. context.Return(target);
  156. }
  157. else
  158. {
  159. EmitJumpTableBranch(context, target, isJump);
  160. }
  161. }
  162. public static void EmitTailContinue(ArmEmitterContext context, Operand address, bool allowRejit)
  163. {
  164. // Left option here as it may be useful if we need to return to managed rather than tail call in future.
  165. // (eg. for debug)
  166. bool useTailContinue = true;
  167. if (useTailContinue)
  168. {
  169. if (context.HighCq)
  170. {
  171. // If we're doing a tail continue in HighCq, reserve a space in the jump table to avoid calling back
  172. // to the translator. This will always try to get a HighCq version of our continue target as well.
  173. EmitJumpTableBranch(context, address, true);
  174. }
  175. else
  176. {
  177. Operand fallbackAddr = context.Call(typeof(NativeInterface).GetMethod(allowRejit
  178. ? nameof(NativeInterface.GetFunctionAddress)
  179. : nameof(NativeInterface.GetFunctionAddressWithoutRejit)), address);
  180. EmitNativeCall(context, fallbackAddr, true);
  181. }
  182. }
  183. else
  184. {
  185. context.Return(address);
  186. }
  187. }
  188. private static void EmitNativeCallWithGuestAddress(ArmEmitterContext context, Operand funcAddr, Operand guestAddress, bool isJump)
  189. {
  190. Operand nativeContextPtr = context.LoadArgument(OperandType.I64, 0);
  191. context.Store(context.Add(nativeContextPtr, Const((long)NativeContext.GetCallAddressOffset())), guestAddress);
  192. EmitNativeCall(context, nativeContextPtr, funcAddr, isJump);
  193. }
  194. private static void EmitBranchFallback(ArmEmitterContext context, Operand address, bool isJump)
  195. {
  196. Operand fallbackAddr = context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFunctionAddress)), address);
  197. EmitNativeCall(context, fallbackAddr, isJump);
  198. }
  199. public static void EmitDynamicTableCall(ArmEmitterContext context, Operand tableAddress, Operand address, bool isJump)
  200. {
  201. // Loop over elements of the dynamic table. Unrolled loop.
  202. Operand endLabel = Label();
  203. Operand fallbackLabel = Label();
  204. void EmitTableEntry(Operand entrySkipLabel)
  205. {
  206. // Try to take this entry in the table if its guest address equals 0.
  207. Operand gotResult = context.CompareAndSwap(tableAddress, Const(0L), address);
  208. // Is the address ours? (either taken via CompareAndSwap (0), or what was already here)
  209. context.BranchIfFalse(entrySkipLabel,
  210. context.BitwiseOr(
  211. context.ICompareEqual(gotResult, address),
  212. context.ICompareEqual(gotResult, Const(0L)))
  213. );
  214. // It's ours, so what function is it pointing to?
  215. Operand targetFunctionPtr = context.Add(tableAddress, Const(8L));
  216. Operand targetFunction = context.Load(OperandType.I64, targetFunctionPtr);
  217. // Call the function.
  218. // We pass in the entry address as the guest address, as the entry may need to be updated by the
  219. // indirect call stub.
  220. EmitNativeCallWithGuestAddress(context, targetFunction, tableAddress, isJump);
  221. context.Branch(endLabel);
  222. }
  223. // Currently this uses a size of 1, as higher values inflate code size for no real benefit.
  224. for (int i = 0; i < JumpTable.DynamicTableElems; i++)
  225. {
  226. if (i == JumpTable.DynamicTableElems - 1)
  227. {
  228. // If this is the last entry, avoid emitting the additional label and add.
  229. EmitTableEntry(fallbackLabel);
  230. }
  231. else
  232. {
  233. Operand nextLabel = Label();
  234. EmitTableEntry(nextLabel);
  235. context.MarkLabel(nextLabel);
  236. // Move to the next table entry.
  237. tableAddress = context.Add(tableAddress, Const((long)JumpTable.JumpTableStride));
  238. }
  239. }
  240. context.MarkLabel(fallbackLabel);
  241. EmitBranchFallback(context, address, isJump);
  242. context.MarkLabel(endLabel);
  243. }
  244. public static void EmitJumpTableBranch(ArmEmitterContext context, Operand address, bool isJump = false)
  245. {
  246. if (address.Type == OperandType.I32)
  247. {
  248. address = context.ZeroExtend32(OperandType.I64, address);
  249. }
  250. // TODO: Constant folding. Indirect calls are slower in the best case and emit more code so we want to
  251. // avoid them when possible.
  252. bool isConst = address.Kind == OperandKind.Constant;
  253. ulong constAddr = address.Value;
  254. if (!context.HighCq)
  255. {
  256. // Don't emit indirect calls or jumps if we're compiling in lowCq mode. This avoids wasting space on the
  257. // jump and indirect tables. Just ask the translator for the function address.
  258. EmitBranchFallback(context, address, isJump);
  259. }
  260. else if (!isConst)
  261. {
  262. // Virtual branch/call - store first used addresses on a small table for fast lookup.
  263. int entry = context.JumpTable.ReserveDynamicEntry(context.EntryAddress, isJump);
  264. int jumpOffset = entry * JumpTable.JumpTableStride * JumpTable.DynamicTableElems;
  265. Operand dynTablePtr;
  266. if (Ptc.State == PtcState.Disabled)
  267. {
  268. dynTablePtr = Const(context.JumpTable.DynamicPointer.ToInt64() + jumpOffset);
  269. }
  270. else
  271. {
  272. dynTablePtr = Const(context.JumpTable.DynamicPointer.ToInt64(), true, Ptc.DynamicPointerIndex);
  273. dynTablePtr = context.Add(dynTablePtr, Const((long)jumpOffset));
  274. }
  275. EmitDynamicTableCall(context, dynTablePtr, address, isJump);
  276. }
  277. else
  278. {
  279. int entry = context.JumpTable.ReserveTableEntry(context.EntryAddress, constAddr, isJump);
  280. int jumpOffset = entry * JumpTable.JumpTableStride + 8; // Offset directly to the host address.
  281. Operand tableEntryPtr;
  282. if (Ptc.State == PtcState.Disabled)
  283. {
  284. tableEntryPtr = Const(context.JumpTable.JumpPointer.ToInt64() + jumpOffset);
  285. }
  286. else
  287. {
  288. tableEntryPtr = Const(context.JumpTable.JumpPointer.ToInt64(), true, Ptc.JumpPointerIndex);
  289. tableEntryPtr = context.Add(tableEntryPtr, Const((long)jumpOffset));
  290. }
  291. Operand funcAddr = context.Load(OperandType.I64, tableEntryPtr);
  292. // Call the function directly. If it's not present yet, this will call the direct call stub.
  293. EmitNativeCallWithGuestAddress(context, funcAddr, address, isJump);
  294. }
  295. }
  296. }
  297. }