DirectCallStubs.cs 5.2 KB

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