xref: /llvm-project/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.cpp (revision 8201ae2aa662a1bcba80751f3ef162f228f626f7)
12c8e7849SLang Hames //===------------------------ OrcRTBootstrap.cpp --------------------------===//
22c8e7849SLang Hames //
32c8e7849SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42c8e7849SLang Hames // See https://llvm.org/LICENSE.txt for license information.
52c8e7849SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62c8e7849SLang Hames //
72c8e7849SLang Hames //===----------------------------------------------------------------------===//
82c8e7849SLang Hames 
92c8e7849SLang Hames #include "OrcRTBootstrap.h"
102c8e7849SLang Hames 
112c8e7849SLang Hames #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
122c8e7849SLang Hames #include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
136498b0e9SLang Hames #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
142c8e7849SLang Hames #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
152c8e7849SLang Hames 
162c8e7849SLang Hames #define DEBUG_TYPE "orc"
172c8e7849SLang Hames 
182c8e7849SLang Hames using namespace llvm::orc::shared;
192c8e7849SLang Hames 
202c8e7849SLang Hames namespace llvm {
212c8e7849SLang Hames namespace orc {
222c8e7849SLang Hames namespace rt_bootstrap {
232c8e7849SLang Hames 
242c8e7849SLang Hames template <typename WriteT, typename SPSWriteT>
25213666f8SLang Hames static llvm::orc::shared::CWrapperFunctionResult
262c8e7849SLang Hames writeUIntsWrapper(const char *ArgData, size_t ArgSize) {
272c8e7849SLang Hames   return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
282c8e7849SLang Hames              ArgData, ArgSize,
292c8e7849SLang Hames              [](std::vector<WriteT> Ws) {
302c8e7849SLang Hames                for (auto &W : Ws)
31999c6a23SLang Hames                  *W.Addr.template toPtr<decltype(W.Value) *>() = W.Value;
322c8e7849SLang Hames              })
332c8e7849SLang Hames       .release();
342c8e7849SLang Hames }
352c8e7849SLang Hames 
36213666f8SLang Hames static llvm::orc::shared::CWrapperFunctionResult
372c8e7849SLang Hames writeBuffersWrapper(const char *ArgData, size_t ArgSize) {
382c8e7849SLang Hames   return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle(
392c8e7849SLang Hames              ArgData, ArgSize,
402c8e7849SLang Hames              [](std::vector<tpctypes::BufferWrite> Ws) {
412c8e7849SLang Hames                for (auto &W : Ws)
42999c6a23SLang Hames                  memcpy(W.Addr.template toPtr<char *>(), W.Buffer.data(),
43999c6a23SLang Hames                         W.Buffer.size());
442c8e7849SLang Hames              })
452c8e7849SLang Hames       .release();
462c8e7849SLang Hames }
472c8e7849SLang Hames 
48213666f8SLang Hames static llvm::orc::shared::CWrapperFunctionResult
49*8201ae2aSLang Hames writePointersWrapper(const char *ArgData, size_t ArgSize) {
50*8201ae2aSLang Hames   return WrapperFunction<void(SPSSequence<SPSMemoryAccessPointerWrite>)>::
51*8201ae2aSLang Hames       handle(ArgData, ArgSize,
52*8201ae2aSLang Hames              [](std::vector<tpctypes::PointerWrite> Ws) {
53*8201ae2aSLang Hames                for (auto &W : Ws)
54*8201ae2aSLang Hames                  *W.Addr.template toPtr<void **>() =
55*8201ae2aSLang Hames                      W.Value.template toPtr<void *>();
56*8201ae2aSLang Hames              })
57*8201ae2aSLang Hames           .release();
58*8201ae2aSLang Hames }
59*8201ae2aSLang Hames 
60*8201ae2aSLang Hames static llvm::orc::shared::CWrapperFunctionResult
612c8e7849SLang Hames runAsMainWrapper(const char *ArgData, size_t ArgSize) {
622c8e7849SLang Hames   return WrapperFunction<rt::SPSRunAsMainSignature>::handle(
632c8e7849SLang Hames              ArgData, ArgSize,
64ef391df2SLang Hames              [](ExecutorAddr MainAddr,
652c8e7849SLang Hames                 std::vector<std::string> Args) -> int64_t {
662c8e7849SLang Hames                return runAsMain(MainAddr.toPtr<int (*)(int, char *[])>(), Args);
672c8e7849SLang Hames              })
682c8e7849SLang Hames       .release();
692c8e7849SLang Hames }
702c8e7849SLang Hames 
717260cdd2SSunho Kim static llvm::orc::shared::CWrapperFunctionResult
727260cdd2SSunho Kim runAsVoidFunctionWrapper(const char *ArgData, size_t ArgSize) {
737260cdd2SSunho Kim   return WrapperFunction<rt::SPSRunAsVoidFunctionSignature>::handle(
747260cdd2SSunho Kim              ArgData, ArgSize,
757260cdd2SSunho Kim              [](ExecutorAddr MainAddr) -> int32_t {
767260cdd2SSunho Kim                return runAsVoidFunction(MainAddr.toPtr<int32_t (*)(void)>());
777260cdd2SSunho Kim              })
787260cdd2SSunho Kim       .release();
797260cdd2SSunho Kim }
807260cdd2SSunho Kim 
817260cdd2SSunho Kim static llvm::orc::shared::CWrapperFunctionResult
827260cdd2SSunho Kim runAsIntFunctionWrapper(const char *ArgData, size_t ArgSize) {
837260cdd2SSunho Kim   return WrapperFunction<rt::SPSRunAsIntFunctionSignature>::handle(
847260cdd2SSunho Kim              ArgData, ArgSize,
857260cdd2SSunho Kim              [](ExecutorAddr MainAddr, int32_t Arg) -> int32_t {
867260cdd2SSunho Kim                return runAsIntFunction(MainAddr.toPtr<int32_t (*)(int32_t)>(),
877260cdd2SSunho Kim                                        Arg);
887260cdd2SSunho Kim              })
897260cdd2SSunho Kim       .release();
907260cdd2SSunho Kim }
917260cdd2SSunho Kim 
92ef391df2SLang Hames void addTo(StringMap<ExecutorAddr> &M) {
93ef391df2SLang Hames   M[rt::MemoryWriteUInt8sWrapperName] = ExecutorAddr::fromPtr(
942c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt8Write,
952c8e7849SLang Hames                          shared::SPSMemoryAccessUInt8Write>);
96ef391df2SLang Hames   M[rt::MemoryWriteUInt16sWrapperName] = ExecutorAddr::fromPtr(
972c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt16Write,
982c8e7849SLang Hames                          shared::SPSMemoryAccessUInt16Write>);
99ef391df2SLang Hames   M[rt::MemoryWriteUInt32sWrapperName] = ExecutorAddr::fromPtr(
1002c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt32Write,
1012c8e7849SLang Hames                          shared::SPSMemoryAccessUInt32Write>);
102ef391df2SLang Hames   M[rt::MemoryWriteUInt64sWrapperName] = ExecutorAddr::fromPtr(
1032c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt64Write,
1042c8e7849SLang Hames                          shared::SPSMemoryAccessUInt64Write>);
1052c8e7849SLang Hames   M[rt::MemoryWriteBuffersWrapperName] =
106ef391df2SLang Hames       ExecutorAddr::fromPtr(&writeBuffersWrapper);
107*8201ae2aSLang Hames   M[rt::MemoryWritePointersWrapperName] =
108*8201ae2aSLang Hames       ExecutorAddr::fromPtr(&writePointersWrapper);
109089acf25SLang Hames   M[rt::RegisterEHFrameSectionWrapperName] =
110089acf25SLang Hames       ExecutorAddr::fromPtr(&llvm_orc_registerEHFrameSectionWrapper);
111089acf25SLang Hames   M[rt::DeregisterEHFrameSectionWrapperName] =
112089acf25SLang Hames       ExecutorAddr::fromPtr(&llvm_orc_deregisterEHFrameSectionWrapper);
113ef391df2SLang Hames   M[rt::RunAsMainWrapperName] = ExecutorAddr::fromPtr(&runAsMainWrapper);
1147260cdd2SSunho Kim   M[rt::RunAsVoidFunctionWrapperName] =
1157260cdd2SSunho Kim       ExecutorAddr::fromPtr(&runAsVoidFunctionWrapper);
1167260cdd2SSunho Kim   M[rt::RunAsIntFunctionWrapperName] =
1177260cdd2SSunho Kim       ExecutorAddr::fromPtr(&runAsIntFunctionWrapper);
1182c8e7849SLang Hames }
1192c8e7849SLang Hames 
1202c8e7849SLang Hames } // end namespace rt_bootstrap
1212c8e7849SLang Hames } // end namespace orc
1222c8e7849SLang Hames } // end namespace llvm
123