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/RegisterEHFrames.h"
14#include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
15
16#define DEBUG_TYPE "orc"
17
18using namespace llvm::orc::shared;
19
20namespace llvm {
21namespace orc {
22namespace rt_bootstrap {
23
24template <typename WriteT, typename SPSWriteT>
25static llvm::orc::shared::CWrapperFunctionResult
26writeUIntsWrapper(const char *ArgData, size_t ArgSize) {
27 return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
28 ArgData, ArgSize,
29 [](std::vector<WriteT> Ws) {
30 for (auto &W : Ws)
31 *W.Addr.template toPtr<decltype(W.Value) *>() = W.Value;
32 })
33 .release();
34}
35
36static llvm::orc::shared::CWrapperFunctionResult
37writeBuffersWrapper(const char *ArgData, size_t ArgSize) {
38 return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle(
39 ArgData, ArgSize,
40 Handler: [](std::vector<tpctypes::BufferWrite> Ws) {
41 for (auto &W : Ws)
42 memcpy(dest: W.Addr.template toPtr<char *>(), src: W.Buffer.data(),
43 n: W.Buffer.size());
44 })
45 .release();
46}
47
48static llvm::orc::shared::CWrapperFunctionResult
49runAsMainWrapper(const char *ArgData, size_t ArgSize) {
50 return WrapperFunction<rt::SPSRunAsMainSignature>::handle(
51 ArgData, ArgSize,
52 Handler: [](ExecutorAddr MainAddr,
53 std::vector<std::string> Args) -> int64_t {
54 return runAsMain(Main: MainAddr.toPtr<int (*)(int, char *[])>(), Args);
55 })
56 .release();
57}
58
59static llvm::orc::shared::CWrapperFunctionResult
60runAsVoidFunctionWrapper(const char *ArgData, size_t ArgSize) {
61 return WrapperFunction<rt::SPSRunAsVoidFunctionSignature>::handle(
62 ArgData, ArgSize,
63 Handler: [](ExecutorAddr MainAddr) -> int32_t {
64 return runAsVoidFunction(Func: MainAddr.toPtr<int32_t (*)(void)>());
65 })
66 .release();
67}
68
69static llvm::orc::shared::CWrapperFunctionResult
70runAsIntFunctionWrapper(const char *ArgData, size_t ArgSize) {
71 return WrapperFunction<rt::SPSRunAsIntFunctionSignature>::handle(
72 ArgData, ArgSize,
73 Handler: [](ExecutorAddr MainAddr, int32_t Arg) -> int32_t {
74 return runAsIntFunction(Func: MainAddr.toPtr<int32_t (*)(int32_t)>(),
75 Arg);
76 })
77 .release();
78}
79
80void addTo(StringMap<ExecutorAddr> &M) {
81 M[rt::MemoryWriteUInt8sWrapperName] = ExecutorAddr::fromPtr(
82 Ptr: &writeUIntsWrapper<tpctypes::UInt8Write,
83 shared::SPSMemoryAccessUInt8Write>);
84 M[rt::MemoryWriteUInt16sWrapperName] = ExecutorAddr::fromPtr(
85 Ptr: &writeUIntsWrapper<tpctypes::UInt16Write,
86 shared::SPSMemoryAccessUInt16Write>);
87 M[rt::MemoryWriteUInt32sWrapperName] = ExecutorAddr::fromPtr(
88 Ptr: &writeUIntsWrapper<tpctypes::UInt32Write,
89 shared::SPSMemoryAccessUInt32Write>);
90 M[rt::MemoryWriteUInt64sWrapperName] = ExecutorAddr::fromPtr(
91 Ptr: &writeUIntsWrapper<tpctypes::UInt64Write,
92 shared::SPSMemoryAccessUInt64Write>);
93 M[rt::MemoryWriteBuffersWrapperName] =
94 ExecutorAddr::fromPtr(Ptr: &writeBuffersWrapper);
95 M[rt::RegisterEHFrameSectionWrapperName] =
96 ExecutorAddr::fromPtr(Ptr: &llvm_orc_registerEHFrameSectionWrapper);
97 M[rt::DeregisterEHFrameSectionWrapperName] =
98 ExecutorAddr::fromPtr(Ptr: &llvm_orc_deregisterEHFrameSectionWrapper);
99 M[rt::RunAsMainWrapperName] = ExecutorAddr::fromPtr(Ptr: &runAsMainWrapper);
100 M[rt::RunAsVoidFunctionWrapperName] =
101 ExecutorAddr::fromPtr(Ptr: &runAsVoidFunctionWrapper);
102 M[rt::RunAsIntFunctionWrapperName] =
103 ExecutorAddr::fromPtr(Ptr: &runAsIntFunctionWrapper);
104}
105
106} // end namespace rt_bootstrap
107} // end namespace orc
108} // end namespace llvm
109