blob: a66b868c22cdc5f064c888b2816b8f6810ea0e7a [file] [log] [blame]
Chris Lattner7915a1e2003-10-14 21:34:11 +00001//===-- ToolRunner.cpp ----------------------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner7915a1e2003-10-14 21:34:11 +00009//
10// This file implements the interfaces described in the ToolRunner.h file.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner0b1fe842003-10-19 02:27:40 +000014#define DEBUG_TYPE "toolrunner"
Misha Brukman68734502003-10-06 18:37:24 +000015#include "llvm/Support/ToolRunner.h"
Misha Brukman9558c6a2003-09-29 22:39:25 +000016#include "Support/Debug.h"
17#include "Support/FileUtilities.h"
Chris Lattner7915a1e2003-10-14 21:34:11 +000018#include <iostream>
19#include <fstream>
Misha Brukman9558c6a2003-09-29 22:39:25 +000020
21//===---------------------------------------------------------------------===//
22// LLI Implementation of AbstractIntepreter interface
23//
24class LLI : public AbstractInterpreter {
25 std::string LLIPath; // The path to the LLI executable
26public:
27 LLI(const std::string &Path) : LLIPath(Path) { }
28
29
30 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000031 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000032 const std::string &InputFile,
33 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000034 const std::vector<std::string> &SharedLibs =
35 std::vector<std::string>());
Misha Brukman9558c6a2003-09-29 22:39:25 +000036};
37
38int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000039 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000040 const std::string &InputFile,
41 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000042 const std::vector<std::string> &SharedLibs) {
43 if (!SharedLibs.empty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +000044 std::cerr << "LLI currently does not support loading shared libraries.\n"
45 << "Exiting.\n";
46 exit(1);
47 }
48
49 std::vector<const char*> LLIArgs;
50 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +000051 LLIArgs.push_back("-quiet");
52 LLIArgs.push_back("-force-interpreter=true");
53 LLIArgs.push_back(Bytecode.c_str());
54 // Add optional parameters to the running program from Argv
55 for (unsigned i=0, e = Args.size(); i != e; ++i)
56 LLIArgs.push_back(Args[i].c_str());
57 LLIArgs.push_back(0);
58
59 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +000060 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +000061 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +000062 std::cerr << " " << LLIArgs[i];
63 std::cerr << "\n";
64 );
65 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
66 InputFile, OutputFile, OutputFile);
67}
68
69// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +000070AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
71 std::string &Message) {
72 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +000073 if (!LLIPath.empty()) {
74 Message = "Found lli: " + LLIPath + "\n";
75 return new LLI(LLIPath);
76 }
77
78 Message = "Cannot find `lli' in executable directory or PATH!\n";
79 return 0;
80}
81
82//===----------------------------------------------------------------------===//
83// LLC Implementation of AbstractIntepreter interface
84//
Chris Lattner7915a1e2003-10-14 21:34:11 +000085int LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
Misha Brukman9558c6a2003-09-29 22:39:25 +000086 OutputAsmFile = getUniqueFilename(Bytecode+".llc.s");
87 const char *LLCArgs[] = {
88 LLCPath.c_str(),
89 "-o", OutputAsmFile.c_str(), // Output to the Asm file
90 "-f", // Overwrite as necessary...
91 Bytecode.c_str(), // This is the input bytecode
92 0
93 };
94
95 std::cout << "<llc>" << std::flush;
96 if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null",
97 "/dev/null")) {
98 // If LLC failed on the bytecode, print error...
99 std::cerr << "Error: `llc' failed!\n";
100 removeFile(OutputAsmFile);
101 return 1;
102 }
103
104 return 0;
105}
106
107int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000108 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000109 const std::string &InputFile,
110 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000111 const std::vector<std::string> &SharedLibs) {
112
Misha Brukman9558c6a2003-09-29 22:39:25 +0000113 std::string OutputAsmFile;
114 if (OutputAsm(Bytecode, OutputAsmFile)) {
115 std::cerr << "Could not generate asm code with `llc', exiting.\n";
116 exit(1);
117 }
118
119 // Assuming LLC worked, compile the result with GCC and run it.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000120 int Result = gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000121 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000122 removeFile(OutputAsmFile);
123 return Result;
124}
125
Chris Lattner7915a1e2003-10-14 21:34:11 +0000126/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000127///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000128LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
129 std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000130 std::string LLCPath = FindExecutable("llc", ProgramPath);
131 if (LLCPath.empty()) {
132 Message = "Cannot find `llc' in executable directory or PATH!\n";
133 return 0;
134 }
135
136 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000137 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000138 if (!gcc) {
139 std::cerr << Message << "\n";
140 exit(1);
141 }
142 return new LLC(LLCPath, gcc);
143}
144
145//===---------------------------------------------------------------------===//
146// JIT Implementation of AbstractIntepreter interface
147//
148class JIT : public AbstractInterpreter {
149 std::string LLIPath; // The path to the LLI executable
150public:
151 JIT(const std::string &Path) : LLIPath(Path) { }
152
153
154 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000155 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000156 const std::string &InputFile,
157 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000158 const std::vector<std::string> &SharedLibs =
159 std::vector<std::string>());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000160};
161
162int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000163 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000164 const std::string &InputFile,
165 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000166 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000167 // Construct a vector of parameters, incorporating those from the command-line
168 std::vector<const char*> JITArgs;
169 JITArgs.push_back(LLIPath.c_str());
170 JITArgs.push_back("-quiet");
171 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000172
173 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000174 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000175 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000176 }
177 JITArgs.push_back(Bytecode.c_str());
178 // Add optional parameters to the running program from Argv
179 for (unsigned i=0, e = Args.size(); i != e; ++i)
180 JITArgs.push_back(Args[i].c_str());
181 JITArgs.push_back(0);
182
183 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000184 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000185 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000186 std::cerr << " " << JITArgs[i];
187 std::cerr << "\n";
188 );
189 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
190 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
191 InputFile, OutputFile, OutputFile);
192}
193
Chris Lattner7915a1e2003-10-14 21:34:11 +0000194/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000195///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000196AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
197 std::string &Message) {
198 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000199 if (!LLIPath.empty()) {
200 Message = "Found lli: " + LLIPath + "\n";
201 return new JIT(LLIPath);
202 }
203
204 Message = "Cannot find `lli' in executable directory or PATH!\n";
205 return 0;
206}
207
208int CBE::OutputC(const std::string &Bytecode,
209 std::string &OutputCFile) {
210 OutputCFile = getUniqueFilename(Bytecode+".cbe.c");
211 const char *DisArgs[] = {
212 DISPath.c_str(),
213 "-o", OutputCFile.c_str(), // Output to the C file
214 "-c", // Output to C
215 "-f", // Overwrite as necessary...
216 Bytecode.c_str(), // This is the input bytecode
217 0
218 };
219
220 std::cout << "<cbe>" << std::flush;
221 if (RunProgramWithTimeout(DISPath, DisArgs, "/dev/null", "/dev/null",
222 "/dev/null")) {
223 // If dis failed on the bytecode, print error...
224 std::cerr << "Error: `llvm-dis -c' failed!\n";
225 return 1;
226 }
227
228 return 0;
229}
230
231int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000232 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000233 const std::string &InputFile,
234 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000235 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000236 std::string OutputCFile;
237 if (OutputC(Bytecode, OutputCFile)) {
238 std::cerr << "Could not generate C code with `llvm-dis', exiting.\n";
239 exit(1);
240 }
241
Chris Lattner7915a1e2003-10-14 21:34:11 +0000242 int Result = gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000243 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000244 removeFile(OutputCFile);
245
246 return Result;
247}
248
Chris Lattner7915a1e2003-10-14 21:34:11 +0000249/// createCBE - Try to find the 'llvm-dis' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000250///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000251CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
252 std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000253 std::string DISPath = FindExecutable("llvm-dis", ProgramPath);
254 if (DISPath.empty()) {
255 Message =
256 "Cannot find `llvm-dis' in executable directory or PATH!\n";
257 return 0;
258 }
259
260 Message = "Found llvm-dis: " + DISPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000261 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000262 if (!gcc) {
263 std::cerr << Message << "\n";
264 exit(1);
265 }
266 return new CBE(DISPath, gcc);
267}
268
269//===---------------------------------------------------------------------===//
270// GCC abstraction
271//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000272int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000273 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000274 FileType fileType,
275 const std::string &InputFile,
276 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000277 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000278 std::vector<const char*> GCCArgs;
279
280 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000281
282 // Specify the shared libraries to link in...
283 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
284 GCCArgs.push_back(SharedLibs[i].c_str());
285
286 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000287 GCCArgs.push_back("-x");
288 if (fileType == CFile) {
289 GCCArgs.push_back("c");
290 GCCArgs.push_back("-fno-strict-aliasing");
291 } else {
292 GCCArgs.push_back("assembler");
293 }
294 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
295 GCCArgs.push_back("-o");
Chris Lattnereeed9832003-10-14 21:52:52 +0000296 std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000297 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
298 GCCArgs.push_back("-lm"); // Hard-code the math library...
299 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Chris Lattner1f0f1622003-10-18 21:54:47 +0000300 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Misha Brukman9558c6a2003-09-29 22:39:25 +0000301 GCCArgs.push_back(0); // NULL terminator
302
303 std::cout << "<gcc>" << std::flush;
304 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
305 "/dev/null")) {
306 ProcessFailure(&GCCArgs[0]);
307 exit(1);
308 }
309
310 std::vector<const char*> ProgramArgs;
311 ProgramArgs.push_back(OutputBinary.c_str());
312 // Add optional parameters to the running program from Argv
313 for (unsigned i=0, e = Args.size(); i != e; ++i)
314 ProgramArgs.push_back(Args[i].c_str());
315 ProgramArgs.push_back(0); // NULL terminator
316
317 // Now that we have a binary, run it!
318 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000319 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000320 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000321 std::cerr << " " << ProgramArgs[i];
322 std::cerr << "\n";
323 );
324 int ProgramResult = RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
325 InputFile, OutputFile, OutputFile);
326 removeFile(OutputBinary);
327 return ProgramResult;
328}
329
Chris Lattner1798e4a2003-10-14 21:07:25 +0000330int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000331 std::string &OutputFile) {
332 OutputFile = getUniqueFilename(InputFile+".so");
333 // Compile the C/asm file into a shared object
334 const char* GCCArgs[] = {
335 GCCPath.c_str(),
336 "-x", (fileType == AsmFile) ? "assembler" : "c",
337 "-fno-strict-aliasing",
338 InputFile.c_str(), // Specify the input filename...
339#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
340 "-G", // Compile a shared library, `-G' for Sparc
341#else
342 "-shared", // `-shared' for Linux/X86, maybe others
343#endif
344 "-o", OutputFile.c_str(), // Output to the right filename...
345 "-O2", // Optimize the program a bit...
346 0
347 };
348
349 std::cout << "<gcc>" << std::flush;
Chris Lattner1798e4a2003-10-14 21:07:25 +0000350 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
351 "/dev/null")) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000352 ProcessFailure(GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000353 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000354 }
355 return 0;
356}
357
358void GCC::ProcessFailure(const char** GCCArgs) {
359 std::cerr << "\n*** Error: invocation of the C compiler failed!\n";
360 for (const char **Arg = GCCArgs; *Arg; ++Arg)
361 std::cerr << " " << *Arg;
362 std::cerr << "\n";
363
364 // Rerun the compiler, capturing any error messages to print them.
365 std::string ErrorFilename = getUniqueFilename("gcc.errors");
366 RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", ErrorFilename.c_str(),
367 ErrorFilename.c_str());
368
369 // Print out the error messages generated by GCC if possible...
370 std::ifstream ErrorFile(ErrorFilename.c_str());
371 if (ErrorFile) {
372 std::copy(std::istreambuf_iterator<char>(ErrorFile),
373 std::istreambuf_iterator<char>(),
374 std::ostreambuf_iterator<char>(std::cerr));
375 ErrorFile.close();
376 std::cerr << "\n";
377 }
378
379 removeFile(ErrorFilename);
380}
381
Chris Lattner7915a1e2003-10-14 21:34:11 +0000382/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000383///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000384GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000385 std::string GCCPath = FindExecutable("gcc", ProgramPath);
386 if (GCCPath.empty()) {
387 Message = "Cannot find `gcc' in executable directory or PATH!\n";
388 return 0;
389 }
390
391 Message = "Found gcc: " + GCCPath + "\n";
392 return new GCC(GCCPath);
393}