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" |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame^] | 18 | #include "Support/ToolRunner.h" |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 19 | #include "Support/CommandLine.h" |
Chris Lattner | c648dab | 2003-08-01 22:13:59 +0000 | [diff] [blame] | 20 | #include "Support/Debug.h" |
Chris Lattner | 65f6279 | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 21 | #include "Support/FileUtilities.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 22 | #include "Support/SystemUtils.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:"), |
Misha Brukman | 5073336 | 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 | 3c053a0 | 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 | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Misha Brukman | 9d679cb | 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 | 4a10645 | 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 | 4148556 | 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 | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 68 | std::string Message; |
Chris Lattner | cc876a7 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 69 | switch (InterpreterSel) { |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame^] | 70 | case RunLLI: Interpreter = createLLItool(getToolName(), Message); break; |
| 71 | case RunLLC: Interpreter = createLLCtool(getToolName(), Message); break; |
| 72 | case RunJIT: Interpreter = createJITtool(getToolName(), Message); break; |
| 73 | case RunCBE: Interpreter = createCBEtool(getToolName(), Message); break; |
Chris Lattner | cc876a7 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 74 | default: |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame^] | 75 | 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] | 76 | break; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 77 | } |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame^] | 78 | std::cerr << Message; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 79 | |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 80 | // Initialize auxiliary tools for debugging |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame^] | 81 | cbe = createCBEtool(getToolName(), Message); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 82 | if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); } |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame^] | 83 | gcc = createGCCtool(getToolName(), Message); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 84 | if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); } |
| 85 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 86 | // If there was an error creating the selected interpreter, quit with error. |
| 87 | return Interpreter == 0; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /// executeProgram - This method runs "Program", capturing the output of the |
| 92 | /// program to a file, returning the filename of the file. A recommended |
| 93 | /// filename may be optionally specified. |
| 94 | /// |
| 95 | std::string BugDriver::executeProgram(std::string OutputFile, |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 96 | std::string BytecodeFile, |
| 97 | std::string SharedObject, |
| 98 | AbstractInterpreter *AI) { |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame^] | 99 | assert((Interpreter||AI) && "Interpreter should have been created already!"); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 100 | bool CreatedBytecode = false; |
| 101 | if (BytecodeFile.empty()) { |
| 102 | // Emit the program to a bytecode file... |
| 103 | BytecodeFile = getUniqueFilename("bugpoint-test-program.bc"); |
| 104 | |
| 105 | if (writeProgramToFile(BytecodeFile, Program)) { |
| 106 | std::cerr << ToolName << ": Error emitting bytecode to file '" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 107 | << BytecodeFile << "'!\n"; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 108 | exit(1); |
| 109 | } |
| 110 | CreatedBytecode = true; |
| 111 | } |
| 112 | |
| 113 | if (OutputFile.empty()) OutputFile = "bugpoint-execution-output"; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 115 | // Check to see if this is a valid output filename... |
| 116 | OutputFile = getUniqueFilename(OutputFile); |
| 117 | |
| 118 | // Actually execute the program! |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 119 | int RetVal = (AI != 0) ? |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame^] | 120 | AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile, OutputFile, |
| 121 | SharedObject) : |
| 122 | Interpreter->ExecuteProgram(BytecodeFile, InputArgv, |
| 123 | InputFile, OutputFile, SharedObject); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 124 | |
| 125 | // Remove the temporary bytecode file. |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 126 | if (CreatedBytecode) removeFile(BytecodeFile); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 127 | |
| 128 | // Return the filename we captured the output to. |
| 129 | return OutputFile; |
| 130 | } |
| 131 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 132 | std::string BugDriver::executeProgramWithCBE(std::string OutputFile, |
| 133 | std::string BytecodeFile, |
| 134 | std::string SharedObject) { |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 135 | return executeProgram(OutputFile, BytecodeFile, SharedObject, cbe); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | int BugDriver::compileSharedObject(const std::string &BytecodeFile, |
| 139 | std::string &SharedObject) { |
| 140 | assert(Interpreter && "Interpreter should have been created already!"); |
| 141 | std::string Message, OutputCFile; |
| 142 | |
| 143 | // Using CBE |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 144 | cbe->OutputC(BytecodeFile, OutputCFile); |
| 145 | |
| 146 | #if 0 /* This is an alternative, as yet unimplemented */ |
| 147 | // Using LLC |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame^] | 148 | LLC *llc = createLLCtool(Message); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 149 | if (llc->OutputAsm(BytecodeFile, OutputFile)) { |
| 150 | std::cerr << "Could not generate asm code with `llc', exiting.\n"; |
| 151 | exit(1); |
| 152 | } |
| 153 | #endif |
| 154 | |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 155 | gcc->MakeSharedObject(OutputCFile, CFile, SharedObject); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 156 | |
| 157 | // Remove the intermediate C file |
| 158 | removeFile(OutputCFile); |
| 159 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 164 | /// diffProgram - This method executes the specified module and diffs the output |
| 165 | /// against the file specified by ReferenceOutputFile. If the output is |
| 166 | /// different, true is returned. |
| 167 | /// |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 168 | bool BugDriver::diffProgram(const std::string &BytecodeFile, |
| 169 | const std::string &SharedObject, |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 170 | bool RemoveBytecode) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 171 | // Execute the program, generating an output file... |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 172 | std::string Output = executeProgram("", BytecodeFile, SharedObject); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 65f6279 | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 174 | std::string Error; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 175 | bool FilesDifferent = false; |
Chris Lattner | 65f6279 | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 176 | if (DiffFiles(ReferenceOutputFile, Output, &Error)) { |
| 177 | if (!Error.empty()) { |
| 178 | std::cerr << "While diffing output: " << Error << "\n"; |
| 179 | exit(1); |
| 180 | } |
| 181 | FilesDifferent = true; |
| 182 | } |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 184 | if (RemoveBytecode) removeFile(BytecodeFile); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 185 | return FilesDifferent; |
| 186 | } |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 187 | |
| 188 | bool BugDriver::isExecutingJIT() { |
| 189 | return InterpreterSel == RunJIT; |
| 190 | } |