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