| 1 | //===--- TargetExecutionUtils.cpp - Execution utils for target processes --===// |
|---|---|
| 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 "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h" |
| 10 | |
| 11 | #include <vector> |
| 12 | |
| 13 | namespace llvm { |
| 14 | namespace orc { |
| 15 | |
| 16 | int runAsMain(int (*Main)(int, char *[]), ArrayRef<std::string> Args, |
| 17 | std::optional<StringRef> ProgramName) { |
| 18 | std::vector<std::unique_ptr<char[]>> ArgVStorage; |
| 19 | std::vector<char *> ArgV; |
| 20 | |
| 21 | ArgVStorage.reserve(n: Args.size() + (ProgramName ? 1 : 0)); |
| 22 | ArgV.reserve(n: Args.size() + 1 + (ProgramName ? 1 : 0)); |
| 23 | |
| 24 | if (ProgramName) { |
| 25 | ArgVStorage.push_back(x: std::make_unique<char[]>(num: ProgramName->size() + 1)); |
| 26 | llvm::copy(Range&: *ProgramName, Out: &ArgVStorage.back()[0]); |
| 27 | ArgVStorage.back()[ProgramName->size()] = '\0'; |
| 28 | ArgV.push_back(x: ArgVStorage.back().get()); |
| 29 | } |
| 30 | |
| 31 | for (const auto &Arg : Args) { |
| 32 | ArgVStorage.push_back(x: std::make_unique<char[]>(num: Arg.size() + 1)); |
| 33 | llvm::copy(Range: Arg, Out: &ArgVStorage.back()[0]); |
| 34 | ArgVStorage.back()[Arg.size()] = '\0'; |
| 35 | ArgV.push_back(x: ArgVStorage.back().get()); |
| 36 | } |
| 37 | ArgV.push_back(x: nullptr); |
| 38 | |
| 39 | return Main(Args.size() + !!ProgramName, ArgV.data()); |
| 40 | } |
| 41 | |
| 42 | int runAsVoidFunction(int (*Func)(void)) { return Func(); } |
| 43 | |
| 44 | int runAsIntFunction(int (*Func)(int), int Arg) { return Func(Arg); } |
| 45 | |
| 46 | } // End namespace orc. |
| 47 | } // End namespace llvm. |
| 48 |