InstEmitFlowHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 (allowRejit)
  180. {
  181. address = context.BitwiseOr(address, Const(1L));
  182. }
  183. Operand fallbackAddr = context.Call(new _U64_U64(NativeInterface.GetFunctionAddress), address);
  184. EmitNativeCall(context, fallbackAddr, true);
  185. }
  186. else
  187. {
  188. context.Return(address);
  189. }
  190. }
  191. private static void EmitNativeCallWithGuestAddress(ArmEmitterContext context, Operand funcAddr, Operand guestAddress, bool isJump)
  192. {
  193. Operand nativeContextPtr = context.LoadArgument(OperandType.I64, 0);
  194. context.Store(context.Add(nativeContextPtr, Const(NativeContext.GetCallAddressOffset())), guestAddress);
  195. EmitNativeCall(context, nativeContextPtr, funcAddr, isJump);
  196. }
  197. private static void EmitBranchFallback(ArmEmitterContext context, Operand address, bool isJump)
  198. {
  199. address = context.BitwiseOr(address, Const(address.Type, (long)CallFlag)); // Set call flag.
  200. Operand fallbackAddr = context.Call(new _U64_U64(NativeInterface.GetFunctionAddress), address);
  201. EmitNativeCall(context, fallbackAddr, isJump);
  202. }
  203. public static void EmitDynamicTableCall(ArmEmitterContext context, Operand tableAddress, Operand address, bool isJump)
  204. {
  205. // Loop over elements of the dynamic table. Unrolled loop.
  206. Operand endLabel = Label();
  207. Operand fallbackLabel = Label();
  208. Action<Operand> emitTableEntry = (Operand entrySkipLabel) =>
  209. {
  210. // Try to take this entry in the table if its guest address equals 0.
  211. Operand gotResult = context.CompareAndSwap(tableAddress, Const(0L), address);
  212. // Is the address ours? (either taken via CompareAndSwap (0), or what was already here)
  213. context.BranchIfFalse(entrySkipLabel, context.BitwiseOr(context.ICompareEqual(gotResult, address), context.ICompareEqual(gotResult, Const(0L))));
  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 indirect call stub.
  219. EmitNativeCallWithGuestAddress(context, targetFunction, tableAddress, isJump);
  220. context.Branch(endLabel);
  221. };
  222. // Currently this uses a size of 1, as higher values inflate code size for no real benefit.
  223. for (int i = 0; i < JumpTable.DynamicTableElems; i++)
  224. {
  225. if (i == JumpTable.DynamicTableElems - 1)
  226. {
  227. emitTableEntry(fallbackLabel); // If this is the last entry, avoid emitting the additional label and add.
  228. }
  229. else
  230. {
  231. Operand nextLabel = Label();
  232. emitTableEntry(nextLabel);
  233. context.MarkLabel(nextLabel);
  234. tableAddress = context.Add(tableAddress, Const((long)JumpTable.JumpTableStride)); // Move to the next table entry.
  235. }
  236. }
  237. context.MarkLabel(fallbackLabel);
  238. EmitBranchFallback(context, address, isJump);
  239. context.MarkLabel(endLabel);
  240. }
  241. public static void EmitJumpTableBranch(ArmEmitterContext context, Operand address, bool isJump = false)
  242. {
  243. if (address.Type == OperandType.I32)
  244. {
  245. address = context.ZeroExtend32(OperandType.I64, address);
  246. }
  247. // TODO: Constant folding. Indirect calls are slower in the best case and emit more code so we want to avoid them when possible.
  248. bool isConst = address.Kind == OperandKind.Constant;
  249. long constAddr = (long)address.Value;
  250. if (!context.HighCq)
  251. {
  252. // Don't emit indirect calls or jumps if we're compiling in lowCq mode.
  253. // This avoids wasting space on the jump and indirect tables.
  254. // Just ask the translator for the function address.
  255. EmitBranchFallback(context, address, isJump);
  256. }
  257. else if (!isConst)
  258. {
  259. // Virtual branch/call - store first used addresses on a small table for fast lookup.
  260. int entry = context.JumpTable.ReserveDynamicEntry(isJump);
  261. int jumpOffset = entry * JumpTable.JumpTableStride * JumpTable.DynamicTableElems;
  262. Operand dynTablePtr = Const(context.JumpTable.DynamicPointer.ToInt64() + jumpOffset);
  263. EmitDynamicTableCall(context, dynTablePtr, address, isJump);
  264. }
  265. else
  266. {
  267. int entry = context.JumpTable.ReserveTableEntry(context.BaseAddress & (~3L), constAddr, isJump);
  268. int jumpOffset = entry * JumpTable.JumpTableStride + 8; // Offset directly to the host address.
  269. // 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.
  270. Operand tableEntryPtr = Const(context.JumpTable.JumpPointer.ToInt64() + jumpOffset);
  271. Operand funcAddr = context.Load(OperandType.I64, tableEntryPtr);
  272. EmitNativeCallWithGuestAddress(context, funcAddr, address, isJump); // Call the function directly. If it's not present yet, this will call the direct call stub.
  273. }
  274. }
  275. }
  276. }