Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 1 | //===-- ToolRunner.cpp ----------------------------------------------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the interfaces described in the ToolRunner.h file. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "toolrunner" |
Chris Lattner | f1b20d8 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 15 | #include "ToolRunner.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" // for HAVE_LINK_R |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 17 | #include "llvm/System/Program.h" |
Evan Cheng | 34e400e | 2007-05-03 18:36:15 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 21 | #include <fstream> |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 22 | #include <sstream> |
Chris Lattner | 86a5484 | 2006-01-22 22:53:01 +0000 | [diff] [blame] | 23 | #include <iostream> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Evan Cheng | 34e400e | 2007-05-03 18:36:15 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | cl::opt<std::string> |
| 28 | RSHHost("rsh-host", |
| 29 | cl::desc("Remote execution (rsh) host")); |
| 30 | |
| 31 | cl::opt<std::string> |
| 32 | RSHUser("rsh-user", |
| 33 | cl::desc("Remote execution (rsh) user id")); |
| 34 | } |
| 35 | |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 36 | ToolExecutionError::~ToolExecutionError() throw() { } |
| 37 | |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 38 | /// RunProgramWithTimeout - This function provides an alternate interface to the |
| 39 | /// sys::Program::ExecuteAndWait interface. |
| 40 | /// @see sys:Program::ExecuteAndWait |
| 41 | static int RunProgramWithTimeout(const sys::Path &ProgramPath, |
| 42 | const char **Args, |
| 43 | const sys::Path &StdInFile, |
| 44 | const sys::Path &StdOutFile, |
| 45 | const sys::Path &StdErrFile, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 46 | unsigned NumSeconds = 0, |
| 47 | unsigned MemoryLimit = 0) { |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 48 | const sys::Path* redirects[3]; |
| 49 | redirects[0] = &StdInFile; |
| 50 | redirects[1] = &StdOutFile; |
| 51 | redirects[2] = &StdErrFile; |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 52 | |
Chris Lattner | d422350 | 2006-09-15 23:01:10 +0000 | [diff] [blame] | 53 | if (0) { |
| 54 | std::cerr << "RUN:"; |
| 55 | for (unsigned i = 0; Args[i]; ++i) |
| 56 | std::cerr << " " << Args[i]; |
| 57 | std::cerr << "\n"; |
| 58 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 59 | |
| 60 | return |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 61 | sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, |
| 62 | NumSeconds, MemoryLimit); |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
| 66 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 67 | static void ProcessFailure(sys::Path ProgPath, const char** Args) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 68 | std::ostringstream OS; |
Chris Lattner | a3de117 | 2004-02-18 20:58:00 +0000 | [diff] [blame] | 69 | OS << "\nError running tool:\n "; |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 70 | for (const char **Arg = Args; *Arg; ++Arg) |
| 71 | OS << " " << *Arg; |
| 72 | OS << "\n"; |
| 73 | |
| 74 | // Rerun the compiler, capturing any error messages to print them. |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 75 | sys::Path ErrorFilename("error_messages"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 76 | std::string ErrMsg; |
| 77 | if (ErrorFilename.makeUnique(true, &ErrMsg)) { |
| 78 | std::cerr << "Error making unique filename: " << ErrMsg << "\n"; |
| 79 | exit(1); |
| 80 | } |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 81 | RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename, |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 82 | ErrorFilename); // FIXME: check return code ? |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 83 | |
| 84 | // Print out the error messages generated by GCC if possible... |
| 85 | std::ifstream ErrorFile(ErrorFilename.c_str()); |
| 86 | if (ErrorFile) { |
| 87 | std::copy(std::istreambuf_iterator<char>(ErrorFile), |
| 88 | std::istreambuf_iterator<char>(), |
| 89 | std::ostreambuf_iterator<char>(OS)); |
| 90 | ErrorFile.close(); |
| 91 | } |
| 92 | |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 93 | ErrorFilename.eraseFromDisk(); |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 94 | throw ToolExecutionError(OS.str()); |
| 95 | } |
| 96 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 97 | //===---------------------------------------------------------------------===// |
| 98 | // LLI Implementation of AbstractIntepreter interface |
| 99 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 100 | namespace { |
| 101 | class LLI : public AbstractInterpreter { |
| 102 | std::string LLIPath; // The path to the LLI executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 103 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 104 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 105 | LLI(const std::string &Path, const std::vector<std::string> *Args) |
| 106 | : LLIPath(Path) { |
| 107 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 108 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 109 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 110 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 111 | virtual int ExecuteProgram(const std::string &Bitcode, |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 112 | const std::vector<std::string> &Args, |
| 113 | const std::string &InputFile, |
| 114 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 115 | const std::vector<std::string> &GCCArgs, |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 116 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 117 | std::vector<std::string>(), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 118 | unsigned Timeout = 0, |
| 119 | unsigned MemoryLimit = 0); |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 120 | }; |
| 121 | } |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 122 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 123 | int LLI::ExecuteProgram(const std::string &Bitcode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 124 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 125 | const std::string &InputFile, |
| 126 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 127 | const std::vector<std::string> &GCCArgs, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 128 | const std::vector<std::string> &SharedLibs, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 129 | unsigned Timeout, |
| 130 | unsigned MemoryLimit) { |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 131 | if (!SharedLibs.empty()) |
| 132 | throw ToolExecutionError("LLI currently does not support " |
| 133 | "loading shared libraries."); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 134 | |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 135 | if (!GCCArgs.empty()) |
| 136 | throw ToolExecutionError("LLI currently does not support " |
| 137 | "GCC Arguments."); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 138 | std::vector<const char*> LLIArgs; |
| 139 | LLIArgs.push_back(LLIPath.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 140 | LLIArgs.push_back("-force-interpreter=true"); |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 141 | |
| 142 | // Add any extra LLI args. |
| 143 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 144 | LLIArgs.push_back(ToolArgs[i].c_str()); |
| 145 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 146 | LLIArgs.push_back(Bitcode.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 147 | // Add optional parameters to the running program from Argv |
| 148 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 149 | LLIArgs.push_back(Args[i].c_str()); |
| 150 | LLIArgs.push_back(0); |
| 151 | |
| 152 | std::cout << "<lli>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 153 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 154 | for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 155 | std::cerr << " " << LLIArgs[i]; |
| 156 | std::cerr << "\n"; |
| 157 | ); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 158 | return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 159 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 160 | Timeout, MemoryLimit); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // LLI create method - Try to find the LLI executable |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 164 | AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 165 | std::string &Message, |
| 166 | const std::vector<std::string> *ToolArgs) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 167 | std::string LLIPath = FindExecutable("lli", ProgPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 168 | if (!LLIPath.empty()) { |
| 169 | Message = "Found lli: " + LLIPath + "\n"; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 170 | return new LLI(LLIPath, ToolArgs); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | Message = "Cannot find `lli' in executable directory or PATH!\n"; |
| 174 | return 0; |
| 175 | } |
| 176 | |
Anton Korobeynikov | 9ef7425 | 2008-04-28 20:53:48 +0000 | [diff] [blame^] | 177 | //===---------------------------------------------------------------------===// |
| 178 | // Custom execution command implementation of AbstractIntepreter interface |
| 179 | // |
| 180 | // Allows using a custom command for executing the bitcode, thus allows, |
| 181 | // for example, to invoke a cross compiler for code generation followed by |
| 182 | // a simulator that executes the generated binary. |
| 183 | namespace { |
| 184 | class CustomExecutor : public AbstractInterpreter { |
| 185 | std::string ExecutionCommand; |
| 186 | std::vector<std::string> ExecutorArgs; |
| 187 | public: |
| 188 | CustomExecutor( |
| 189 | const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) : |
| 190 | ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {} |
| 191 | |
| 192 | virtual int ExecuteProgram(const std::string &Bitcode, |
| 193 | const std::vector<std::string> &Args, |
| 194 | const std::string &InputFile, |
| 195 | const std::string &OutputFile, |
| 196 | const std::vector<std::string> &GCCArgs, |
| 197 | const std::vector<std::string> &SharedLibs = |
| 198 | std::vector<std::string>(), |
| 199 | unsigned Timeout = 0, |
| 200 | unsigned MemoryLimit = 0); |
| 201 | }; |
| 202 | } |
| 203 | |
| 204 | int CustomExecutor::ExecuteProgram(const std::string &Bitcode, |
| 205 | const std::vector<std::string> &Args, |
| 206 | const std::string &InputFile, |
| 207 | const std::string &OutputFile, |
| 208 | const std::vector<std::string> &GCCArgs, |
| 209 | const std::vector<std::string> &SharedLibs, |
| 210 | unsigned Timeout, |
| 211 | unsigned MemoryLimit) { |
| 212 | |
| 213 | std::vector<const char*> ProgramArgs; |
| 214 | ProgramArgs.push_back(ExecutionCommand.c_str()); |
| 215 | |
| 216 | for (std::size_t i = 0; i < ExecutorArgs.size(); ++i) |
| 217 | ProgramArgs.push_back(ExecutorArgs.at(i).c_str()); |
| 218 | ProgramArgs.push_back(Bitcode.c_str()); |
| 219 | ProgramArgs.push_back(0); |
| 220 | |
| 221 | // Add optional parameters to the running program from Argv |
| 222 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 223 | ProgramArgs.push_back(Args[i].c_str()); |
| 224 | |
| 225 | return RunProgramWithTimeout( |
| 226 | sys::Path(ExecutionCommand), |
| 227 | &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile), |
| 228 | sys::Path(OutputFile), Timeout, MemoryLimit); |
| 229 | } |
| 230 | |
| 231 | // Custom execution environment create method, takes the execution command |
| 232 | // as arguments |
| 233 | AbstractInterpreter *AbstractInterpreter::createCustom( |
| 234 | const std::string &ProgramPath, |
| 235 | std::string &Message, |
| 236 | const std::string &ExecCommandLine) { |
| 237 | |
| 238 | std::string Command = ""; |
| 239 | std::vector<std::string> Args; |
| 240 | std::string delimiters = " "; |
| 241 | |
| 242 | // Tokenize the ExecCommandLine to the command and the args to allow |
| 243 | // defining a full command line as the command instead of just the |
| 244 | // executed program. We cannot just pass the whole string after the command |
| 245 | // as a single argument because then program sees only a single |
| 246 | // command line argument (with spaces in it: "foo bar" instead |
| 247 | // of "foo" and "bar"). |
| 248 | |
| 249 | // code borrowed from: |
| 250 | // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html |
| 251 | std::string::size_type lastPos = |
| 252 | ExecCommandLine.find_first_not_of(delimiters, 0); |
| 253 | std::string::size_type pos = |
| 254 | ExecCommandLine.find_first_of(delimiters, lastPos); |
| 255 | |
| 256 | while (std::string::npos != pos || std::string::npos != lastPos) { |
| 257 | std::string token = ExecCommandLine.substr(lastPos, pos - lastPos); |
| 258 | if (Command == "") |
| 259 | Command = token; |
| 260 | else |
| 261 | Args.push_back(token); |
| 262 | // Skip delimiters. Note the "not_of" |
| 263 | lastPos = ExecCommandLine.find_first_not_of(delimiters, pos); |
| 264 | // Find next "non-delimiter" |
| 265 | pos = ExecCommandLine.find_first_of(delimiters, lastPos); |
| 266 | } |
| 267 | |
| 268 | std::string CmdPath = FindExecutable(Command, ProgramPath).toString(); |
| 269 | if (CmdPath.empty()) { |
| 270 | Message = |
| 271 | std::string("Cannot find '") + Command + |
| 272 | "' in executable directory or PATH!\n"; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | Message = "Found command in: " + CmdPath + "\n"; |
| 277 | |
| 278 | return new CustomExecutor(CmdPath, Args); |
| 279 | } |
| 280 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 281 | //===----------------------------------------------------------------------===// |
| 282 | // LLC Implementation of AbstractIntepreter interface |
| 283 | // |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 284 | GCC::FileType LLC::OutputCode(const std::string &Bitcode, |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 285 | sys::Path &OutputAsmFile) { |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 286 | sys::Path uniqueFile(Bitcode+".llc.s"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 287 | std::string ErrMsg; |
| 288 | if (uniqueFile.makeUnique(true, &ErrMsg)) { |
| 289 | std::cerr << "Error making unique filename: " << ErrMsg << "\n"; |
| 290 | exit(1); |
| 291 | } |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 292 | OutputAsmFile = uniqueFile; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 293 | std::vector<const char *> LLCArgs; |
| 294 | LLCArgs.push_back (LLCPath.c_str()); |
| 295 | |
| 296 | // Add any extra LLC args. |
| 297 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 298 | LLCArgs.push_back(ToolArgs[i].c_str()); |
| 299 | |
| 300 | LLCArgs.push_back ("-o"); |
| 301 | LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file |
| 302 | LLCArgs.push_back ("-f"); // Overwrite as necessary... |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 303 | LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 304 | LLCArgs.push_back (0); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 305 | |
| 306 | std::cout << "<llc>" << std::flush; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 307 | DEBUG(std::cerr << "\nAbout to run:\t"; |
| 308 | for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) |
| 309 | std::cerr << " " << LLCArgs[i]; |
| 310 | std::cerr << "\n"; |
| 311 | ); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 312 | if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0], |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 313 | sys::Path(), sys::Path(), sys::Path())) |
| 314 | ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]); |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 315 | |
| 316 | return GCC::AsmFile; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 319 | void LLC::compileProgram(const std::string &Bitcode) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 320 | sys::Path OutputAsmFile; |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 321 | OutputCode(Bitcode, OutputAsmFile); |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 322 | OutputAsmFile.eraseFromDisk(); |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 325 | int LLC::ExecuteProgram(const std::string &Bitcode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 326 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 327 | const std::string &InputFile, |
| 328 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 329 | const std::vector<std::string> &ArgsForGCC, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 330 | const std::vector<std::string> &SharedLibs, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 331 | unsigned Timeout, |
| 332 | unsigned MemoryLimit) { |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 333 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 334 | sys::Path OutputAsmFile; |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 335 | OutputCode(Bitcode, OutputAsmFile); |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 336 | FileRemover OutFileRemover(OutputAsmFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 337 | |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 338 | std::vector<std::string> GCCArgs(ArgsForGCC); |
| 339 | GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end()); |
| 340 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 341 | // Assuming LLC worked, compile the result with GCC and run it. |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 342 | return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 343 | InputFile, OutputFile, GCCArgs, |
| 344 | Timeout, MemoryLimit); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 347 | /// createLLC - Try to find the LLC executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 348 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 349 | LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 350 | std::string &Message, |
| 351 | const std::vector<std::string> *Args) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 352 | std::string LLCPath = FindExecutable("llc", ProgramPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 353 | if (LLCPath.empty()) { |
| 354 | Message = "Cannot find `llc' in executable directory or PATH!\n"; |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | Message = "Found llc: " + LLCPath + "\n"; |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 359 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 360 | if (!gcc) { |
| 361 | std::cerr << Message << "\n"; |
| 362 | exit(1); |
| 363 | } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 364 | return new LLC(LLCPath, gcc, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | //===---------------------------------------------------------------------===// |
| 368 | // JIT Implementation of AbstractIntepreter interface |
| 369 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 370 | namespace { |
| 371 | class JIT : public AbstractInterpreter { |
| 372 | std::string LLIPath; // The path to the LLI executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 373 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 374 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 375 | JIT(const std::string &Path, const std::vector<std::string> *Args) |
| 376 | : LLIPath(Path) { |
| 377 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 378 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 379 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 380 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 381 | virtual int ExecuteProgram(const std::string &Bitcode, |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 382 | const std::vector<std::string> &Args, |
| 383 | const std::string &InputFile, |
| 384 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 385 | const std::vector<std::string> &GCCArgs = |
| 386 | std::vector<std::string>(), |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 387 | const std::vector<std::string> &SharedLibs = |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 388 | std::vector<std::string>(), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 389 | unsigned Timeout =0, |
| 390 | unsigned MemoryLimit =0); |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 391 | }; |
| 392 | } |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 393 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 394 | int JIT::ExecuteProgram(const std::string &Bitcode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 395 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 396 | const std::string &InputFile, |
| 397 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 398 | const std::vector<std::string> &GCCArgs, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 399 | const std::vector<std::string> &SharedLibs, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 400 | unsigned Timeout, |
| 401 | unsigned MemoryLimit) { |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 402 | if (!GCCArgs.empty()) |
| 403 | throw ToolExecutionError("JIT does not support GCC Arguments."); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 404 | // Construct a vector of parameters, incorporating those from the command-line |
| 405 | std::vector<const char*> JITArgs; |
| 406 | JITArgs.push_back(LLIPath.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 407 | JITArgs.push_back("-force-interpreter=false"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 408 | |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 409 | // Add any extra LLI args. |
| 410 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 411 | JITArgs.push_back(ToolArgs[i].c_str()); |
| 412 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 413 | for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 414 | JITArgs.push_back("-load"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 415 | JITArgs.push_back(SharedLibs[i].c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 416 | } |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 417 | JITArgs.push_back(Bitcode.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 418 | // Add optional parameters to the running program from Argv |
| 419 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 420 | JITArgs.push_back(Args[i].c_str()); |
| 421 | JITArgs.push_back(0); |
| 422 | |
| 423 | std::cout << "<jit>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 424 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 425 | for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 426 | std::cerr << " " << JITArgs[i]; |
| 427 | std::cerr << "\n"; |
| 428 | ); |
| 429 | DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n"); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 430 | return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 431 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 432 | Timeout, MemoryLimit); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 435 | /// createJIT - Try to find the LLI executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 436 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 437 | AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 438 | std::string &Message, const std::vector<std::string> *Args) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 439 | std::string LLIPath = FindExecutable("lli", ProgPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 440 | if (!LLIPath.empty()) { |
| 441 | Message = "Found lli: " + LLIPath + "\n"; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 442 | return new JIT(LLIPath, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | Message = "Cannot find `lli' in executable directory or PATH!\n"; |
| 446 | return 0; |
| 447 | } |
| 448 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 449 | GCC::FileType CBE::OutputCode(const std::string &Bitcode, |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 450 | sys::Path &OutputCFile) { |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 451 | sys::Path uniqueFile(Bitcode+".cbe.c"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 452 | std::string ErrMsg; |
| 453 | if (uniqueFile.makeUnique(true, &ErrMsg)) { |
| 454 | std::cerr << "Error making unique filename: " << ErrMsg << "\n"; |
| 455 | exit(1); |
| 456 | } |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 457 | OutputCFile = uniqueFile; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 458 | std::vector<const char *> LLCArgs; |
| 459 | LLCArgs.push_back (LLCPath.c_str()); |
| 460 | |
| 461 | // Add any extra LLC args. |
| 462 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 463 | LLCArgs.push_back(ToolArgs[i].c_str()); |
| 464 | |
| 465 | LLCArgs.push_back ("-o"); |
| 466 | LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file |
| 467 | LLCArgs.push_back ("-march=c"); // Output C language |
| 468 | LLCArgs.push_back ("-f"); // Overwrite as necessary... |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 469 | LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 470 | LLCArgs.push_back (0); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 471 | |
| 472 | std::cout << "<cbe>" << std::flush; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 473 | DEBUG(std::cerr << "\nAbout to run:\t"; |
| 474 | for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) |
| 475 | std::cerr << " " << LLCArgs[i]; |
| 476 | std::cerr << "\n"; |
| 477 | ); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 478 | if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 479 | sys::Path())) |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 480 | ProcessFailure(LLCPath, &LLCArgs[0]); |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 481 | return GCC::CFile; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 484 | void CBE::compileProgram(const std::string &Bitcode) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 485 | sys::Path OutputCFile; |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 486 | OutputCode(Bitcode, OutputCFile); |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 487 | OutputCFile.eraseFromDisk(); |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 490 | int CBE::ExecuteProgram(const std::string &Bitcode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 491 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 492 | const std::string &InputFile, |
| 493 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 494 | const std::vector<std::string> &ArgsForGCC, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 495 | const std::vector<std::string> &SharedLibs, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 496 | unsigned Timeout, |
| 497 | unsigned MemoryLimit) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 498 | sys::Path OutputCFile; |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 499 | OutputCode(Bitcode, OutputCFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 501 | FileRemover CFileRemove(OutputCFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 502 | |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 503 | std::vector<std::string> GCCArgs(ArgsForGCC); |
| 504 | GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end()); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 505 | return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 506 | InputFile, OutputFile, GCCArgs, |
| 507 | Timeout, MemoryLimit); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Chris Lattner | 9915cd9 | 2004-02-17 06:40:06 +0000 | [diff] [blame] | 510 | /// createCBE - Try to find the 'llc' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 511 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 512 | CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 513 | std::string &Message, |
| 514 | const std::vector<std::string> *Args) { |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 515 | sys::Path LLCPath = FindExecutable("llc", ProgramPath); |
| 516 | if (LLCPath.isEmpty()) { |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 517 | Message = |
Chris Lattner | 9915cd9 | 2004-02-17 06:40:06 +0000 | [diff] [blame] | 518 | "Cannot find `llc' in executable directory or PATH!\n"; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 522 | Message = "Found llc: " + LLCPath.toString() + "\n"; |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 523 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 524 | if (!gcc) { |
| 525 | std::cerr << Message << "\n"; |
| 526 | exit(1); |
| 527 | } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 528 | return new CBE(LLCPath, gcc, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | //===---------------------------------------------------------------------===// |
| 532 | // GCC abstraction |
| 533 | // |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 534 | int GCC::ExecuteProgram(const std::string &ProgramFile, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 535 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 536 | FileType fileType, |
| 537 | const std::string &InputFile, |
| 538 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 539 | const std::vector<std::string> &ArgsForGCC, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 540 | unsigned Timeout, |
| 541 | unsigned MemoryLimit) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 542 | std::vector<const char*> GCCArgs; |
| 543 | |
| 544 | GCCArgs.push_back(GCCPath.c_str()); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 545 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 546 | // Specify -x explicitly in case the extension is wonky |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 547 | GCCArgs.push_back("-x"); |
| 548 | if (fileType == CFile) { |
| 549 | GCCArgs.push_back("c"); |
| 550 | GCCArgs.push_back("-fno-strict-aliasing"); |
| 551 | } else { |
| 552 | GCCArgs.push_back("assembler"); |
Chris Lattner | d00b288 | 2005-08-29 13:14:24 +0000 | [diff] [blame] | 553 | #ifdef __APPLE__ |
| 554 | GCCArgs.push_back("-force_cpusubtype_ALL"); |
| 555 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 556 | } |
| 557 | GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename... |
Chris Lattner | 629e487 | 2006-06-09 21:31:53 +0000 | [diff] [blame] | 558 | GCCArgs.push_back("-x"); |
| 559 | GCCArgs.push_back("none"); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 560 | GCCArgs.push_back("-o"); |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 561 | sys::Path OutputBinary (ProgramFile+".gcc.exe"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 562 | std::string ErrMsg; |
| 563 | if (OutputBinary.makeUnique(true, &ErrMsg)) { |
| 564 | std::cerr << "Error making unique filename: " << ErrMsg << "\n"; |
| 565 | exit(1); |
| 566 | } |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 567 | GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file... |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 568 | |
| 569 | // Add any arguments intended for GCC. We locate them here because this is |
| 570 | // most likely -L and -l options that need to come before other libraries but |
| 571 | // after the source. Other options won't be sensitive to placement on the |
| 572 | // command line, so this should be safe. |
| 573 | for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i) |
| 574 | GCCArgs.push_back(ArgsForGCC[i].c_str()); |
| 575 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 576 | GCCArgs.push_back("-lm"); // Hard-code the math library... |
| 577 | GCCArgs.push_back("-O2"); // Optimize the program a bit... |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 578 | #if defined (HAVE_LINK_R) |
Chris Lattner | 1f0f162 | 2003-10-18 21:54:47 +0000 | [diff] [blame] | 579 | GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 580 | #endif |
Chris Lattner | fdcc71e | 2006-02-04 05:02:27 +0000 | [diff] [blame] | 581 | #ifdef __sparc__ |
| 582 | GCCArgs.push_back("-mcpu=v9"); |
| 583 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 584 | GCCArgs.push_back(0); // NULL terminator |
| 585 | |
| 586 | std::cout << "<gcc>" << std::flush; |
Evan Cheng | 7f7fdcc | 2007-01-03 07:44:30 +0000 | [diff] [blame] | 587 | DEBUG(std::cerr << "\nAbout to run:\t"; |
| 588 | for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i) |
| 589 | std::cerr << " " << GCCArgs[i]; |
| 590 | std::cerr << "\n"; |
| 591 | ); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 592 | if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), |
| 593 | sys::Path())) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 594 | ProcessFailure(GCCPath, &GCCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 595 | exit(1); |
| 596 | } |
| 597 | |
| 598 | std::vector<const char*> ProgramArgs; |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 599 | |
Evan Cheng | 34e400e | 2007-05-03 18:36:15 +0000 | [diff] [blame] | 600 | if (RSHPath.isEmpty()) |
| 601 | ProgramArgs.push_back(OutputBinary.c_str()); |
| 602 | else { |
| 603 | ProgramArgs.push_back(RSHPath.c_str()); |
| 604 | ProgramArgs.push_back(RSHHost.c_str()); |
| 605 | ProgramArgs.push_back("-l"); |
| 606 | ProgramArgs.push_back(RSHUser.c_str()); |
| 607 | |
| 608 | char* env_pwd = getenv("PWD"); |
| 609 | std::string Exec = "cd "; |
| 610 | Exec += env_pwd; |
| 611 | Exec += "; ./"; |
| 612 | Exec += OutputBinary.c_str(); |
| 613 | ProgramArgs.push_back(Exec.c_str()); |
| 614 | } |
| 615 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 616 | // Add optional parameters to the running program from Argv |
| 617 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 618 | ProgramArgs.push_back(Args[i].c_str()); |
| 619 | ProgramArgs.push_back(0); // NULL terminator |
| 620 | |
| 621 | // Now that we have a binary, run it! |
| 622 | std::cout << "<program>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 623 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 624 | for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 625 | std::cerr << " " << ProgramArgs[i]; |
| 626 | std::cerr << "\n"; |
| 627 | ); |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 628 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 629 | FileRemover OutputBinaryRemover(OutputBinary); |
Evan Cheng | 34e400e | 2007-05-03 18:36:15 +0000 | [diff] [blame] | 630 | |
| 631 | if (RSHPath.isEmpty()) |
| 632 | return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0], |
| 633 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
| 634 | Timeout, MemoryLimit); |
| 635 | else |
| 636 | return RunProgramWithTimeout(sys::Path(RSHPath), &ProgramArgs[0], |
| 637 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
| 638 | Timeout, MemoryLimit); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 641 | int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType, |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 642 | std::string &OutputFile, |
| 643 | const std::vector<std::string> &ArgsForGCC) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 644 | sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 645 | std::string ErrMsg; |
| 646 | if (uniqueFilename.makeUnique(true, &ErrMsg)) { |
| 647 | std::cerr << "Error making unique filename: " << ErrMsg << "\n"; |
| 648 | exit(1); |
| 649 | } |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 650 | OutputFile = uniqueFilename.toString(); |
| 651 | |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 652 | std::vector<const char*> GCCArgs; |
| 653 | |
| 654 | GCCArgs.push_back(GCCPath.c_str()); |
| 655 | |
| 656 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 657 | // Compile the C/asm file into a shared object |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 658 | GCCArgs.push_back("-x"); |
| 659 | GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c"); |
| 660 | GCCArgs.push_back("-fno-strict-aliasing"); |
| 661 | GCCArgs.push_back(InputFile.c_str()); // Specify the input filename. |
Lauro Ramos Venancio | 497391a | 2007-06-06 23:10:56 +0000 | [diff] [blame] | 662 | GCCArgs.push_back("-x"); |
| 663 | GCCArgs.push_back("none"); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 664 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 665 | GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 666 | #elif defined(__APPLE__) |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 667 | // link all source files into a single module in data segment, rather than |
| 668 | // generating blocks. dynamic_lookup requires that you set |
| 669 | // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for |
| 670 | // bugpoint to just pass that in the environment of GCC. |
| 671 | GCCArgs.push_back("-single_module"); |
| 672 | GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC |
| 673 | GCCArgs.push_back("-undefined"); |
| 674 | GCCArgs.push_back("dynamic_lookup"); |
Misha Brukman | b3998ec | 2004-07-16 19:45:45 +0000 | [diff] [blame] | 675 | #else |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 676 | GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 677 | #endif |
Chris Lattner | 1c81f13 | 2005-03-09 03:31:02 +0000 | [diff] [blame] | 678 | |
Torok Edwin | af4fc28 | 2008-04-06 12:42:29 +0000 | [diff] [blame] | 679 | #if defined(__ia64__) || defined(__alpha__) || defined(__amd64__) |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 680 | GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC |
Chris Lattner | 1c81f13 | 2005-03-09 03:31:02 +0000 | [diff] [blame] | 681 | #endif |
Chris Lattner | fdcc71e | 2006-02-04 05:02:27 +0000 | [diff] [blame] | 682 | #ifdef __sparc__ |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 683 | GCCArgs.push_back("-mcpu=v9"); |
Chris Lattner | fdcc71e | 2006-02-04 05:02:27 +0000 | [diff] [blame] | 684 | #endif |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 685 | GCCArgs.push_back("-o"); |
| 686 | GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename. |
| 687 | GCCArgs.push_back("-O2"); // Optimize the program a bit. |
| 688 | |
| 689 | |
| 690 | |
| 691 | // Add any arguments intended for GCC. We locate them here because this is |
| 692 | // most likely -L and -l options that need to come before other libraries but |
| 693 | // after the source. Other options won't be sensitive to placement on the |
| 694 | // command line, so this should be safe. |
| 695 | for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i) |
| 696 | GCCArgs.push_back(ArgsForGCC[i].c_str()); |
| 697 | GCCArgs.push_back(0); // NULL terminator |
| 698 | |
| 699 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 700 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 701 | std::cout << "<gcc>" << std::flush; |
Evan Cheng | 7f7fdcc | 2007-01-03 07:44:30 +0000 | [diff] [blame] | 702 | DEBUG(std::cerr << "\nAbout to run:\t"; |
| 703 | for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i) |
| 704 | std::cerr << " " << GCCArgs[i]; |
| 705 | std::cerr << "\n"; |
| 706 | ); |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 707 | if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 708 | sys::Path())) { |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 709 | ProcessFailure(GCCPath, &GCCArgs[0]); |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 710 | return 1; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 711 | } |
| 712 | return 0; |
| 713 | } |
| 714 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 715 | /// create - Try to find the `gcc' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 716 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 717 | GCC *GCC::create(const std::string &ProgramPath, std::string &Message) { |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 718 | sys::Path GCCPath = FindExecutable("gcc", ProgramPath); |
| 719 | if (GCCPath.isEmpty()) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 720 | Message = "Cannot find `gcc' in executable directory or PATH!\n"; |
| 721 | return 0; |
| 722 | } |
| 723 | |
Evan Cheng | 34e400e | 2007-05-03 18:36:15 +0000 | [diff] [blame] | 724 | sys::Path RSHPath; |
| 725 | if (!RSHHost.empty()) |
| 726 | RSHPath = FindExecutable("rsh", ProgramPath); |
| 727 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 728 | Message = "Found gcc: " + GCCPath.toString() + "\n"; |
Evan Cheng | 34e400e | 2007-05-03 18:36:15 +0000 | [diff] [blame] | 729 | return new GCC(GCCPath, RSHPath); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 730 | } |