InstEmitFlowHelper.cs 14 KB

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