0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===-- ToolRunner.cpp ----------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
147
|
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
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 // This file implements the interfaces described in the ToolRunner.h file.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 #include "ToolRunner.h"
|
120
|
14 #include "llvm/Config/config.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 #include "llvm/Support/CommandLine.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include "llvm/Support/Debug.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #include "llvm/Support/FileSystem.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 #include "llvm/Support/FileUtilities.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 #include "llvm/Support/Program.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 #include "llvm/Support/raw_ostream.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 #include <fstream>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 #include <sstream>
|
120
|
23 #include <utility>
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25
|
77
|
26 #define DEBUG_TYPE "toolrunner"
|
|
27
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 namespace llvm {
|
120
|
29 cl::opt<bool> SaveTemps("save-temps", cl::init(false),
|
|
30 cl::desc("Save temporary files"));
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 namespace {
|
120
|
34 cl::opt<std::string>
|
|
35 RemoteClient("remote-client",
|
|
36 cl::desc("Remote execution client (rsh/ssh)"));
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37
|
120
|
38 cl::opt<std::string> RemoteHost("remote-host",
|
|
39 cl::desc("Remote execution (rsh/ssh) host"));
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40
|
120
|
41 cl::opt<std::string> RemotePort("remote-port",
|
|
42 cl::desc("Remote execution (rsh/ssh) port"));
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43
|
120
|
44 cl::opt<std::string> RemoteUser("remote-user",
|
|
45 cl::desc("Remote execution (rsh/ssh) user id"));
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46
|
120
|
47 cl::opt<std::string>
|
|
48 RemoteExtra("remote-extra-options",
|
|
49 cl::desc("Remote execution (rsh/ssh) extra options"));
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 /// RunProgramWithTimeout - This function provides an alternate interface
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 /// to the sys::Program::ExecuteAndWait interface.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 /// @see sys::Program::ExecuteAndWait
|
147
|
55 static int RunProgramWithTimeout(StringRef ProgramPath,
|
|
56 ArrayRef<StringRef> Args, StringRef StdInFile,
|
|
57 StringRef StdOutFile, StringRef StdErrFile,
|
|
58 unsigned NumSeconds = 0,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 unsigned MemoryLimit = 0,
|
77
|
60 std::string *ErrMsg = nullptr) {
|
121
|
61 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile};
|
147
|
62 return sys::ExecuteAndWait(ProgramPath, Args, None, Redirects, NumSeconds,
|
120
|
63 MemoryLimit, ErrMsg);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 /// RunProgramRemotelyWithTimeout - This function runs the given program
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 /// Returns the remote program exit code or reports a remote client error if it
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 /// fails. Remote client is required to return 255 if it failed or program exit
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 /// code otherwise.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 /// @see sys::Program::ExecuteAndWait
|
147
|
72 static int RunProgramRemotelyWithTimeout(
|
|
73 StringRef RemoteClientPath, ArrayRef<StringRef> Args, StringRef StdInFile,
|
|
74 StringRef StdOutFile, StringRef StdErrFile, unsigned NumSeconds = 0,
|
|
75 unsigned MemoryLimit = 0) {
|
121
|
76 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile};
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 // Run the program remotely with the remote client
|
147
|
79 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, None, Redirects,
|
|
80 NumSeconds, MemoryLimit);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 // Has the remote client fail?
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 if (255 == ReturnCode) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 std::ostringstream OS;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 OS << "\nError running remote client:\n ";
|
147
|
86 for (StringRef Arg : Args)
|
|
87 OS << " " << Arg.str();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 OS << "\n";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
89
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 // The error message is in the output file, let's print it out from there.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91 std::string StdOutFileName = StdOutFile.str();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 std::ifstream ErrorFile(StdOutFileName.c_str());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
93 if (ErrorFile) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
94 std::copy(std::istreambuf_iterator<char>(ErrorFile),
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
95 std::istreambuf_iterator<char>(),
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
96 std::ostreambuf_iterator<char>(OS));
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
97 ErrorFile.close();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
98 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
100 errs() << OS.str();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
101 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
102
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
103 return ReturnCode;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
104 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
105
|
147
|
106 static Error ProcessFailure(StringRef ProgPath, ArrayRef<StringRef> Args,
|
120
|
107 unsigned Timeout = 0, unsigned MemoryLimit = 0) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
108 std::ostringstream OS;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
109 OS << "\nError running tool:\n ";
|
147
|
110 for (StringRef Arg : Args)
|
|
111 OS << " " << Arg.str();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
112 OS << "\n";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
113
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
114 // Rerun the compiler, capturing any error messages to print them.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
115 SmallString<128> ErrorFilename;
|
77
|
116 std::error_code EC = sys::fs::createTemporaryFile(
|
83
|
117 "bugpoint.program_error_messages", "", ErrorFilename);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
118 if (EC) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
119 errs() << "Error making unique filename: " << EC.message() << "\n";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
120 exit(1);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
121 }
|
77
|
122
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
123 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
124 ErrorFilename.str(), Timeout, MemoryLimit);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
125 // FIXME: check return code ?
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
126
|
100
|
127 // Print out the error messages generated by CC if possible...
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
128 std::ifstream ErrorFile(ErrorFilename.c_str());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
129 if (ErrorFile) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
130 std::copy(std::istreambuf_iterator<char>(ErrorFile),
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
131 std::istreambuf_iterator<char>(),
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
132 std::ostreambuf_iterator<char>(OS));
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
133 ErrorFile.close();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
134 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
135
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
136 sys::fs::remove(ErrorFilename.c_str());
|
120
|
137 return make_error<StringError>(OS.str(), inconvertibleErrorCode());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
138 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
139
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
140 //===---------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
141 // LLI Implementation of AbstractIntepreter interface
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
142 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
143 namespace {
|
120
|
144 class LLI : public AbstractInterpreter {
|
|
145 std::string LLIPath; // The path to the LLI executable
|
|
146 std::vector<std::string> ToolArgs; // Args to pass to LLI
|
|
147 public:
|
|
148 LLI(const std::string &Path, const std::vector<std::string> *Args)
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
149 : LLIPath(Path) {
|
120
|
150 ToolArgs.clear();
|
|
151 if (Args) {
|
|
152 ToolArgs = *Args;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
153 }
|
120
|
154 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155
|
120
|
156 Expected<int> ExecuteProgram(
|
|
157 const std::string &Bitcode, const std::vector<std::string> &Args,
|
|
158 const std::string &InputFile, const std::string &OutputFile,
|
|
159 const std::vector<std::string> &CCArgs,
|
|
160 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
|
|
161 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
|
|
162 };
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
163 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
164
|
120
|
165 Expected<int> LLI::ExecuteProgram(const std::string &Bitcode,
|
|
166 const std::vector<std::string> &Args,
|
|
167 const std::string &InputFile,
|
|
168 const std::string &OutputFile,
|
|
169 const std::vector<std::string> &CCArgs,
|
|
170 const std::vector<std::string> &SharedLibs,
|
|
171 unsigned Timeout, unsigned MemoryLimit) {
|
147
|
172 std::vector<StringRef> LLIArgs;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
173 LLIArgs.push_back(LLIPath.c_str());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
174 LLIArgs.push_back("-force-interpreter=true");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
175
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
|
120
|
177 e = SharedLibs.end();
|
|
178 i != e; ++i) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
179 LLIArgs.push_back("-load");
|
147
|
180 LLIArgs.push_back(*i);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
181 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
183 // Add any extra LLI args.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
184 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
147
|
185 LLIArgs.push_back(ToolArgs[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186
|
147
|
187 LLIArgs.push_back(Bitcode);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188 // Add optional parameters to the running program from Argv
|
120
|
189 for (unsigned i = 0, e = Args.size(); i != e; ++i)
|
147
|
190 LLIArgs.push_back(Args[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
191
|
120
|
192 outs() << "<lli>";
|
|
193 outs().flush();
|
147
|
194 LLVM_DEBUG(errs() << "\nAbout to run:\t";
|
|
195 for (unsigned i = 0, e = LLIArgs.size() - 1; i != e; ++i) errs()
|
|
196 << " " << LLIArgs[i];
|
|
197 errs() << "\n";);
|
|
198 return RunProgramWithTimeout(LLIPath, LLIArgs, InputFile, OutputFile,
|
120
|
199 OutputFile, Timeout, MemoryLimit);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
200 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
201
|
120
|
202 void AbstractInterpreter::anchor() {}
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
203
|
147
|
204 ErrorOr<std::string> llvm::FindProgramByName(const std::string &ExeName,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
205 const char *Argv0,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
206 void *MainAddr) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
207 // Check the directory that the calling program is in. We can do
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
208 // this if ProgramPath contains at least one / character, indicating that it
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
209 // is a relative path to the executable itself.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
210 std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
211 StringRef Result = sys::path::parent_path(Main);
|
147
|
212 if (ErrorOr<std::string> Path = sys::findProgramByName(ExeName, Result))
|
|
213 return *Path;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
214
|
147
|
215 // Check the user PATH.
|
|
216 return sys::findProgramByName(ExeName);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
217 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
218
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
219 // LLI create method - Try to find the LLI executable
|
120
|
220 AbstractInterpreter *
|
|
221 AbstractInterpreter::createLLI(const char *Argv0, std::string &Message,
|
|
222 const std::vector<std::string> *ToolArgs) {
|
147
|
223 if (ErrorOr<std::string> LLIPath =
|
|
224 FindProgramByName("lli", Argv0, (void *)(intptr_t)&createLLI)) {
|
|
225 Message = "Found lli: " + *LLIPath + "\n";
|
|
226 return new LLI(*LLIPath, ToolArgs);
|
|
227 } else {
|
|
228 Message = LLIPath.getError().message() + "\n";
|
|
229 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
230 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
231 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
232
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
233 //===---------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
234 // Custom compiler command implementation of AbstractIntepreter interface
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
235 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
236 // Allows using a custom command for compiling the bitcode, thus allows, for
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
237 // example, to compile a bitcode fragment without linking or executing, then
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
238 // using a custom wrapper script to check for compiler errors.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
239 namespace {
|
120
|
240 class CustomCompiler : public AbstractInterpreter {
|
|
241 std::string CompilerCommand;
|
|
242 std::vector<std::string> CompilerArgs;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
243
|
120
|
244 public:
|
|
245 CustomCompiler(const std::string &CompilerCmd,
|
|
246 std::vector<std::string> CompArgs)
|
|
247 : CompilerCommand(CompilerCmd), CompilerArgs(std::move(CompArgs)) {}
|
|
248
|
|
249 Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
|
|
250 unsigned MemoryLimit = 0) override;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
251
|
120
|
252 Expected<int> ExecuteProgram(
|
|
253 const std::string &Bitcode, const std::vector<std::string> &Args,
|
|
254 const std::string &InputFile, const std::string &OutputFile,
|
|
255 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
|
|
256 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
|
|
257 unsigned Timeout = 0, unsigned MemoryLimit = 0) override {
|
|
258 return make_error<StringError>(
|
|
259 "Execution not supported with -compile-custom",
|
|
260 inconvertibleErrorCode());
|
|
261 }
|
|
262 };
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
263 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
264
|
120
|
265 Error CustomCompiler::compileProgram(const std::string &Bitcode,
|
|
266 unsigned Timeout, unsigned MemoryLimit) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
267
|
147
|
268 std::vector<StringRef> ProgramArgs;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
269 ProgramArgs.push_back(CompilerCommand.c_str());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
270
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
271 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
272 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
|
147
|
273 ProgramArgs.push_back(Bitcode);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
274
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
275 // Add optional parameters to the running program from Argv
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
276 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
277 ProgramArgs.push_back(CompilerArgs[i].c_str());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
278
|
147
|
279 if (RunProgramWithTimeout(CompilerCommand, ProgramArgs, "", "", "", Timeout,
|
|
280 MemoryLimit))
|
|
281 return ProcessFailure(CompilerCommand, ProgramArgs, Timeout, MemoryLimit);
|
120
|
282 return Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
283 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
284
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
285 //===---------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
286 // Custom execution command implementation of AbstractIntepreter interface
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
287 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
288 // Allows using a custom command for executing the bitcode, thus allows,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
289 // for example, to invoke a cross compiler for code generation followed by
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
290 // a simulator that executes the generated binary.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
291 namespace {
|
120
|
292 class CustomExecutor : public AbstractInterpreter {
|
|
293 std::string ExecutionCommand;
|
|
294 std::vector<std::string> ExecutorArgs;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
295
|
120
|
296 public:
|
|
297 CustomExecutor(const std::string &ExecutionCmd,
|
|
298 std::vector<std::string> ExecArgs)
|
|
299 : ExecutionCommand(ExecutionCmd), ExecutorArgs(std::move(ExecArgs)) {}
|
|
300
|
|
301 Expected<int> ExecuteProgram(
|
|
302 const std::string &Bitcode, const std::vector<std::string> &Args,
|
|
303 const std::string &InputFile, const std::string &OutputFile,
|
|
304 const std::vector<std::string> &CCArgs,
|
|
305 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
|
|
306 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
|
|
307 };
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
308 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
309
|
120
|
310 Expected<int> CustomExecutor::ExecuteProgram(
|
|
311 const std::string &Bitcode, const std::vector<std::string> &Args,
|
|
312 const std::string &InputFile, const std::string &OutputFile,
|
|
313 const std::vector<std::string> &CCArgs,
|
|
314 const std::vector<std::string> &SharedLibs, unsigned Timeout,
|
|
315 unsigned MemoryLimit) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
316
|
147
|
317 std::vector<StringRef> ProgramArgs;
|
|
318 ProgramArgs.push_back(ExecutionCommand);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
319
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
320 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
|
147
|
321 ProgramArgs.push_back(ExecutorArgs[i]);
|
|
322 ProgramArgs.push_back(Bitcode);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
323
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
324 // Add optional parameters to the running program from Argv
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
325 for (unsigned i = 0, e = Args.size(); i != e; ++i)
|
147
|
326 ProgramArgs.push_back(Args[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
327
|
147
|
328 return RunProgramWithTimeout(ExecutionCommand, ProgramArgs, InputFile,
|
120
|
329 OutputFile, OutputFile, Timeout, MemoryLimit);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
330 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
331
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
332 // Tokenize the CommandLine to the command and the args to allow
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
333 // defining a full command line as the command instead of just the
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
334 // executed program. We cannot just pass the whole string after the command
|
121
|
335 // as a single argument because then the program sees only a single
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
336 // command line argument (with spaces in it: "foo bar" instead
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
337 // of "foo" and "bar").
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
338 //
|
121
|
339 // Spaces are used as a delimiter; however repeated, leading, and trailing
|
|
340 // whitespace are ignored. Simple escaping is allowed via the '\'
|
|
341 // character, as seen below:
|
|
342 //
|
|
343 // Two consecutive '\' evaluate to a single '\'.
|
|
344 // A space after a '\' evaluates to a space that is not interpreted as a
|
|
345 // delimiter.
|
|
346 // Any other instances of the '\' character are removed.
|
|
347 //
|
|
348 // Example:
|
|
349 // '\\' -> '\'
|
|
350 // '\ ' -> ' '
|
|
351 // 'exa\mple' -> 'example'
|
|
352 //
|
147
|
353 static void lexCommand(const char *Argv0, std::string &Message,
|
|
354 const std::string &CommandLine, std::string &CmdPath,
|
|
355 std::vector<std::string> &Args) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
356
|
121
|
357 std::string Token;
|
|
358 std::string Command;
|
|
359 bool FoundPath = false;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
360
|
121
|
361 // first argument is the PATH.
|
|
362 // Skip repeated whitespace, leading whitespace and trailing whitespace.
|
|
363 for (std::size_t Pos = 0u; Pos <= CommandLine.size(); ++Pos) {
|
|
364 if ('\\' == CommandLine[Pos]) {
|
|
365 if (Pos + 1 < CommandLine.size())
|
|
366 Token.push_back(CommandLine[++Pos]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
367
|
121
|
368 continue;
|
|
369 }
|
|
370 if (' ' == CommandLine[Pos] || CommandLine.size() == Pos) {
|
|
371 if (Token.empty())
|
|
372 continue;
|
|
373
|
|
374 if (!FoundPath) {
|
|
375 Command = Token;
|
|
376 FoundPath = true;
|
|
377 Token.clear();
|
|
378 continue;
|
|
379 }
|
|
380
|
|
381 Args.push_back(Token);
|
|
382 Token.clear();
|
|
383 continue;
|
|
384 }
|
|
385 Token.push_back(CommandLine[Pos]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
386 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
387
|
147
|
388 auto Path = FindProgramByName(Command, Argv0, (void *)(intptr_t)&lexCommand);
|
83
|
389 if (!Path) {
|
121
|
390 Message = std::string("Cannot find '") + Command +
|
|
391 "' in PATH: " + Path.getError().message() + "\n";
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
392 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
393 }
|
83
|
394 CmdPath = *Path;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
395
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
396 Message = "Found command in: " + CmdPath + "\n";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
397 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
398
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
399 // Custom execution environment create method, takes the execution command
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
400 // as arguments
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
401 AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
|
147
|
402 const char *Argv0, std::string &Message,
|
|
403 const std::string &CompileCommandLine) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
404
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
405 std::string CmdPath;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
406 std::vector<std::string> Args;
|
147
|
407 lexCommand(Argv0, Message, CompileCommandLine, CmdPath, Args);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
408 if (CmdPath.empty())
|
77
|
409 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
410
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
411 return new CustomCompiler(CmdPath, Args);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
412 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
413
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
414 // Custom execution environment create method, takes the execution command
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
415 // as arguments
|
120
|
416 AbstractInterpreter *
|
147
|
417 AbstractInterpreter::createCustomExecutor(const char *Argv0,
|
|
418 std::string &Message,
|
120
|
419 const std::string &ExecCommandLine) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
420
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
421 std::string CmdPath;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
422 std::vector<std::string> Args;
|
147
|
423 lexCommand(Argv0, Message, ExecCommandLine, CmdPath, Args);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
424 if (CmdPath.empty())
|
77
|
425 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
426
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
427 return new CustomExecutor(CmdPath, Args);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
428 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
429
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
430 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
431 // LLC Implementation of AbstractIntepreter interface
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
432 //
|
120
|
433 Expected<CC::FileType> LLC::OutputCode(const std::string &Bitcode,
|
|
434 std::string &OutputAsmFile,
|
|
435 unsigned Timeout, unsigned MemoryLimit) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
436 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
437
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
438 SmallString<128> UniqueFile;
|
77
|
439 std::error_code EC =
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
440 sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
441 if (EC) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
442 errs() << "Error making unique filename: " << EC.message() << "\n";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
443 exit(1);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
444 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
445 OutputAsmFile = UniqueFile.str();
|
147
|
446 std::vector<StringRef> LLCArgs;
|
|
447 LLCArgs.push_back(LLCPath);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
448
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
449 // Add any extra LLC args.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
450 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
147
|
451 LLCArgs.push_back(ToolArgs[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
452
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
453 LLCArgs.push_back("-o");
|
147
|
454 LLCArgs.push_back(OutputAsmFile); // Output to the Asm file
|
|
455 LLCArgs.push_back(Bitcode); // This is the input bitcode
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
456
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
457 if (UseIntegratedAssembler)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
458 LLCArgs.push_back("-filetype=obj");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
459
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
460 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
461 outs().flush();
|
147
|
462 LLVM_DEBUG(errs() << "\nAbout to run:\t";
|
|
463 for (unsigned i = 0, e = LLCArgs.size() - 1; i != e; ++i) errs()
|
|
464 << " " << LLCArgs[i];
|
|
465 errs() << "\n";);
|
|
466 if (RunProgramWithTimeout(LLCPath, LLCArgs, "", "", "", Timeout, MemoryLimit))
|
|
467 return ProcessFailure(LLCPath, LLCArgs, Timeout, MemoryLimit);
|
100
|
468 return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
469 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
470
|
120
|
471 Error LLC::compileProgram(const std::string &Bitcode, unsigned Timeout,
|
|
472 unsigned MemoryLimit) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
473 std::string OutputAsmFile;
|
120
|
474 Expected<CC::FileType> Result =
|
|
475 OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
476 sys::fs::remove(OutputAsmFile);
|
120
|
477 if (Error E = Result.takeError())
|
|
478 return E;
|
|
479 return Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
480 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
481
|
120
|
482 Expected<int> LLC::ExecuteProgram(const std::string &Bitcode,
|
|
483 const std::vector<std::string> &Args,
|
|
484 const std::string &InputFile,
|
|
485 const std::string &OutputFile,
|
|
486 const std::vector<std::string> &ArgsForCC,
|
|
487 const std::vector<std::string> &SharedLibs,
|
|
488 unsigned Timeout, unsigned MemoryLimit) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
489
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
490 std::string OutputAsmFile;
|
120
|
491 Expected<CC::FileType> FileKind =
|
|
492 OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
493 FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
|
120
|
494 if (Error E = FileKind.takeError())
|
|
495 return std::move(E);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
496
|
100
|
497 std::vector<std::string> CCArgs(ArgsForCC);
|
|
498 CCArgs.insert(CCArgs.end(), SharedLibs.begin(), SharedLibs.end());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
499
|
100
|
500 // Assuming LLC worked, compile the result with CC and run it.
|
120
|
501 return cc->ExecuteProgram(OutputAsmFile, Args, *FileKind, InputFile,
|
|
502 OutputFile, CCArgs, Timeout, MemoryLimit);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
503 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
504
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
505 /// createLLC - Try to find the LLC executable
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
506 ///
|
120
|
507 LLC *AbstractInterpreter::createLLC(const char *Argv0, std::string &Message,
|
100
|
508 const std::string &CCBinary,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
509 const std::vector<std::string> *Args,
|
100
|
510 const std::vector<std::string> *CCArgs,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
511 bool UseIntegratedAssembler) {
|
147
|
512 ErrorOr<std::string> LLCPath =
|
|
513 FindProgramByName("llc", Argv0, (void *)(intptr_t)&createLLC);
|
|
514 if (!LLCPath) {
|
|
515 Message = LLCPath.getError().message() + "\n";
|
77
|
516 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
517 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
518
|
147
|
519 CC *cc = CC::create(Argv0, Message, CCBinary, CCArgs);
|
100
|
520 if (!cc) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
521 errs() << Message << "\n";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
522 exit(1);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
523 }
|
147
|
524 Message = "Found llc: " + *LLCPath + "\n";
|
|
525 return new LLC(*LLCPath, cc, Args, UseIntegratedAssembler);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
526 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
527
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
528 //===---------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
529 // JIT Implementation of AbstractIntepreter interface
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
530 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
531 namespace {
|
120
|
532 class JIT : public AbstractInterpreter {
|
|
533 std::string LLIPath; // The path to the LLI executable
|
|
534 std::vector<std::string> ToolArgs; // Args to pass to LLI
|
|
535 public:
|
|
536 JIT(const std::string &Path, const std::vector<std::string> *Args)
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
537 : LLIPath(Path) {
|
120
|
538 ToolArgs.clear();
|
|
539 if (Args) {
|
|
540 ToolArgs = *Args;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
541 }
|
120
|
542 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
543
|
120
|
544 Expected<int> ExecuteProgram(
|
|
545 const std::string &Bitcode, const std::vector<std::string> &Args,
|
|
546 const std::string &InputFile, const std::string &OutputFile,
|
|
547 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
|
|
548 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
|
|
549 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
|
|
550 };
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
551 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
552
|
120
|
553 Expected<int> JIT::ExecuteProgram(const std::string &Bitcode,
|
|
554 const std::vector<std::string> &Args,
|
|
555 const std::string &InputFile,
|
|
556 const std::string &OutputFile,
|
|
557 const std::vector<std::string> &CCArgs,
|
|
558 const std::vector<std::string> &SharedLibs,
|
|
559 unsigned Timeout, unsigned MemoryLimit) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
560 // Construct a vector of parameters, incorporating those from the command-line
|
147
|
561 std::vector<StringRef> JITArgs;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
562 JITArgs.push_back(LLIPath.c_str());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
563 JITArgs.push_back("-force-interpreter=false");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
564
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
565 // Add any extra LLI args.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
566 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
147
|
567 JITArgs.push_back(ToolArgs[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
568
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
569 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
570 JITArgs.push_back("-load");
|
147
|
571 JITArgs.push_back(SharedLibs[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
572 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
573 JITArgs.push_back(Bitcode.c_str());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
574 // Add optional parameters to the running program from Argv
|
120
|
575 for (unsigned i = 0, e = Args.size(); i != e; ++i)
|
147
|
576 JITArgs.push_back(Args[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
577
|
120
|
578 outs() << "<jit>";
|
|
579 outs().flush();
|
147
|
580 LLVM_DEBUG(errs() << "\nAbout to run:\t";
|
|
581 for (unsigned i = 0, e = JITArgs.size() - 1; i != e; ++i) errs()
|
|
582 << " " << JITArgs[i];
|
|
583 errs() << "\n";);
|
|
584 LLVM_DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
|
|
585 return RunProgramWithTimeout(LLIPath, JITArgs, InputFile, OutputFile,
|
120
|
586 OutputFile, Timeout, MemoryLimit);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
587 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
588
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
589 /// createJIT - Try to find the LLI executable
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
590 ///
|
120
|
591 AbstractInterpreter *
|
|
592 AbstractInterpreter::createJIT(const char *Argv0, std::string &Message,
|
|
593 const std::vector<std::string> *Args) {
|
147
|
594 if (ErrorOr<std::string> LLIPath =
|
|
595 FindProgramByName("lli", Argv0, (void *)(intptr_t)&createJIT)) {
|
|
596 Message = "Found lli: " + *LLIPath + "\n";
|
|
597 return new JIT(*LLIPath, Args);
|
|
598 } else {
|
|
599 Message = LLIPath.getError().message() + "\n";
|
|
600 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
601 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
602 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
603
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
604 //===---------------------------------------------------------------------===//
|
100
|
605 // CC abstraction
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
606 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
607
|
147
|
608 static bool IsARMArchitecture(std::vector<StringRef> Args) {
|
|
609 for (size_t I = 0; I < Args.size(); ++I) {
|
|
610 if (!Args[I].equals_lower("-arch"))
|
|
611 continue;
|
|
612 ++I;
|
|
613 if (I == Args.size())
|
|
614 break;
|
|
615 if (Args[I].startswith_lower("arm"))
|
|
616 return true;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
617 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
618
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
619 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
620 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
621
|
120
|
622 Expected<int> CC::ExecuteProgram(const std::string &ProgramFile,
|
|
623 const std::vector<std::string> &Args,
|
|
624 FileType fileType,
|
|
625 const std::string &InputFile,
|
|
626 const std::string &OutputFile,
|
|
627 const std::vector<std::string> &ArgsForCC,
|
|
628 unsigned Timeout, unsigned MemoryLimit) {
|
147
|
629 std::vector<StringRef> CCArgs;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
630
|
147
|
631 CCArgs.push_back(CCPath);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
632
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
633 if (TargetTriple.getArch() == Triple::x86)
|
100
|
634 CCArgs.push_back("-m32");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
635
|
120
|
636 for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
|
|
637 E = ccArgs.end();
|
|
638 I != E; ++I)
|
147
|
639 CCArgs.push_back(*I);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
640
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
641 // Specify -x explicitly in case the extension is wonky
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
642 if (fileType != ObjectFile) {
|
100
|
643 CCArgs.push_back("-x");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
644 if (fileType == CFile) {
|
100
|
645 CCArgs.push_back("c");
|
|
646 CCArgs.push_back("-fno-strict-aliasing");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
647 } else {
|
100
|
648 CCArgs.push_back("assembler");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
649
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
650 // For ARM architectures we don't want this flag. bugpoint isn't
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
651 // explicitly told what architecture it is working on, so we get
|
100
|
652 // it from cc flags
|
|
653 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(CCArgs))
|
|
654 CCArgs.push_back("-force_cpusubtype_ALL");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
655 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
656 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
657
|
147
|
658 CCArgs.push_back(ProgramFile); // Specify the input filename.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
659
|
100
|
660 CCArgs.push_back("-x");
|
|
661 CCArgs.push_back("none");
|
|
662 CCArgs.push_back("-o");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
663
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
664 SmallString<128> OutputBinary;
|
77
|
665 std::error_code EC =
|
100
|
666 sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.cc.exe", OutputBinary);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
667 if (EC) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
668 errs() << "Error making unique filename: " << EC.message() << "\n";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
669 exit(1);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
670 }
|
147
|
671 CCArgs.push_back(OutputBinary); // Output to the right file...
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
672
|
100
|
673 // Add any arguments intended for CC. We locate them here because this is
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
674 // most likely -L and -l options that need to come before other libraries but
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
675 // after the source. Other options won't be sensitive to placement on the
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
676 // command line, so this should be safe.
|
100
|
677 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
|
147
|
678 CCArgs.push_back(ArgsForCC[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
679
|
120
|
680 CCArgs.push_back("-lm"); // Hard-code the math library...
|
|
681 CCArgs.push_back("-O2"); // Optimize the program a bit...
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
682 if (TargetTriple.getArch() == Triple::sparc)
|
100
|
683 CCArgs.push_back("-mcpu=v9");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
684
|
120
|
685 outs() << "<CC>";
|
|
686 outs().flush();
|
147
|
687 LLVM_DEBUG(errs() << "\nAbout to run:\t";
|
|
688 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
|
|
689 << " " << CCArgs[i];
|
|
690 errs() << "\n";);
|
|
691 if (RunProgramWithTimeout(CCPath, CCArgs, "", "", ""))
|
|
692 return ProcessFailure(CCPath, CCArgs);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
693
|
147
|
694 std::vector<StringRef> ProgramArgs;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
695
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
696 // Declared here so that the destructor only runs after
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
697 // ProgramArgs is used.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
698 std::string Exec;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
699
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
700 if (RemoteClientPath.empty())
|
147
|
701 ProgramArgs.push_back(OutputBinary);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
702 else {
|
147
|
703 ProgramArgs.push_back(RemoteClientPath);
|
|
704 ProgramArgs.push_back(RemoteHost);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
705 if (!RemoteUser.empty()) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
706 ProgramArgs.push_back("-l");
|
147
|
707 ProgramArgs.push_back(RemoteUser);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
708 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
709 if (!RemotePort.empty()) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
710 ProgramArgs.push_back("-p");
|
147
|
711 ProgramArgs.push_back(RemotePort);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
712 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
713 if (!RemoteExtra.empty()) {
|
147
|
714 ProgramArgs.push_back(RemoteExtra);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
715 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
716
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
717 // Full path to the binary. We need to cd to the exec directory because
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
718 // there is a dylib there that the exec expects to find in the CWD
|
120
|
719 char *env_pwd = getenv("PWD");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
720 Exec = "cd ";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
721 Exec += env_pwd;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
722 Exec += "; ./";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
723 Exec += OutputBinary.c_str();
|
147
|
724 ProgramArgs.push_back(Exec);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
725 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
726
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
727 // Add optional parameters to the running program from Argv
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
728 for (unsigned i = 0, e = Args.size(); i != e; ++i)
|
147
|
729 ProgramArgs.push_back(Args[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
730
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
731 // Now that we have a binary, run it!
|
120
|
732 outs() << "<program>";
|
|
733 outs().flush();
|
147
|
734 LLVM_DEBUG(
|
|
735 errs() << "\nAbout to run:\t";
|
|
736 for (unsigned i = 0, e = ProgramArgs.size() - 1; i != e; ++i) errs()
|
|
737 << " " << ProgramArgs[i];
|
|
738 errs() << "\n";);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
739
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
740 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
741
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
742 if (RemoteClientPath.empty()) {
|
147
|
743 LLVM_DEBUG(errs() << "<run locally>");
|
120
|
744 std::string Error;
|
147
|
745 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), ProgramArgs,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
746 InputFile, OutputFile, OutputFile,
|
120
|
747 Timeout, MemoryLimit, &Error);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
748 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
749 // so that crash-causing miscompilation is handled seamlessly.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
750 if (ExitCode < -1) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
751 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
|
120
|
752 outFile << Error << '\n';
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
753 outFile.close();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
754 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
755 return ExitCode;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
756 } else {
|
120
|
757 outs() << "<run remotely>";
|
|
758 outs().flush();
|
147
|
759 return RunProgramRemotelyWithTimeout(RemoteClientPath, ProgramArgs,
|
120
|
760 InputFile, OutputFile, OutputFile,
|
|
761 Timeout, MemoryLimit);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
762 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
763 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
764
|
120
|
765 Error CC::MakeSharedObject(const std::string &InputFile, FileType fileType,
|
|
766 std::string &OutputFile,
|
|
767 const std::vector<std::string> &ArgsForCC) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
768 SmallString<128> UniqueFilename;
|
77
|
769 std::error_code EC = sys::fs::createUniqueFile(
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
770 InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
771 if (EC) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
772 errs() << "Error making unique filename: " << EC.message() << "\n";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
773 exit(1);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
774 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
775 OutputFile = UniqueFilename.str();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
776
|
147
|
777 std::vector<StringRef> CCArgs;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
778
|
147
|
779 CCArgs.push_back(CCPath);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
780
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
781 if (TargetTriple.getArch() == Triple::x86)
|
100
|
782 CCArgs.push_back("-m32");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
783
|
120
|
784 for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
|
|
785 E = ccArgs.end();
|
|
786 I != E; ++I)
|
147
|
787 CCArgs.push_back(*I);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
788
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
789 // Compile the C/asm file into a shared object
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
790 if (fileType != ObjectFile) {
|
100
|
791 CCArgs.push_back("-x");
|
|
792 CCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
793 }
|
100
|
794 CCArgs.push_back("-fno-strict-aliasing");
|
147
|
795 CCArgs.push_back(InputFile); // Specify the input filename.
|
100
|
796 CCArgs.push_back("-x");
|
|
797 CCArgs.push_back("none");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
798 if (TargetTriple.getArch() == Triple::sparc)
|
120
|
799 CCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
800 else if (TargetTriple.isOSDarwin()) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
801 // link all source files into a single module in data segment, rather than
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
802 // generating blocks. dynamic_lookup requires that you set
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
803 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
|
100
|
804 // bugpoint to just pass that in the environment of CC.
|
|
805 CCArgs.push_back("-single_module");
|
120
|
806 CCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
|
100
|
807 CCArgs.push_back("-undefined");
|
|
808 CCArgs.push_back("dynamic_lookup");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
809 } else
|
120
|
810 CCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
811
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
812 if (TargetTriple.getArch() == Triple::x86_64)
|
120
|
813 CCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
814
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
815 if (TargetTriple.getArch() == Triple::sparc)
|
100
|
816 CCArgs.push_back("-mcpu=v9");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
817
|
100
|
818 CCArgs.push_back("-o");
|
147
|
819 CCArgs.push_back(OutputFile); // Output to the right filename.
|
100
|
820 CCArgs.push_back("-O2"); // Optimize the program a bit.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
821
|
100
|
822 // Add any arguments intended for CC. We locate them here because this is
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
823 // most likely -L and -l options that need to come before other libraries but
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
824 // after the source. Other options won't be sensitive to placement on the
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
825 // command line, so this should be safe.
|
100
|
826 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
|
147
|
827 CCArgs.push_back(ArgsForCC[i]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
828
|
120
|
829 outs() << "<CC>";
|
|
830 outs().flush();
|
147
|
831 LLVM_DEBUG(errs() << "\nAbout to run:\t";
|
|
832 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
|
|
833 << " " << CCArgs[i];
|
|
834 errs() << "\n";);
|
|
835 if (RunProgramWithTimeout(CCPath, CCArgs, "", "", ""))
|
|
836 return ProcessFailure(CCPath, CCArgs);
|
121
|
837 return Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
838 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
839
|
100
|
840 /// create - Try to find the CC executable
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
841 ///
|
147
|
842 CC *CC::create(const char *Argv0, std::string &Message,
|
|
843 const std::string &CCBinary,
|
120
|
844 const std::vector<std::string> *Args) {
|
147
|
845 auto CCPath = FindProgramByName(CCBinary, Argv0, (void *)(intptr_t)&create);
|
100
|
846 if (!CCPath) {
|
|
847 Message = "Cannot find `" + CCBinary + "' in PATH: " +
|
|
848 CCPath.getError().message() + "\n";
|
77
|
849 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
850 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
851
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
852 std::string RemoteClientPath;
|
83
|
853 if (!RemoteClient.empty()) {
|
|
854 auto Path = sys::findProgramByName(RemoteClient);
|
|
855 if (!Path) {
|
|
856 Message = "Cannot find `" + RemoteClient + "' in PATH: " +
|
|
857 Path.getError().message() + "\n";
|
|
858 return nullptr;
|
|
859 }
|
|
860 RemoteClientPath = *Path;
|
|
861 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
862
|
100
|
863 Message = "Found CC: " + *CCPath + "\n";
|
|
864 return new CC(*CCPath, RemoteClientPath, Args);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
865 }
|