blob: 4a7be56461a5cb19053460bf9da34d2bc5bee9b7 [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"
Brian Gaekec8db76c2003-11-18 06:31:17 +000016#include "Config/config.h" // for HAVE_LINK_R
Misha Brukman9558c6a2003-09-29 22:39:25 +000017#include "Support/Debug.h"
18#include "Support/FileUtilities.h"
Chris Lattner7915a1e2003-10-14 21:34:11 +000019#include <iostream>
20#include <fstream>
Chris Lattner89bf9ea2004-02-18 20:38:00 +000021#include <sstream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000024ToolExecutionError::~ToolExecutionError() throw() { }
25
Chris Lattner89bf9ea2004-02-18 20:38:00 +000026static void ProcessFailure(std::string ProgPath, const char** Args) {
27 std::ostringstream OS;
Chris Lattnera3de1172004-02-18 20:58:00 +000028 OS << "\nError running tool:\n ";
Chris Lattner89bf9ea2004-02-18 20:38:00 +000029 for (const char **Arg = Args; *Arg; ++Arg)
30 OS << " " << *Arg;
31 OS << "\n";
32
33 // Rerun the compiler, capturing any error messages to print them.
34 std::string ErrorFilename = getUniqueFilename("error_messages");
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);
48 throw ToolExecutionError(OS.str());
49}
50
Misha Brukman9558c6a2003-09-29 22:39:25 +000051//===---------------------------------------------------------------------===//
52// LLI Implementation of AbstractIntepreter interface
53//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000054namespace {
55 class LLI : public AbstractInterpreter {
56 std::string LLIPath; // The path to the LLI executable
57 public:
58 LLI(const std::string &Path) : LLIPath(Path) { }
59
60
61 virtual int ExecuteProgram(const std::string &Bytecode,
62 const std::vector<std::string> &Args,
63 const std::string &InputFile,
64 const std::string &OutputFile,
65 const std::vector<std::string> &SharedLibs =
Chris Lattnereeed9832003-10-14 21:52:52 +000066 std::vector<std::string>());
Chris Lattner2cdd21c2003-12-14 21:35:53 +000067 };
68}
Misha Brukman9558c6a2003-09-29 22:39:25 +000069
70int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000071 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000072 const std::string &InputFile,
73 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000074 const std::vector<std::string> &SharedLibs) {
Chris Lattner8c56be52004-02-18 20:21:57 +000075 if (!SharedLibs.empty())
76 throw ToolExecutionError("LLI currently does not support "
77 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +000078
79 std::vector<const char*> LLIArgs;
80 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +000081 LLIArgs.push_back("-quiet");
82 LLIArgs.push_back("-force-interpreter=true");
83 LLIArgs.push_back(Bytecode.c_str());
84 // Add optional parameters to the running program from Argv
85 for (unsigned i=0, e = Args.size(); i != e; ++i)
86 LLIArgs.push_back(Args[i].c_str());
87 LLIArgs.push_back(0);
88
89 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +000090 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +000091 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +000092 std::cerr << " " << LLIArgs[i];
93 std::cerr << "\n";
94 );
95 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
96 InputFile, OutputFile, OutputFile);
97}
98
99// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000100AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
101 std::string &Message) {
102 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000103 if (!LLIPath.empty()) {
104 Message = "Found lli: " + LLIPath + "\n";
105 return new LLI(LLIPath);
106 }
107
108 Message = "Cannot find `lli' in executable directory or PATH!\n";
109 return 0;
110}
111
112//===----------------------------------------------------------------------===//
113// LLC Implementation of AbstractIntepreter interface
114//
Chris Lattner8c56be52004-02-18 20:21:57 +0000115void LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000116 OutputAsmFile = getUniqueFilename(Bytecode+".llc.s");
117 const char *LLCArgs[] = {
118 LLCPath.c_str(),
119 "-o", OutputAsmFile.c_str(), // Output to the Asm file
120 "-f", // Overwrite as necessary...
121 Bytecode.c_str(), // This is the input bytecode
122 0
123 };
124
125 std::cout << "<llc>" << std::flush;
126 if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000127 "/dev/null"))
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000128 ProcessFailure(LLCPath, LLCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000129}
130
Chris Lattner9cbbee32004-02-18 23:24:41 +0000131void LLC::compileProgram(const std::string &Bytecode) {
132 std::string OutputAsmFile;
133 OutputAsm(Bytecode, OutputAsmFile);
134 removeFile(OutputAsmFile);
135}
136
Misha Brukman9558c6a2003-09-29 22:39:25 +0000137int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000138 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000139 const std::string &InputFile,
140 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000141 const std::vector<std::string> &SharedLibs) {
142
Misha Brukman9558c6a2003-09-29 22:39:25 +0000143 std::string OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000144 OutputAsm(Bytecode, OutputAsmFile);
145 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000146
147 // Assuming LLC worked, compile the result with GCC and run it.
Chris Lattner8c56be52004-02-18 20:21:57 +0000148 return gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
149 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000150}
151
Chris Lattner7915a1e2003-10-14 21:34:11 +0000152/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000153///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000154LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
155 std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000156 std::string LLCPath = FindExecutable("llc", ProgramPath);
157 if (LLCPath.empty()) {
158 Message = "Cannot find `llc' in executable directory or PATH!\n";
159 return 0;
160 }
161
162 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000163 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000164 if (!gcc) {
165 std::cerr << Message << "\n";
166 exit(1);
167 }
168 return new LLC(LLCPath, gcc);
169}
170
171//===---------------------------------------------------------------------===//
172// JIT Implementation of AbstractIntepreter interface
173//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000174namespace {
175 class JIT : public AbstractInterpreter {
176 std::string LLIPath; // The path to the LLI executable
177 public:
178 JIT(const std::string &Path) : LLIPath(Path) { }
179
180
181 virtual int ExecuteProgram(const std::string &Bytecode,
182 const std::vector<std::string> &Args,
183 const std::string &InputFile,
184 const std::string &OutputFile,
185 const std::vector<std::string> &SharedLibs =
Chris Lattnereeed9832003-10-14 21:52:52 +0000186 std::vector<std::string>());
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000187 };
188}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000189
190int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000191 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000192 const std::string &InputFile,
193 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000194 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000195 // Construct a vector of parameters, incorporating those from the command-line
196 std::vector<const char*> JITArgs;
197 JITArgs.push_back(LLIPath.c_str());
198 JITArgs.push_back("-quiet");
199 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000200
201 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000202 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000203 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000204 }
205 JITArgs.push_back(Bytecode.c_str());
206 // Add optional parameters to the running program from Argv
207 for (unsigned i=0, e = Args.size(); i != e; ++i)
208 JITArgs.push_back(Args[i].c_str());
209 JITArgs.push_back(0);
210
211 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000212 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000213 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000214 std::cerr << " " << JITArgs[i];
215 std::cerr << "\n";
216 );
217 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
218 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
219 InputFile, OutputFile, OutputFile);
220}
221
Chris Lattner7915a1e2003-10-14 21:34:11 +0000222/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000223///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000224AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
225 std::string &Message) {
226 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000227 if (!LLIPath.empty()) {
228 Message = "Found lli: " + LLIPath + "\n";
229 return new JIT(LLIPath);
230 }
231
232 Message = "Cannot find `lli' in executable directory or PATH!\n";
233 return 0;
234}
235
Chris Lattner8c56be52004-02-18 20:21:57 +0000236void CBE::OutputC(const std::string &Bytecode,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000237 std::string &OutputCFile) {
238 OutputCFile = getUniqueFilename(Bytecode+".cbe.c");
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000239 const char *LLCArgs[] = {
Chris Lattner9915cd92004-02-17 06:40:06 +0000240 LLCPath.c_str(),
Misha Brukman9558c6a2003-09-29 22:39:25 +0000241 "-o", OutputCFile.c_str(), // Output to the C file
Chris Lattner9915cd92004-02-17 06:40:06 +0000242 "-march=c", // Output to C
Misha Brukman9558c6a2003-09-29 22:39:25 +0000243 "-f", // Overwrite as necessary...
244 Bytecode.c_str(), // This is the input bytecode
245 0
246 };
247
248 std::cout << "<cbe>" << std::flush;
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000249 if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000250 "/dev/null"))
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000251 ProcessFailure(LLCPath, LLCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000252}
253
Chris Lattner9cbbee32004-02-18 23:24:41 +0000254void CBE::compileProgram(const std::string &Bytecode) {
255 std::string OutputCFile;
256 OutputC(Bytecode, OutputCFile);
257 removeFile(OutputCFile);
258}
259
Misha Brukman9558c6a2003-09-29 22:39:25 +0000260int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000261 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000262 const std::string &InputFile,
263 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000264 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000265 std::string OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000266 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000267
Chris Lattner8c56be52004-02-18 20:21:57 +0000268 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000269
Chris Lattner8c56be52004-02-18 20:21:57 +0000270 return gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile,
271 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000272}
273
Chris Lattner9915cd92004-02-17 06:40:06 +0000274/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000275///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000276CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
277 std::string &Message) {
Chris Lattner9915cd92004-02-17 06:40:06 +0000278 std::string LLCPath = FindExecutable("llc", ProgramPath);
279 if (LLCPath.empty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000280 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000281 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000282 return 0;
283 }
284
Chris Lattner9915cd92004-02-17 06:40:06 +0000285 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000286 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000287 if (!gcc) {
288 std::cerr << Message << "\n";
289 exit(1);
290 }
Chris Lattner9915cd92004-02-17 06:40:06 +0000291 return new CBE(LLCPath, gcc);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000292}
293
294//===---------------------------------------------------------------------===//
295// GCC abstraction
296//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000297int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000298 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000299 FileType fileType,
300 const std::string &InputFile,
301 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000302 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000303 std::vector<const char*> GCCArgs;
304
305 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000306
307 // Specify the shared libraries to link in...
308 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
309 GCCArgs.push_back(SharedLibs[i].c_str());
310
311 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000312 GCCArgs.push_back("-x");
313 if (fileType == CFile) {
314 GCCArgs.push_back("c");
315 GCCArgs.push_back("-fno-strict-aliasing");
316 } else {
317 GCCArgs.push_back("assembler");
318 }
319 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
320 GCCArgs.push_back("-o");
Chris Lattnereeed9832003-10-14 21:52:52 +0000321 std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000322 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
323 GCCArgs.push_back("-lm"); // Hard-code the math library...
324 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000325#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000326 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000327#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000328 GCCArgs.push_back(0); // NULL terminator
329
330 std::cout << "<gcc>" << std::flush;
331 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
332 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000333 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000334 exit(1);
335 }
336
337 std::vector<const char*> ProgramArgs;
338 ProgramArgs.push_back(OutputBinary.c_str());
339 // Add optional parameters to the running program from Argv
340 for (unsigned i=0, e = Args.size(); i != e; ++i)
341 ProgramArgs.push_back(Args[i].c_str());
342 ProgramArgs.push_back(0); // NULL terminator
343
344 // Now that we have a binary, run it!
345 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000346 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000347 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000348 std::cerr << " " << ProgramArgs[i];
349 std::cerr << "\n";
350 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000351
352 FileRemover OutputBinaryRemover(OutputBinary);
353 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
354 InputFile, OutputFile, OutputFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000355}
356
Chris Lattner1798e4a2003-10-14 21:07:25 +0000357int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000358 std::string &OutputFile) {
John Criswell7f7d16b2004-01-26 20:59:41 +0000359 OutputFile = getUniqueFilename(InputFile+SHLIBEXT);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000360 // Compile the C/asm file into a shared object
361 const char* GCCArgs[] = {
362 GCCPath.c_str(),
363 "-x", (fileType == AsmFile) ? "assembler" : "c",
364 "-fno-strict-aliasing",
365 InputFile.c_str(), // Specify the input filename...
366#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
367 "-G", // Compile a shared library, `-G' for Sparc
368#else
369 "-shared", // `-shared' for Linux/X86, maybe others
370#endif
371 "-o", OutputFile.c_str(), // Output to the right filename...
372 "-O2", // Optimize the program a bit...
373 0
374 };
375
376 std::cout << "<gcc>" << std::flush;
Chris Lattner1798e4a2003-10-14 21:07:25 +0000377 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
378 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000379 ProcessFailure(GCCPath, GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000380 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000381 }
382 return 0;
383}
384
Chris Lattner7915a1e2003-10-14 21:34:11 +0000385/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000386///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000387GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000388 std::string GCCPath = FindExecutable("gcc", ProgramPath);
389 if (GCCPath.empty()) {
390 Message = "Cannot find `gcc' in executable directory or PATH!\n";
391 return 0;
392 }
393
394 Message = "Found gcc: " + GCCPath + "\n";
395 return new GCC(GCCPath);
396}