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" |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 18 | #include "Support/CommandLine.h" |
Chris Lattner | f0c6964 | 2003-08-01 22:13:59 +0000 | [diff] [blame] | 19 | #include "Support/Debug.h" |
Chris Lattner | aa997fb | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 20 | #include "Support/FileUtilities.h" |
Misha Brukman | 0c2305b | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 21 | #include "Support/SystemUtils.h" |
Chris Lattner | 10bd1c2 | 2003-10-07 13:45:51 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ToolRunner.h" |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 23 | #include <fstream> |
Chris Lattner | 7c0f862 | 2002-12-24 00:44:34 +0000 | [diff] [blame] | 24 | #include <iostream> |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | // OutputType - Allow the user to specify the way code should be run, to test |
| 28 | // for miscompilation. |
| 29 | // |
| 30 | enum OutputType { |
| 31 | RunLLI, RunJIT, RunLLC, RunCBE |
| 32 | }; |
Misha Brukman | 5bc6a8f | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 33 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 34 | cl::opt<OutputType> |
| 35 | InterpreterSel(cl::desc("Specify how LLVM code should be executed:"), |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 36 | cl::values(clEnumValN(RunLLI, "run-lli", "Execute with LLI"), |
| 37 | clEnumValN(RunJIT, "run-jit", "Execute with JIT"), |
| 38 | clEnumValN(RunLLC, "run-llc", "Compile with LLC"), |
| 39 | clEnumValN(RunCBE, "run-cbe", "Compile with CBE"), |
| 40 | 0)); |
Chris Lattner | 68efaa7 | 2003-04-23 20:31:37 +0000 | [diff] [blame] | 41 | |
| 42 | cl::opt<std::string> |
| 43 | InputFile("input", cl::init("/dev/null"), |
| 44 | cl::desc("Filename to pipe in as stdin (default: /dev/null)")); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Misha Brukman | 40feb36 | 2003-07-30 20:15:44 +0000 | [diff] [blame] | 47 | // Anything specified after the --args option are taken as arguments to the |
| 48 | // program being debugged. |
| 49 | cl::list<std::string> |
| 50 | InputArgv("args", cl::Positional, cl::desc("<program arguments>..."), |
| 51 | cl::ZeroOrMore); |
| 52 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 53 | //===----------------------------------------------------------------------===// |
| 54 | // BugDriver method implementation |
| 55 | // |
| 56 | |
| 57 | /// initializeExecutionEnvironment - This method is used to set up the |
| 58 | /// environment for executing LLVM programs. |
| 59 | /// |
| 60 | bool BugDriver::initializeExecutionEnvironment() { |
| 61 | std::cout << "Initializing execution environment: "; |
| 62 | |
| 63 | // FIXME: This should default to searching for the best interpreter to use on |
| 64 | // this platform, which would be JIT, then LLC, then CBE, then LLI. |
| 65 | |
Misha Brukman | 5bc6a8f | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 66 | // Create an instance of the AbstractInterpreter interface as specified on |
| 67 | // the command line |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 68 | std::string Message; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 69 | switch (InterpreterSel) { |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame^] | 70 | case RunLLI: |
| 71 | Interpreter = AbstractInterpreter::createLLI(getToolName(), Message); |
| 72 | break; |
| 73 | case RunLLC: |
| 74 | Interpreter = AbstractInterpreter::createLLC(getToolName(), Message); |
| 75 | break; |
| 76 | case RunJIT: |
| 77 | Interpreter = AbstractInterpreter::createJIT(getToolName(), Message); |
| 78 | break; |
| 79 | case RunCBE: |
| 80 | Interpreter = AbstractInterpreter::createCBE(getToolName(), Message); |
| 81 | break; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 82 | default: |
Misha Brukman | 5bc6a8f | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 83 | Message = "Sorry, this back-end is not supported by bugpoint right now!\n"; |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 84 | break; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 85 | } |
Misha Brukman | 5bc6a8f | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 86 | std::cerr << Message; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 87 | |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 88 | // Initialize auxiliary tools for debugging |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame^] | 89 | cbe = AbstractInterpreter::createCBE(getToolName(), Message); |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 90 | if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); } |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame^] | 91 | gcc = GCC::create(getToolName(), Message); |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 92 | if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); } |
| 93 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 94 | // If there was an error creating the selected interpreter, quit with error. |
| 95 | return Interpreter == 0; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /// executeProgram - This method runs "Program", capturing the output of the |
| 100 | /// program to a file, returning the filename of the file. A recommended |
| 101 | /// filename may be optionally specified. |
| 102 | /// |
| 103 | std::string BugDriver::executeProgram(std::string OutputFile, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 104 | std::string BytecodeFile, |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame^] | 105 | const std::string &SharedObj, |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 106 | AbstractInterpreter *AI) { |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame^] | 107 | if (AI == 0) AI = Interpreter; |
| 108 | assert(AI && "Interpreter should have been created already!"); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 109 | bool CreatedBytecode = false; |
| 110 | if (BytecodeFile.empty()) { |
| 111 | // Emit the program to a bytecode file... |
| 112 | BytecodeFile = getUniqueFilename("bugpoint-test-program.bc"); |
| 113 | |
| 114 | if (writeProgramToFile(BytecodeFile, Program)) { |
| 115 | std::cerr << ToolName << ": Error emitting bytecode to file '" |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 116 | << BytecodeFile << "'!\n"; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 117 | exit(1); |
| 118 | } |
| 119 | CreatedBytecode = true; |
| 120 | } |
| 121 | |
| 122 | if (OutputFile.empty()) OutputFile = "bugpoint-execution-output"; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 123 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 124 | // Check to see if this is a valid output filename... |
| 125 | OutputFile = getUniqueFilename(OutputFile); |
| 126 | |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame^] | 127 | // Figure out which shared objects to run, if any. |
| 128 | std::vector<std::string> SharedObjs; |
| 129 | if (!SharedObj.empty()) |
| 130 | SharedObjs.push_back(SharedObj); |
| 131 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 132 | // Actually execute the program! |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame^] | 133 | int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile, |
| 134 | OutputFile, SharedObjs); |
| 135 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 136 | |
| 137 | // Remove the temporary bytecode file. |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 138 | if (CreatedBytecode) removeFile(BytecodeFile); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 139 | |
| 140 | // Return the filename we captured the output to. |
| 141 | return OutputFile; |
| 142 | } |
| 143 | |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 144 | |
Chris Lattner | f8a84db | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 145 | std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) { |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 146 | assert(Interpreter && "Interpreter should have been created already!"); |
Chris Lattner | f8a84db | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 147 | std::string OutputCFile; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 148 | |
| 149 | // Using CBE |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 150 | cbe->OutputC(BytecodeFile, OutputCFile); |
| 151 | |
| 152 | #if 0 /* This is an alternative, as yet unimplemented */ |
| 153 | // Using LLC |
Chris Lattner | f8a84db | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 154 | std::string Message; |
Misha Brukman | 5bc6a8f | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 155 | LLC *llc = createLLCtool(Message); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 156 | if (llc->OutputAsm(BytecodeFile, OutputFile)) { |
| 157 | std::cerr << "Could not generate asm code with `llc', exiting.\n"; |
| 158 | exit(1); |
| 159 | } |
| 160 | #endif |
| 161 | |
Chris Lattner | f8a84db | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 162 | std::string SharedObjectFile; |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame^] | 163 | if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile)) |
Chris Lattner | f8a84db | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 164 | exit(1); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 165 | |
| 166 | // Remove the intermediate C file |
| 167 | removeFile(OutputCFile); |
| 168 | |
Chris Lattner | f8a84db | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 169 | return SharedObjectFile; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 173 | /// diffProgram - This method executes the specified module and diffs the output |
| 174 | /// against the file specified by ReferenceOutputFile. If the output is |
| 175 | /// different, true is returned. |
| 176 | /// |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 177 | bool BugDriver::diffProgram(const std::string &BytecodeFile, |
| 178 | const std::string &SharedObject, |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 179 | bool RemoveBytecode) { |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 180 | // Execute the program, generating an output file... |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 181 | std::string Output = executeProgram("", BytecodeFile, SharedObject); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 182 | |
Chris Lattner | aa997fb | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 183 | std::string Error; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 184 | bool FilesDifferent = false; |
Chris Lattner | aa997fb | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 185 | if (DiffFiles(ReferenceOutputFile, Output, &Error)) { |
| 186 | if (!Error.empty()) { |
| 187 | std::cerr << "While diffing output: " << Error << "\n"; |
| 188 | exit(1); |
| 189 | } |
| 190 | FilesDifferent = true; |
| 191 | } |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 193 | if (RemoveBytecode) removeFile(BytecodeFile); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 194 | return FilesDifferent; |
| 195 | } |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 196 | |
| 197 | bool BugDriver::isExecutingJIT() { |
| 198 | return InterpreterSel == RunJIT; |
| 199 | } |