blob: 35f27db9cabff58973ac40feaa803d35830d61a0 [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;
Chris Lattnerc600f3c2006-09-15 21:29:15 +000040
Chris Lattnerd4223502006-09-15 23:01:10 +000041 if (0) {
42 std::cerr << "RUN:";
43 for (unsigned i = 0; Args[i]; ++i)
44 std::cerr << " " << Args[i];
45 std::cerr << "\n";
46 }
Misha Brukmanf976c852005-04-21 22:55:34 +000047
48 return
Chris Lattner45495c52005-02-13 23:13:47 +000049 sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds);
50}
51
52
53
Reid Spencerb31baa82004-12-19 18:00:21 +000054static void ProcessFailure(sys::Path ProgPath, const char** Args) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +000055 std::ostringstream OS;
Chris Lattnera3de1172004-02-18 20:58:00 +000056 OS << "\nError running tool:\n ";
Chris Lattner89bf9ea2004-02-18 20:38:00 +000057 for (const char **Arg = Args; *Arg; ++Arg)
58 OS << " " << *Arg;
59 OS << "\n";
60
61 // Rerun the compiler, capturing any error messages to print them.
Reid Spencercda985e2004-12-15 01:51:56 +000062 sys::Path ErrorFilename("error_messages");
Reid Spencer51c5a282006-08-23 20:34:57 +000063 std::string ErrMsg;
64 if (ErrorFilename.makeUnique(true, &ErrMsg)) {
65 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
66 exit(1);
67 }
Reid Spencerb31baa82004-12-19 18:00:21 +000068 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
Reid Spencer8ea5ecb2006-08-21 06:04:45 +000069 ErrorFilename); // FIXME: check return code ?
Chris Lattner89bf9ea2004-02-18 20:38:00 +000070
71 // Print out the error messages generated by GCC if possible...
72 std::ifstream ErrorFile(ErrorFilename.c_str());
73 if (ErrorFile) {
74 std::copy(std::istreambuf_iterator<char>(ErrorFile),
75 std::istreambuf_iterator<char>(),
76 std::ostreambuf_iterator<char>(OS));
77 ErrorFile.close();
78 }
79
Reid Spencera229c5c2005-07-08 03:08:58 +000080 ErrorFilename.eraseFromDisk();
Chris Lattner89bf9ea2004-02-18 20:38:00 +000081 throw ToolExecutionError(OS.str());
82}
83
Misha Brukman9558c6a2003-09-29 22:39:25 +000084//===---------------------------------------------------------------------===//
85// LLI Implementation of AbstractIntepreter interface
86//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000087namespace {
88 class LLI : public AbstractInterpreter {
89 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +000090 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +000091 public:
Brian Gaeked11577b2004-05-04 21:09:01 +000092 LLI(const std::string &Path, const std::vector<std::string> *Args)
93 : LLIPath(Path) {
94 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +000095 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +000096 }
Misha Brukmanf976c852005-04-21 22:55:34 +000097
Chris Lattner2cdd21c2003-12-14 21:35:53 +000098 virtual int ExecuteProgram(const std::string &Bytecode,
99 const std::vector<std::string> &Args,
100 const std::string &InputFile,
101 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000102 const std::vector<std::string> &GCCArgs,
Misha Brukmanf976c852005-04-21 22:55:34 +0000103 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000104 std::vector<std::string>(),
105 unsigned Timeout = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000106 };
107}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000108
109int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000110 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000111 const std::string &InputFile,
112 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000113 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000114 const std::vector<std::string> &SharedLibs,
115 unsigned Timeout) {
Chris Lattner8c56be52004-02-18 20:21:57 +0000116 if (!SharedLibs.empty())
117 throw ToolExecutionError("LLI currently does not support "
118 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000119
Reid Spencer51ab5c82006-06-06 00:00:42 +0000120 if (!GCCArgs.empty())
121 throw ToolExecutionError("LLI currently does not support "
122 "GCC Arguments.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000123 std::vector<const char*> LLIArgs;
124 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000125 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +0000126
127 // Add any extra LLI args.
128 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
129 LLIArgs.push_back(ToolArgs[i].c_str());
130
Misha Brukman9558c6a2003-09-29 22:39:25 +0000131 LLIArgs.push_back(Bytecode.c_str());
132 // Add optional parameters to the running program from Argv
133 for (unsigned i=0, e = Args.size(); i != e; ++i)
134 LLIArgs.push_back(Args[i].c_str());
135 LLIArgs.push_back(0);
136
137 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000138 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000139 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000140 std::cerr << " " << LLIArgs[i];
141 std::cerr << "\n";
142 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000143 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000144 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000145 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000146}
147
148// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000149AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000150 std::string &Message,
151 const std::vector<std::string> *ToolArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000152 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000153 if (!LLIPath.empty()) {
154 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000155 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000156 }
157
158 Message = "Cannot find `lli' in executable directory or PATH!\n";
159 return 0;
160}
161
162//===----------------------------------------------------------------------===//
163// LLC Implementation of AbstractIntepreter interface
164//
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000165GCC::FileType LLC::OutputCode(const std::string &Bytecode,
166 sys::Path &OutputAsmFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000167 sys::Path uniqueFile(Bytecode+".llc.s");
Reid Spencer51c5a282006-08-23 20:34:57 +0000168 std::string ErrMsg;
169 if (uniqueFile.makeUnique(true, &ErrMsg)) {
170 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
171 exit(1);
172 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000173 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000174 std::vector<const char *> LLCArgs;
175 LLCArgs.push_back (LLCPath.c_str());
176
177 // Add any extra LLC args.
178 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
179 LLCArgs.push_back(ToolArgs[i].c_str());
180
181 LLCArgs.push_back ("-o");
182 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
183 LLCArgs.push_back ("-f"); // Overwrite as necessary...
184 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
185 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000186
187 std::cout << "<llc>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000188 DEBUG(std::cerr << "\nAbout to run:\t";
189 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
190 std::cerr << " " << LLCArgs[i];
191 std::cerr << "\n";
192 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000193 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
Reid Spencerb31baa82004-12-19 18:00:21 +0000194 sys::Path(), sys::Path(), sys::Path()))
195 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000196
197 return GCC::AsmFile;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000198}
199
Chris Lattner9cbbee32004-02-18 23:24:41 +0000200void LLC::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000201 sys::Path OutputAsmFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000202 OutputCode(Bytecode, OutputAsmFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000203 OutputAsmFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000204}
205
Misha Brukman9558c6a2003-09-29 22:39:25 +0000206int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000207 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000208 const std::string &InputFile,
209 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000210 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000211 const std::vector<std::string> &SharedLibs,
212 unsigned Timeout) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000213
Reid Spencer9ac14182004-12-16 23:01:34 +0000214 sys::Path OutputAsmFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000215 OutputCode(Bytecode, OutputAsmFile);
Chris Lattner8c56be52004-02-18 20:21:57 +0000216 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000217
Reid Spencer51ab5c82006-06-06 00:00:42 +0000218 std::vector<std::string> GCCArgs(ArgsForGCC);
219 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
220
Misha Brukman9558c6a2003-09-29 22:39:25 +0000221 // Assuming LLC worked, compile the result with GCC and run it.
Reid Spencer9ac14182004-12-16 23:01:34 +0000222 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000223 InputFile, OutputFile, GCCArgs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000224}
225
Chris Lattner7915a1e2003-10-14 21:34:11 +0000226/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000227///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000228LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000229 std::string &Message,
230 const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000231 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000232 if (LLCPath.empty()) {
233 Message = "Cannot find `llc' in executable directory or PATH!\n";
234 return 0;
235 }
236
237 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000238 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000239 if (!gcc) {
240 std::cerr << Message << "\n";
241 exit(1);
242 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000243 return new LLC(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000244}
245
246//===---------------------------------------------------------------------===//
247// JIT Implementation of AbstractIntepreter interface
248//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000249namespace {
250 class JIT : public AbstractInterpreter {
251 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000252 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000253 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000254 JIT(const std::string &Path, const std::vector<std::string> *Args)
255 : LLIPath(Path) {
256 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000257 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000258 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000259
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000260 virtual int ExecuteProgram(const std::string &Bytecode,
261 const std::vector<std::string> &Args,
262 const std::string &InputFile,
263 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000264 const std::vector<std::string> &GCCArgs =
265 std::vector<std::string>(),
Misha Brukmanf976c852005-04-21 22:55:34 +0000266 const std::vector<std::string> &SharedLibs =
Reid Spencer51ab5c82006-06-06 00:00:42 +0000267 std::vector<std::string>(),
268 unsigned Timeout =0 );
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000269 };
270}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000271
272int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000273 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000274 const std::string &InputFile,
275 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000276 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000277 const std::vector<std::string> &SharedLibs,
278 unsigned Timeout) {
Reid Spencer51ab5c82006-06-06 00:00:42 +0000279 if (!GCCArgs.empty())
280 throw ToolExecutionError("JIT does not support GCC Arguments.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000281 // Construct a vector of parameters, incorporating those from the command-line
282 std::vector<const char*> JITArgs;
283 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000284 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000285
Brian Gaeked11577b2004-05-04 21:09:01 +0000286 // Add any extra LLI args.
287 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
288 JITArgs.push_back(ToolArgs[i].c_str());
289
Chris Lattnereeed9832003-10-14 21:52:52 +0000290 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000291 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000292 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000293 }
294 JITArgs.push_back(Bytecode.c_str());
295 // Add optional parameters to the running program from Argv
296 for (unsigned i=0, e = Args.size(); i != e; ++i)
297 JITArgs.push_back(Args[i].c_str());
298 JITArgs.push_back(0);
299
300 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000301 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000302 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000303 std::cerr << " " << JITArgs[i];
304 std::cerr << "\n";
305 );
306 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
Reid Spencerb31baa82004-12-19 18:00:21 +0000307 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000308 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000309 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000310}
311
Chris Lattner7915a1e2003-10-14 21:34:11 +0000312/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000313///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000314AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000315 std::string &Message, const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000316 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000317 if (!LLIPath.empty()) {
318 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000319 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000320 }
321
322 Message = "Cannot find `lli' in executable directory or PATH!\n";
323 return 0;
324}
325
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000326GCC::FileType CBE::OutputCode(const std::string &Bytecode,
327 sys::Path &OutputCFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000328 sys::Path uniqueFile(Bytecode+".cbe.c");
Reid Spencer51c5a282006-08-23 20:34:57 +0000329 std::string ErrMsg;
330 if (uniqueFile.makeUnique(true, &ErrMsg)) {
331 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
332 exit(1);
333 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000334 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000335 std::vector<const char *> LLCArgs;
336 LLCArgs.push_back (LLCPath.c_str());
337
338 // Add any extra LLC args.
339 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
340 LLCArgs.push_back(ToolArgs[i].c_str());
341
342 LLCArgs.push_back ("-o");
343 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
344 LLCArgs.push_back ("-march=c"); // Output C language
345 LLCArgs.push_back ("-f"); // Overwrite as necessary...
346 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
347 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000348
349 std::cout << "<cbe>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000350 DEBUG(std::cerr << "\nAbout to run:\t";
351 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
352 std::cerr << " " << LLCArgs[i];
353 std::cerr << "\n";
354 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000355 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000356 sys::Path()))
Brian Gaeked11577b2004-05-04 21:09:01 +0000357 ProcessFailure(LLCPath, &LLCArgs[0]);
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000358 return GCC::CFile;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000359}
360
Chris Lattner9cbbee32004-02-18 23:24:41 +0000361void CBE::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000362 sys::Path OutputCFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000363 OutputCode(Bytecode, OutputCFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000364 OutputCFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000365}
366
Misha Brukman9558c6a2003-09-29 22:39:25 +0000367int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000368 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000369 const std::string &InputFile,
370 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000371 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000372 const std::vector<std::string> &SharedLibs,
373 unsigned Timeout) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000374 sys::Path OutputCFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000375 OutputCode(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000376
Chris Lattner8c56be52004-02-18 20:21:57 +0000377 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000378
Reid Spencer51ab5c82006-06-06 00:00:42 +0000379 std::vector<std::string> GCCArgs(ArgsForGCC);
380 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
Misha Brukmanf976c852005-04-21 22:55:34 +0000381 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000382 InputFile, OutputFile, GCCArgs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000383}
384
Chris Lattner9915cd92004-02-17 06:40:06 +0000385/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000386///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000387CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000388 std::string &Message,
389 const std::vector<std::string> *Args) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000390 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
391 if (LLCPath.isEmpty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000392 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000393 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000394 return 0;
395 }
396
Reid Spencerb31baa82004-12-19 18:00:21 +0000397 Message = "Found llc: " + LLCPath.toString() + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000398 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000399 if (!gcc) {
400 std::cerr << Message << "\n";
401 exit(1);
402 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000403 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000404}
405
406//===---------------------------------------------------------------------===//
407// GCC abstraction
408//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000409int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000410 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000411 FileType fileType,
412 const std::string &InputFile,
413 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000414 const std::vector<std::string> &ArgsForGCC,
415 unsigned Timeout ) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000416 std::vector<const char*> GCCArgs;
417
418 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000419
Chris Lattnereeed9832003-10-14 21:52:52 +0000420 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000421 GCCArgs.push_back("-x");
422 if (fileType == CFile) {
423 GCCArgs.push_back("c");
424 GCCArgs.push_back("-fno-strict-aliasing");
425 } else {
426 GCCArgs.push_back("assembler");
Chris Lattnerd00b2882005-08-29 13:14:24 +0000427#ifdef __APPLE__
428 GCCArgs.push_back("-force_cpusubtype_ALL");
429#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000430 }
431 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
Chris Lattner629e4872006-06-09 21:31:53 +0000432 GCCArgs.push_back("-x");
433 GCCArgs.push_back("none");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000434 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000435 sys::Path OutputBinary (ProgramFile+".gcc.exe");
Reid Spencer51c5a282006-08-23 20:34:57 +0000436 std::string ErrMsg;
437 if (OutputBinary.makeUnique(true, &ErrMsg)) {
438 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
439 exit(1);
440 }
Misha Brukman9558c6a2003-09-29 22:39:25 +0000441 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
Reid Spencer51ab5c82006-06-06 00:00:42 +0000442
443 // Add any arguments intended for GCC. We locate them here because this is
444 // most likely -L and -l options that need to come before other libraries but
445 // after the source. Other options won't be sensitive to placement on the
446 // command line, so this should be safe.
447 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
448 GCCArgs.push_back(ArgsForGCC[i].c_str());
449
Misha Brukman9558c6a2003-09-29 22:39:25 +0000450 GCCArgs.push_back("-lm"); // Hard-code the math library...
451 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000452#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000453 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000454#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000455#ifdef __sparc__
456 GCCArgs.push_back("-mcpu=v9");
457#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000458 GCCArgs.push_back(0); // NULL terminator
459
460 std::cout << "<gcc>" << std::flush;
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000461 DEBUG(std::cerr << "\nAbout to run:\t";
462 for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
463 std::cerr << " " << GCCArgs[i];
464 std::cerr << "\n";
465 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000466 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
467 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000468 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000469 exit(1);
470 }
471
472 std::vector<const char*> ProgramArgs;
Chris Lattner45495c52005-02-13 23:13:47 +0000473
Misha Brukman9558c6a2003-09-29 22:39:25 +0000474 ProgramArgs.push_back(OutputBinary.c_str());
475 // Add optional parameters to the running program from Argv
476 for (unsigned i=0, e = Args.size(); i != e; ++i)
477 ProgramArgs.push_back(Args[i].c_str());
478 ProgramArgs.push_back(0); // NULL terminator
479
480 // Now that we have a binary, run it!
481 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000482 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000483 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000484 std::cerr << " " << ProgramArgs[i];
485 std::cerr << "\n";
486 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000487
Reid Spencer9ac14182004-12-16 23:01:34 +0000488 FileRemover OutputBinaryRemover(OutputBinary);
Reid Spencerb31baa82004-12-19 18:00:21 +0000489 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000490 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000491 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000492}
493
Chris Lattner1798e4a2003-10-14 21:07:25 +0000494int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +0000495 std::string &OutputFile,
496 const std::vector<std::string> &ArgsForGCC) {
Reid Spencercda985e2004-12-15 01:51:56 +0000497 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
Reid Spencer51c5a282006-08-23 20:34:57 +0000498 std::string ErrMsg;
499 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
500 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
501 exit(1);
502 }
Reid Spencercda985e2004-12-15 01:51:56 +0000503 OutputFile = uniqueFilename.toString();
504
Chris Lattner130e2a32006-06-27 20:35:36 +0000505 std::vector<const char*> GCCArgs;
506
507 GCCArgs.push_back(GCCPath.c_str());
508
509
Misha Brukman9558c6a2003-09-29 22:39:25 +0000510 // Compile the C/asm file into a shared object
Chris Lattner130e2a32006-06-27 20:35:36 +0000511 GCCArgs.push_back("-x");
512 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
513 GCCArgs.push_back("-fno-strict-aliasing");
514 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
Misha Brukman9558c6a2003-09-29 22:39:25 +0000515#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Chris Lattner130e2a32006-06-27 20:35:36 +0000516 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
Nate Begeman72b286b2005-07-08 00:23:26 +0000517#elif defined(__APPLE__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000518 // link all source files into a single module in data segment, rather than
519 // generating blocks. dynamic_lookup requires that you set
520 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
521 // bugpoint to just pass that in the environment of GCC.
522 GCCArgs.push_back("-single_module");
523 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
524 GCCArgs.push_back("-undefined");
525 GCCArgs.push_back("dynamic_lookup");
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000526#else
Chris Lattner130e2a32006-06-27 20:35:36 +0000527 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
Misha Brukman9558c6a2003-09-29 22:39:25 +0000528#endif
Chris Lattner1c81f132005-03-09 03:31:02 +0000529
Andrew Lenharth572668a2005-03-10 20:15:09 +0000530#if defined(__ia64__) || defined(__alpha__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000531 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
Chris Lattner1c81f132005-03-09 03:31:02 +0000532#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000533#ifdef __sparc__
Chris Lattner130e2a32006-06-27 20:35:36 +0000534 GCCArgs.push_back("-mcpu=v9");
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000535#endif
Chris Lattner130e2a32006-06-27 20:35:36 +0000536 GCCArgs.push_back("-o");
537 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
538 GCCArgs.push_back("-O2"); // Optimize the program a bit.
539
540
541
542 // Add any arguments intended for GCC. We locate them here because this is
543 // most likely -L and -l options that need to come before other libraries but
544 // after the source. Other options won't be sensitive to placement on the
545 // command line, so this should be safe.
546 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
547 GCCArgs.push_back(ArgsForGCC[i].c_str());
548 GCCArgs.push_back(0); // NULL terminator
549
550
Misha Brukmanf976c852005-04-21 22:55:34 +0000551
Misha Brukman9558c6a2003-09-29 22:39:25 +0000552 std::cout << "<gcc>" << std::flush;
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000553 DEBUG(std::cerr << "\nAbout to run:\t";
554 for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
555 std::cerr << " " << GCCArgs[i];
556 std::cerr << "\n";
557 );
Chris Lattner130e2a32006-06-27 20:35:36 +0000558 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000559 sys::Path())) {
Chris Lattner130e2a32006-06-27 20:35:36 +0000560 ProcessFailure(GCCPath, &GCCArgs[0]);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000561 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000562 }
563 return 0;
564}
565
Chris Lattner7915a1e2003-10-14 21:34:11 +0000566/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000567///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000568GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000569 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
570 if (GCCPath.isEmpty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000571 Message = "Cannot find `gcc' in executable directory or PATH!\n";
572 return 0;
573 }
574
Reid Spencerb31baa82004-12-19 18:00:21 +0000575 Message = "Found gcc: " + GCCPath.toString() + "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000576 return new GCC(GCCPath);
577}