DirectCallStubs.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using ARMeilleure.Instructions;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  7. namespace ARMeilleure.Translation
  8. {
  9. static class DirectCallStubs
  10. {
  11. private delegate long GuestFunction(IntPtr nativeContextPtr);
  12. private static GuestFunction _directCallStub;
  13. private static GuestFunction _directTailCallStub;
  14. private static GuestFunction _indirectCallStub;
  15. private static GuestFunction _indirectTailCallStub;
  16. private static readonly object _lock = new object();
  17. private static bool _initialized;
  18. public static void InitializeStubs()
  19. {
  20. if (_initialized) return;
  21. lock (_lock)
  22. {
  23. if (_initialized) return;
  24. _directCallStub = GenerateDirectCallStub(false);
  25. _directTailCallStub = GenerateDirectCallStub(true);
  26. _indirectCallStub = GenerateIndirectCallStub(false);
  27. _indirectTailCallStub = GenerateIndirectCallStub(true);
  28. _initialized = true;
  29. }
  30. }
  31. public static IntPtr DirectCallStub(bool tailCall)
  32. {
  33. return Marshal.GetFunctionPointerForDelegate(tailCall ? _directTailCallStub : _directCallStub);
  34. }
  35. public static IntPtr IndirectCallStub(bool tailCall)
  36. {
  37. return Marshal.GetFunctionPointerForDelegate(tailCall ? _indirectTailCallStub : _indirectCallStub);
  38. }
  39. private static void EmitCall(EmitterContext context, Operand address, bool tailCall)
  40. {
  41. if (tailCall)
  42. {
  43. context.Tailcall(address, context.LoadArgument(OperandType.I64, 0));
  44. }
  45. else
  46. {
  47. context.Return(context.Call(address, OperandType.I64, context.LoadArgument(OperandType.I64, 0)));
  48. }
  49. }
  50. /// <summary>
  51. /// 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.
  52. /// Takes a NativeContext like a translated guest function, and extracts the target address from the NativeContext.
  53. /// 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.
  54. /// </summary>
  55. private static GuestFunction GenerateDirectCallStub(bool tailCall)
  56. {
  57. EmitterContext context = new EmitterContext();
  58. Operand nativeContextPtr = context.LoadArgument(OperandType.I64, 0);
  59. Operand address = context.Load(OperandType.I64, context.Add(nativeContextPtr, Const((long)NativeContext.GetCallAddressOffset())));
  60. address = context.BitwiseOr(address, Const(address.Type, 1)); // Set call flag.
  61. Operand functionAddr = context.Call(new _U64_U64(NativeInterface.GetFunctionAddress), address);
  62. EmitCall(context, functionAddr, tailCall);
  63. ControlFlowGraph cfg = context.GetControlFlowGraph();
  64. OperandType[] argTypes = new OperandType[]
  65. {
  66. OperandType.I64
  67. };
  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(new _U64_U64_U64(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[]
  89. {
  90. OperandType.I64
  91. };
  92. return Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, CompilerOptions.HighCq);
  93. }
  94. }
  95. }