blob: a967e3e8377728a820d90c56a120504f4a96f671 [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"
Misha Brukman68734502003-10-06 18:37:24 +000015#include "llvm/Support/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,
Misha Brukmanf976c852005-04-21 22:55:34 +000091 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +000092 std::vector<std::string>(),
93 unsigned Timeout = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +000094 };
95}
Misha Brukman9558c6a2003-09-29 22:39:25 +000096
97int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000098 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000099 const std::string &InputFile,
100 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000101 const std::vector<std::string> &SharedLibs,
102 unsigned Timeout) {
Chris Lattner8c56be52004-02-18 20:21:57 +0000103 if (!SharedLibs.empty())
104 throw ToolExecutionError("LLI currently does not support "
105 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000106
107 std::vector<const char*> LLIArgs;
108 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000109 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +0000110
111 // Add any extra LLI args.
112 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
113 LLIArgs.push_back(ToolArgs[i].c_str());
114
Misha Brukman9558c6a2003-09-29 22:39:25 +0000115 LLIArgs.push_back(Bytecode.c_str());
116 // Add optional parameters to the running program from Argv
117 for (unsigned i=0, e = Args.size(); i != e; ++i)
118 LLIArgs.push_back(Args[i].c_str());
119 LLIArgs.push_back(0);
120
121 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000122 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000123 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000124 std::cerr << " " << LLIArgs[i];
125 std::cerr << "\n";
126 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000127 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000128 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000129 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000130}
131
132// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000133AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000134 std::string &Message,
135 const std::vector<std::string> *ToolArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000136 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000137 if (!LLIPath.empty()) {
138 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000139 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000140 }
141
142 Message = "Cannot find `lli' in executable directory or PATH!\n";
143 return 0;
144}
145
146//===----------------------------------------------------------------------===//
147// LLC Implementation of AbstractIntepreter interface
148//
Reid Spencer9ac14182004-12-16 23:01:34 +0000149void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000150 sys::Path uniqueFile(Bytecode+".llc.s");
151 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000152 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000153 std::vector<const char *> LLCArgs;
154 LLCArgs.push_back (LLCPath.c_str());
155
156 // Add any extra LLC args.
157 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
158 LLCArgs.push_back(ToolArgs[i].c_str());
159
160 LLCArgs.push_back ("-o");
161 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
162 LLCArgs.push_back ("-f"); // Overwrite as necessary...
163 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
164 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000165
166 std::cout << "<llc>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000167 DEBUG(std::cerr << "\nAbout to run:\t";
168 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
169 std::cerr << " " << LLCArgs[i];
170 std::cerr << "\n";
171 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000172 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
Reid Spencerb31baa82004-12-19 18:00:21 +0000173 sys::Path(), sys::Path(), sys::Path()))
174 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000175}
176
Chris Lattner9cbbee32004-02-18 23:24:41 +0000177void LLC::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000178 sys::Path OutputAsmFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000179 OutputAsm(Bytecode, OutputAsmFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000180 OutputAsmFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000181}
182
Misha Brukman9558c6a2003-09-29 22:39:25 +0000183int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000184 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000185 const std::string &InputFile,
186 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000187 const std::vector<std::string> &SharedLibs,
188 unsigned Timeout) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000189
Reid Spencer9ac14182004-12-16 23:01:34 +0000190 sys::Path OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000191 OutputAsm(Bytecode, OutputAsmFile);
192 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000193
194 // Assuming LLC worked, compile the result with GCC and run it.
Reid Spencer9ac14182004-12-16 23:01:34 +0000195 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000196 InputFile, OutputFile, SharedLibs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000197}
198
Chris Lattner7915a1e2003-10-14 21:34:11 +0000199/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000200///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000201LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000202 std::string &Message,
203 const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000204 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000205 if (LLCPath.empty()) {
206 Message = "Cannot find `llc' in executable directory or PATH!\n";
207 return 0;
208 }
209
210 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000211 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000212 if (!gcc) {
213 std::cerr << Message << "\n";
214 exit(1);
215 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000216 return new LLC(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000217}
218
219//===---------------------------------------------------------------------===//
220// JIT Implementation of AbstractIntepreter interface
221//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000222namespace {
223 class JIT : public AbstractInterpreter {
224 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000225 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000226 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000227 JIT(const std::string &Path, const std::vector<std::string> *Args)
228 : LLIPath(Path) {
229 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000230 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000231 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000232
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000233 virtual int ExecuteProgram(const std::string &Bytecode,
234 const std::vector<std::string> &Args,
235 const std::string &InputFile,
236 const std::string &OutputFile,
Misha Brukmanf976c852005-04-21 22:55:34 +0000237 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000238 std::vector<std::string>(), unsigned Timeout =0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000239 };
240}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000241
242int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000243 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000244 const std::string &InputFile,
245 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000246 const std::vector<std::string> &SharedLibs,
247 unsigned Timeout) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000248 // Construct a vector of parameters, incorporating those from the command-line
249 std::vector<const char*> JITArgs;
250 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000251 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000252
Brian Gaeked11577b2004-05-04 21:09:01 +0000253 // Add any extra LLI args.
254 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
255 JITArgs.push_back(ToolArgs[i].c_str());
256
Chris Lattnereeed9832003-10-14 21:52:52 +0000257 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000258 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000259 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000260 }
261 JITArgs.push_back(Bytecode.c_str());
262 // Add optional parameters to the running program from Argv
263 for (unsigned i=0, e = Args.size(); i != e; ++i)
264 JITArgs.push_back(Args[i].c_str());
265 JITArgs.push_back(0);
266
267 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000268 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000269 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000270 std::cerr << " " << JITArgs[i];
271 std::cerr << "\n";
272 );
273 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
Reid Spencerb31baa82004-12-19 18:00:21 +0000274 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000275 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000276 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000277}
278
Chris Lattner7915a1e2003-10-14 21:34:11 +0000279/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000280///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000281AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000282 std::string &Message, const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000283 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000284 if (!LLIPath.empty()) {
285 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000286 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000287 }
288
289 Message = "Cannot find `lli' in executable directory or PATH!\n";
290 return 0;
291}
292
Reid Spencer9ac14182004-12-16 23:01:34 +0000293void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000294 sys::Path uniqueFile(Bytecode+".cbe.c");
295 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000296 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000297 std::vector<const char *> LLCArgs;
298 LLCArgs.push_back (LLCPath.c_str());
299
300 // Add any extra LLC args.
301 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
302 LLCArgs.push_back(ToolArgs[i].c_str());
303
304 LLCArgs.push_back ("-o");
305 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
306 LLCArgs.push_back ("-march=c"); // Output C language
307 LLCArgs.push_back ("-f"); // Overwrite as necessary...
308 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
309 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000310
311 std::cout << "<cbe>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000312 DEBUG(std::cerr << "\nAbout to run:\t";
313 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
314 std::cerr << " " << LLCArgs[i];
315 std::cerr << "\n";
316 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000317 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000318 sys::Path()))
Brian Gaeked11577b2004-05-04 21:09:01 +0000319 ProcessFailure(LLCPath, &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000320}
321
Chris Lattner9cbbee32004-02-18 23:24:41 +0000322void CBE::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000323 sys::Path OutputCFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000324 OutputC(Bytecode, OutputCFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000325 OutputCFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000326}
327
Misha Brukman9558c6a2003-09-29 22:39:25 +0000328int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000329 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000330 const std::string &InputFile,
331 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000332 const std::vector<std::string> &SharedLibs,
333 unsigned Timeout) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000334 sys::Path OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000335 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000336
Chris Lattner8c56be52004-02-18 20:21:57 +0000337 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000338
Misha Brukmanf976c852005-04-21 22:55:34 +0000339 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000340 InputFile, OutputFile, SharedLibs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000341}
342
Chris Lattner9915cd92004-02-17 06:40:06 +0000343/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000344///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000345CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000346 std::string &Message,
347 const std::vector<std::string> *Args) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000348 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
349 if (LLCPath.isEmpty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000350 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000351 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000352 return 0;
353 }
354
Reid Spencerb31baa82004-12-19 18:00:21 +0000355 Message = "Found llc: " + LLCPath.toString() + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000356 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000357 if (!gcc) {
358 std::cerr << Message << "\n";
359 exit(1);
360 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000361 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000362}
363
364//===---------------------------------------------------------------------===//
365// GCC abstraction
366//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000367int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000368 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000369 FileType fileType,
370 const std::string &InputFile,
371 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000372 const std::vector<std::string> &SharedLibs,
373 unsigned Timeout) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000374 std::vector<const char*> GCCArgs;
375
376 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000377
378 // Specify the shared libraries to link in...
379 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
380 GCCArgs.push_back(SharedLibs[i].c_str());
Misha Brukmanf976c852005-04-21 22:55:34 +0000381
Chris Lattnereeed9832003-10-14 21:52:52 +0000382 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000383 GCCArgs.push_back("-x");
384 if (fileType == CFile) {
385 GCCArgs.push_back("c");
386 GCCArgs.push_back("-fno-strict-aliasing");
387 } else {
388 GCCArgs.push_back("assembler");
Chris Lattnerd00b2882005-08-29 13:14:24 +0000389#ifdef __APPLE__
390 GCCArgs.push_back("-force_cpusubtype_ALL");
391#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000392 }
393 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
394 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000395 sys::Path OutputBinary (ProgramFile+".gcc.exe");
396 OutputBinary.makeUnique();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000397 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
398 GCCArgs.push_back("-lm"); // Hard-code the math library...
399 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000400#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000401 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000402#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000403#ifdef __sparc__
404 GCCArgs.push_back("-mcpu=v9");
405#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000406 GCCArgs.push_back(0); // NULL terminator
407
408 std::cout << "<gcc>" << std::flush;
Reid Spencerb31baa82004-12-19 18:00:21 +0000409 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
410 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000411 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000412 exit(1);
413 }
414
415 std::vector<const char*> ProgramArgs;
Chris Lattner45495c52005-02-13 23:13:47 +0000416
Misha Brukman9558c6a2003-09-29 22:39:25 +0000417 ProgramArgs.push_back(OutputBinary.c_str());
418 // Add optional parameters to the running program from Argv
419 for (unsigned i=0, e = Args.size(); i != e; ++i)
420 ProgramArgs.push_back(Args[i].c_str());
421 ProgramArgs.push_back(0); // NULL terminator
422
423 // Now that we have a binary, run it!
424 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000425 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000426 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000427 std::cerr << " " << ProgramArgs[i];
428 std::cerr << "\n";
429 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000430
Reid Spencer9ac14182004-12-16 23:01:34 +0000431 FileRemover OutputBinaryRemover(OutputBinary);
Reid Spencerb31baa82004-12-19 18:00:21 +0000432 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000433 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000434 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000435}
436
Chris Lattner1798e4a2003-10-14 21:07:25 +0000437int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000438 std::string &OutputFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000439 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
440 uniqueFilename.makeUnique();
441 OutputFile = uniqueFilename.toString();
442
Misha Brukman9558c6a2003-09-29 22:39:25 +0000443 // Compile the C/asm file into a shared object
444 const char* GCCArgs[] = {
445 GCCPath.c_str(),
446 "-x", (fileType == AsmFile) ? "assembler" : "c",
447 "-fno-strict-aliasing",
448 InputFile.c_str(), // Specify the input filename...
449#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
450 "-G", // Compile a shared library, `-G' for Sparc
Nate Begeman72b286b2005-07-08 00:23:26 +0000451#elif defined(__APPLE__)
Nate Begeman79255882004-10-17 23:03:32 +0000452 "-single_module", // link all source files into a single module
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000453 "-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC
Chris Lattnere312e152004-07-19 06:03:51 +0000454 "-undefined", // in data segment, rather than generating
Chris Lattner5fbf29c2004-07-19 06:00:17 +0000455 "dynamic_lookup", // blocks. dynamic_lookup requires that you set
456 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000457#else
Misha Brukman9558c6a2003-09-29 22:39:25 +0000458 "-shared", // `-shared' for Linux/X86, maybe others
459#endif
Chris Lattner1c81f132005-03-09 03:31:02 +0000460
Andrew Lenharth572668a2005-03-10 20:15:09 +0000461#if defined(__ia64__) || defined(__alpha__)
Chris Lattner1c81f132005-03-09 03:31:02 +0000462 "-fPIC", // IA64 requires shared objs to contain PIC
463#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000464#ifdef __sparc__
465 "-mcpu=v9",
466#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000467 "-o", OutputFile.c_str(), // Output to the right filename...
468 "-O2", // Optimize the program a bit...
469 0
470 };
Misha Brukmanf976c852005-04-21 22:55:34 +0000471
Misha Brukman9558c6a2003-09-29 22:39:25 +0000472 std::cout << "<gcc>" << std::flush;
Misha Brukmanf976c852005-04-21 22:55:34 +0000473 if (RunProgramWithTimeout(GCCPath, GCCArgs, sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000474 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000475 ProcessFailure(GCCPath, GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000476 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000477 }
478 return 0;
479}
480
Chris Lattner7915a1e2003-10-14 21:34:11 +0000481/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000482///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000483GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000484 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
485 if (GCCPath.isEmpty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000486 Message = "Cannot find `gcc' in executable directory or PATH!\n";
487 return 0;
488 }
489
Reid Spencerb31baa82004-12-19 18:00:21 +0000490 Message = "Found gcc: " + GCCPath.toString() + "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000491 return new GCC(GCCPath);
492}