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