xref: /freebsd/contrib/llvm-project/compiler-rt/lib/orc/run_program_wrapper.cpp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 //===- run_program_wrapper.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 // This file is a part of the ORC runtime support library.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "adt.h"
14 #include "common.h"
15 #include "wrapper_function_utils.h"
16 
17 #include <vector>
18 
19 using namespace __orc_rt;
20 
21 extern "C" int64_t __orc_rt_run_program(const char *JITDylibName,
22                                         const char *EntrySymbolName, int argc,
23                                         char *argv[]);
24 
25 ORC_RT_INTERFACE __orc_rt_CWrapperFunctionResult
26 __orc_rt_run_program_wrapper(const char *ArgData, size_t ArgSize) {
27   return WrapperFunction<int64_t(SPSString, SPSString,
28                                  SPSSequence<SPSString>)>::
29       handle(ArgData, ArgSize,
30              [](const std::string &JITDylibName,
31                 const std::string &EntrySymbolName,
32                 const std::vector<string_view> &Args) {
33                std::vector<std::unique_ptr<char[]>> ArgVStorage;
34                ArgVStorage.reserve(Args.size());
35                for (auto &Arg : Args) {
36                  ArgVStorage.push_back(
37                      std::make_unique<char[]>(Arg.size() + 1));
38                  memcpy(ArgVStorage.back().get(), Arg.data(), Arg.size());
39                  ArgVStorage.back()[Arg.size()] = '\0';
40                }
41                std::vector<char *> ArgV;
42                ArgV.reserve(ArgVStorage.size() + 1);
43                for (auto &ArgStorage : ArgVStorage)
44                  ArgV.push_back(ArgStorage.get());
45                ArgV.push_back(nullptr);
46                return __orc_rt_run_program(JITDylibName.c_str(),
47                                            EntrySymbolName.c_str(),
48                                            ArgV.size() - 1, ArgV.data());
49              })
50           .release();
51 }
52