Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===// |
| 2 | // |
| 3 | // This file contains code used to execute the program utilizing one of the |
| 4 | // various ways of running LLVM bytecode. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | /* |
| 9 | BUGPOINT NOTES: |
| 10 | |
| 11 | 1. Bugpoint should not leave any files behind if the program works properly |
| 12 | 2. There should be an option to specify the program name, which specifies a |
| 13 | unique string to put into output files. This allows operation in the |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 14 | SingleSource directory, e.g. default to the first input filename. |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "BugDriver.h" |
| 18 | #include "SystemUtils.h" |
| 19 | #include "Support/CommandLine.h" |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 20 | #include "Support/Statistic.h" |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 21 | #include <fstream> |
Chris Lattner | 7c0f862 | 2002-12-24 00:44:34 +0000 | [diff] [blame] | 22 | #include <iostream> |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | // OutputType - Allow the user to specify the way code should be run, to test |
| 26 | // for miscompilation. |
| 27 | // |
| 28 | enum OutputType { |
| 29 | RunLLI, RunJIT, RunLLC, RunCBE |
| 30 | }; |
| 31 | cl::opt<OutputType> |
| 32 | InterpreterSel(cl::desc("Specify how LLVM code should be executed:"), |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 33 | cl::values(clEnumValN(RunLLI, "run-lli", "Execute with LLI"), |
| 34 | clEnumValN(RunJIT, "run-jit", "Execute with JIT"), |
| 35 | clEnumValN(RunLLC, "run-llc", "Compile with LLC"), |
| 36 | clEnumValN(RunCBE, "run-cbe", "Compile with CBE"), |
| 37 | 0)); |
Chris Lattner | 68efaa7 | 2003-04-23 20:31:37 +0000 | [diff] [blame] | 38 | |
| 39 | cl::opt<std::string> |
| 40 | InputFile("input", cl::init("/dev/null"), |
| 41 | cl::desc("Filename to pipe in as stdin (default: /dev/null)")); |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 42 | |
| 43 | enum FileType { AsmFile, CFile }; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /// AbstractInterpreter Class - Subclasses of this class are used to execute |
| 47 | /// LLVM bytecode in a variety of ways. This abstract interface hides this |
| 48 | /// complexity behind a simple interface. |
| 49 | /// |
| 50 | struct AbstractInterpreter { |
| 51 | |
| 52 | virtual ~AbstractInterpreter() {} |
| 53 | |
| 54 | /// ExecuteProgram - Run the specified bytecode file, emitting output to the |
| 55 | /// specified filename. This returns the exit code of the program. |
| 56 | /// |
| 57 | virtual int ExecuteProgram(const std::string &Bytecode, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 58 | const std::string &OutputFile, |
| 59 | const std::string &SharedLib = "") = 0; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | // LLI Implementation of AbstractIntepreter interface |
| 65 | // |
| 66 | class LLI : public AbstractInterpreter { |
| 67 | std::string LLIPath; // The path to the LLI executable |
| 68 | public: |
| 69 | LLI(const std::string &Path) : LLIPath(Path) { } |
| 70 | |
| 71 | // LLI create method - Try to find the LLI executable |
| 72 | static LLI *create(BugDriver *BD, std::string &Message) { |
| 73 | std::string LLIPath = FindExecutable("lli", BD->getToolName()); |
| 74 | if (!LLIPath.empty()) { |
| 75 | Message = "Found lli: " + LLIPath + "\n"; |
| 76 | return new LLI(LLIPath); |
| 77 | } |
| 78 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 79 | Message = "Cannot find `lli' in bugpoint executable directory or PATH!\n"; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | virtual int ExecuteProgram(const std::string &Bytecode, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 83 | const std::string &OutputFile, |
| 84 | const std::string &SharedLib = ""); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | int LLI::ExecuteProgram(const std::string &Bytecode, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 88 | const std::string &OutputFile, |
| 89 | const std::string &SharedLib) { |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 90 | if (!SharedLib.empty()) { |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 91 | std::cerr << "LLI currently does not support loading shared libraries.\n" |
| 92 | << "Exiting.\n"; |
| 93 | exit(1); |
| 94 | } |
| 95 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 96 | const char *Args[] = { |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 97 | LLIPath.c_str(), |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 98 | "-abort-on-exception", |
| 99 | "-quiet", |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 100 | "-force-interpreter=true", |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 101 | Bytecode.c_str(), |
| 102 | 0 |
| 103 | }; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 104 | |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 105 | std::cout << "<lli>"; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 106 | return RunProgramWithTimeout(LLIPath, Args, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 107 | InputFile, OutputFile, OutputFile); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 110 | //===----------------------------------------------------------------------===// |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 111 | // GCC abstraction |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 112 | // |
| 113 | // This is not a *real* AbstractInterpreter as it does not accept bytecode |
| 114 | // files, but only input acceptable to GCC, i.e. C, C++, and assembly files |
| 115 | // |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 116 | class GCC { |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 117 | std::string GCCPath; // The path to the gcc executable |
| 118 | public: |
| 119 | GCC(const std::string &gccPath) : GCCPath(gccPath) { } |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 120 | virtual ~GCC() {} |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 121 | |
| 122 | // GCC create method - Try to find the `gcc' executable |
| 123 | static GCC *create(BugDriver *BD, std::string &Message) { |
| 124 | std::string GCCPath = FindExecutable("gcc", BD->getToolName()); |
| 125 | if (GCCPath.empty()) { |
| 126 | Message = "Cannot find `gcc' in bugpoint executable directory or PATH!\n"; |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | Message = "Found gcc: " + GCCPath + "\n"; |
| 131 | return new GCC(GCCPath); |
| 132 | } |
| 133 | |
| 134 | virtual int ExecuteProgram(const std::string &ProgramFile, |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 135 | FileType fileType, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 136 | const std::string &OutputFile, |
| 137 | const std::string &SharedLib = ""); |
| 138 | |
| 139 | int MakeSharedObject(const std::string &InputFile, |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 140 | FileType fileType, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 141 | std::string &OutputFile); |
| 142 | |
| 143 | void ProcessFailure(const char **Args); |
| 144 | }; |
| 145 | |
| 146 | int GCC::ExecuteProgram(const std::string &ProgramFile, |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 147 | FileType fileType, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 148 | const std::string &OutputFile, |
| 149 | const std::string &SharedLib) { |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 150 | std::string OutputBinary = getUniqueFilename("bugpoint.gcc.exe"); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 151 | const char **GCCArgs; |
| 152 | |
| 153 | const char *ArgsWithoutSO[] = { |
| 154 | GCCPath.c_str(), |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 155 | "-x", (fileType == AsmFile) ? "assembler" : "c", |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 156 | ProgramFile.c_str(), // Specify the input filename... |
| 157 | "-o", OutputBinary.c_str(), // Output to the right filename... |
| 158 | "-lm", // Hard-code the math library... |
| 159 | "-O2", // Optimize the program a bit... |
| 160 | 0 |
| 161 | }; |
| 162 | const char *ArgsWithSO[] = { |
| 163 | GCCPath.c_str(), |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 164 | SharedLib.c_str(), // Specify the shared library to link in... |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 165 | "-x", (fileType == AsmFile) ? "assembler" : "c", |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 166 | ProgramFile.c_str(), // Specify the input filename... |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 167 | "-o", OutputBinary.c_str(), // Output to the right filename... |
| 168 | "-lm", // Hard-code the math library... |
| 169 | "-O2", // Optimize the program a bit... |
| 170 | 0 |
| 171 | }; |
| 172 | |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 173 | GCCArgs = (SharedLib.empty()) ? ArgsWithoutSO : ArgsWithSO; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 174 | std::cout << "<gcc>"; |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 175 | if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null", |
| 176 | "/dev/null")) { |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 177 | ProcessFailure(GCCArgs); |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 178 | exit(1); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | const char *ProgramArgs[] = { |
| 182 | OutputBinary.c_str(), |
| 183 | 0 |
| 184 | }; |
| 185 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 186 | // Now that we have a binary, run it! |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 187 | std::cout << "<program>"; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 188 | int ProgramResult = RunProgramWithTimeout(OutputBinary, ProgramArgs, |
| 189 | InputFile, OutputFile, OutputFile); |
| 190 | std::cout << "\n"; |
| 191 | removeFile(OutputBinary); |
| 192 | return ProgramResult; |
| 193 | } |
| 194 | |
| 195 | int GCC::MakeSharedObject(const std::string &InputFile, |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 196 | FileType fileType, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 197 | std::string &OutputFile) { |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 198 | OutputFile = getUniqueFilename("./bugpoint.so"); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 199 | // Compile the C/asm file into a shared object |
| 200 | const char* GCCArgs[] = { |
| 201 | GCCPath.c_str(), |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 202 | "-x", (fileType == AsmFile) ? "assembler" : "c", |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 203 | InputFile.c_str(), // Specify the input filename... |
| 204 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 205 | "-G", // Compile a shared library, `-G' for Sparc |
| 206 | #else |
| 207 | "-shared", // `-shared' for Linux/X86, maybe others |
| 208 | #endif |
| 209 | "-o", OutputFile.c_str(), // Output to the right filename... |
| 210 | "-O2", // Optimize the program a bit... |
| 211 | 0 |
| 212 | }; |
| 213 | |
| 214 | std::cout << "<gcc>"; |
| 215 | if(RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null", |
| 216 | "/dev/null")) { |
| 217 | ProcessFailure(GCCArgs); |
| 218 | exit(1); |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | void GCC::ProcessFailure(const char** GCCArgs) { |
| 224 | std::cerr << "\n*** bugpoint error: invocation of the C compiler failed!\n"; |
| 225 | for (const char **Arg = GCCArgs; *Arg; ++Arg) |
| 226 | std::cerr << " " << *Arg; |
| 227 | std::cerr << "\n"; |
| 228 | |
| 229 | // Rerun the compiler, capturing any error messages to print them. |
| 230 | std::string ErrorFilename = getUniqueFilename("bugpoint.gcc.errors"); |
| 231 | RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", ErrorFilename.c_str(), |
| 232 | ErrorFilename.c_str()); |
| 233 | |
| 234 | // Print out the error messages generated by GCC if possible... |
| 235 | std::ifstream ErrorFile(ErrorFilename.c_str()); |
| 236 | if (ErrorFile) { |
| 237 | std::copy(std::istreambuf_iterator<char>(ErrorFile), |
| 238 | std::istreambuf_iterator<char>(), |
| 239 | std::ostreambuf_iterator<char>(std::cerr)); |
| 240 | ErrorFile.close(); |
| 241 | std::cerr << "\n"; |
| 242 | } |
| 243 | |
| 244 | removeFile(ErrorFilename); |
| 245 | } |
| 246 | |
| 247 | //===----------------------------------------------------------------------===// |
| 248 | // LLC Implementation of AbstractIntepreter interface |
| 249 | // |
| 250 | class LLC : public AbstractInterpreter { |
| 251 | std::string LLCPath; // The path to the LLC executable |
| 252 | GCC *gcc; |
| 253 | public: |
| 254 | LLC(const std::string &llcPath, GCC *Gcc) |
| 255 | : LLCPath(llcPath), gcc(Gcc) { } |
| 256 | ~LLC() { delete gcc; } |
| 257 | |
| 258 | // LLC create method - Try to find the LLC executable |
| 259 | static LLC *create(BugDriver *BD, std::string &Message) { |
| 260 | std::string LLCPath = FindExecutable("llc", BD->getToolName()); |
| 261 | if (LLCPath.empty()) { |
| 262 | Message = "Cannot find `llc' in bugpoint executable directory or PATH!\n"; |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | Message = "Found llc: " + LLCPath + "\n"; |
| 267 | GCC *gcc = GCC::create(BD, Message); |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 268 | if (!gcc) { |
| 269 | std::cerr << Message << "\n"; |
| 270 | exit(1); |
| 271 | } |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 272 | return new LLC(LLCPath, gcc); |
| 273 | } |
| 274 | |
| 275 | virtual int ExecuteProgram(const std::string &Bytecode, |
| 276 | const std::string &OutputFile, |
| 277 | const std::string &SharedLib = ""); |
| 278 | |
| 279 | int OutputAsm(const std::string &Bytecode, |
| 280 | std::string &OutputAsmFile); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | int LLC::OutputAsm(const std::string &Bytecode, |
| 284 | std::string &OutputAsmFile) { |
| 285 | OutputAsmFile = "bugpoint.llc.s"; |
| 286 | const char *LLCArgs[] = { |
| 287 | LLCPath.c_str(), |
| 288 | "-o", OutputAsmFile.c_str(), // Output to the Asm file |
| 289 | "-f", // Overwrite as necessary... |
| 290 | Bytecode.c_str(), // This is the input bytecode |
| 291 | 0 |
| 292 | }; |
| 293 | |
| 294 | std::cout << "<llc>"; |
| 295 | if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null", |
| 296 | "/dev/null")) { |
| 297 | // If LLC failed on the bytecode, print error... |
| 298 | std::cerr << "bugpoint error: `llc' failed!\n"; |
| 299 | removeFile(OutputAsmFile); |
| 300 | return 1; |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | int LLC::ExecuteProgram(const std::string &Bytecode, |
| 307 | const std::string &OutputFile, |
| 308 | const std::string &SharedLib) { |
| 309 | |
| 310 | std::string OutputAsmFile; |
| 311 | if (OutputAsm(Bytecode, OutputAsmFile)) { |
| 312 | std::cerr << "Could not generate asm code with `llc', exiting.\n"; |
| 313 | exit(1); |
| 314 | } |
| 315 | |
| 316 | // Assuming LLC worked, compile the result with GCC and run it. |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 317 | int Result = gcc->ExecuteProgram(OutputAsmFile,AsmFile,OutputFile,SharedLib); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 318 | removeFile(OutputAsmFile); |
| 319 | return Result; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 324 | // JIT Implementation of AbstractIntepreter interface |
| 325 | // |
| 326 | class JIT : public AbstractInterpreter { |
| 327 | std::string LLIPath; // The path to the LLI executable |
| 328 | public: |
| 329 | JIT(const std::string &Path) : LLIPath(Path) { } |
| 330 | |
| 331 | // JIT create method - Try to find the LLI executable |
| 332 | static JIT *create(BugDriver *BD, std::string &Message) { |
| 333 | std::string LLIPath = FindExecutable("lli", BD->getToolName()); |
| 334 | if (!LLIPath.empty()) { |
| 335 | Message = "Found lli: " + LLIPath + "\n"; |
| 336 | return new JIT(LLIPath); |
| 337 | } |
| 338 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 339 | Message = "Cannot find `lli' in bugpoint executable directory or PATH!\n"; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | virtual int ExecuteProgram(const std::string &Bytecode, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 343 | const std::string &OutputFile, |
| 344 | const std::string &SharedLib = ""); |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | int JIT::ExecuteProgram(const std::string &Bytecode, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 348 | const std::string &OutputFile, |
| 349 | const std::string &SharedLib) { |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 350 | const char* ArgsWithoutSO[] = { |
| 351 | LLIPath.c_str(), "-quiet", "-force-interpreter=false", |
| 352 | Bytecode.c_str(), |
| 353 | 0 |
| 354 | }; |
| 355 | |
| 356 | const char* ArgsWithSO[] = { |
| 357 | LLIPath.c_str(), "-quiet", "-force-interpreter=false", |
| 358 | "-load", SharedLib.c_str(), |
| 359 | Bytecode.c_str(), |
| 360 | 0 |
| 361 | }; |
| 362 | |
| 363 | const char** JITArgs = SharedLib.empty() ? ArgsWithoutSO : ArgsWithSO; |
| 364 | |
| 365 | std::cout << "<jit>"; |
| 366 | DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n"); |
| 367 | return RunProgramWithTimeout(LLIPath, JITArgs, |
| 368 | InputFile, OutputFile, OutputFile); |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | //===----------------------------------------------------------------------===// |
| 372 | // CBE Implementation of AbstractIntepreter interface |
| 373 | // |
| 374 | class CBE : public AbstractInterpreter { |
| 375 | std::string DISPath; // The path to the LLVM 'dis' executable |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 376 | GCC *gcc; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 377 | public: |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 378 | CBE(const std::string &disPath, GCC *Gcc) : DISPath(disPath), gcc(Gcc) { } |
| 379 | ~CBE() { delete gcc; } |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 380 | |
| 381 | // CBE create method - Try to find the 'dis' executable |
| 382 | static CBE *create(BugDriver *BD, std::string &Message) { |
| 383 | std::string DISPath = FindExecutable("dis", BD->getToolName()); |
| 384 | if (DISPath.empty()) { |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 385 | Message = "Cannot find `dis' in bugpoint executable directory or PATH!\n"; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | Message = "Found dis: " + DISPath + "\n"; |
| 390 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 391 | GCC *gcc = GCC::create(BD, Message); |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 392 | if (!gcc) { |
| 393 | std::cerr << Message << "\n"; |
| 394 | exit(1); |
| 395 | } |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 396 | return new CBE(DISPath, gcc); |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 397 | } |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 399 | virtual int ExecuteProgram(const std::string &Bytecode, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 400 | const std::string &OutputFile, |
| 401 | const std::string &SharedLib = ""); |
| 402 | |
| 403 | // Sometimes we just want to go half-way and only generate the C file, |
| 404 | // not necessarily compile it with GCC and run the program |
| 405 | virtual int OutputC(const std::string &Bytecode, |
| 406 | std::string &OutputCFile); |
| 407 | |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 408 | }; |
| 409 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 410 | int CBE::OutputC(const std::string &Bytecode, |
| 411 | std::string &OutputCFile) { |
| 412 | OutputCFile = "bugpoint.cbe.c"; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 413 | const char *DisArgs[] = { |
| 414 | DISPath.c_str(), |
| 415 | "-o", OutputCFile.c_str(), // Output to the C file |
| 416 | "-c", // Output to C |
| 417 | "-f", // Overwrite as necessary... |
| 418 | Bytecode.c_str(), // This is the input bytecode |
| 419 | 0 |
| 420 | }; |
| 421 | |
| 422 | std::cout << "<cbe>"; |
| 423 | if (RunProgramWithTimeout(DISPath, DisArgs, "/dev/null", "/dev/null", |
| 424 | "/dev/null")) { |
| 425 | // If dis failed on the bytecode, print error... |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 426 | std::cerr << "bugpoint error: `dis -c' failed!\n"; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 427 | return 1; |
| 428 | } |
| 429 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 433 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 434 | int CBE::ExecuteProgram(const std::string &Bytecode, |
| 435 | const std::string &OutputFile, |
| 436 | const std::string &SharedLib) { |
| 437 | std::string OutputCFile; |
| 438 | if (OutputC(Bytecode, OutputCFile)) { |
| 439 | std::cerr << "Could not generate C code with `dis', exiting.\n"; |
| 440 | exit(1); |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 443 | int Result = gcc->ExecuteProgram(OutputCFile, CFile, OutputFile, SharedLib); |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 444 | removeFile(OutputCFile); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 446 | return Result; |
| 447 | } |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 448 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 449 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 450 | //===----------------------------------------------------------------------===// |
| 451 | // BugDriver method implementation |
| 452 | // |
| 453 | |
| 454 | /// initializeExecutionEnvironment - This method is used to set up the |
| 455 | /// environment for executing LLVM programs. |
| 456 | /// |
| 457 | bool BugDriver::initializeExecutionEnvironment() { |
| 458 | std::cout << "Initializing execution environment: "; |
| 459 | |
| 460 | // FIXME: This should default to searching for the best interpreter to use on |
| 461 | // this platform, which would be JIT, then LLC, then CBE, then LLI. |
| 462 | |
| 463 | // Create an instance of the AbstractInterpreter interface as specified on the |
| 464 | // command line |
| 465 | std::string Message; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 466 | switch (InterpreterSel) { |
| 467 | case RunLLI: Interpreter = LLI::create(this, Message); break; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 468 | case RunLLC: Interpreter = LLC::create(this, Message); break; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 469 | case RunJIT: Interpreter = JIT::create(this, Message); break; |
| 470 | case RunCBE: Interpreter = CBE::create(this, Message); break; |
| 471 | default: |
| 472 | Message = " Sorry, this back-end is not supported by bugpoint right now!\n"; |
| 473 | break; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | std::cout << Message; |
| 477 | |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 478 | // Initialize auxiliary tools for debugging |
| 479 | cbe = CBE::create(this, Message); |
| 480 | if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); } |
| 481 | gcc = GCC::create(this, Message); |
| 482 | if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); } |
| 483 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 484 | // If there was an error creating the selected interpreter, quit with error. |
| 485 | return Interpreter == 0; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | /// executeProgram - This method runs "Program", capturing the output of the |
| 490 | /// program to a file, returning the filename of the file. A recommended |
| 491 | /// filename may be optionally specified. |
| 492 | /// |
| 493 | std::string BugDriver::executeProgram(std::string OutputFile, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 494 | std::string BytecodeFile, |
| 495 | std::string SharedObject, |
| 496 | AbstractInterpreter *AI) { |
| 497 | assert((Interpreter || AI) &&"Interpreter should have been created already!"); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 498 | bool CreatedBytecode = false; |
| 499 | if (BytecodeFile.empty()) { |
| 500 | // Emit the program to a bytecode file... |
| 501 | BytecodeFile = getUniqueFilename("bugpoint-test-program.bc"); |
| 502 | |
| 503 | if (writeProgramToFile(BytecodeFile, Program)) { |
| 504 | std::cerr << ToolName << ": Error emitting bytecode to file '" |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 505 | << BytecodeFile << "'!\n"; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 506 | exit(1); |
| 507 | } |
| 508 | CreatedBytecode = true; |
| 509 | } |
| 510 | |
| 511 | if (OutputFile.empty()) OutputFile = "bugpoint-execution-output"; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 512 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 513 | // Check to see if this is a valid output filename... |
| 514 | OutputFile = getUniqueFilename(OutputFile); |
| 515 | |
| 516 | // Actually execute the program! |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 517 | int RetVal = (AI != 0) ? |
| 518 | AI->ExecuteProgram(BytecodeFile, OutputFile, SharedObject) : |
| 519 | Interpreter->ExecuteProgram(BytecodeFile, OutputFile, SharedObject); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 520 | |
| 521 | // Remove the temporary bytecode file. |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 522 | if (CreatedBytecode) removeFile(BytecodeFile); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 523 | |
| 524 | // Return the filename we captured the output to. |
| 525 | return OutputFile; |
| 526 | } |
| 527 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 528 | std::string BugDriver::executeProgramWithCBE(std::string OutputFile, |
| 529 | std::string BytecodeFile, |
| 530 | std::string SharedObject) { |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 531 | return executeProgram(OutputFile, BytecodeFile, SharedObject, cbe); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | int BugDriver::compileSharedObject(const std::string &BytecodeFile, |
| 535 | std::string &SharedObject) { |
| 536 | assert(Interpreter && "Interpreter should have been created already!"); |
| 537 | std::string Message, OutputCFile; |
| 538 | |
| 539 | // Using CBE |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 540 | cbe->OutputC(BytecodeFile, OutputCFile); |
| 541 | |
| 542 | #if 0 /* This is an alternative, as yet unimplemented */ |
| 543 | // Using LLC |
| 544 | LLC *llc = LLC::create(this, Message); |
| 545 | if (llc->OutputAsm(BytecodeFile, OutputFile)) { |
| 546 | std::cerr << "Could not generate asm code with `llc', exiting.\n"; |
| 547 | exit(1); |
| 548 | } |
| 549 | #endif |
| 550 | |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 551 | gcc->MakeSharedObject(OutputCFile, CFile, SharedObject); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 552 | |
| 553 | // Remove the intermediate C file |
| 554 | removeFile(OutputCFile); |
| 555 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 560 | /// diffProgram - This method executes the specified module and diffs the output |
| 561 | /// against the file specified by ReferenceOutputFile. If the output is |
| 562 | /// different, true is returned. |
| 563 | /// |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 564 | bool BugDriver::diffProgram(const std::string &BytecodeFile, |
| 565 | const std::string &SharedObject, |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 566 | bool RemoveBytecode) { |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 567 | // Execute the program, generating an output file... |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 568 | std::string Output = executeProgram("", BytecodeFile, SharedObject); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 569 | |
| 570 | std::ifstream ReferenceFile(ReferenceOutputFile.c_str()); |
| 571 | if (!ReferenceFile) { |
| 572 | std::cerr << "Couldn't open reference output file '" |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 573 | << ReferenceOutputFile << "'\n"; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 574 | exit(1); |
| 575 | } |
| 576 | |
| 577 | std::ifstream OutputFile(Output.c_str()); |
| 578 | if (!OutputFile) { |
| 579 | std::cerr << "Couldn't open output file: " << Output << "'!\n"; |
| 580 | exit(1); |
| 581 | } |
| 582 | |
| 583 | bool FilesDifferent = false; |
| 584 | |
| 585 | // Compare the two files... |
| 586 | int C1, C2; |
| 587 | do { |
| 588 | C1 = ReferenceFile.get(); |
| 589 | C2 = OutputFile.get(); |
| 590 | if (C1 != C2) { FilesDifferent = true; break; } |
| 591 | } while (C1 != EOF); |
| 592 | |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 593 | //removeFile(Output); |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 594 | if (RemoveBytecode) removeFile(BytecodeFile); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 595 | return FilesDifferent; |
| 596 | } |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame^] | 597 | |
| 598 | bool BugDriver::isExecutingJIT() { |
| 599 | return InterpreterSel == RunJIT; |
| 600 | } |