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