DirectCallStubs.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. _directCallStubPtr = Marshal.GetFunctionPointerForDelegate<GuestFunction>(GenerateDirectCallStub(false));
  26. _directTailCallStubPtr = Marshal.GetFunctionPointerForDelegate<GuestFunction>(GenerateDirectCallStub(true));
  27. _indirectCallStubPtr = Marshal.GetFunctionPointerForDelegate<GuestFunction>(GenerateIndirectCallStub(false));
  28. _indirectTailCallStubPtr = Marshal.GetFunctionPointerForDelegate<GuestFunction>(GenerateIndirectCallStub(true));
  29. _initialized = true;
  30. }
  31. }
  32. public static IntPtr DirectCallStub(bool tailCall)
  33. {
  34. Debug.Assert(_initialized);
  35. return tailCall ? _directTailCallStubPtr : _directCallStubPtr;
  36. }
  37. public static IntPtr IndirectCallStub(bool tailCall)
  38. {
  39. Debug.Assert(_initialized);
  40. return tailCall ? _indirectTailCallStubPtr : _indirectCallStubPtr;
  41. }
  42. private static void EmitCall(EmitterContext context, Operand address, bool tailCall)
  43. {
  44. if (tailCall)
  45. {
  46. context.Tailcall(address, context.LoadArgument(OperandType.I64, 0));
  47. }
  48. else
  49. {
  50. context.Return(context.Call(address, OperandType.I64, context.LoadArgument(OperandType.I64, 0)));
  51. }
  52. }
  53. /// <summary>
  54. /// 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.
  55. /// Takes a NativeContext like a translated guest function, and extracts the target address from the NativeContext.
  56. /// 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.
  57. /// </summary>
  58. private static GuestFunction GenerateDirectCallStub(bool tailCall)
  59. {
  60. EmitterContext context = new EmitterContext();
  61. Operand nativeContextPtr = context.LoadArgument(OperandType.I64, 0);
  62. Operand address = context.Load(OperandType.I64, context.Add(nativeContextPtr, Const((long)NativeContext.GetCallAddressOffset())));
  63. address = context.BitwiseOr(address, Const(address.Type, 1)); // Set call flag.
  64. Operand functionAddr = context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFunctionAddress)), address);
  65. EmitCall(context, functionAddr, tailCall);
  66. ControlFlowGraph cfg = context.GetControlFlowGraph();
  67. OperandType[] argTypes = new OperandType[] { OperandType.I64 };
  68. return Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, CompilerOptions.HighCq);
  69. }
  70. /// <summary>
  71. /// Generates a stub that is used to find function addresses and add them to an indirect table.
  72. /// Used for indirect calls entries (already claimed) when their jump table does not have the host address yet.
  73. /// Takes a NativeContext like a translated guest function, and extracts the target indirect table entry from the NativeContext.
  74. /// If the function we find is highCq, the entry in the table is updated to point to that function rather than this stub.
  75. /// </summary>
  76. private static GuestFunction GenerateIndirectCallStub(bool tailCall)
  77. {
  78. EmitterContext context = new EmitterContext();
  79. Operand nativeContextPtr = context.LoadArgument(OperandType.I64, 0);
  80. Operand entryAddress = context.Load(OperandType.I64, context.Add(nativeContextPtr, Const((long)NativeContext.GetCallAddressOffset())));
  81. Operand address = context.Load(OperandType.I64, entryAddress);
  82. // We need to find the missing function. If the function is HighCq, then it replaces this stub in the indirect table.
  83. // Either way, we call it afterwards.
  84. Operand functionAddr = context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetIndirectFunctionAddress)), address, entryAddress);
  85. // Call and save the function.
  86. EmitCall(context, functionAddr, tailCall);
  87. ControlFlowGraph cfg = context.GetControlFlowGraph();
  88. OperandType[] argTypes = new OperandType[] { OperandType.I64 };
  89. return Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, CompilerOptions.HighCq);
  90. }
  91. }
  92. }