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