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