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 | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 24 | ToolExecutionError::~ToolExecutionError() throw() { } |
| 25 | |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 26 | /// RunProgramWithTimeout - This function provides an alternate interface to the |
| 27 | /// sys::Program::ExecuteAndWait interface. |
| 28 | /// @see sys:Program::ExecuteAndWait |
| 29 | static int RunProgramWithTimeout(const sys::Path &ProgramPath, |
| 30 | const char **Args, |
| 31 | const sys::Path &StdInFile, |
| 32 | const sys::Path &StdOutFile, |
| 33 | const sys::Path &StdErrFile, |
| 34 | unsigned NumSeconds = 0) { |
| 35 | const sys::Path* redirects[3]; |
| 36 | redirects[0] = &StdInFile; |
| 37 | redirects[1] = &StdOutFile; |
| 38 | redirects[2] = &StdErrFile; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 39 | |
| 40 | return |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 41 | sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 46 | static void ProcessFailure(sys::Path ProgPath, const char** Args) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 47 | std::ostringstream OS; |
Chris Lattner | a3de117 | 2004-02-18 20:58:00 +0000 | [diff] [blame] | 48 | OS << "\nError running tool:\n "; |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 49 | for (const char **Arg = Args; *Arg; ++Arg) |
| 50 | OS << " " << *Arg; |
| 51 | OS << "\n"; |
| 52 | |
| 53 | // Rerun the compiler, capturing any error messages to print them. |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 54 | sys::Path ErrorFilename("error_messages"); |
| 55 | ErrorFilename.makeUnique(); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 56 | RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename, |
| 57 | ErrorFilename); |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 58 | |
| 59 | // Print out the error messages generated by GCC if possible... |
| 60 | std::ifstream ErrorFile(ErrorFilename.c_str()); |
| 61 | if (ErrorFile) { |
| 62 | std::copy(std::istreambuf_iterator<char>(ErrorFile), |
| 63 | std::istreambuf_iterator<char>(), |
| 64 | std::ostreambuf_iterator<char>(OS)); |
| 65 | ErrorFile.close(); |
| 66 | } |
| 67 | |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 68 | ErrorFilename.eraseFromDisk(); |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 69 | throw ToolExecutionError(OS.str()); |
| 70 | } |
| 71 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 72 | //===---------------------------------------------------------------------===// |
| 73 | // LLI Implementation of AbstractIntepreter interface |
| 74 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 75 | namespace { |
| 76 | class LLI : public AbstractInterpreter { |
| 77 | std::string LLIPath; // The path to the LLI executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 78 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 79 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 80 | LLI(const std::string &Path, const std::vector<std::string> *Args) |
| 81 | : LLIPath(Path) { |
| 82 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 83 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 84 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 86 | virtual int ExecuteProgram(const std::string &Bytecode, |
| 87 | const std::vector<std::string> &Args, |
| 88 | const std::string &InputFile, |
| 89 | const std::string &OutputFile, |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 90 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 91 | std::vector<std::string>(), |
| 92 | unsigned Timeout = 0); |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 93 | }; |
| 94 | } |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 95 | |
| 96 | int LLI::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 97 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 98 | const std::string &InputFile, |
| 99 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 100 | const std::vector<std::string> &SharedLibs, |
| 101 | unsigned Timeout) { |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 102 | if (!SharedLibs.empty()) |
| 103 | throw ToolExecutionError("LLI currently does not support " |
| 104 | "loading shared libraries."); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 105 | |
| 106 | std::vector<const char*> LLIArgs; |
| 107 | LLIArgs.push_back(LLIPath.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 108 | LLIArgs.push_back("-force-interpreter=true"); |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 109 | |
| 110 | // Add any extra LLI args. |
| 111 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 112 | LLIArgs.push_back(ToolArgs[i].c_str()); |
| 113 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 114 | LLIArgs.push_back(Bytecode.c_str()); |
| 115 | // Add optional parameters to the running program from Argv |
| 116 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 117 | LLIArgs.push_back(Args[i].c_str()); |
| 118 | LLIArgs.push_back(0); |
| 119 | |
| 120 | std::cout << "<lli>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 121 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 122 | for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 123 | std::cerr << " " << LLIArgs[i]; |
| 124 | std::cerr << "\n"; |
| 125 | ); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 126 | return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 127 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 128 | Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // LLI create method - Try to find the LLI executable |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 132 | AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 133 | std::string &Message, |
| 134 | const std::vector<std::string> *ToolArgs) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 135 | std::string LLIPath = FindExecutable("lli", ProgPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 136 | if (!LLIPath.empty()) { |
| 137 | Message = "Found lli: " + LLIPath + "\n"; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 138 | return new LLI(LLIPath, ToolArgs); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | Message = "Cannot find `lli' in executable directory or PATH!\n"; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | // LLC Implementation of AbstractIntepreter interface |
| 147 | // |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 148 | void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 149 | sys::Path uniqueFile(Bytecode+".llc.s"); |
| 150 | uniqueFile.makeUnique(); |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 151 | OutputAsmFile = uniqueFile; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 152 | std::vector<const char *> LLCArgs; |
| 153 | LLCArgs.push_back (LLCPath.c_str()); |
| 154 | |
| 155 | // Add any extra LLC args. |
| 156 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 157 | LLCArgs.push_back(ToolArgs[i].c_str()); |
| 158 | |
| 159 | LLCArgs.push_back ("-o"); |
| 160 | LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file |
| 161 | LLCArgs.push_back ("-f"); // Overwrite as necessary... |
| 162 | LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode |
| 163 | LLCArgs.push_back (0); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 164 | |
| 165 | std::cout << "<llc>" << std::flush; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 166 | DEBUG(std::cerr << "\nAbout to run:\t"; |
| 167 | for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) |
| 168 | std::cerr << " " << LLCArgs[i]; |
| 169 | std::cerr << "\n"; |
| 170 | ); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 171 | if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0], |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 172 | sys::Path(), sys::Path(), sys::Path())) |
| 173 | ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 176 | void LLC::compileProgram(const std::string &Bytecode) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 177 | sys::Path OutputAsmFile; |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 178 | OutputAsm(Bytecode, OutputAsmFile); |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 179 | OutputAsmFile.eraseFromDisk(); |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 182 | int LLC::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 183 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 184 | const std::string &InputFile, |
| 185 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 186 | const std::vector<std::string> &SharedLibs, |
| 187 | unsigned Timeout) { |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 188 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 189 | sys::Path OutputAsmFile; |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 190 | OutputAsm(Bytecode, OutputAsmFile); |
| 191 | FileRemover OutFileRemover(OutputAsmFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 192 | |
| 193 | // Assuming LLC worked, compile the result with GCC and run it. |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 194 | return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 195 | InputFile, OutputFile, SharedLibs, Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 198 | /// createLLC - Try to find the LLC executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 199 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 200 | LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 201 | std::string &Message, |
| 202 | const std::vector<std::string> *Args) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 203 | std::string LLCPath = FindExecutable("llc", ProgramPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 204 | if (LLCPath.empty()) { |
| 205 | Message = "Cannot find `llc' in executable directory or PATH!\n"; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | Message = "Found llc: " + LLCPath + "\n"; |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 210 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 211 | if (!gcc) { |
| 212 | std::cerr << Message << "\n"; |
| 213 | exit(1); |
| 214 | } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 215 | return new LLC(LLCPath, gcc, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | //===---------------------------------------------------------------------===// |
| 219 | // JIT Implementation of AbstractIntepreter interface |
| 220 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 221 | namespace { |
| 222 | class JIT : public AbstractInterpreter { |
| 223 | std::string LLIPath; // The path to the LLI executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 224 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 225 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 226 | JIT(const std::string &Path, const std::vector<std::string> *Args) |
| 227 | : LLIPath(Path) { |
| 228 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 229 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 230 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 232 | virtual int ExecuteProgram(const std::string &Bytecode, |
| 233 | const std::vector<std::string> &Args, |
| 234 | const std::string &InputFile, |
| 235 | const std::string &OutputFile, |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 236 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 237 | std::vector<std::string>(), unsigned Timeout =0); |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 238 | }; |
| 239 | } |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 240 | |
| 241 | int JIT::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 242 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 243 | const std::string &InputFile, |
| 244 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 245 | const std::vector<std::string> &SharedLibs, |
| 246 | unsigned Timeout) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 247 | // Construct a vector of parameters, incorporating those from the command-line |
| 248 | std::vector<const char*> JITArgs; |
| 249 | JITArgs.push_back(LLIPath.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 250 | JITArgs.push_back("-force-interpreter=false"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 251 | |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 252 | // Add any extra LLI args. |
| 253 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 254 | JITArgs.push_back(ToolArgs[i].c_str()); |
| 255 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 256 | for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 257 | JITArgs.push_back("-load"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 258 | JITArgs.push_back(SharedLibs[i].c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 259 | } |
| 260 | JITArgs.push_back(Bytecode.c_str()); |
| 261 | // Add optional parameters to the running program from Argv |
| 262 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 263 | JITArgs.push_back(Args[i].c_str()); |
| 264 | JITArgs.push_back(0); |
| 265 | |
| 266 | std::cout << "<jit>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 267 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 268 | for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 269 | std::cerr << " " << JITArgs[i]; |
| 270 | std::cerr << "\n"; |
| 271 | ); |
| 272 | DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n"); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 273 | return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 274 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 275 | Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 278 | /// createJIT - Try to find the LLI executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 279 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 280 | AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 281 | std::string &Message, const std::vector<std::string> *Args) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 282 | std::string LLIPath = FindExecutable("lli", ProgPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 283 | if (!LLIPath.empty()) { |
| 284 | Message = "Found lli: " + LLIPath + "\n"; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 285 | return new JIT(LLIPath, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | Message = "Cannot find `lli' in executable directory or PATH!\n"; |
| 289 | return 0; |
| 290 | } |
| 291 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 292 | void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 293 | sys::Path uniqueFile(Bytecode+".cbe.c"); |
| 294 | uniqueFile.makeUnique(); |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 295 | OutputCFile = uniqueFile; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 296 | std::vector<const char *> LLCArgs; |
| 297 | LLCArgs.push_back (LLCPath.c_str()); |
| 298 | |
| 299 | // Add any extra LLC args. |
| 300 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 301 | LLCArgs.push_back(ToolArgs[i].c_str()); |
| 302 | |
| 303 | LLCArgs.push_back ("-o"); |
| 304 | LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file |
| 305 | LLCArgs.push_back ("-march=c"); // Output C language |
| 306 | LLCArgs.push_back ("-f"); // Overwrite as necessary... |
| 307 | LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode |
| 308 | LLCArgs.push_back (0); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 309 | |
| 310 | std::cout << "<cbe>" << std::flush; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 311 | DEBUG(std::cerr << "\nAbout to run:\t"; |
| 312 | for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) |
| 313 | std::cerr << " " << LLCArgs[i]; |
| 314 | std::cerr << "\n"; |
| 315 | ); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 316 | if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 317 | sys::Path())) |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 318 | ProcessFailure(LLCPath, &LLCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 321 | void CBE::compileProgram(const std::string &Bytecode) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 322 | sys::Path OutputCFile; |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 323 | OutputC(Bytecode, OutputCFile); |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 324 | OutputCFile.eraseFromDisk(); |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 327 | int CBE::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 328 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 329 | const std::string &InputFile, |
| 330 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 331 | const std::vector<std::string> &SharedLibs, |
| 332 | unsigned Timeout) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 333 | sys::Path OutputCFile; |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 334 | OutputC(Bytecode, OutputCFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 336 | FileRemover CFileRemove(OutputCFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 337 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 338 | return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 339 | InputFile, OutputFile, SharedLibs, Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Chris Lattner | 9915cd9 | 2004-02-17 06:40:06 +0000 | [diff] [blame] | 342 | /// createCBE - Try to find the 'llc' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 343 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 344 | CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 345 | std::string &Message, |
| 346 | const std::vector<std::string> *Args) { |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 347 | sys::Path LLCPath = FindExecutable("llc", ProgramPath); |
| 348 | if (LLCPath.isEmpty()) { |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 349 | Message = |
Chris Lattner | 9915cd9 | 2004-02-17 06:40:06 +0000 | [diff] [blame] | 350 | "Cannot find `llc' in executable directory or PATH!\n"; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 354 | Message = "Found llc: " + LLCPath.toString() + "\n"; |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 355 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 356 | if (!gcc) { |
| 357 | std::cerr << Message << "\n"; |
| 358 | exit(1); |
| 359 | } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 360 | return new CBE(LLCPath, gcc, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | //===---------------------------------------------------------------------===// |
| 364 | // GCC abstraction |
| 365 | // |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 366 | int GCC::ExecuteProgram(const std::string &ProgramFile, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 367 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 368 | FileType fileType, |
| 369 | const std::string &InputFile, |
| 370 | const std::string &OutputFile, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 371 | const std::vector<std::string> &SharedLibs, |
| 372 | unsigned Timeout) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 373 | std::vector<const char*> GCCArgs; |
| 374 | |
| 375 | GCCArgs.push_back(GCCPath.c_str()); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 376 | |
| 377 | // Specify the shared libraries to link in... |
| 378 | for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) |
| 379 | GCCArgs.push_back(SharedLibs[i].c_str()); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 380 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 381 | // Specify -x explicitly in case the extension is wonky |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 382 | GCCArgs.push_back("-x"); |
| 383 | if (fileType == CFile) { |
| 384 | GCCArgs.push_back("c"); |
| 385 | GCCArgs.push_back("-fno-strict-aliasing"); |
| 386 | } else { |
| 387 | GCCArgs.push_back("assembler"); |
| 388 | } |
| 389 | GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename... |
| 390 | GCCArgs.push_back("-o"); |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 391 | sys::Path OutputBinary (ProgramFile+".gcc.exe"); |
| 392 | OutputBinary.makeUnique(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 393 | GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file... |
| 394 | GCCArgs.push_back("-lm"); // Hard-code the math library... |
| 395 | GCCArgs.push_back("-O2"); // Optimize the program a bit... |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 396 | #if defined (HAVE_LINK_R) |
Chris Lattner | 1f0f162 | 2003-10-18 21:54:47 +0000 | [diff] [blame] | 397 | GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 398 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 399 | GCCArgs.push_back(0); // NULL terminator |
| 400 | |
| 401 | std::cout << "<gcc>" << std::flush; |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 402 | if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), |
| 403 | sys::Path())) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 404 | ProcessFailure(GCCPath, &GCCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 405 | exit(1); |
| 406 | } |
| 407 | |
| 408 | std::vector<const char*> ProgramArgs; |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 409 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 410 | ProgramArgs.push_back(OutputBinary.c_str()); |
| 411 | // Add optional parameters to the running program from Argv |
| 412 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 413 | ProgramArgs.push_back(Args[i].c_str()); |
| 414 | ProgramArgs.push_back(0); // NULL terminator |
| 415 | |
| 416 | // Now that we have a binary, run it! |
| 417 | std::cout << "<program>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 418 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 419 | for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 420 | std::cerr << " " << ProgramArgs[i]; |
| 421 | std::cerr << "\n"; |
| 422 | ); |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 423 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 424 | FileRemover OutputBinaryRemover(OutputBinary); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 425 | return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 426 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 427 | Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 430 | int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 431 | std::string &OutputFile) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 432 | sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT); |
| 433 | uniqueFilename.makeUnique(); |
| 434 | OutputFile = uniqueFilename.toString(); |
| 435 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 436 | // Compile the C/asm file into a shared object |
| 437 | const char* GCCArgs[] = { |
| 438 | GCCPath.c_str(), |
| 439 | "-x", (fileType == AsmFile) ? "assembler" : "c", |
| 440 | "-fno-strict-aliasing", |
| 441 | InputFile.c_str(), // Specify the input filename... |
| 442 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 443 | "-G", // Compile a shared library, `-G' for Sparc |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 444 | #elif defined(__APPLE__) |
Nate Begeman | 7925588 | 2004-10-17 23:03:32 +0000 | [diff] [blame] | 445 | "-single_module", // link all source files into a single module |
Misha Brukman | b3998ec | 2004-07-16 19:45:45 +0000 | [diff] [blame] | 446 | "-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC |
Chris Lattner | e312e15 | 2004-07-19 06:03:51 +0000 | [diff] [blame] | 447 | "-undefined", // in data segment, rather than generating |
Chris Lattner | 5fbf29c | 2004-07-19 06:00:17 +0000 | [diff] [blame] | 448 | "dynamic_lookup", // blocks. dynamic_lookup requires that you set |
| 449 | // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. |
Misha Brukman | b3998ec | 2004-07-16 19:45:45 +0000 | [diff] [blame] | 450 | #else |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 451 | "-shared", // `-shared' for Linux/X86, maybe others |
| 452 | #endif |
Chris Lattner | 1c81f13 | 2005-03-09 03:31:02 +0000 | [diff] [blame] | 453 | |
Andrew Lenharth | 572668a | 2005-03-10 20:15:09 +0000 | [diff] [blame] | 454 | #if defined(__ia64__) || defined(__alpha__) |
Chris Lattner | 1c81f13 | 2005-03-09 03:31:02 +0000 | [diff] [blame] | 455 | "-fPIC", // IA64 requires shared objs to contain PIC |
| 456 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 457 | "-o", OutputFile.c_str(), // Output to the right filename... |
| 458 | "-O2", // Optimize the program a bit... |
| 459 | 0 |
| 460 | }; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 461 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 462 | std::cout << "<gcc>" << std::flush; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 463 | if (RunProgramWithTimeout(GCCPath, GCCArgs, sys::Path(), sys::Path(), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 464 | sys::Path())) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 465 | ProcessFailure(GCCPath, GCCArgs); |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 466 | return 1; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 467 | } |
| 468 | return 0; |
| 469 | } |
| 470 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 471 | /// create - Try to find the `gcc' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 472 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 473 | GCC *GCC::create(const std::string &ProgramPath, std::string &Message) { |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 474 | sys::Path GCCPath = FindExecutable("gcc", ProgramPath); |
| 475 | if (GCCPath.isEmpty()) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 476 | Message = "Cannot find `gcc' in executable directory or PATH!\n"; |
| 477 | return 0; |
| 478 | } |
| 479 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 480 | Message = "Found gcc: " + GCCPath.toString() + "\n"; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 481 | return new GCC(GCCPath); |
| 482 | } |