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
writeUIntsWrapper(const char * ArgData,size_t ArgSize)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
writeBuffersWrapper(const char * ArgData,size_t ArgSize)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
runAsMainWrapper(const char * ArgData,size_t ArgSize)492c8e7849SLang Hames runAsMainWrapper(const char *ArgData, size_t ArgSize) {
502c8e7849SLang Hames   return WrapperFunction<rt::SPSRunAsMainSignature>::handle(
512c8e7849SLang Hames              ArgData, ArgSize,
52ef391df2SLang Hames              [](ExecutorAddr MainAddr,
532c8e7849SLang Hames                 std::vector<std::string> Args) -> int64_t {
542c8e7849SLang Hames                return runAsMain(MainAddr.toPtr<int (*)(int, char *[])>(), Args);
552c8e7849SLang Hames              })
562c8e7849SLang Hames       .release();
572c8e7849SLang Hames }
582c8e7849SLang Hames 
addTo(StringMap<ExecutorAddr> & M)59ef391df2SLang Hames void addTo(StringMap<ExecutorAddr> &M) {
60ef391df2SLang Hames   M[rt::MemoryWriteUInt8sWrapperName] = ExecutorAddr::fromPtr(
612c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt8Write,
622c8e7849SLang Hames                          shared::SPSMemoryAccessUInt8Write>);
63ef391df2SLang Hames   M[rt::MemoryWriteUInt16sWrapperName] = ExecutorAddr::fromPtr(
642c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt16Write,
652c8e7849SLang Hames                          shared::SPSMemoryAccessUInt16Write>);
66ef391df2SLang Hames   M[rt::MemoryWriteUInt32sWrapperName] = ExecutorAddr::fromPtr(
672c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt32Write,
682c8e7849SLang Hames                          shared::SPSMemoryAccessUInt32Write>);
69ef391df2SLang Hames   M[rt::MemoryWriteUInt64sWrapperName] = ExecutorAddr::fromPtr(
702c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt64Write,
712c8e7849SLang Hames                          shared::SPSMemoryAccessUInt64Write>);
722c8e7849SLang Hames   M[rt::MemoryWriteBuffersWrapperName] =
73ef391df2SLang Hames       ExecutorAddr::fromPtr(&writeBuffersWrapper);
74*089acf25SLang Hames   M[rt::RegisterEHFrameSectionWrapperName] =
75*089acf25SLang Hames       ExecutorAddr::fromPtr(&llvm_orc_registerEHFrameSectionWrapper);
76*089acf25SLang Hames   M[rt::DeregisterEHFrameSectionWrapperName] =
77*089acf25SLang Hames       ExecutorAddr::fromPtr(&llvm_orc_deregisterEHFrameSectionWrapper);
78ef391df2SLang Hames   M[rt::RunAsMainWrapperName] = ExecutorAddr::fromPtr(&runAsMainWrapper);
792c8e7849SLang Hames }
802c8e7849SLang Hames 
812c8e7849SLang Hames } // end namespace rt_bootstrap
822c8e7849SLang Hames } // end namespace orc
832c8e7849SLang Hames } // end namespace llvm
84