InstEmitFlowHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.IntermediateRepresentation.OperandHelper;
  8. namespace ARMeilleure.Instructions
  9. {
  10. static class InstEmitFlowHelper
  11. {
  12. public const ulong CallFlag = 1;
  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. EmitJumpTableBranch(context, Const(immediate));
  112. }
  113. private static void EmitNativeCall(ArmEmitterContext context, Operand nativeContextPtr, Operand funcAddr, bool isJump = false)
  114. {
  115. context.StoreToContext();
  116. Operand returnAddress;
  117. if (isJump)
  118. {
  119. context.Tailcall(funcAddr, nativeContextPtr);
  120. }
  121. else
  122. {
  123. returnAddress = context.Call(funcAddr, OperandType.I64, nativeContextPtr);
  124. context.LoadFromContext();
  125. EmitContinueOrReturnCheck(context, returnAddress);
  126. }
  127. }
  128. private static void EmitNativeCall(ArmEmitterContext context, Operand funcAddr, bool isJump = false)
  129. {
  130. EmitNativeCall(context, context.LoadArgument(OperandType.I64, 0), funcAddr, isJump);
  131. }
  132. public static void EmitVirtualCall(ArmEmitterContext context, Operand target)
  133. {
  134. EmitVirtualCallOrJump(context, target, isJump: false);
  135. }
  136. public static void EmitVirtualJump(ArmEmitterContext context, Operand target, bool isReturn)
  137. {
  138. EmitVirtualCallOrJump(context, target, isJump: true, isReturn: isReturn);
  139. }
  140. private static void EmitVirtualCallOrJump(ArmEmitterContext context, Operand target, bool isJump, bool isReturn = false)
  141. {
  142. if (isReturn)
  143. {
  144. context.Return(target);
  145. }
  146. else
  147. {
  148. EmitJumpTableBranch(context, target, isJump);
  149. }
  150. }
  151. private static void EmitContinueOrReturnCheck(ArmEmitterContext context, Operand returnAddress)
  152. {
  153. // Note: The return value of a translated function is always an Int64 with the
  154. // address execution has returned to. We expect this address to be immediately after the
  155. // current instruction, if it isn't we keep returning until we reach the dispatcher.
  156. Operand nextAddr = Const(GetNextOpAddress(context.CurrOp));
  157. // Try to continue within this block.
  158. // If the return address isn't to our next instruction, we need to return so the JIT can figure out what to do.
  159. Operand lblContinue = Label();
  160. // We need to clear out the call flag for the return address before comparing it.
  161. context.BranchIfTrue(lblContinue, context.ICompareEqual(context.BitwiseAnd(returnAddress, Const(~CallFlag)), nextAddr));
  162. context.Return(returnAddress);
  163. context.MarkLabel(lblContinue);
  164. if (context.CurrBlock.Next == null)
  165. {
  166. // No code following this instruction, try and find the next block and jump to it.
  167. EmitTailContinue(context, nextAddr);
  168. }
  169. }
  170. private static ulong GetNextOpAddress(OpCode op)
  171. {
  172. return op.Address + (ulong)op.OpCodeSizeInBytes;
  173. }
  174. public static void EmitTailContinue(ArmEmitterContext context, Operand address, bool allowRejit = false)
  175. {
  176. 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)
  177. if (useTailContinue)
  178. {
  179. if (context.HighCq)
  180. {
  181. // If we're doing a tail continue in HighCq, reserve a space in the jump table to avoid calling back to the translator.
  182. // This will always try to get a HighCq version of our continue target as well.
  183. EmitJumpTableBranch(context, address, true);
  184. }
  185. else
  186. {
  187. if (allowRejit)
  188. {
  189. address = context.BitwiseOr(address, Const(CallFlag));
  190. }
  191. Operand fallbackAddr = context.Call(new _U64_U64(NativeInterface.GetFunctionAddress), address);
  192. EmitNativeCall(context, fallbackAddr, true);
  193. }
  194. }
  195. else
  196. {
  197. context.Return(address);
  198. }
  199. }
  200. private static void EmitNativeCallWithGuestAddress(ArmEmitterContext context, Operand funcAddr, Operand guestAddress, bool isJump)
  201. {
  202. Operand nativeContextPtr = context.LoadArgument(OperandType.I64, 0);
  203. context.Store(context.Add(nativeContextPtr, Const(NativeContext.GetCallAddressOffset())), guestAddress);
  204. EmitNativeCall(context, nativeContextPtr, funcAddr, isJump);
  205. }
  206. private static void EmitBranchFallback(ArmEmitterContext context, Operand address, bool isJump)
  207. {
  208. address = context.BitwiseOr(address, Const(address.Type, (long)CallFlag)); // Set call flag.
  209. Operand fallbackAddr = context.Call(new _U64_U64(NativeInterface.GetFunctionAddress), address);
  210. EmitNativeCall(context, fallbackAddr, isJump);
  211. }
  212. public static void EmitDynamicTableCall(ArmEmitterContext context, Operand tableAddress, Operand address, bool isJump)
  213. {
  214. // Loop over elements of the dynamic table. Unrolled loop.
  215. Operand endLabel = Label();
  216. Operand fallbackLabel = Label();
  217. Action<Operand> emitTableEntry = (Operand entrySkipLabel) =>
  218. {
  219. // Try to take this entry in the table if its guest address equals 0.
  220. Operand gotResult = context.CompareAndSwap(tableAddress, Const(0L), address);
  221. // Is the address ours? (either taken via CompareAndSwap (0), or what was already here)
  222. context.BranchIfFalse(entrySkipLabel, context.BitwiseOr(context.ICompareEqual(gotResult, address), context.ICompareEqual(gotResult, Const(0L))));
  223. // It's ours, so what function is it pointing to?
  224. Operand targetFunctionPtr = context.Add(tableAddress, Const(8L));
  225. Operand targetFunction = context.Load(OperandType.I64, targetFunctionPtr);
  226. // Call the function.
  227. // We pass in the entry address as the guest address, as the entry may need to be updated by the indirect call stub.
  228. EmitNativeCallWithGuestAddress(context, targetFunction, tableAddress, isJump);
  229. context.Branch(endLabel);
  230. };
  231. // Currently this uses a size of 1, as higher values inflate code size for no real benefit.
  232. for (int i = 0; i < JumpTable.DynamicTableElems; i++)
  233. {
  234. if (i == JumpTable.DynamicTableElems - 1)
  235. {
  236. emitTableEntry(fallbackLabel); // If this is the last entry, avoid emitting the additional label and add.
  237. }
  238. else
  239. {
  240. Operand nextLabel = Label();
  241. emitTableEntry(nextLabel);
  242. context.MarkLabel(nextLabel);
  243. tableAddress = context.Add(tableAddress, Const((long)JumpTable.JumpTableStride)); // Move to the next table entry.
  244. }
  245. }
  246. context.MarkLabel(fallbackLabel);
  247. EmitBranchFallback(context, address, isJump);
  248. context.MarkLabel(endLabel);
  249. }
  250. public static void EmitJumpTableBranch(ArmEmitterContext context, Operand address, bool isJump = false)
  251. {
  252. if (address.Type == OperandType.I32)
  253. {
  254. address = context.ZeroExtend32(OperandType.I64, address);
  255. }
  256. // TODO: Constant folding. Indirect calls are slower in the best case and emit more code so we want to avoid them when possible.
  257. bool isConst = address.Kind == OperandKind.Constant;
  258. long constAddr = (long)address.Value;
  259. if (!context.HighCq)
  260. {
  261. // Don't emit indirect calls or jumps if we're compiling in lowCq mode.
  262. // This avoids wasting space on the jump and indirect tables.
  263. // Just ask the translator for the function address.
  264. EmitBranchFallback(context, address, isJump);
  265. }
  266. else if (!isConst)
  267. {
  268. // Virtual branch/call - store first used addresses on a small table for fast lookup.
  269. int entry = context.JumpTable.ReserveDynamicEntry(isJump);
  270. int jumpOffset = entry * JumpTable.JumpTableStride * JumpTable.DynamicTableElems;
  271. Operand dynTablePtr = Const(context.JumpTable.DynamicPointer.ToInt64() + jumpOffset);
  272. EmitDynamicTableCall(context, dynTablePtr, address, isJump);
  273. }
  274. else
  275. {
  276. int entry = context.JumpTable.ReserveTableEntry(context.BaseAddress & (~3L), constAddr, isJump);
  277. int jumpOffset = entry * JumpTable.JumpTableStride + 8; // Offset directly to the host address.
  278. // TODO: Relocatable jump table ptr for AOT. Would prefer a solution to patch this constant into functions as they are loaded rather than calculate at runtime.
  279. Operand tableEntryPtr = Const(context.JumpTable.JumpPointer.ToInt64() + jumpOffset);
  280. Operand funcAddr = context.Load(OperandType.I64, tableEntryPtr);
  281. EmitNativeCallWithGuestAddress(context, funcAddr, address, isJump); // Call the function directly. If it's not present yet, this will call the direct call stub.
  282. }
  283. }
  284. }
  285. }