blob: 94cccf277412cb252b4df047d60b7af633a7a99b [file] [log] [blame]
Chris Lattner7915a1e2003-10-14 21:34:11 +00001//===-- ToolRunner.cpp ----------------------------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000015#include "ToolRunner.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000016#include "llvm/Config/config.h" // for HAVE_LINK_R
Chris Lattner45495c52005-02-13 23:13:47 +000017#include "llvm/System/Program.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
Chris Lattner7915a1e2003-10-14 21:34:11 +000020#include <fstream>
Chris Lattner89bf9ea2004-02-18 20:38:00 +000021#include <sstream>
Chris Lattner86a54842006-01-22 22:53:01 +000022#include <iostream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000025ToolExecutionError::~ToolExecutionError() throw() { }
26
Chris Lattner45495c52005-02-13 23:13:47 +000027/// RunProgramWithTimeout - This function provides an alternate interface to the
28/// sys::Program::ExecuteAndWait interface.
29/// @see sys:Program::ExecuteAndWait
30static int RunProgramWithTimeout(const sys::Path &ProgramPath,
31 const char **Args,
32 const sys::Path &StdInFile,
33 const sys::Path &StdOutFile,
34 const sys::Path &StdErrFile,
35 unsigned NumSeconds = 0) {
36 const sys::Path* redirects[3];
37 redirects[0] = &StdInFile;
38 redirects[1] = &StdOutFile;
39 redirects[2] = &StdErrFile;
Misha Brukmanf976c852005-04-21 22:55:34 +000040
41 return
Chris Lattner45495c52005-02-13 23:13:47 +000042 sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds);
43}
44
45
46
Reid Spencerb31baa82004-12-19 18:00:21 +000047static void ProcessFailure(sys::Path ProgPath, const char** Args) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +000048 std::ostringstream OS;
Chris Lattnera3de1172004-02-18 20:58:00 +000049 OS << "\nError running tool:\n ";
Chris Lattner89bf9ea2004-02-18 20:38:00 +000050 for (const char **Arg = Args; *Arg; ++Arg)
51 OS << " " << *Arg;
52 OS << "\n";
53
54 // Rerun the compiler, capturing any error messages to print them.
Reid Spencercda985e2004-12-15 01:51:56 +000055 sys::Path ErrorFilename("error_messages");
56 ErrorFilename.makeUnique();
Reid Spencerb31baa82004-12-19 18:00:21 +000057 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
58 ErrorFilename);
Chris Lattner89bf9ea2004-02-18 20:38:00 +000059
60 // Print out the error messages generated by GCC if possible...
61 std::ifstream ErrorFile(ErrorFilename.c_str());
62 if (ErrorFile) {
63 std::copy(std::istreambuf_iterator<char>(ErrorFile),
64 std::istreambuf_iterator<char>(),
65 std::ostreambuf_iterator<char>(OS));
66 ErrorFile.close();
67 }
68
Reid Spencera229c5c2005-07-08 03:08:58 +000069 ErrorFilename.eraseFromDisk();
Chris Lattner89bf9ea2004-02-18 20:38:00 +000070 throw ToolExecutionError(OS.str());
71}
72
Misha Brukman9558c6a2003-09-29 22:39:25 +000073//===---------------------------------------------------------------------===//
74// LLI Implementation of AbstractIntepreter interface
75//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000076namespace {
77 class LLI : public AbstractInterpreter {
78 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +000079 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +000080 public:
Brian Gaeked11577b2004-05-04 21:09:01 +000081 LLI(const std::string &Path, const std::vector<std::string> *Args)
82 : LLIPath(Path) {
83 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +000084 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +000085 }
Misha Brukmanf976c852005-04-21 22:55:34 +000086
Chris Lattner2cdd21c2003-12-14 21:35:53 +000087 virtual int ExecuteProgram(const std::string &Bytecode,
88 const std::vector<std::string> &Args,
89 const std::string &InputFile,
90 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +000091 const std::vector<std::string> &GCCArgs,
Misha Brukmanf976c852005-04-21 22:55:34 +000092 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +000093 std::vector<std::string>(),
94 unsigned Timeout = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +000095 };
96}
Misha Brukman9558c6a2003-09-29 22:39:25 +000097
98int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000099 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000100 const std::string &InputFile,
101 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000102 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000103 const std::vector<std::string> &SharedLibs,
104 unsigned Timeout) {
Chris Lattner8c56be52004-02-18 20:21:57 +0000105 if (!SharedLibs.empty())
106 throw ToolExecutionError("LLI currently does not support "
107 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000108
Reid Spencer51ab5c82006-06-06 00:00:42 +0000109 if (!GCCArgs.empty())
110 throw ToolExecutionError("LLI currently does not support "
111 "GCC Arguments.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000112 std::vector<const char*> LLIArgs;
113 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000114 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +0000115
116 // Add any extra LLI args.
117 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
118 LLIArgs.push_back(ToolArgs[i].c_str());
119
Misha Brukman9558c6a2003-09-29 22:39:25 +0000120 LLIArgs.push_back(Bytecode.c_str());
121 // Add optional parameters to the running program from Argv
122 for (unsigned i=0, e = Args.size(); i != e; ++i)
123 LLIArgs.push_back(Args[i].c_str());
124 LLIArgs.push_back(0);
125
126 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000127 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000128 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000129 std::cerr << " " << LLIArgs[i];
130 std::cerr << "\n";
131 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000132 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000133 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000134 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000135}
136
137// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000138AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000139 std::string &Message,
140 const std::vector<std::string> *ToolArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000141 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000142 if (!LLIPath.empty()) {
143 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000144 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000145 }
146
147 Message = "Cannot find `lli' in executable directory or PATH!\n";
148 return 0;
149}
150
151//===----------------------------------------------------------------------===//
152// LLC Implementation of AbstractIntepreter interface
153//
Reid Spencer9ac14182004-12-16 23:01:34 +0000154void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000155 sys::Path uniqueFile(Bytecode+".llc.s");
156 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000157 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000158 std::vector<const char *> LLCArgs;
159 LLCArgs.push_back (LLCPath.c_str());
160
161 // Add any extra LLC args.
162 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
163 LLCArgs.push_back(ToolArgs[i].c_str());
164
165 LLCArgs.push_back ("-o");
166 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
167 LLCArgs.push_back ("-f"); // Overwrite as necessary...
168 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
169 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000170
171 std::cout << "<llc>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000172 DEBUG(std::cerr << "\nAbout to run:\t";
173 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
174 std::cerr << " " << LLCArgs[i];
175 std::cerr << "\n";
176 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000177 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
Reid Spencerb31baa82004-12-19 18:00:21 +0000178 sys::Path(), sys::Path(), sys::Path()))
179 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000180}
181
Chris Lattner9cbbee32004-02-18 23:24:41 +0000182void LLC::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000183 sys::Path OutputAsmFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000184 OutputAsm(Bytecode, OutputAsmFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000185 OutputAsmFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000186}
187
Misha Brukman9558c6a2003-09-29 22:39:25 +0000188int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000189 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000190 const std::string &InputFile,
191 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000192 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000193 const std::vector<std::string> &SharedLibs,
194 unsigned Timeout) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000195
Reid Spencer9ac14182004-12-16 23:01:34 +0000196 sys::Path OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000197 OutputAsm(Bytecode, OutputAsmFile);
198 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000199
Reid Spencer51ab5c82006-06-06 00:00:42 +0000200 std::vector<std::string> GCCArgs(ArgsForGCC);
201 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
202
Misha Brukman9558c6a2003-09-29 22:39:25 +0000203 // Assuming LLC worked, compile the result with GCC and run it.
Reid Spencer9ac14182004-12-16 23:01:34 +0000204 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000205 InputFile, OutputFile, GCCArgs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000206}
207
Chris Lattner7915a1e2003-10-14 21:34:11 +0000208/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000209///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000210LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000211 std::string &Message,
212 const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000213 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000214 if (LLCPath.empty()) {
215 Message = "Cannot find `llc' in executable directory or PATH!\n";
216 return 0;
217 }
218
219 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000220 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000221 if (!gcc) {
222 std::cerr << Message << "\n";
223 exit(1);
224 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000225 return new LLC(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000226}
227
228//===---------------------------------------------------------------------===//
229// JIT Implementation of AbstractIntepreter interface
230//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000231namespace {
232 class JIT : public AbstractInterpreter {
233 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000234 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000235 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000236 JIT(const std::string &Path, const std::vector<std::string> *Args)
237 : LLIPath(Path) {
238 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000239 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000240 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000241
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000242 virtual int ExecuteProgram(const std::string &Bytecode,
243 const std::vector<std::string> &Args,
244 const std::string &InputFile,
245 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000246 const std::vector<std::string> &GCCArgs =
247 std::vector<std::string>(),
Misha Brukmanf976c852005-04-21 22:55:34 +0000248 const std::vector<std::string> &SharedLibs =
Reid Spencer51ab5c82006-06-06 00:00:42 +0000249 std::vector<std::string>(),
250 unsigned Timeout =0 );
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000251 };
252}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000253
254int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000255 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000256 const std::string &InputFile,
257 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000258 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000259 const std::vector<std::string> &SharedLibs,
260 unsigned Timeout) {
Reid Spencer51ab5c82006-06-06 00:00:42 +0000261 if (!GCCArgs.empty())
262 throw ToolExecutionError("JIT does not support GCC Arguments.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000263 // Construct a vector of parameters, incorporating those from the command-line
264 std::vector<const char*> JITArgs;
265 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000266 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000267
Brian Gaeked11577b2004-05-04 21:09:01 +0000268 // Add any extra LLI args.
269 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
270 JITArgs.push_back(ToolArgs[i].c_str());
271
Chris Lattnereeed9832003-10-14 21:52:52 +0000272 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000273 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000274 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000275 }
276 JITArgs.push_back(Bytecode.c_str());
277 // Add optional parameters to the running program from Argv
278 for (unsigned i=0, e = Args.size(); i != e; ++i)
279 JITArgs.push_back(Args[i].c_str());
280 JITArgs.push_back(0);
281
282 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000283 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000284 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000285 std::cerr << " " << JITArgs[i];
286 std::cerr << "\n";
287 );
288 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
Reid Spencerb31baa82004-12-19 18:00:21 +0000289 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000290 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000291 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000292}
293
Chris Lattner7915a1e2003-10-14 21:34:11 +0000294/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000295///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000296AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000297 std::string &Message, const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000298 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000299 if (!LLIPath.empty()) {
300 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000301 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000302 }
303
304 Message = "Cannot find `lli' in executable directory or PATH!\n";
305 return 0;
306}
307
Reid Spencer9ac14182004-12-16 23:01:34 +0000308void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000309 sys::Path uniqueFile(Bytecode+".cbe.c");
310 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000311 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000312 std::vector<const char *> LLCArgs;
313 LLCArgs.push_back (LLCPath.c_str());
314
315 // Add any extra LLC args.
316 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
317 LLCArgs.push_back(ToolArgs[i].c_str());
318
319 LLCArgs.push_back ("-o");
320 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
321 LLCArgs.push_back ("-march=c"); // Output C language
322 LLCArgs.push_back ("-f"); // Overwrite as necessary...
323 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
324 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000325
326 std::cout << "<cbe>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000327 DEBUG(std::cerr << "\nAbout to run:\t";
328 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
329 std::cerr << " " << LLCArgs[i];
330 std::cerr << "\n";
331 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000332 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000333 sys::Path()))
Brian Gaeked11577b2004-05-04 21:09:01 +0000334 ProcessFailure(LLCPath, &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000335}
336
Chris Lattner9cbbee32004-02-18 23:24:41 +0000337void CBE::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000338 sys::Path OutputCFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000339 OutputC(Bytecode, OutputCFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000340 OutputCFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000341}
342
Misha Brukman9558c6a2003-09-29 22:39:25 +0000343int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000344 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000345 const std::string &InputFile,
346 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000347 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000348 const std::vector<std::string> &SharedLibs,
349 unsigned Timeout) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000350 sys::Path OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000351 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000352
Chris Lattner8c56be52004-02-18 20:21:57 +0000353 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000354
Reid Spencer51ab5c82006-06-06 00:00:42 +0000355 std::vector<std::string> GCCArgs(ArgsForGCC);
356 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
Misha Brukmanf976c852005-04-21 22:55:34 +0000357 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000358 InputFile, OutputFile, GCCArgs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000359}
360
Chris Lattner9915cd92004-02-17 06:40:06 +0000361/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000362///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000363CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000364 std::string &Message,
365 const std::vector<std::string> *Args) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000366 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
367 if (LLCPath.isEmpty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000368 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000369 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000370 return 0;
371 }
372
Reid Spencerb31baa82004-12-19 18:00:21 +0000373 Message = "Found llc: " + LLCPath.toString() + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000374 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000375 if (!gcc) {
376 std::cerr << Message << "\n";
377 exit(1);
378 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000379 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000380}
381
382//===---------------------------------------------------------------------===//
383// GCC abstraction
384//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000385int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000386 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000387 FileType fileType,
388 const std::string &InputFile,
389 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000390 const std::vector<std::string> &ArgsForGCC,
391 unsigned Timeout ) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000392 std::vector<const char*> GCCArgs;
393
394 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000395
Chris Lattnereeed9832003-10-14 21:52:52 +0000396 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000397 GCCArgs.push_back("-x");
398 if (fileType == CFile) {
399 GCCArgs.push_back("c");
400 GCCArgs.push_back("-fno-strict-aliasing");
401 } else {
402 GCCArgs.push_back("assembler");
Chris Lattnerd00b2882005-08-29 13:14:24 +0000403#ifdef __APPLE__
404 GCCArgs.push_back("-force_cpusubtype_ALL");
405#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000406 }
407 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
Chris Lattner629e4872006-06-09 21:31:53 +0000408 GCCArgs.push_back("-x");
409 GCCArgs.push_back("none");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000410 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000411 sys::Path OutputBinary (ProgramFile+".gcc.exe");
412 OutputBinary.makeUnique();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000413 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
Reid Spencer51ab5c82006-06-06 00:00:42 +0000414
415 // Add any arguments intended for GCC. We locate them here because this is
416 // most likely -L and -l options that need to come before other libraries but
417 // after the source. Other options won't be sensitive to placement on the
418 // command line, so this should be safe.
419 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
420 GCCArgs.push_back(ArgsForGCC[i].c_str());
421
Misha Brukman9558c6a2003-09-29 22:39:25 +0000422 GCCArgs.push_back("-lm"); // Hard-code the math library...
423 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000424#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000425 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000426#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000427#ifdef __sparc__
428 GCCArgs.push_back("-mcpu=v9");
429#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000430 GCCArgs.push_back(0); // NULL terminator
431
432 std::cout << "<gcc>" << std::flush;
Reid Spencerb31baa82004-12-19 18:00:21 +0000433 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
434 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000435 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000436 exit(1);
437 }
438
439 std::vector<const char*> ProgramArgs;
Chris Lattner45495c52005-02-13 23:13:47 +0000440
Misha Brukman9558c6a2003-09-29 22:39:25 +0000441 ProgramArgs.push_back(OutputBinary.c_str());
442 // Add optional parameters to the running program from Argv
443 for (unsigned i=0, e = Args.size(); i != e; ++i)
444 ProgramArgs.push_back(Args[i].c_str());
445 ProgramArgs.push_back(0); // NULL terminator
446
447 // Now that we have a binary, run it!
448 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000449 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000450 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000451 std::cerr << " " << ProgramArgs[i];
452 std::cerr << "\n";
453 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000454
Reid Spencer9ac14182004-12-16 23:01:34 +0000455 FileRemover OutputBinaryRemover(OutputBinary);
Reid Spencerb31baa82004-12-19 18:00:21 +0000456 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000457 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000458 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000459}
460
Chris Lattner1798e4a2003-10-14 21:07:25 +0000461int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +0000462 std::string &OutputFile,
463 const std::vector<std::string> &ArgsForGCC) {
Reid Spencercda985e2004-12-15 01:51:56 +0000464 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
465 uniqueFilename.makeUnique();
466 OutputFile = uniqueFilename.toString();
467
Chris Lattner130e2a32006-06-27 20:35:36 +0000468 std::vector<const char*> GCCArgs;
469
470 GCCArgs.push_back(GCCPath.c_str());
471
472
Misha Brukman9558c6a2003-09-29 22:39:25 +0000473 // Compile the C/asm file into a shared object
Chris Lattner130e2a32006-06-27 20:35:36 +0000474 GCCArgs.push_back("-x");
475 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
476 GCCArgs.push_back("-fno-strict-aliasing");
477 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
Misha Brukman9558c6a2003-09-29 22:39:25 +0000478#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Chris Lattner130e2a32006-06-27 20:35:36 +0000479 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
Nate Begeman72b286b2005-07-08 00:23:26 +0000480#elif defined(__APPLE__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000481 // link all source files into a single module in data segment, rather than
482 // generating blocks. dynamic_lookup requires that you set
483 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
484 // bugpoint to just pass that in the environment of GCC.
485 GCCArgs.push_back("-single_module");
486 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
487 GCCArgs.push_back("-undefined");
488 GCCArgs.push_back("dynamic_lookup");
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000489#else
Chris Lattner130e2a32006-06-27 20:35:36 +0000490 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
Misha Brukman9558c6a2003-09-29 22:39:25 +0000491#endif
Chris Lattner1c81f132005-03-09 03:31:02 +0000492
Andrew Lenharth572668a2005-03-10 20:15:09 +0000493#if defined(__ia64__) || defined(__alpha__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000494 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
Chris Lattner1c81f132005-03-09 03:31:02 +0000495#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000496#ifdef __sparc__
Chris Lattner130e2a32006-06-27 20:35:36 +0000497 GCCArgs.push_back("-mcpu=v9");
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000498#endif
Chris Lattner130e2a32006-06-27 20:35:36 +0000499 GCCArgs.push_back("-o");
500 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
501 GCCArgs.push_back("-O2"); // Optimize the program a bit.
502
503
504
505 // Add any arguments intended for GCC. We locate them here because this is
506 // most likely -L and -l options that need to come before other libraries but
507 // after the source. Other options won't be sensitive to placement on the
508 // command line, so this should be safe.
509 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
510 GCCArgs.push_back(ArgsForGCC[i].c_str());
511 GCCArgs.push_back(0); // NULL terminator
512
513
Misha Brukmanf976c852005-04-21 22:55:34 +0000514
Misha Brukman9558c6a2003-09-29 22:39:25 +0000515 std::cout << "<gcc>" << std::flush;
Chris Lattner130e2a32006-06-27 20:35:36 +0000516 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000517 sys::Path())) {
Chris Lattner130e2a32006-06-27 20:35:36 +0000518 ProcessFailure(GCCPath, &GCCArgs[0]);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000519 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000520 }
521 return 0;
522}
523
Chris Lattner7915a1e2003-10-14 21:34:11 +0000524/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000525///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000526GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000527 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
528 if (GCCPath.isEmpty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000529 Message = "Cannot find `gcc' in executable directory or PATH!\n";
530 return 0;
531 }
532
Reid Spencerb31baa82004-12-19 18:00:21 +0000533 Message = "Found gcc: " + GCCPath.toString() + "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000534 return new GCC(GCCPath);
535}