OrcRTBootstrap.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===------------------------ OrcRTBootstrap.cpp --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "OrcRTBootstrap.h"
  9. #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
  10. #include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
  11. #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
  12. #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
  13. #define DEBUG_TYPE "orc"
  14. using namespace llvm::orc::shared;
  15. namespace llvm {
  16. namespace orc {
  17. namespace rt_bootstrap {
  18. template <typename WriteT, typename SPSWriteT>
  19. static llvm::orc::shared::CWrapperFunctionResult
  20. writeUIntsWrapper(const char *ArgData, size_t ArgSize) {
  21. return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
  22. ArgData, ArgSize,
  23. [](std::vector<WriteT> Ws) {
  24. for (auto &W : Ws)
  25. *W.Addr.template toPtr<decltype(W.Value) *>() = W.Value;
  26. })
  27. .release();
  28. }
  29. static llvm::orc::shared::CWrapperFunctionResult
  30. writeBuffersWrapper(const char *ArgData, size_t ArgSize) {
  31. return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle(
  32. ArgData, ArgSize,
  33. [](std::vector<tpctypes::BufferWrite> Ws) {
  34. for (auto &W : Ws)
  35. memcpy(W.Addr.template toPtr<char *>(), W.Buffer.data(),
  36. W.Buffer.size());
  37. })
  38. .release();
  39. }
  40. static llvm::orc::shared::CWrapperFunctionResult
  41. runAsMainWrapper(const char *ArgData, size_t ArgSize) {
  42. return WrapperFunction<rt::SPSRunAsMainSignature>::handle(
  43. ArgData, ArgSize,
  44. [](ExecutorAddr MainAddr,
  45. std::vector<std::string> Args) -> int64_t {
  46. return runAsMain(MainAddr.toPtr<int (*)(int, char *[])>(), Args);
  47. })
  48. .release();
  49. }
  50. static llvm::orc::shared::CWrapperFunctionResult
  51. runAsVoidFunctionWrapper(const char *ArgData, size_t ArgSize) {
  52. return WrapperFunction<rt::SPSRunAsVoidFunctionSignature>::handle(
  53. ArgData, ArgSize,
  54. [](ExecutorAddr MainAddr) -> int32_t {
  55. return runAsVoidFunction(MainAddr.toPtr<int32_t (*)(void)>());
  56. })
  57. .release();
  58. }
  59. static llvm::orc::shared::CWrapperFunctionResult
  60. runAsIntFunctionWrapper(const char *ArgData, size_t ArgSize) {
  61. return WrapperFunction<rt::SPSRunAsIntFunctionSignature>::handle(
  62. ArgData, ArgSize,
  63. [](ExecutorAddr MainAddr, int32_t Arg) -> int32_t {
  64. return runAsIntFunction(MainAddr.toPtr<int32_t (*)(int32_t)>(),
  65. Arg);
  66. })
  67. .release();
  68. }
  69. void addTo(StringMap<ExecutorAddr> &M) {
  70. M[rt::MemoryWriteUInt8sWrapperName] = ExecutorAddr::fromPtr(
  71. &writeUIntsWrapper<tpctypes::UInt8Write,
  72. shared::SPSMemoryAccessUInt8Write>);
  73. M[rt::MemoryWriteUInt16sWrapperName] = ExecutorAddr::fromPtr(
  74. &writeUIntsWrapper<tpctypes::UInt16Write,
  75. shared::SPSMemoryAccessUInt16Write>);
  76. M[rt::MemoryWriteUInt32sWrapperName] = ExecutorAddr::fromPtr(
  77. &writeUIntsWrapper<tpctypes::UInt32Write,
  78. shared::SPSMemoryAccessUInt32Write>);
  79. M[rt::MemoryWriteUInt64sWrapperName] = ExecutorAddr::fromPtr(
  80. &writeUIntsWrapper<tpctypes::UInt64Write,
  81. shared::SPSMemoryAccessUInt64Write>);
  82. M[rt::MemoryWriteBuffersWrapperName] =
  83. ExecutorAddr::fromPtr(&writeBuffersWrapper);
  84. M[rt::RegisterEHFrameSectionWrapperName] =
  85. ExecutorAddr::fromPtr(&llvm_orc_registerEHFrameSectionWrapper);
  86. M[rt::DeregisterEHFrameSectionWrapperName] =
  87. ExecutorAddr::fromPtr(&llvm_orc_deregisterEHFrameSectionWrapper);
  88. M[rt::RunAsMainWrapperName] = ExecutorAddr::fromPtr(&runAsMainWrapper);
  89. M[rt::RunAsVoidFunctionWrapperName] =
  90. ExecutorAddr::fromPtr(&runAsVoidFunctionWrapper);
  91. M[rt::RunAsIntFunctionWrapperName] =
  92. ExecutorAddr::fromPtr(&runAsIntFunctionWrapper);
  93. }
  94. } // end namespace rt_bootstrap
  95. } // end namespace orc
  96. } // end namespace llvm