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