blob: 150155fc3db3c2cbfa21f54415161be8cfe98a3a [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,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000035 unsigned NumSeconds = 0,
36 unsigned MemoryLimit = 0) {
Chris Lattner45495c52005-02-13 23:13:47 +000037 const sys::Path* redirects[3];
38 redirects[0] = &StdInFile;
39 redirects[1] = &StdOutFile;
40 redirects[2] = &StdErrFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +000041
Chris Lattnerd4223502006-09-15 23:01:10 +000042 if (0) {
43 std::cerr << "RUN:";
44 for (unsigned i = 0; Args[i]; ++i)
45 std::cerr << " " << Args[i];
46 std::cerr << "\n";
47 }
Misha Brukmanf976c852005-04-21 22:55:34 +000048
49 return
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000050 sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects,
51 NumSeconds, MemoryLimit);
Chris Lattner45495c52005-02-13 23:13:47 +000052}
53
54
55
Reid Spencerb31baa82004-12-19 18:00:21 +000056static void ProcessFailure(sys::Path ProgPath, const char** Args) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +000057 std::ostringstream OS;
Chris Lattnera3de1172004-02-18 20:58:00 +000058 OS << "\nError running tool:\n ";
Chris Lattner89bf9ea2004-02-18 20:38:00 +000059 for (const char **Arg = Args; *Arg; ++Arg)
60 OS << " " << *Arg;
61 OS << "\n";
62
63 // Rerun the compiler, capturing any error messages to print them.
Reid Spencercda985e2004-12-15 01:51:56 +000064 sys::Path ErrorFilename("error_messages");
Reid Spencer51c5a282006-08-23 20:34:57 +000065 std::string ErrMsg;
66 if (ErrorFilename.makeUnique(true, &ErrMsg)) {
67 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
68 exit(1);
69 }
Reid Spencerb31baa82004-12-19 18:00:21 +000070 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
Reid Spencer8ea5ecb2006-08-21 06:04:45 +000071 ErrorFilename); // FIXME: check return code ?
Chris Lattner89bf9ea2004-02-18 20:38:00 +000072
73 // Print out the error messages generated by GCC if possible...
74 std::ifstream ErrorFile(ErrorFilename.c_str());
75 if (ErrorFile) {
76 std::copy(std::istreambuf_iterator<char>(ErrorFile),
77 std::istreambuf_iterator<char>(),
78 std::ostreambuf_iterator<char>(OS));
79 ErrorFile.close();
80 }
81
Reid Spencera229c5c2005-07-08 03:08:58 +000082 ErrorFilename.eraseFromDisk();
Chris Lattner89bf9ea2004-02-18 20:38:00 +000083 throw ToolExecutionError(OS.str());
84}
85
Misha Brukman9558c6a2003-09-29 22:39:25 +000086//===---------------------------------------------------------------------===//
87// LLI Implementation of AbstractIntepreter interface
88//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000089namespace {
90 class LLI : public AbstractInterpreter {
91 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +000092 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +000093 public:
Brian Gaeked11577b2004-05-04 21:09:01 +000094 LLI(const std::string &Path, const std::vector<std::string> *Args)
95 : LLIPath(Path) {
96 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +000097 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +000098 }
Misha Brukmanf976c852005-04-21 22:55:34 +000099
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000100 virtual int ExecuteProgram(const std::string &Bytecode,
101 const std::vector<std::string> &Args,
102 const std::string &InputFile,
103 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000104 const std::vector<std::string> &GCCArgs,
Misha Brukmanf976c852005-04-21 22:55:34 +0000105 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000106 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000107 unsigned Timeout = 0,
108 unsigned MemoryLimit = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000109 };
110}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000111
112int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000113 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000114 const std::string &InputFile,
115 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000116 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000117 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000118 unsigned Timeout,
119 unsigned MemoryLimit) {
Chris Lattner8c56be52004-02-18 20:21:57 +0000120 if (!SharedLibs.empty())
121 throw ToolExecutionError("LLI currently does not support "
122 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000123
Reid Spencer51ab5c82006-06-06 00:00:42 +0000124 if (!GCCArgs.empty())
125 throw ToolExecutionError("LLI currently does not support "
126 "GCC Arguments.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000127 std::vector<const char*> LLIArgs;
128 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000129 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +0000130
131 // Add any extra LLI args.
132 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
133 LLIArgs.push_back(ToolArgs[i].c_str());
134
Misha Brukman9558c6a2003-09-29 22:39:25 +0000135 LLIArgs.push_back(Bytecode.c_str());
136 // Add optional parameters to the running program from Argv
137 for (unsigned i=0, e = Args.size(); i != e; ++i)
138 LLIArgs.push_back(Args[i].c_str());
139 LLIArgs.push_back(0);
140
141 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000142 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000143 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000144 std::cerr << " " << LLIArgs[i];
145 std::cerr << "\n";
146 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000147 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000148 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000149 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000150}
151
152// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000153AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000154 std::string &Message,
155 const std::vector<std::string> *ToolArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000156 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000157 if (!LLIPath.empty()) {
158 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000159 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000160 }
161
162 Message = "Cannot find `lli' in executable directory or PATH!\n";
163 return 0;
164}
165
166//===----------------------------------------------------------------------===//
167// LLC Implementation of AbstractIntepreter interface
168//
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000169GCC::FileType LLC::OutputCode(const std::string &Bytecode,
170 sys::Path &OutputAsmFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000171 sys::Path uniqueFile(Bytecode+".llc.s");
Reid Spencer51c5a282006-08-23 20:34:57 +0000172 std::string ErrMsg;
173 if (uniqueFile.makeUnique(true, &ErrMsg)) {
174 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
175 exit(1);
176 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000177 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000178 std::vector<const char *> LLCArgs;
179 LLCArgs.push_back (LLCPath.c_str());
180
181 // Add any extra LLC args.
182 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
183 LLCArgs.push_back(ToolArgs[i].c_str());
184
185 LLCArgs.push_back ("-o");
186 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
187 LLCArgs.push_back ("-f"); // Overwrite as necessary...
188 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
189 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000190
191 std::cout << "<llc>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000192 DEBUG(std::cerr << "\nAbout to run:\t";
193 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
194 std::cerr << " " << LLCArgs[i];
195 std::cerr << "\n";
196 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000197 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
Reid Spencerb31baa82004-12-19 18:00:21 +0000198 sys::Path(), sys::Path(), sys::Path()))
199 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000200
201 return GCC::AsmFile;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000202}
203
Chris Lattner9cbbee32004-02-18 23:24:41 +0000204void LLC::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000205 sys::Path OutputAsmFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000206 OutputCode(Bytecode, OutputAsmFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000207 OutputAsmFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000208}
209
Misha Brukman9558c6a2003-09-29 22:39:25 +0000210int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000211 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000212 const std::string &InputFile,
213 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000214 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000215 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000216 unsigned Timeout,
217 unsigned MemoryLimit) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000218
Reid Spencer9ac14182004-12-16 23:01:34 +0000219 sys::Path OutputAsmFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000220 OutputCode(Bytecode, OutputAsmFile);
Chris Lattner8c56be52004-02-18 20:21:57 +0000221 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000222
Reid Spencer51ab5c82006-06-06 00:00:42 +0000223 std::vector<std::string> GCCArgs(ArgsForGCC);
224 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
225
Misha Brukman9558c6a2003-09-29 22:39:25 +0000226 // Assuming LLC worked, compile the result with GCC and run it.
Reid Spencer9ac14182004-12-16 23:01:34 +0000227 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000228 InputFile, OutputFile, GCCArgs,
229 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000230}
231
Chris Lattner7915a1e2003-10-14 21:34:11 +0000232/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000233///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000234LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000235 std::string &Message,
236 const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000237 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000238 if (LLCPath.empty()) {
239 Message = "Cannot find `llc' in executable directory or PATH!\n";
240 return 0;
241 }
242
243 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000244 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000245 if (!gcc) {
246 std::cerr << Message << "\n";
247 exit(1);
248 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000249 return new LLC(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000250}
251
252//===---------------------------------------------------------------------===//
253// JIT Implementation of AbstractIntepreter interface
254//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000255namespace {
256 class JIT : public AbstractInterpreter {
257 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000258 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000259 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000260 JIT(const std::string &Path, const std::vector<std::string> *Args)
261 : LLIPath(Path) {
262 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000263 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000264 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000265
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000266 virtual int ExecuteProgram(const std::string &Bytecode,
267 const std::vector<std::string> &Args,
268 const std::string &InputFile,
269 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000270 const std::vector<std::string> &GCCArgs =
271 std::vector<std::string>(),
Misha Brukmanf976c852005-04-21 22:55:34 +0000272 const std::vector<std::string> &SharedLibs =
Reid Spencer51ab5c82006-06-06 00:00:42 +0000273 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000274 unsigned Timeout =0,
275 unsigned MemoryLimit =0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000276 };
277}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000278
279int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000280 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000281 const std::string &InputFile,
282 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000283 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000284 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000285 unsigned Timeout,
286 unsigned MemoryLimit) {
Reid Spencer51ab5c82006-06-06 00:00:42 +0000287 if (!GCCArgs.empty())
288 throw ToolExecutionError("JIT does not support GCC Arguments.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000289 // Construct a vector of parameters, incorporating those from the command-line
290 std::vector<const char*> JITArgs;
291 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000292 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000293
Brian Gaeked11577b2004-05-04 21:09:01 +0000294 // Add any extra LLI args.
295 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
296 JITArgs.push_back(ToolArgs[i].c_str());
297
Chris Lattnereeed9832003-10-14 21:52:52 +0000298 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000299 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000300 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000301 }
302 JITArgs.push_back(Bytecode.c_str());
303 // Add optional parameters to the running program from Argv
304 for (unsigned i=0, e = Args.size(); i != e; ++i)
305 JITArgs.push_back(Args[i].c_str());
306 JITArgs.push_back(0);
307
308 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000309 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000310 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000311 std::cerr << " " << JITArgs[i];
312 std::cerr << "\n";
313 );
314 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
Reid Spencerb31baa82004-12-19 18:00:21 +0000315 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000316 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000317 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000318}
319
Chris Lattner7915a1e2003-10-14 21:34:11 +0000320/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000321///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000322AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000323 std::string &Message, const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000324 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000325 if (!LLIPath.empty()) {
326 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000327 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000328 }
329
330 Message = "Cannot find `lli' in executable directory or PATH!\n";
331 return 0;
332}
333
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000334GCC::FileType CBE::OutputCode(const std::string &Bytecode,
335 sys::Path &OutputCFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000336 sys::Path uniqueFile(Bytecode+".cbe.c");
Reid Spencer51c5a282006-08-23 20:34:57 +0000337 std::string ErrMsg;
338 if (uniqueFile.makeUnique(true, &ErrMsg)) {
339 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
340 exit(1);
341 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000342 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000343 std::vector<const char *> LLCArgs;
344 LLCArgs.push_back (LLCPath.c_str());
345
346 // Add any extra LLC args.
347 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
348 LLCArgs.push_back(ToolArgs[i].c_str());
349
350 LLCArgs.push_back ("-o");
351 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
352 LLCArgs.push_back ("-march=c"); // Output C language
353 LLCArgs.push_back ("-f"); // Overwrite as necessary...
354 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
355 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000356
357 std::cout << "<cbe>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000358 DEBUG(std::cerr << "\nAbout to run:\t";
359 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
360 std::cerr << " " << LLCArgs[i];
361 std::cerr << "\n";
362 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000363 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000364 sys::Path()))
Brian Gaeked11577b2004-05-04 21:09:01 +0000365 ProcessFailure(LLCPath, &LLCArgs[0]);
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000366 return GCC::CFile;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000367}
368
Chris Lattner9cbbee32004-02-18 23:24:41 +0000369void CBE::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000370 sys::Path OutputCFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000371 OutputCode(Bytecode, OutputCFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000372 OutputCFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000373}
374
Misha Brukman9558c6a2003-09-29 22:39:25 +0000375int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000376 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000377 const std::string &InputFile,
378 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000379 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000380 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000381 unsigned Timeout,
382 unsigned MemoryLimit) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000383 sys::Path OutputCFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000384 OutputCode(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000385
Chris Lattner8c56be52004-02-18 20:21:57 +0000386 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000387
Reid Spencer51ab5c82006-06-06 00:00:42 +0000388 std::vector<std::string> GCCArgs(ArgsForGCC);
389 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
Misha Brukmanf976c852005-04-21 22:55:34 +0000390 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000391 InputFile, OutputFile, GCCArgs,
392 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000393}
394
Chris Lattner9915cd92004-02-17 06:40:06 +0000395/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000396///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000397CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000398 std::string &Message,
399 const std::vector<std::string> *Args) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000400 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
401 if (LLCPath.isEmpty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000402 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000403 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000404 return 0;
405 }
406
Reid Spencerb31baa82004-12-19 18:00:21 +0000407 Message = "Found llc: " + LLCPath.toString() + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000408 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000409 if (!gcc) {
410 std::cerr << Message << "\n";
411 exit(1);
412 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000413 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000414}
415
416//===---------------------------------------------------------------------===//
417// GCC abstraction
418//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000419int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000420 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000421 FileType fileType,
422 const std::string &InputFile,
423 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000424 const std::vector<std::string> &ArgsForGCC,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000425 unsigned Timeout,
426 unsigned MemoryLimit) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000427 std::vector<const char*> GCCArgs;
428
429 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000430
Chris Lattnereeed9832003-10-14 21:52:52 +0000431 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000432 GCCArgs.push_back("-x");
433 if (fileType == CFile) {
434 GCCArgs.push_back("c");
435 GCCArgs.push_back("-fno-strict-aliasing");
436 } else {
437 GCCArgs.push_back("assembler");
Chris Lattnerd00b2882005-08-29 13:14:24 +0000438#ifdef __APPLE__
439 GCCArgs.push_back("-force_cpusubtype_ALL");
440#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000441 }
442 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
Chris Lattner629e4872006-06-09 21:31:53 +0000443 GCCArgs.push_back("-x");
444 GCCArgs.push_back("none");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000445 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000446 sys::Path OutputBinary (ProgramFile+".gcc.exe");
Reid Spencer51c5a282006-08-23 20:34:57 +0000447 std::string ErrMsg;
448 if (OutputBinary.makeUnique(true, &ErrMsg)) {
449 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
450 exit(1);
451 }
Misha Brukman9558c6a2003-09-29 22:39:25 +0000452 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
Reid Spencer51ab5c82006-06-06 00:00:42 +0000453
454 // Add any arguments intended for GCC. We locate them here because this is
455 // most likely -L and -l options that need to come before other libraries but
456 // after the source. Other options won't be sensitive to placement on the
457 // command line, so this should be safe.
458 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
459 GCCArgs.push_back(ArgsForGCC[i].c_str());
460
Misha Brukman9558c6a2003-09-29 22:39:25 +0000461 GCCArgs.push_back("-lm"); // Hard-code the math library...
462 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000463#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000464 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000465#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000466#ifdef __sparc__
467 GCCArgs.push_back("-mcpu=v9");
468#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000469 GCCArgs.push_back(0); // NULL terminator
470
471 std::cout << "<gcc>" << std::flush;
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000472 DEBUG(std::cerr << "\nAbout to run:\t";
473 for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
474 std::cerr << " " << GCCArgs[i];
475 std::cerr << "\n";
476 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000477 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
478 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000479 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000480 exit(1);
481 }
482
483 std::vector<const char*> ProgramArgs;
Chris Lattner45495c52005-02-13 23:13:47 +0000484
Misha Brukman9558c6a2003-09-29 22:39:25 +0000485 ProgramArgs.push_back(OutputBinary.c_str());
486 // Add optional parameters to the running program from Argv
487 for (unsigned i=0, e = Args.size(); i != e; ++i)
488 ProgramArgs.push_back(Args[i].c_str());
489 ProgramArgs.push_back(0); // NULL terminator
490
491 // Now that we have a binary, run it!
492 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000493 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000494 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000495 std::cerr << " " << ProgramArgs[i];
496 std::cerr << "\n";
497 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000498
Reid Spencer9ac14182004-12-16 23:01:34 +0000499 FileRemover OutputBinaryRemover(OutputBinary);
Reid Spencerb31baa82004-12-19 18:00:21 +0000500 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000501 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000502 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000503}
504
Chris Lattner1798e4a2003-10-14 21:07:25 +0000505int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +0000506 std::string &OutputFile,
507 const std::vector<std::string> &ArgsForGCC) {
Reid Spencercda985e2004-12-15 01:51:56 +0000508 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
Reid Spencer51c5a282006-08-23 20:34:57 +0000509 std::string ErrMsg;
510 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
511 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
512 exit(1);
513 }
Reid Spencercda985e2004-12-15 01:51:56 +0000514 OutputFile = uniqueFilename.toString();
515
Chris Lattner130e2a32006-06-27 20:35:36 +0000516 std::vector<const char*> GCCArgs;
517
518 GCCArgs.push_back(GCCPath.c_str());
519
520
Misha Brukman9558c6a2003-09-29 22:39:25 +0000521 // Compile the C/asm file into a shared object
Chris Lattner130e2a32006-06-27 20:35:36 +0000522 GCCArgs.push_back("-x");
523 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
524 GCCArgs.push_back("-fno-strict-aliasing");
525 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
Misha Brukman9558c6a2003-09-29 22:39:25 +0000526#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Chris Lattner130e2a32006-06-27 20:35:36 +0000527 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
Nate Begeman72b286b2005-07-08 00:23:26 +0000528#elif defined(__APPLE__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000529 // link all source files into a single module in data segment, rather than
530 // generating blocks. dynamic_lookup requires that you set
531 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
532 // bugpoint to just pass that in the environment of GCC.
533 GCCArgs.push_back("-single_module");
534 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
535 GCCArgs.push_back("-undefined");
536 GCCArgs.push_back("dynamic_lookup");
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000537#else
Chris Lattner130e2a32006-06-27 20:35:36 +0000538 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
Misha Brukman9558c6a2003-09-29 22:39:25 +0000539#endif
Chris Lattner1c81f132005-03-09 03:31:02 +0000540
Andrew Lenharth572668a2005-03-10 20:15:09 +0000541#if defined(__ia64__) || defined(__alpha__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000542 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
Chris Lattner1c81f132005-03-09 03:31:02 +0000543#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000544#ifdef __sparc__
Chris Lattner130e2a32006-06-27 20:35:36 +0000545 GCCArgs.push_back("-mcpu=v9");
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000546#endif
Chris Lattner130e2a32006-06-27 20:35:36 +0000547 GCCArgs.push_back("-o");
548 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
549 GCCArgs.push_back("-O2"); // Optimize the program a bit.
550
551
552
553 // Add any arguments intended for GCC. We locate them here because this is
554 // most likely -L and -l options that need to come before other libraries but
555 // after the source. Other options won't be sensitive to placement on the
556 // command line, so this should be safe.
557 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
558 GCCArgs.push_back(ArgsForGCC[i].c_str());
559 GCCArgs.push_back(0); // NULL terminator
560
561
Misha Brukmanf976c852005-04-21 22:55:34 +0000562
Misha Brukman9558c6a2003-09-29 22:39:25 +0000563 std::cout << "<gcc>" << std::flush;
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000564 DEBUG(std::cerr << "\nAbout to run:\t";
565 for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
566 std::cerr << " " << GCCArgs[i];
567 std::cerr << "\n";
568 );
Chris Lattner130e2a32006-06-27 20:35:36 +0000569 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000570 sys::Path())) {
Chris Lattner130e2a32006-06-27 20:35:36 +0000571 ProcessFailure(GCCPath, &GCCArgs[0]);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000572 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000573 }
574 return 0;
575}
576
Chris Lattner7915a1e2003-10-14 21:34:11 +0000577/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000578///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000579GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000580 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
581 if (GCCPath.isEmpty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000582 Message = "Cannot find `gcc' in executable directory or PATH!\n";
583 return 0;
584 }
585
Reid Spencerb31baa82004-12-19 18:00:21 +0000586 Message = "Found gcc: " + GCCPath.toString() + "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000587 return new GCC(GCCPath);
588}