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"
132c8e7849SLang Hames #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
142c8e7849SLang Hames 
152c8e7849SLang Hames #define DEBUG_TYPE "orc"
162c8e7849SLang Hames 
172c8e7849SLang Hames using namespace llvm::orc::shared;
182c8e7849SLang Hames 
192c8e7849SLang Hames namespace llvm {
202c8e7849SLang Hames namespace orc {
212c8e7849SLang Hames namespace rt_bootstrap {
222c8e7849SLang Hames 
232c8e7849SLang Hames template <typename WriteT, typename SPSWriteT>
242c8e7849SLang Hames static llvm::orc::shared::detail::CWrapperFunctionResult
252c8e7849SLang Hames writeUIntsWrapper(const char *ArgData, size_t ArgSize) {
262c8e7849SLang Hames   return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
272c8e7849SLang Hames              ArgData, ArgSize,
282c8e7849SLang Hames              [](std::vector<WriteT> Ws) {
292c8e7849SLang Hames                for (auto &W : Ws)
302c8e7849SLang Hames                  *jitTargetAddressToPointer<decltype(W.Value) *>(W.Address) =
312c8e7849SLang Hames                      W.Value;
322c8e7849SLang Hames              })
332c8e7849SLang Hames       .release();
342c8e7849SLang Hames }
352c8e7849SLang Hames 
362c8e7849SLang Hames static llvm::orc::shared::detail::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)
422c8e7849SLang Hames                  memcpy(jitTargetAddressToPointer<char *>(W.Address),
432c8e7849SLang Hames                         W.Buffer.data(), W.Buffer.size());
442c8e7849SLang Hames              })
452c8e7849SLang Hames       .release();
462c8e7849SLang Hames }
472c8e7849SLang Hames 
482c8e7849SLang Hames static llvm::orc::shared::detail::CWrapperFunctionResult
492c8e7849SLang Hames runAsMainWrapper(const char *ArgData, size_t ArgSize) {
502c8e7849SLang Hames   return WrapperFunction<rt::SPSRunAsMainSignature>::handle(
512c8e7849SLang Hames              ArgData, ArgSize,
52*ef391df2SLang 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 
59*ef391df2SLang Hames void addTo(StringMap<ExecutorAddr> &M) {
60*ef391df2SLang Hames   M[rt::MemoryWriteUInt8sWrapperName] = ExecutorAddr::fromPtr(
612c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt8Write,
622c8e7849SLang Hames                          shared::SPSMemoryAccessUInt8Write>);
63*ef391df2SLang Hames   M[rt::MemoryWriteUInt16sWrapperName] = ExecutorAddr::fromPtr(
642c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt16Write,
652c8e7849SLang Hames                          shared::SPSMemoryAccessUInt16Write>);
66*ef391df2SLang Hames   M[rt::MemoryWriteUInt32sWrapperName] = ExecutorAddr::fromPtr(
672c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt32Write,
682c8e7849SLang Hames                          shared::SPSMemoryAccessUInt32Write>);
69*ef391df2SLang Hames   M[rt::MemoryWriteUInt64sWrapperName] = ExecutorAddr::fromPtr(
702c8e7849SLang Hames       &writeUIntsWrapper<tpctypes::UInt64Write,
712c8e7849SLang Hames                          shared::SPSMemoryAccessUInt64Write>);
722c8e7849SLang Hames   M[rt::MemoryWriteBuffersWrapperName] =
73*ef391df2SLang Hames       ExecutorAddr::fromPtr(&writeBuffersWrapper);
74*ef391df2SLang Hames   M[rt::RunAsMainWrapperName] = ExecutorAddr::fromPtr(&runAsMainWrapper);
752c8e7849SLang Hames }
762c8e7849SLang Hames 
772c8e7849SLang Hames } // end namespace rt_bootstrap
782c8e7849SLang Hames } // end namespace orc
792c8e7849SLang Hames } // end namespace llvm
80