DirectCallStubs.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using ARMeilleure.Instructions;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  8. namespace ARMeilleure.Translation
  9. {
  10. static class DirectCallStubs
  11. {
  12. private delegate long GuestFunction(IntPtr nativeContextPtr);
  13. private static IntPtr _directCallStubPtr;
  14. private static IntPtr _directTailCallStubPtr;
  15. private static IntPtr _indirectCallStubPtr;
  16. private static IntPtr _indirectTailCallStubPtr;
  17. private static readonly object _lock = new object();
  18. private static bool _initialized;
  19. public static void InitializeStubs()
  20. {
  21. if (_initialized) return;
  22. lock (_lock)
  23. {
  24. if (_initialized) return;
  25. Translator.PreparePool();
  26. _directCallStubPtr = Marshal.GetFunctionPointerForDelegate<GuestFunction>(GenerateDirectCallStub(false));
  27. _directTailCallStubPtr = Marshal.GetFunctionPointerForDelegate<GuestFunction>(GenerateDirectCallStub(true));
  28. _indirectCallStubPtr = Marshal.GetFunctionPointerForDelegate<GuestFunction>(GenerateIndirectCallStub(false));
  29. _indirectTailCallStubPtr = Marshal.GetFunctionPointerForDelegate<GuestFunction>(GenerateIndirectCallStub(true));
  30. Translator.ResetPool();
  31. Translator.DisposePools();
  32. _initialized = true;
  33. }
  34. }
  35. public static IntPtr DirectCallStub(bool tailCall)
  36. {
  37. Debug.Assert(_initialized);
  38. return tailCall ? _directTailCallStubPtr : _directCallStubPtr;
  39. }
  40. public static IntPtr IndirectCallStub(bool tailCall)
  41. {
  42. Debug.Assert(_initialized);
  43. return tailCall ? _indirectTailCallStubPtr : _indirectCallStubPtr;
  44. }
  45. private static void EmitCall(EmitterContext context, Operand address, bool tailCall)
  46. {
  47. if (tailCall)
  48. {
  49. context.Tailcall(address, context.LoadArgument(OperandType.I64, 0));
  50. }
  51. else
  52. {
  53. context.Return(context.Call(address, OperandType.I64, context.LoadArgument(OperandType.I64, 0)));
  54. }
  55. }
  56. /// <summary>
  57. /// Generates a stub that is used to find function addresses. Used for direct calls when their jump table does not have the host address yet.
  58. /// Takes a NativeContext like a translated guest function, and extracts the target address from the NativeContext.
  59. /// When the target function is compiled in highCq, all table entries are updated to point to that function instead of this stub by the translator.
  60. /// </summary>
  61. private static GuestFunction GenerateDirectCallStub(bool tailCall)
  62. {
  63. EmitterContext context = new EmitterContext();
  64. Operand nativeContextPtr = context.LoadArgument(OperandType.I64, 0);
  65. Operand address = context.Load(OperandType.I64, context.Add(nativeContextPtr, Const((long)NativeContext.GetCallAddressOffset())));
  66. Operand functionAddr = context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFunctionAddress)), address);
  67. EmitCall(context, functionAddr, tailCall);
  68. ControlFlowGraph cfg = context.GetControlFlowGraph();
  69. OperandType[] argTypes = new OperandType[] { OperandType.I64 };
  70. return Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, CompilerOptions.HighCq);
  71. }
  72. /// <summary>
  73. /// Generates a stub that is used to find function addresses and add them to an indirect table.
  74. /// Used for indirect calls entries (already claimed) when their jump table does not have the host address yet.
  75. /// Takes a NativeContext like a translated guest function, and extracts the target indirect table entry from the NativeContext.
  76. /// If the function we find is highCq, the entry in the table is updated to point to that function rather than this stub.
  77. /// </summary>
  78. private static GuestFunction GenerateIndirectCallStub(bool tailCall)
  79. {
  80. EmitterContext context = new EmitterContext();
  81. Operand nativeContextPtr = context.LoadArgument(OperandType.I64, 0);
  82. Operand entryAddress = context.Load(OperandType.I64, context.Add(nativeContextPtr, Const((long)NativeContext.GetCallAddressOffset())));
  83. Operand address = context.Load(OperandType.I64, entryAddress);
  84. // We need to find the missing function. If the function is HighCq, then it replaces this stub in the indirect table.
  85. // Either way, we call it afterwards.
  86. Operand functionAddr = context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetIndirectFunctionAddress)), address, entryAddress);
  87. // Call and save the function.
  88. EmitCall(context, functionAddr, tailCall);
  89. ControlFlowGraph cfg = context.GetControlFlowGraph();
  90. OperandType[] argTypes = new OperandType[] { OperandType.I64 };
  91. return Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, CompilerOptions.HighCq);
  92. }
  93. }
  94. }