5f6336e672f0c5079e12faea69f9eb9c67075148
[oota-llvm.git] / tools / bugpoint / ToolRunner.cpp
1 //===-- ToolRunner.cpp ----------------------------------------------------===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the interfaces described in the ToolRunner.h file.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "toolrunner"
15 #include "llvm/Support/ToolRunner.h"
16 #include "llvm/Config/config.h"   // for HAVE_LINK_R
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/FileUtilities.h"
19 #include <fstream>
20 #include <sstream>
21 using namespace llvm;
22
23 ToolExecutionError::~ToolExecutionError() throw() { }
24
25 static void ProcessFailure(std::string ProgPath, const char** Args) {
26   std::ostringstream OS;
27   OS << "\nError running tool:\n ";
28   for (const char **Arg = Args; *Arg; ++Arg)
29     OS << " " << *Arg;
30   OS << "\n";
31
32   // Rerun the compiler, capturing any error messages to print them.
33   sys::Path ErrorFilename("error_messages");
34   ErrorFilename.makeUnique();
35   RunProgramWithTimeout(ProgPath, Args, "/dev/null", ErrorFilename.c_str(),
36                         ErrorFilename.c_str());
37
38   // Print out the error messages generated by GCC if possible...
39   std::ifstream ErrorFile(ErrorFilename.c_str());
40   if (ErrorFile) {
41     std::copy(std::istreambuf_iterator<char>(ErrorFile),
42               std::istreambuf_iterator<char>(),
43               std::ostreambuf_iterator<char>(OS));
44     ErrorFile.close();
45   }
46
47   removeFile(ErrorFilename.toString());
48   throw ToolExecutionError(OS.str());
49 }
50
51 //===---------------------------------------------------------------------===//
52 // LLI Implementation of AbstractIntepreter interface
53 //
54 namespace {
55   class LLI : public AbstractInterpreter {
56     std::string LLIPath;          // The path to the LLI executable
57     std::vector<std::string> ToolArgs; // Args to pass to LLI
58   public:
59     LLI(const std::string &Path, const std::vector<std::string> *Args)
60       : LLIPath(Path) {
61       ToolArgs.clear ();
62       if (Args) { ToolArgs = *Args; }
63     }
64     
65     virtual int ExecuteProgram(const std::string &Bytecode,
66                                const std::vector<std::string> &Args,
67                                const std::string &InputFile,
68                                const std::string &OutputFile,
69                                const std::vector<std::string> &SharedLibs = 
70                                std::vector<std::string>(),
71                                unsigned Timeout = 0);
72   };
73 }
74
75 int LLI::ExecuteProgram(const std::string &Bytecode,
76                         const std::vector<std::string> &Args,
77                         const std::string &InputFile,
78                         const std::string &OutputFile,
79                         const std::vector<std::string> &SharedLibs,
80                         unsigned Timeout) {
81   if (!SharedLibs.empty())
82     throw ToolExecutionError("LLI currently does not support "
83                              "loading shared libraries.");
84
85   std::vector<const char*> LLIArgs;
86   LLIArgs.push_back(LLIPath.c_str());
87   LLIArgs.push_back("-force-interpreter=true");
88
89   // Add any extra LLI args.
90   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
91     LLIArgs.push_back(ToolArgs[i].c_str());
92
93   LLIArgs.push_back(Bytecode.c_str());
94   // Add optional parameters to the running program from Argv
95   for (unsigned i=0, e = Args.size(); i != e; ++i)
96     LLIArgs.push_back(Args[i].c_str());
97   LLIArgs.push_back(0);
98
99   std::cout << "<lli>" << std::flush;
100   DEBUG(std::cerr << "\nAbout to run:\t";
101         for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
102           std::cerr << " " << LLIArgs[i];
103         std::cerr << "\n";
104         );
105   return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
106                                InputFile, OutputFile, OutputFile, Timeout);
107 }
108
109 // LLI create method - Try to find the LLI executable
110 AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
111                                                     std::string &Message,
112                                      const std::vector<std::string> *ToolArgs) {
113   std::string LLIPath = FindExecutable("lli", ProgPath).toString();
114   if (!LLIPath.empty()) {
115     Message = "Found lli: " + LLIPath + "\n";
116     return new LLI(LLIPath, ToolArgs);
117   }
118
119   Message = "Cannot find `lli' in executable directory or PATH!\n";
120   return 0;
121 }
122
123 //===----------------------------------------------------------------------===//
124 // LLC Implementation of AbstractIntepreter interface
125 //
126 void LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
127   sys::Path uniqueFile(Bytecode+".llc.s");
128   uniqueFile.makeUnique();
129   OutputAsmFile = uniqueFile.toString();
130   std::vector<const char *> LLCArgs;
131   LLCArgs.push_back (LLCPath.c_str());
132
133   // Add any extra LLC args.
134   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
135     LLCArgs.push_back(ToolArgs[i].c_str());
136
137   LLCArgs.push_back ("-o");
138   LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
139   LLCArgs.push_back ("-f");                  // Overwrite as necessary...
140   LLCArgs.push_back (Bytecode.c_str());      // This is the input bytecode
141   LLCArgs.push_back (0);
142
143   std::cout << "<llc>" << std::flush;
144   DEBUG(std::cerr << "\nAbout to run:\t";
145         for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
146           std::cerr << " " << LLCArgs[i];
147         std::cerr << "\n";
148         );
149   if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
150                             "/dev/null"))
151     ProcessFailure(LLCPath, &LLCArgs[0]);
152 }
153
154 void LLC::compileProgram(const std::string &Bytecode) {
155   std::string OutputAsmFile;
156   OutputAsm(Bytecode, OutputAsmFile);
157   removeFile(OutputAsmFile);
158 }
159
160 int LLC::ExecuteProgram(const std::string &Bytecode,
161                         const std::vector<std::string> &Args,
162                         const std::string &InputFile,
163                         const std::string &OutputFile,
164                         const std::vector<std::string> &SharedLibs,
165                         unsigned Timeout) {
166
167   std::string OutputAsmFile;
168   OutputAsm(Bytecode, OutputAsmFile);
169   FileRemover OutFileRemover(OutputAsmFile);
170
171   // Assuming LLC worked, compile the result with GCC and run it.
172   return gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
173                              InputFile, OutputFile, SharedLibs, Timeout);
174 }
175
176 /// createLLC - Try to find the LLC executable
177 ///
178 LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
179                                     std::string &Message,
180                                     const std::vector<std::string> *Args) {
181   std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
182   if (LLCPath.empty()) {
183     Message = "Cannot find `llc' in executable directory or PATH!\n";
184     return 0;
185   }
186
187   Message = "Found llc: " + LLCPath + "\n";
188   GCC *gcc = GCC::create(ProgramPath, Message);
189   if (!gcc) {
190     std::cerr << Message << "\n";
191     exit(1);
192   }
193   return new LLC(LLCPath, gcc, Args);
194 }
195
196 //===---------------------------------------------------------------------===//
197 // JIT Implementation of AbstractIntepreter interface
198 //
199 namespace {
200   class JIT : public AbstractInterpreter {
201     std::string LLIPath;          // The path to the LLI executable
202     std::vector<std::string> ToolArgs; // Args to pass to LLI
203   public:
204     JIT(const std::string &Path, const std::vector<std::string> *Args)
205       : LLIPath(Path) {
206       ToolArgs.clear ();
207       if (Args) { ToolArgs = *Args; }
208     }
209     
210     virtual int ExecuteProgram(const std::string &Bytecode,
211                                const std::vector<std::string> &Args,
212                                const std::string &InputFile,
213                                const std::string &OutputFile,
214                                const std::vector<std::string> &SharedLibs = 
215                                std::vector<std::string>(), unsigned Timeout =0);
216   };
217 }
218
219 int JIT::ExecuteProgram(const std::string &Bytecode,
220                         const std::vector<std::string> &Args,
221                         const std::string &InputFile,
222                         const std::string &OutputFile,
223                         const std::vector<std::string> &SharedLibs,
224                         unsigned Timeout) {
225   // Construct a vector of parameters, incorporating those from the command-line
226   std::vector<const char*> JITArgs;
227   JITArgs.push_back(LLIPath.c_str());
228   JITArgs.push_back("-force-interpreter=false");
229
230   // Add any extra LLI args.
231   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
232     JITArgs.push_back(ToolArgs[i].c_str());
233
234   for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
235     JITArgs.push_back("-load");
236     JITArgs.push_back(SharedLibs[i].c_str());
237   }
238   JITArgs.push_back(Bytecode.c_str());
239   // Add optional parameters to the running program from Argv
240   for (unsigned i=0, e = Args.size(); i != e; ++i)
241     JITArgs.push_back(Args[i].c_str());
242   JITArgs.push_back(0);
243
244   std::cout << "<jit>" << std::flush;
245   DEBUG(std::cerr << "\nAbout to run:\t";
246         for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
247           std::cerr << " " << JITArgs[i];
248         std::cerr << "\n";
249         );
250   DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
251   return RunProgramWithTimeout(LLIPath, &JITArgs[0],
252                                InputFile, OutputFile, OutputFile, Timeout);
253 }
254
255 /// createJIT - Try to find the LLI executable
256 ///
257 AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
258                    std::string &Message, const std::vector<std::string> *Args) {
259   std::string LLIPath = FindExecutable("lli", ProgPath).toString();
260   if (!LLIPath.empty()) {
261     Message = "Found lli: " + LLIPath + "\n";
262     return new JIT(LLIPath, Args);
263   }
264
265   Message = "Cannot find `lli' in executable directory or PATH!\n";
266   return 0;
267 }
268
269 void CBE::OutputC(const std::string &Bytecode,
270                  std::string &OutputCFile) {
271   sys::Path uniqueFile(Bytecode+".cbe.c");
272   uniqueFile.makeUnique();
273   OutputCFile = uniqueFile.toString();
274   std::vector<const char *> LLCArgs;
275   LLCArgs.push_back (LLCPath.c_str());
276
277   // Add any extra LLC args.
278   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
279     LLCArgs.push_back(ToolArgs[i].c_str());
280
281   LLCArgs.push_back ("-o");
282   LLCArgs.push_back (OutputCFile.c_str());   // Output to the C file
283   LLCArgs.push_back ("-march=c");            // Output C language
284   LLCArgs.push_back ("-f");                  // Overwrite as necessary...
285   LLCArgs.push_back (Bytecode.c_str());      // This is the input bytecode
286   LLCArgs.push_back (0);
287
288   std::cout << "<cbe>" << std::flush;
289   DEBUG(std::cerr << "\nAbout to run:\t";
290         for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
291           std::cerr << " " << LLCArgs[i];
292         std::cerr << "\n";
293         );
294   if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
295                             "/dev/null"))
296     ProcessFailure(LLCPath, &LLCArgs[0]);
297 }
298
299 void CBE::compileProgram(const std::string &Bytecode) {
300   std::string OutputCFile;
301   OutputC(Bytecode, OutputCFile);
302   removeFile(OutputCFile);
303 }
304
305 int CBE::ExecuteProgram(const std::string &Bytecode,
306                         const std::vector<std::string> &Args,
307                         const std::string &InputFile,
308                         const std::string &OutputFile,
309                         const std::vector<std::string> &SharedLibs,
310                         unsigned Timeout) {
311   std::string OutputCFile;
312   OutputC(Bytecode, OutputCFile);
313
314   FileRemover CFileRemove(OutputCFile);
315
316   return gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile, 
317                              InputFile, OutputFile, SharedLibs, Timeout);
318 }
319
320 /// createCBE - Try to find the 'llc' executable
321 ///
322 CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
323                                     std::string &Message,
324                                     const std::vector<std::string> *Args) {
325   std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
326   if (LLCPath.empty()) {
327     Message = 
328       "Cannot find `llc' in executable directory or PATH!\n";
329     return 0;
330   }
331
332   Message = "Found llc: " + LLCPath + "\n";
333   GCC *gcc = GCC::create(ProgramPath, Message);
334   if (!gcc) {
335     std::cerr << Message << "\n";
336     exit(1);
337   }
338   return new CBE(LLCPath, gcc, Args);
339 }
340
341 //===---------------------------------------------------------------------===//
342 // GCC abstraction
343 //
344 int GCC::ExecuteProgram(const std::string &ProgramFile,
345                         const std::vector<std::string> &Args,
346                         FileType fileType,
347                         const std::string &InputFile,
348                         const std::string &OutputFile,
349                         const std::vector<std::string> &SharedLibs,
350                         unsigned Timeout) {
351   std::vector<const char*> GCCArgs;
352
353   GCCArgs.push_back(GCCPath.c_str());
354
355   // Specify the shared libraries to link in...
356   for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
357     GCCArgs.push_back(SharedLibs[i].c_str());
358   
359   // Specify -x explicitly in case the extension is wonky
360   GCCArgs.push_back("-x");
361   if (fileType == CFile) {
362     GCCArgs.push_back("c");
363     GCCArgs.push_back("-fno-strict-aliasing");
364   } else {
365     GCCArgs.push_back("assembler");
366   }
367   GCCArgs.push_back(ProgramFile.c_str());  // Specify the input filename...
368   GCCArgs.push_back("-o");
369   sys::Path OutputBinary (ProgramFile+".gcc.exe");
370   OutputBinary.makeUnique();
371   GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
372   GCCArgs.push_back("-lm");                // Hard-code the math library...
373   GCCArgs.push_back("-O2");                // Optimize the program a bit...
374 #if defined (HAVE_LINK_R)
375   GCCArgs.push_back("-Wl,-R.");            // Search this dir for .so files
376 #endif
377   GCCArgs.push_back(0);                    // NULL terminator
378
379   std::cout << "<gcc>" << std::flush;
380   if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
381                             "/dev/null")) {
382     ProcessFailure(GCCPath, &GCCArgs[0]);
383     exit(1);
384   }
385
386   std::vector<const char*> ProgramArgs;
387   ProgramArgs.push_back(OutputBinary.c_str());
388   // Add optional parameters to the running program from Argv
389   for (unsigned i=0, e = Args.size(); i != e; ++i)
390     ProgramArgs.push_back(Args[i].c_str());
391   ProgramArgs.push_back(0);                // NULL terminator
392
393   // Now that we have a binary, run it!
394   std::cout << "<program>" << std::flush;
395   DEBUG(std::cerr << "\nAbout to run:\t";
396         for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
397           std::cerr << " " << ProgramArgs[i];
398         std::cerr << "\n";
399         );
400
401   FileRemover OutputBinaryRemover(OutputBinary.toString());
402   return RunProgramWithTimeout(OutputBinary.toString(), &ProgramArgs[0],
403                                InputFile, OutputFile, OutputFile, Timeout);
404 }
405
406 int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
407                           std::string &OutputFile) {
408   sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
409   uniqueFilename.makeUnique();
410   OutputFile = uniqueFilename.toString();
411
412   // Compile the C/asm file into a shared object
413   const char* GCCArgs[] = {
414     GCCPath.c_str(),
415     "-x", (fileType == AsmFile) ? "assembler" : "c",
416     "-fno-strict-aliasing",
417     InputFile.c_str(),           // Specify the input filename...
418 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
419     "-G",                        // Compile a shared library, `-G' for Sparc
420 #elif (defined(__POWERPC__) || defined(__ppc__)) && defined(__APPLE__)
421     "-single_module",            // link all source files into a single module
422     "-dynamiclib",               // `-dynamiclib' for MacOS X/PowerPC
423     "-undefined",                // in data segment, rather than generating
424     "dynamic_lookup",            // blocks. dynamic_lookup requires that you set
425                                  // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
426 #else
427     "-shared",                   // `-shared' for Linux/X86, maybe others
428 #endif
429     "-o", OutputFile.c_str(),    // Output to the right filename...
430     "-O2",                       // Optimize the program a bit...
431     0
432   };
433   
434   std::cout << "<gcc>" << std::flush;
435   if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
436                             "/dev/null")) {
437     ProcessFailure(GCCPath, GCCArgs);
438     return 1;
439   }
440   return 0;
441 }
442
443 /// create - Try to find the `gcc' executable
444 ///
445 GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
446   std::string GCCPath = FindExecutable("gcc", ProgramPath).toString();
447   if (GCCPath.empty()) {
448     Message = "Cannot find `gcc' in executable directory or PATH!\n";
449     return 0;
450   }
451
452   Message = "Found gcc: " + GCCPath + "\n";
453   return new GCC(GCCPath);
454 }