blob: 589f5158d141449ded00c1c89d915b77e6a84b11 [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");
Reid Spencer51c5a282006-08-23 20:34:57 +000056 std::string ErrMsg;
57 if (ErrorFilename.makeUnique(true, &ErrMsg)) {
58 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
59 exit(1);
60 }
Reid Spencerb31baa82004-12-19 18:00:21 +000061 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
Reid Spencer8ea5ecb2006-08-21 06:04:45 +000062 ErrorFilename); // FIXME: check return code ?
Chris Lattner89bf9ea2004-02-18 20:38:00 +000063
64 // Print out the error messages generated by GCC if possible...
65 std::ifstream ErrorFile(ErrorFilename.c_str());
66 if (ErrorFile) {
67 std::copy(std::istreambuf_iterator<char>(ErrorFile),
68 std::istreambuf_iterator<char>(),
69 std::ostreambuf_iterator<char>(OS));
70 ErrorFile.close();
71 }
72
Reid Spencera229c5c2005-07-08 03:08:58 +000073 ErrorFilename.eraseFromDisk();
Chris Lattner89bf9ea2004-02-18 20:38:00 +000074 throw ToolExecutionError(OS.str());
75}
76
Misha Brukman9558c6a2003-09-29 22:39:25 +000077//===---------------------------------------------------------------------===//
78// LLI Implementation of AbstractIntepreter interface
79//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000080namespace {
81 class LLI : public AbstractInterpreter {
82 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +000083 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +000084 public:
Brian Gaeked11577b2004-05-04 21:09:01 +000085 LLI(const std::string &Path, const std::vector<std::string> *Args)
86 : LLIPath(Path) {
87 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +000088 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +000089 }
Misha Brukmanf976c852005-04-21 22:55:34 +000090
Chris Lattner2cdd21c2003-12-14 21:35:53 +000091 virtual int ExecuteProgram(const std::string &Bytecode,
92 const std::vector<std::string> &Args,
93 const std::string &InputFile,
94 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +000095 const std::vector<std::string> &GCCArgs,
Misha Brukmanf976c852005-04-21 22:55:34 +000096 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +000097 std::vector<std::string>(),
98 unsigned Timeout = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +000099 };
100}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000101
102int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000103 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000104 const std::string &InputFile,
105 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000106 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000107 const std::vector<std::string> &SharedLibs,
108 unsigned Timeout) {
Chris Lattner8c56be52004-02-18 20:21:57 +0000109 if (!SharedLibs.empty())
110 throw ToolExecutionError("LLI currently does not support "
111 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000112
Reid Spencer51ab5c82006-06-06 00:00:42 +0000113 if (!GCCArgs.empty())
114 throw ToolExecutionError("LLI currently does not support "
115 "GCC Arguments.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000116 std::vector<const char*> LLIArgs;
117 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000118 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +0000119
120 // Add any extra LLI args.
121 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
122 LLIArgs.push_back(ToolArgs[i].c_str());
123
Misha Brukman9558c6a2003-09-29 22:39:25 +0000124 LLIArgs.push_back(Bytecode.c_str());
125 // Add optional parameters to the running program from Argv
126 for (unsigned i=0, e = Args.size(); i != e; ++i)
127 LLIArgs.push_back(Args[i].c_str());
128 LLIArgs.push_back(0);
129
130 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000131 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000132 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000133 std::cerr << " " << LLIArgs[i];
134 std::cerr << "\n";
135 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000136 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000137 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000138 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000139}
140
141// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000142AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000143 std::string &Message,
144 const std::vector<std::string> *ToolArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000145 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000146 if (!LLIPath.empty()) {
147 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000148 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000149 }
150
151 Message = "Cannot find `lli' in executable directory or PATH!\n";
152 return 0;
153}
154
155//===----------------------------------------------------------------------===//
156// LLC Implementation of AbstractIntepreter interface
157//
Reid Spencer9ac14182004-12-16 23:01:34 +0000158void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000159 sys::Path uniqueFile(Bytecode+".llc.s");
Reid Spencer51c5a282006-08-23 20:34:57 +0000160 std::string ErrMsg;
161 if (uniqueFile.makeUnique(true, &ErrMsg)) {
162 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
163 exit(1);
164 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000165 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000166 std::vector<const char *> LLCArgs;
167 LLCArgs.push_back (LLCPath.c_str());
168
169 // Add any extra LLC args.
170 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
171 LLCArgs.push_back(ToolArgs[i].c_str());
172
173 LLCArgs.push_back ("-o");
174 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
175 LLCArgs.push_back ("-f"); // Overwrite as necessary...
176 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
177 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000178
179 std::cout << "<llc>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000180 DEBUG(std::cerr << "\nAbout to run:\t";
181 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
182 std::cerr << " " << LLCArgs[i];
183 std::cerr << "\n";
184 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000185 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
Reid Spencerb31baa82004-12-19 18:00:21 +0000186 sys::Path(), sys::Path(), sys::Path()))
187 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000188}
189
Chris Lattner9cbbee32004-02-18 23:24:41 +0000190void LLC::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000191 sys::Path OutputAsmFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000192 OutputAsm(Bytecode, OutputAsmFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000193 OutputAsmFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000194}
195
Misha Brukman9558c6a2003-09-29 22:39:25 +0000196int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000197 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000198 const std::string &InputFile,
199 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000200 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000201 const std::vector<std::string> &SharedLibs,
202 unsigned Timeout) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000203
Reid Spencer9ac14182004-12-16 23:01:34 +0000204 sys::Path OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000205 OutputAsm(Bytecode, OutputAsmFile);
206 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000207
Reid Spencer51ab5c82006-06-06 00:00:42 +0000208 std::vector<std::string> GCCArgs(ArgsForGCC);
209 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
210
Misha Brukman9558c6a2003-09-29 22:39:25 +0000211 // Assuming LLC worked, compile the result with GCC and run it.
Reid Spencer9ac14182004-12-16 23:01:34 +0000212 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000213 InputFile, OutputFile, GCCArgs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000214}
215
Chris Lattner7915a1e2003-10-14 21:34:11 +0000216/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000217///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000218LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000219 std::string &Message,
220 const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000221 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000222 if (LLCPath.empty()) {
223 Message = "Cannot find `llc' in executable directory or PATH!\n";
224 return 0;
225 }
226
227 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000228 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000229 if (!gcc) {
230 std::cerr << Message << "\n";
231 exit(1);
232 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000233 return new LLC(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000234}
235
236//===---------------------------------------------------------------------===//
237// JIT Implementation of AbstractIntepreter interface
238//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000239namespace {
240 class JIT : public AbstractInterpreter {
241 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000242 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000243 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000244 JIT(const std::string &Path, const std::vector<std::string> *Args)
245 : LLIPath(Path) {
246 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000247 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000248 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000249
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000250 virtual int ExecuteProgram(const std::string &Bytecode,
251 const std::vector<std::string> &Args,
252 const std::string &InputFile,
253 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000254 const std::vector<std::string> &GCCArgs =
255 std::vector<std::string>(),
Misha Brukmanf976c852005-04-21 22:55:34 +0000256 const std::vector<std::string> &SharedLibs =
Reid Spencer51ab5c82006-06-06 00:00:42 +0000257 std::vector<std::string>(),
258 unsigned Timeout =0 );
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000259 };
260}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000261
262int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000263 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000264 const std::string &InputFile,
265 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000266 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000267 const std::vector<std::string> &SharedLibs,
268 unsigned Timeout) {
Reid Spencer51ab5c82006-06-06 00:00:42 +0000269 if (!GCCArgs.empty())
270 throw ToolExecutionError("JIT does not support GCC Arguments.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000271 // Construct a vector of parameters, incorporating those from the command-line
272 std::vector<const char*> JITArgs;
273 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000274 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000275
Brian Gaeked11577b2004-05-04 21:09:01 +0000276 // Add any extra LLI args.
277 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
278 JITArgs.push_back(ToolArgs[i].c_str());
279
Chris Lattnereeed9832003-10-14 21:52:52 +0000280 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000281 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000282 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000283 }
284 JITArgs.push_back(Bytecode.c_str());
285 // Add optional parameters to the running program from Argv
286 for (unsigned i=0, e = Args.size(); i != e; ++i)
287 JITArgs.push_back(Args[i].c_str());
288 JITArgs.push_back(0);
289
290 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000291 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000292 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000293 std::cerr << " " << JITArgs[i];
294 std::cerr << "\n";
295 );
296 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
Reid Spencerb31baa82004-12-19 18:00:21 +0000297 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000298 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000299 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000300}
301
Chris Lattner7915a1e2003-10-14 21:34:11 +0000302/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000303///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000304AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000305 std::string &Message, const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000306 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000307 if (!LLIPath.empty()) {
308 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000309 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000310 }
311
312 Message = "Cannot find `lli' in executable directory or PATH!\n";
313 return 0;
314}
315
Reid Spencer9ac14182004-12-16 23:01:34 +0000316void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000317 sys::Path uniqueFile(Bytecode+".cbe.c");
Reid Spencer51c5a282006-08-23 20:34:57 +0000318 std::string ErrMsg;
319 if (uniqueFile.makeUnique(true, &ErrMsg)) {
320 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
321 exit(1);
322 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000323 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000324 std::vector<const char *> LLCArgs;
325 LLCArgs.push_back (LLCPath.c_str());
326
327 // Add any extra LLC args.
328 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
329 LLCArgs.push_back(ToolArgs[i].c_str());
330
331 LLCArgs.push_back ("-o");
332 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
333 LLCArgs.push_back ("-march=c"); // Output C language
334 LLCArgs.push_back ("-f"); // Overwrite as necessary...
335 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
336 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000337
338 std::cout << "<cbe>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000339 DEBUG(std::cerr << "\nAbout to run:\t";
340 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
341 std::cerr << " " << LLCArgs[i];
342 std::cerr << "\n";
343 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000344 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000345 sys::Path()))
Brian Gaeked11577b2004-05-04 21:09:01 +0000346 ProcessFailure(LLCPath, &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000347}
348
Chris Lattner9cbbee32004-02-18 23:24:41 +0000349void CBE::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000350 sys::Path OutputCFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000351 OutputC(Bytecode, OutputCFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000352 OutputCFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000353}
354
Misha Brukman9558c6a2003-09-29 22:39:25 +0000355int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000356 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000357 const std::string &InputFile,
358 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000359 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000360 const std::vector<std::string> &SharedLibs,
361 unsigned Timeout) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000362 sys::Path OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000363 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000364
Chris Lattner8c56be52004-02-18 20:21:57 +0000365 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000366
Reid Spencer51ab5c82006-06-06 00:00:42 +0000367 std::vector<std::string> GCCArgs(ArgsForGCC);
368 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
Misha Brukmanf976c852005-04-21 22:55:34 +0000369 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000370 InputFile, OutputFile, GCCArgs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000371}
372
Chris Lattner9915cd92004-02-17 06:40:06 +0000373/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000374///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000375CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000376 std::string &Message,
377 const std::vector<std::string> *Args) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000378 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
379 if (LLCPath.isEmpty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000380 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000381 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000382 return 0;
383 }
384
Reid Spencerb31baa82004-12-19 18:00:21 +0000385 Message = "Found llc: " + LLCPath.toString() + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000386 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000387 if (!gcc) {
388 std::cerr << Message << "\n";
389 exit(1);
390 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000391 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000392}
393
394//===---------------------------------------------------------------------===//
395// GCC abstraction
396//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000397int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000398 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000399 FileType fileType,
400 const std::string &InputFile,
401 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000402 const std::vector<std::string> &ArgsForGCC,
403 unsigned Timeout ) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000404 std::vector<const char*> GCCArgs;
405
406 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000407
Chris Lattnereeed9832003-10-14 21:52:52 +0000408 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000409 GCCArgs.push_back("-x");
410 if (fileType == CFile) {
411 GCCArgs.push_back("c");
412 GCCArgs.push_back("-fno-strict-aliasing");
413 } else {
414 GCCArgs.push_back("assembler");
Chris Lattnerd00b2882005-08-29 13:14:24 +0000415#ifdef __APPLE__
416 GCCArgs.push_back("-force_cpusubtype_ALL");
417#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000418 }
419 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
Chris Lattner629e4872006-06-09 21:31:53 +0000420 GCCArgs.push_back("-x");
421 GCCArgs.push_back("none");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000422 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000423 sys::Path OutputBinary (ProgramFile+".gcc.exe");
Reid Spencer51c5a282006-08-23 20:34:57 +0000424 std::string ErrMsg;
425 if (OutputBinary.makeUnique(true, &ErrMsg)) {
426 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
427 exit(1);
428 }
Misha Brukman9558c6a2003-09-29 22:39:25 +0000429 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
Reid Spencer51ab5c82006-06-06 00:00:42 +0000430
431 // Add any arguments intended for GCC. We locate them here because this is
432 // most likely -L and -l options that need to come before other libraries but
433 // after the source. Other options won't be sensitive to placement on the
434 // command line, so this should be safe.
435 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
436 GCCArgs.push_back(ArgsForGCC[i].c_str());
437
Misha Brukman9558c6a2003-09-29 22:39:25 +0000438 GCCArgs.push_back("-lm"); // Hard-code the math library...
439 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000440#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000441 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000442#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000443#ifdef __sparc__
444 GCCArgs.push_back("-mcpu=v9");
445#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000446 GCCArgs.push_back(0); // NULL terminator
447
448 std::cout << "<gcc>" << std::flush;
Reid Spencerb31baa82004-12-19 18:00:21 +0000449 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
450 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000451 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000452 exit(1);
453 }
454
455 std::vector<const char*> ProgramArgs;
Chris Lattner45495c52005-02-13 23:13:47 +0000456
Misha Brukman9558c6a2003-09-29 22:39:25 +0000457 ProgramArgs.push_back(OutputBinary.c_str());
458 // Add optional parameters to the running program from Argv
459 for (unsigned i=0, e = Args.size(); i != e; ++i)
460 ProgramArgs.push_back(Args[i].c_str());
461 ProgramArgs.push_back(0); // NULL terminator
462
463 // Now that we have a binary, run it!
464 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000465 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000466 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000467 std::cerr << " " << ProgramArgs[i];
468 std::cerr << "\n";
469 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000470
Reid Spencer9ac14182004-12-16 23:01:34 +0000471 FileRemover OutputBinaryRemover(OutputBinary);
Reid Spencerb31baa82004-12-19 18:00:21 +0000472 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000473 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000474 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000475}
476
Chris Lattner1798e4a2003-10-14 21:07:25 +0000477int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +0000478 std::string &OutputFile,
479 const std::vector<std::string> &ArgsForGCC) {
Reid Spencercda985e2004-12-15 01:51:56 +0000480 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
Reid Spencer51c5a282006-08-23 20:34:57 +0000481 std::string ErrMsg;
482 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
483 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
484 exit(1);
485 }
Reid Spencercda985e2004-12-15 01:51:56 +0000486 OutputFile = uniqueFilename.toString();
487
Chris Lattner130e2a32006-06-27 20:35:36 +0000488 std::vector<const char*> GCCArgs;
489
490 GCCArgs.push_back(GCCPath.c_str());
491
492
Misha Brukman9558c6a2003-09-29 22:39:25 +0000493 // Compile the C/asm file into a shared object
Chris Lattner130e2a32006-06-27 20:35:36 +0000494 GCCArgs.push_back("-x");
495 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
496 GCCArgs.push_back("-fno-strict-aliasing");
497 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
Misha Brukman9558c6a2003-09-29 22:39:25 +0000498#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Chris Lattner130e2a32006-06-27 20:35:36 +0000499 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
Nate Begeman72b286b2005-07-08 00:23:26 +0000500#elif defined(__APPLE__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000501 // link all source files into a single module in data segment, rather than
502 // generating blocks. dynamic_lookup requires that you set
503 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
504 // bugpoint to just pass that in the environment of GCC.
505 GCCArgs.push_back("-single_module");
506 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
507 GCCArgs.push_back("-undefined");
508 GCCArgs.push_back("dynamic_lookup");
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000509#else
Chris Lattner130e2a32006-06-27 20:35:36 +0000510 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
Misha Brukman9558c6a2003-09-29 22:39:25 +0000511#endif
Chris Lattner1c81f132005-03-09 03:31:02 +0000512
Andrew Lenharth572668a2005-03-10 20:15:09 +0000513#if defined(__ia64__) || defined(__alpha__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000514 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
Chris Lattner1c81f132005-03-09 03:31:02 +0000515#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000516#ifdef __sparc__
Chris Lattner130e2a32006-06-27 20:35:36 +0000517 GCCArgs.push_back("-mcpu=v9");
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000518#endif
Chris Lattner130e2a32006-06-27 20:35:36 +0000519 GCCArgs.push_back("-o");
520 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
521 GCCArgs.push_back("-O2"); // Optimize the program a bit.
522
523
524
525 // Add any arguments intended for GCC. We locate them here because this is
526 // most likely -L and -l options that need to come before other libraries but
527 // after the source. Other options won't be sensitive to placement on the
528 // command line, so this should be safe.
529 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
530 GCCArgs.push_back(ArgsForGCC[i].c_str());
531 GCCArgs.push_back(0); // NULL terminator
532
533
Misha Brukmanf976c852005-04-21 22:55:34 +0000534
Misha Brukman9558c6a2003-09-29 22:39:25 +0000535 std::cout << "<gcc>" << std::flush;
Chris Lattner130e2a32006-06-27 20:35:36 +0000536 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000537 sys::Path())) {
Chris Lattner130e2a32006-06-27 20:35:36 +0000538 ProcessFailure(GCCPath, &GCCArgs[0]);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000539 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000540 }
541 return 0;
542}
543
Chris Lattner7915a1e2003-10-14 21:34:11 +0000544/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000545///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000546GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000547 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
548 if (GCCPath.isEmpty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000549 Message = "Cannot find `gcc' in executable directory or PATH!\n";
550 return 0;
551 }
552
Reid Spencerb31baa82004-12-19 18:00:21 +0000553 Message = "Found gcc: " + GCCPath.toString() + "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000554 return new GCC(GCCPath);
555}