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 
9 #include "OrcRTBootstrap.h"
10 
11 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
12 #include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
13 #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
14 
15 #define DEBUG_TYPE "orc"
16 
17 using namespace llvm::orc::shared;
18 
19 namespace llvm {
20 namespace orc {
21 namespace rt_bootstrap {
22 
23 template <typename WriteT, typename SPSWriteT>
24 static llvm::orc::shared::detail::CWrapperFunctionResult
25 writeUIntsWrapper(const char *ArgData, size_t ArgSize) {
26   return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
27              ArgData, ArgSize,
28              [](std::vector<WriteT> Ws) {
29                for (auto &W : Ws)
30                  *jitTargetAddressToPointer<decltype(W.Value) *>(W.Address) =
31                      W.Value;
32              })
33       .release();
34 }
35 
36 static llvm::orc::shared::detail::CWrapperFunctionResult
37 writeBuffersWrapper(const char *ArgData, size_t ArgSize) {
38   return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle(
39              ArgData, ArgSize,
40              [](std::vector<tpctypes::BufferWrite> Ws) {
41                for (auto &W : Ws)
42                  memcpy(jitTargetAddressToPointer<char *>(W.Address),
43                         W.Buffer.data(), W.Buffer.size());
44              })
45       .release();
46 }
47 
48 static llvm::orc::shared::detail::CWrapperFunctionResult
49 runAsMainWrapper(const char *ArgData, size_t ArgSize) {
50   return WrapperFunction<rt::SPSRunAsMainSignature>::handle(
51              ArgData, ArgSize,
52              [](ExecutorAddr MainAddr,
53                 std::vector<std::string> Args) -> int64_t {
54                return runAsMain(MainAddr.toPtr<int (*)(int, char *[])>(), Args);
55              })
56       .release();
57 }
58 
59 void addTo(StringMap<ExecutorAddr> &M) {
60   M[rt::MemoryWriteUInt8sWrapperName] = ExecutorAddr::fromPtr(
61       &writeUIntsWrapper<tpctypes::UInt8Write,
62                          shared::SPSMemoryAccessUInt8Write>);
63   M[rt::MemoryWriteUInt16sWrapperName] = ExecutorAddr::fromPtr(
64       &writeUIntsWrapper<tpctypes::UInt16Write,
65                          shared::SPSMemoryAccessUInt16Write>);
66   M[rt::MemoryWriteUInt32sWrapperName] = ExecutorAddr::fromPtr(
67       &writeUIntsWrapper<tpctypes::UInt32Write,
68                          shared::SPSMemoryAccessUInt32Write>);
69   M[rt::MemoryWriteUInt64sWrapperName] = ExecutorAddr::fromPtr(
70       &writeUIntsWrapper<tpctypes::UInt64Write,
71                          shared::SPSMemoryAccessUInt64Write>);
72   M[rt::MemoryWriteBuffersWrapperName] =
73       ExecutorAddr::fromPtr(&writeBuffersWrapper);
74   M[rt::RunAsMainWrapperName] = ExecutorAddr::fromPtr(&runAsMainWrapper);
75 }
76 
77 } // end namespace rt_bootstrap
78 } // end namespace orc
79 } // end namespace llvm
80