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 | // |
| 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 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" |
Misha Brukman | 6873450 | 2003-10-06 18:37:24 +0000 | [diff] [blame] | 15 | #include "llvm/Support/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" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 20 | #include <fstream> |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 21 | #include <sstream> |
Chris Lattner | 86a5484 | 2006-01-22 22:53:01 +0000 | [diff] [blame] | 22 | #include <iostream> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 25 | ToolExecutionError::~ToolExecutionError() throw() { } |
| 26 | |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 27 | /// RunProgramWithTimeout - This function provides an alternate interface to the |
| 28 | /// sys::Program::ExecuteAndWait interface. |
| 29 | /// @see sys:Program::ExecuteAndWait |
| 30 | static 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 Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 40 | |
| 41 | return |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 42 | sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 47 | static void ProcessFailure(sys::Path ProgPath, const char** Args) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 48 | std::ostringstream OS; |
Chris Lattner | a3de117 | 2004-02-18 20:58:00 +0000 | [diff] [blame] | 49 | OS << "\nError running tool:\n "; |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 50 | 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 Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 55 | sys::Path ErrorFilename("error_messages"); |
| 56 | ErrorFilename.makeUnique(); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 57 | RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename, |
| 58 | ErrorFilename); |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 59 | |
| 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 Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 69 | ErrorFilename.eraseFromDisk(); |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 70 | throw ToolExecutionError(OS.str()); |
| 71 | } |
| 72 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 73 | //===---------------------------------------------------------------------===// |
| 74 | // LLI Implementation of AbstractIntepreter interface |
| 75 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 76 | namespace { |
| 77 | class LLI : public AbstractInterpreter { |
| 78 | std::string LLIPath; // The path to the LLI executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 79 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 80 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 81 | LLI(const std::string &Path, const std::vector<std::string> *Args) |
| 82 | : LLIPath(Path) { |
| 83 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 84 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 85 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 87 | 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 Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 91 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 92 | std::vector<std::string>(), |
| 93 | unsigned Timeout = 0); |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 94 | }; |
| 95 | } |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 96 | |
| 97 | int LLI::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 98 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 99 | const std::string &InputFile, |
| 100 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 101 | const std::vector<std::string> &SharedLibs, |
| 102 | unsigned Timeout) { |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 103 | if (!SharedLibs.empty()) |
| 104 | throw ToolExecutionError("LLI currently does not support " |
| 105 | "loading shared libraries."); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 106 | |
| 107 | std::vector<const char*> LLIArgs; |
| 108 | LLIArgs.push_back(LLIPath.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 109 | LLIArgs.push_back("-force-interpreter=true"); |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 110 | |
| 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 Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 115 | 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 Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 122 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 123 | for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 124 | std::cerr << " " << LLIArgs[i]; |
| 125 | std::cerr << "\n"; |
| 126 | ); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 127 | return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 128 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 129 | Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // LLI create method - Try to find the LLI executable |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 133 | AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 134 | std::string &Message, |
| 135 | const std::vector<std::string> *ToolArgs) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 136 | std::string LLIPath = FindExecutable("lli", ProgPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 137 | if (!LLIPath.empty()) { |
| 138 | Message = "Found lli: " + LLIPath + "\n"; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 139 | return new LLI(LLIPath, ToolArgs); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 140 | } |
| 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 Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 149 | void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 150 | sys::Path uniqueFile(Bytecode+".llc.s"); |
| 151 | uniqueFile.makeUnique(); |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 152 | OutputAsmFile = uniqueFile; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 153 | 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 Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 165 | |
| 166 | std::cout << "<llc>" << std::flush; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 167 | 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 Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 172 | if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0], |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 173 | sys::Path(), sys::Path(), sys::Path())) |
| 174 | ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 177 | void LLC::compileProgram(const std::string &Bytecode) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 178 | sys::Path OutputAsmFile; |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 179 | OutputAsm(Bytecode, OutputAsmFile); |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 180 | OutputAsmFile.eraseFromDisk(); |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 183 | int LLC::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 184 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 185 | const std::string &InputFile, |
| 186 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 187 | const std::vector<std::string> &SharedLibs, |
| 188 | unsigned Timeout) { |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 189 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 190 | sys::Path OutputAsmFile; |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 191 | OutputAsm(Bytecode, OutputAsmFile); |
| 192 | FileRemover OutFileRemover(OutputAsmFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 193 | |
| 194 | // Assuming LLC worked, compile the result with GCC and run it. |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 195 | return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 196 | InputFile, OutputFile, SharedLibs, Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 199 | /// createLLC - Try to find the LLC executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 200 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 201 | LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 202 | std::string &Message, |
| 203 | const std::vector<std::string> *Args) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 204 | std::string LLCPath = FindExecutable("llc", ProgramPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 205 | 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 Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 211 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 212 | if (!gcc) { |
| 213 | std::cerr << Message << "\n"; |
| 214 | exit(1); |
| 215 | } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 216 | return new LLC(LLCPath, gcc, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | //===---------------------------------------------------------------------===// |
| 220 | // JIT Implementation of AbstractIntepreter interface |
| 221 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 222 | namespace { |
| 223 | class JIT : public AbstractInterpreter { |
| 224 | std::string LLIPath; // The path to the LLI executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 225 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 226 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 227 | JIT(const std::string &Path, const std::vector<std::string> *Args) |
| 228 | : LLIPath(Path) { |
| 229 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 230 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 231 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 233 | 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 Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 237 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 238 | std::vector<std::string>(), unsigned Timeout =0); |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 239 | }; |
| 240 | } |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 241 | |
| 242 | int JIT::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 243 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 244 | const std::string &InputFile, |
| 245 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 246 | const std::vector<std::string> &SharedLibs, |
| 247 | unsigned Timeout) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 248 | // 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 Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 251 | JITArgs.push_back("-force-interpreter=false"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 252 | |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 253 | // 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 Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 257 | for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 258 | JITArgs.push_back("-load"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 259 | JITArgs.push_back(SharedLibs[i].c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 260 | } |
| 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 Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 268 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 269 | for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 270 | std::cerr << " " << JITArgs[i]; |
| 271 | std::cerr << "\n"; |
| 272 | ); |
| 273 | DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n"); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 274 | return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 275 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 276 | Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 279 | /// createJIT - Try to find the LLI executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 280 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 281 | AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 282 | std::string &Message, const std::vector<std::string> *Args) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 283 | std::string LLIPath = FindExecutable("lli", ProgPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 284 | if (!LLIPath.empty()) { |
| 285 | Message = "Found lli: " + LLIPath + "\n"; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 286 | return new JIT(LLIPath, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | Message = "Cannot find `lli' in executable directory or PATH!\n"; |
| 290 | return 0; |
| 291 | } |
| 292 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 293 | void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 294 | sys::Path uniqueFile(Bytecode+".cbe.c"); |
| 295 | uniqueFile.makeUnique(); |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 296 | OutputCFile = uniqueFile; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 297 | 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 Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 310 | |
| 311 | std::cout << "<cbe>" << std::flush; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 312 | 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 Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 317 | if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 318 | sys::Path())) |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 319 | ProcessFailure(LLCPath, &LLCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 322 | void CBE::compileProgram(const std::string &Bytecode) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 323 | sys::Path OutputCFile; |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 324 | OutputC(Bytecode, OutputCFile); |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 325 | OutputCFile.eraseFromDisk(); |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 328 | int CBE::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 329 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 330 | const std::string &InputFile, |
| 331 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 332 | const std::vector<std::string> &SharedLibs, |
| 333 | unsigned Timeout) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 334 | sys::Path OutputCFile; |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 335 | OutputC(Bytecode, OutputCFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 337 | FileRemover CFileRemove(OutputCFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 338 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 339 | return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 340 | InputFile, OutputFile, SharedLibs, Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Chris Lattner | 9915cd9 | 2004-02-17 06:40:06 +0000 | [diff] [blame] | 343 | /// createCBE - Try to find the 'llc' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 344 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 345 | CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 346 | std::string &Message, |
| 347 | const std::vector<std::string> *Args) { |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 348 | sys::Path LLCPath = FindExecutable("llc", ProgramPath); |
| 349 | if (LLCPath.isEmpty()) { |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 350 | Message = |
Chris Lattner | 9915cd9 | 2004-02-17 06:40:06 +0000 | [diff] [blame] | 351 | "Cannot find `llc' in executable directory or PATH!\n"; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 355 | Message = "Found llc: " + LLCPath.toString() + "\n"; |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 356 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 357 | if (!gcc) { |
| 358 | std::cerr << Message << "\n"; |
| 359 | exit(1); |
| 360 | } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 361 | return new CBE(LLCPath, gcc, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | //===---------------------------------------------------------------------===// |
| 365 | // GCC abstraction |
| 366 | // |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 367 | int GCC::ExecuteProgram(const std::string &ProgramFile, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 368 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 369 | FileType fileType, |
| 370 | const std::string &InputFile, |
| 371 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 372 | const std::vector<std::string> &SharedLibs, |
| 373 | unsigned Timeout) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 374 | std::vector<const char*> GCCArgs; |
| 375 | |
| 376 | GCCArgs.push_back(GCCPath.c_str()); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 377 | |
| 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 Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 381 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 382 | // Specify -x explicitly in case the extension is wonky |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 383 | 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 Lattner | d00b288 | 2005-08-29 13:14:24 +0000 | [diff] [blame] | 389 | #ifdef __APPLE__ |
| 390 | GCCArgs.push_back("-force_cpusubtype_ALL"); |
| 391 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 392 | } |
| 393 | GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename... |
| 394 | GCCArgs.push_back("-o"); |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 395 | sys::Path OutputBinary (ProgramFile+".gcc.exe"); |
| 396 | OutputBinary.makeUnique(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 397 | 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 Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 400 | #if defined (HAVE_LINK_R) |
Chris Lattner | 1f0f162 | 2003-10-18 21:54:47 +0000 | [diff] [blame] | 401 | GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 402 | #endif |
Chris Lattner | fdcc71e | 2006-02-04 05:02:27 +0000 | [diff] [blame] | 403 | #ifdef __sparc__ |
| 404 | GCCArgs.push_back("-mcpu=v9"); |
| 405 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 406 | GCCArgs.push_back(0); // NULL terminator |
| 407 | |
| 408 | std::cout << "<gcc>" << std::flush; |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 409 | if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), |
| 410 | sys::Path())) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 411 | ProcessFailure(GCCPath, &GCCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 412 | exit(1); |
| 413 | } |
| 414 | |
| 415 | std::vector<const char*> ProgramArgs; |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 416 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 417 | 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 Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 425 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 426 | for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 427 | std::cerr << " " << ProgramArgs[i]; |
| 428 | std::cerr << "\n"; |
| 429 | ); |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 430 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 431 | FileRemover OutputBinaryRemover(OutputBinary); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 432 | return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 433 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 434 | Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 437 | int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 438 | std::string &OutputFile) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 439 | sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT); |
| 440 | uniqueFilename.makeUnique(); |
| 441 | OutputFile = uniqueFilename.toString(); |
| 442 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 443 | // 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 Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 451 | #elif defined(__APPLE__) |
Nate Begeman | 7925588 | 2004-10-17 23:03:32 +0000 | [diff] [blame] | 452 | "-single_module", // link all source files into a single module |
Misha Brukman | b3998ec | 2004-07-16 19:45:45 +0000 | [diff] [blame] | 453 | "-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC |
Chris Lattner | e312e15 | 2004-07-19 06:03:51 +0000 | [diff] [blame] | 454 | "-undefined", // in data segment, rather than generating |
Chris Lattner | 5fbf29c | 2004-07-19 06:00:17 +0000 | [diff] [blame] | 455 | "dynamic_lookup", // blocks. dynamic_lookup requires that you set |
| 456 | // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. |
Misha Brukman | b3998ec | 2004-07-16 19:45:45 +0000 | [diff] [blame] | 457 | #else |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 458 | "-shared", // `-shared' for Linux/X86, maybe others |
| 459 | #endif |
Chris Lattner | 1c81f13 | 2005-03-09 03:31:02 +0000 | [diff] [blame] | 460 | |
Andrew Lenharth | 572668a | 2005-03-10 20:15:09 +0000 | [diff] [blame] | 461 | #if defined(__ia64__) || defined(__alpha__) |
Chris Lattner | 1c81f13 | 2005-03-09 03:31:02 +0000 | [diff] [blame] | 462 | "-fPIC", // IA64 requires shared objs to contain PIC |
| 463 | #endif |
Chris Lattner | fdcc71e | 2006-02-04 05:02:27 +0000 | [diff] [blame] | 464 | #ifdef __sparc__ |
| 465 | "-mcpu=v9", |
| 466 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 467 | "-o", OutputFile.c_str(), // Output to the right filename... |
| 468 | "-O2", // Optimize the program a bit... |
| 469 | 0 |
| 470 | }; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 471 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 472 | std::cout << "<gcc>" << std::flush; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 473 | if (RunProgramWithTimeout(GCCPath, GCCArgs, sys::Path(), sys::Path(), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 474 | sys::Path())) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 475 | ProcessFailure(GCCPath, GCCArgs); |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 476 | return 1; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 477 | } |
| 478 | return 0; |
| 479 | } |
| 480 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 481 | /// create - Try to find the `gcc' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 482 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 483 | GCC *GCC::create(const std::string &ProgramPath, std::string &Message) { |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 484 | sys::Path GCCPath = FindExecutable("gcc", ProgramPath); |
| 485 | if (GCCPath.isEmpty()) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 486 | Message = "Cannot find `gcc' in executable directory or PATH!\n"; |
| 487 | return 0; |
| 488 | } |
| 489 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 490 | Message = "Found gcc: " + GCCPath.toString() + "\n"; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 491 | return new GCC(GCCPath); |
| 492 | } |