Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 15 | #include "BugDriver.h" |
Chris Lattner | f1b20d8 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 16 | #include "ToolRunner.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
| 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/FileUtilities.h" |
| 20 | #include "llvm/Support/SystemUtils.h" |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 21 | #include <fstream> |
Chris Lattner | e31a9cc | 2006-01-22 22:53:40 +0000 | [diff] [blame] | 22 | #include <iostream> |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 23 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | // OutputType - Allow the user to specify the way code should be run, to test |
| 28 | // for miscompilation. |
| 29 | // |
| 30 | enum OutputType { |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 31 | AutoPick, RunLLI, RunJIT, RunLLC, RunCBE |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 32 | }; |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 33 | |
Chris Lattner | a328c51 | 2005-01-23 03:45:26 +0000 | [diff] [blame] | 34 | cl::opt<double> |
| 35 | AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"), |
| 36 | cl::init(0.0)); |
| 37 | cl::opt<double> |
| 38 | RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"), |
| 39 | cl::init(0.0)); |
| 40 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 41 | cl::opt<OutputType> |
| 42 | InterpreterSel(cl::desc("Specify how LLVM code should be executed:"), |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 43 | cl::values(clEnumValN(AutoPick, "auto", "Use best guess"), |
Misha Brukman | b687d82 | 2004-04-19 03:12:35 +0000 | [diff] [blame] | 44 | clEnumValN(RunLLI, "run-int", |
| 45 | "Execute with the interpreter"), |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 46 | clEnumValN(RunJIT, "run-jit", "Execute with JIT"), |
| 47 | clEnumValN(RunLLC, "run-llc", "Compile with LLC"), |
| 48 | clEnumValN(RunCBE, "run-cbe", "Compile with CBE"), |
Chris Lattner | 4d143ee | 2004-07-16 00:08:28 +0000 | [diff] [blame] | 49 | clEnumValEnd), |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 50 | cl::init(AutoPick)); |
Chris Lattner | 3c053a0 | 2003-04-23 20:31:37 +0000 | [diff] [blame] | 51 | |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 52 | cl::opt<bool> |
| 53 | CheckProgramExitCode("check-exit-code", |
Misha Brukman | eed80e2 | 2004-07-23 01:30:49 +0000 | [diff] [blame] | 54 | cl::desc("Assume nonzero exit code is failure (default on)"), |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 55 | cl::init(true)); |
| 56 | |
Chris Lattner | 3c053a0 | 2003-04-23 20:31:37 +0000 | [diff] [blame] | 57 | cl::opt<std::string> |
| 58 | InputFile("input", cl::init("/dev/null"), |
| 59 | cl::desc("Filename to pipe in as stdin (default: /dev/null)")); |
Chris Lattner | 7dac658 | 2003-10-14 22:24:31 +0000 | [diff] [blame] | 60 | |
| 61 | cl::list<std::string> |
| 62 | AdditionalSOs("additional-so", |
| 63 | cl::desc("Additional shared objects to load " |
| 64 | "into executing programs")); |
Chris Lattner | 7d91e49 | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 65 | |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 66 | cl::list<std::string> |
| 67 | AdditionalLinkerArgs("Xlinker", |
| 68 | cl::desc("Additional arguments to pass to the linker")); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 71 | namespace llvm { |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 72 | // Anything specified after the --args option are taken as arguments to the |
| 73 | // program being debugged. |
| 74 | cl::list<std::string> |
| 75 | InputArgv("args", cl::Positional, cl::desc("<program arguments>..."), |
Chris Lattner | 60083e2 | 2004-05-06 22:05:35 +0000 | [diff] [blame] | 76 | cl::ZeroOrMore, cl::PositionalEatsArgs); |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 77 | |
| 78 | cl::list<std::string> |
| 79 | ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."), |
Chris Lattner | 60083e2 | 2004-05-06 22:05:35 +0000 | [diff] [blame] | 80 | cl::ZeroOrMore, cl::PositionalEatsArgs); |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 81 | } |
Misha Brukman | 9d679cb | 2003-07-30 20:15:44 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 83 | //===----------------------------------------------------------------------===// |
| 84 | // BugDriver method implementation |
| 85 | // |
| 86 | |
| 87 | /// initializeExecutionEnvironment - This method is used to set up the |
| 88 | /// environment for executing LLVM programs. |
| 89 | /// |
| 90 | bool BugDriver::initializeExecutionEnvironment() { |
| 91 | std::cout << "Initializing execution environment: "; |
| 92 | |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 93 | // Create an instance of the AbstractInterpreter interface as specified on |
| 94 | // the command line |
Chris Lattner | 7bb1154 | 2004-02-18 20:52:02 +0000 | [diff] [blame] | 95 | cbe = 0; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 96 | std::string Message; |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 97 | |
Chris Lattner | cc876a7 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 98 | switch (InterpreterSel) { |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 99 | case AutoPick: |
| 100 | InterpreterSel = RunCBE; |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 101 | Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message, |
| 102 | &ToolArgv); |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 103 | if (!Interpreter) { |
| 104 | InterpreterSel = RunJIT; |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 105 | Interpreter = AbstractInterpreter::createJIT(getToolName(), Message, |
| 106 | &ToolArgv); |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 107 | } |
| 108 | if (!Interpreter) { |
| 109 | InterpreterSel = RunLLC; |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 110 | Interpreter = AbstractInterpreter::createLLC(getToolName(), Message, |
| 111 | &ToolArgv); |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 112 | } |
| 113 | if (!Interpreter) { |
| 114 | InterpreterSel = RunLLI; |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 115 | Interpreter = AbstractInterpreter::createLLI(getToolName(), Message, |
| 116 | &ToolArgv); |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 117 | } |
| 118 | if (!Interpreter) { |
| 119 | InterpreterSel = AutoPick; |
| 120 | Message = "Sorry, I can't automatically select an interpreter!\n"; |
| 121 | } |
| 122 | break; |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 123 | case RunLLI: |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 124 | Interpreter = AbstractInterpreter::createLLI(getToolName(), Message, |
| 125 | &ToolArgv); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 126 | break; |
| 127 | case RunLLC: |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 128 | Interpreter = AbstractInterpreter::createLLC(getToolName(), Message, |
| 129 | &ToolArgv); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 130 | break; |
| 131 | case RunJIT: |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 132 | Interpreter = AbstractInterpreter::createJIT(getToolName(), Message, |
| 133 | &ToolArgv); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 134 | break; |
| 135 | case RunCBE: |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 136 | Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message, |
| 137 | &ToolArgv); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 138 | break; |
Chris Lattner | cc876a7 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 139 | default: |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 140 | 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] | 141 | break; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 142 | } |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 143 | std::cerr << Message; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 144 | |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 145 | // Initialize auxiliary tools for debugging |
Chris Lattner | 7bb1154 | 2004-02-18 20:52:02 +0000 | [diff] [blame] | 146 | if (!cbe) { |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 147 | cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv); |
Chris Lattner | 7bb1154 | 2004-02-18 20:52:02 +0000 | [diff] [blame] | 148 | if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); } |
| 149 | } |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 150 | gcc = GCC::create(getToolName(), Message); |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 151 | if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); } |
| 152 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 153 | // If there was an error creating the selected interpreter, quit with error. |
| 154 | return Interpreter == 0; |
| 155 | } |
| 156 | |
Chris Lattner | ea9212c | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 157 | /// compileProgram - Try to compile the specified module, throwing an exception |
| 158 | /// if an error occurs, or returning normally if not. This is used for code |
| 159 | /// generation crash testing. |
| 160 | /// |
| 161 | void BugDriver::compileProgram(Module *M) { |
| 162 | // Emit the program to a bytecode file... |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 163 | sys::Path BytecodeFile ("bugpoint-test-program.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame^] | 164 | std::string ErrMsg; |
| 165 | if (BytecodeFile.makeUnique(true,&ErrMsg)) { |
| 166 | std::cerr << ToolName << ": Error making unique filename: " << ErrMsg |
| 167 | << "\n"; |
| 168 | exit(1); |
| 169 | } |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 170 | if (writeProgramToFile(BytecodeFile.toString(), M)) { |
Chris Lattner | ea9212c | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 171 | std::cerr << ToolName << ": Error emitting bytecode to file '" |
| 172 | << BytecodeFile << "'!\n"; |
| 173 | exit(1); |
| 174 | } |
| 175 | |
| 176 | // Remove the temporary bytecode file when we are done. |
Reid Spencer | 5f76760 | 2004-12-16 23:04:20 +0000 | [diff] [blame] | 177 | FileRemover BytecodeFileRemover(BytecodeFile); |
Chris Lattner | ea9212c | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 178 | |
| 179 | // Actually compile the program! |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 180 | Interpreter->compileProgram(BytecodeFile.toString()); |
Chris Lattner | ea9212c | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 183 | |
| 184 | /// executeProgram - This method runs "Program", capturing the output of the |
| 185 | /// program to a file, returning the filename of the file. A recommended |
| 186 | /// filename may be optionally specified. |
| 187 | /// |
| 188 | std::string BugDriver::executeProgram(std::string OutputFile, |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 189 | std::string BytecodeFile, |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 190 | const std::string &SharedObj, |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 191 | AbstractInterpreter *AI, |
| 192 | bool *ProgramExitedNonzero) { |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 193 | if (AI == 0) AI = Interpreter; |
| 194 | assert(AI && "Interpreter should have been created already!"); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 195 | bool CreatedBytecode = false; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame^] | 196 | std::string ErrMsg; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 197 | if (BytecodeFile.empty()) { |
| 198 | // Emit the program to a bytecode file... |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 199 | sys::Path uniqueFilename("bugpoint-test-program.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame^] | 200 | if (uniqueFilename.makeUnique(true, &ErrMsg)) { |
| 201 | std::cerr << ToolName << ": Error making unique filename: " |
| 202 | << ErrMsg << "!\n"; |
| 203 | exit(1); |
| 204 | } |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 205 | BytecodeFile = uniqueFilename.toString(); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 206 | |
| 207 | if (writeProgramToFile(BytecodeFile, Program)) { |
| 208 | std::cerr << ToolName << ": Error emitting bytecode to file '" |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 209 | << BytecodeFile << "'!\n"; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 210 | exit(1); |
| 211 | } |
| 212 | CreatedBytecode = true; |
| 213 | } |
| 214 | |
Chris Lattner | 9709272 | 2004-02-18 22:01:21 +0000 | [diff] [blame] | 215 | // Remove the temporary bytecode file when we are done. |
Brian Gaeke | 06c375b | 2004-12-22 22:33:33 +0000 | [diff] [blame] | 216 | sys::Path BytecodePath (BytecodeFile); |
| 217 | FileRemover BytecodeFileRemover(BytecodePath, CreatedBytecode); |
Chris Lattner | 9709272 | 2004-02-18 22:01:21 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 219 | if (OutputFile.empty()) OutputFile = "bugpoint-execution-output"; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 221 | // Check to see if this is a valid output filename... |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 222 | sys::Path uniqueFile(OutputFile); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame^] | 223 | if (uniqueFile.makeUnique(true, &ErrMsg)) { |
| 224 | std::cerr << ToolName << ": Error making unique filename: " |
| 225 | << ErrMsg << "\n"; |
| 226 | exit(1); |
| 227 | } |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 228 | OutputFile = uniqueFile.toString(); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 230 | // Figure out which shared objects to run, if any. |
Chris Lattner | 7dac658 | 2003-10-14 22:24:31 +0000 | [diff] [blame] | 231 | std::vector<std::string> SharedObjs(AdditionalSOs); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 232 | if (!SharedObj.empty()) |
| 233 | SharedObjs.push_back(SharedObj); |
| 234 | |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 235 | |
| 236 | // If this is an LLC or CBE run, then the GCC compiler might get run to |
| 237 | // compile the program. If so, we should pass the user's -Xlinker options |
| 238 | // as the GCCArgs. |
| 239 | int RetVal = 0; |
| 240 | if (InterpreterSel == RunLLC || InterpreterSel == RunCBE) |
| 241 | RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile, |
| 242 | OutputFile, AdditionalLinkerArgs, SharedObjs, |
Chris Lattner | 9686ae7 | 2006-06-13 03:10:48 +0000 | [diff] [blame] | 243 | Timeout); |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 244 | else |
| 245 | RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile, |
| 246 | OutputFile, std::vector<std::string>(), |
Chris Lattner | 9686ae7 | 2006-06-13 03:10:48 +0000 | [diff] [blame] | 247 | SharedObjs, Timeout); |
Chris Lattner | 7d91e49 | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 248 | |
| 249 | if (RetVal == -1) { |
| 250 | std::cerr << "<timeout>"; |
| 251 | static bool FirstTimeout = true; |
| 252 | if (FirstTimeout) { |
| 253 | std::cout << "\n" |
| 254 | "*** Program execution timed out! This mechanism is designed to handle\n" |
| 255 | " programs stuck in infinite loops gracefully. The -timeout option\n" |
| 256 | " can be used to change the timeout threshold or disable it completely\n" |
| 257 | " (with -timeout=0). This message is only displayed once.\n"; |
| 258 | FirstTimeout = false; |
| 259 | } |
| 260 | } |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 261 | |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 262 | if (ProgramExitedNonzero != 0) |
| 263 | *ProgramExitedNonzero = (RetVal != 0); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 265 | // Return the filename we captured the output to. |
| 266 | return OutputFile; |
| 267 | } |
| 268 | |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 269 | /// executeProgramWithCBE - Used to create reference output with the C |
| 270 | /// backend, if reference output is not provided. |
| 271 | /// |
| 272 | std::string BugDriver::executeProgramWithCBE(std::string OutputFile) { |
| 273 | bool ProgramExitedNonzero; |
| 274 | std::string outFN = executeProgram(OutputFile, "", "", |
| 275 | (AbstractInterpreter*)cbe, |
| 276 | &ProgramExitedNonzero); |
| 277 | if (ProgramExitedNonzero) { |
| 278 | std::cerr |
| 279 | << "Warning: While generating reference output, program exited with\n" |
| 280 | << "non-zero exit code. This will NOT be treated as a failure.\n"; |
| 281 | CheckProgramExitCode = false; |
| 282 | } |
| 283 | return outFN; |
| 284 | } |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 285 | |
Chris Lattner | a0f5b15 | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 286 | std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) { |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 287 | assert(Interpreter && "Interpreter should have been created already!"); |
Reid Spencer | 5f76760 | 2004-12-16 23:04:20 +0000 | [diff] [blame] | 288 | sys::Path OutputCFile; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 289 | |
| 290 | // Using CBE |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 291 | cbe->OutputC(BytecodeFile, OutputCFile); |
| 292 | |
| 293 | #if 0 /* This is an alternative, as yet unimplemented */ |
| 294 | // Using LLC |
Chris Lattner | a0f5b15 | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 295 | std::string Message; |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 296 | LLC *llc = createLLCtool(Message); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 297 | if (llc->OutputAsm(BytecodeFile, OutputFile)) { |
| 298 | std::cerr << "Could not generate asm code with `llc', exiting.\n"; |
| 299 | exit(1); |
| 300 | } |
| 301 | #endif |
| 302 | |
Chris Lattner | a0f5b15 | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 303 | std::string SharedObjectFile; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 304 | if (gcc->MakeSharedObject(OutputCFile.toString(), GCC::CFile, |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 305 | SharedObjectFile, AdditionalLinkerArgs)) |
Chris Lattner | a0f5b15 | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 306 | exit(1); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 307 | |
| 308 | // Remove the intermediate C file |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 309 | OutputCFile.eraseFromDisk(); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 6ebe44d | 2003-10-19 21:54:13 +0000 | [diff] [blame] | 311 | return "./" + SharedObjectFile; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 314 | /// createReferenceFile - calls compileProgram and then records the output |
| 315 | /// into ReferenceOutputFile. Returns true if reference file created, false |
| 316 | /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE |
| 317 | /// this function. |
| 318 | /// |
| 319 | bool BugDriver::createReferenceFile(Module *M, const std::string &Filename){ |
| 320 | try { |
| 321 | compileProgram(Program); |
| 322 | } catch (ToolExecutionError &TEE) { |
| 323 | return false; |
| 324 | } |
| 325 | try { |
| 326 | ReferenceOutputFile = executeProgramWithCBE(Filename); |
| 327 | std::cout << "Reference output is: " << ReferenceOutputFile << "\n\n"; |
| 328 | } catch (ToolExecutionError &TEE) { |
| 329 | std::cerr << TEE.what(); |
| 330 | if (Interpreter != cbe) { |
| 331 | std::cerr << "*** There is a bug running the C backend. Either debug" |
| 332 | << " it (use the -run-cbe bugpoint option), or fix the error" |
| 333 | << " some other way.\n"; |
| 334 | } |
| 335 | return false; |
| 336 | } |
| 337 | return true; |
| 338 | } |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 339 | |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 340 | /// diffProgram - This method executes the specified module and diffs the |
| 341 | /// output against the file specified by ReferenceOutputFile. If the output |
| 342 | /// is different, true is returned. If there is a problem with the code |
| 343 | /// generator (e.g., llc crashes), this will throw an exception. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 344 | /// |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 345 | bool BugDriver::diffProgram(const std::string &BytecodeFile, |
| 346 | const std::string &SharedObject, |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 347 | bool RemoveBytecode) { |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 348 | bool ProgramExitedNonzero; |
| 349 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 350 | // Execute the program, generating an output file... |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 351 | sys::Path Output(executeProgram("", BytecodeFile, SharedObject, 0, |
Reid Spencer | 5f76760 | 2004-12-16 23:04:20 +0000 | [diff] [blame] | 352 | &ProgramExitedNonzero)); |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 353 | |
| 354 | // If we're checking the program exit code, assume anything nonzero is bad. |
Chris Lattner | 58d84ce | 2004-04-02 05:33:06 +0000 | [diff] [blame] | 355 | if (CheckProgramExitCode && ProgramExitedNonzero) { |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 356 | Output.eraseFromDisk(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 357 | if (RemoveBytecode) |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 358 | sys::Path(BytecodeFile).eraseFromDisk(); |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 359 | return true; |
Chris Lattner | 58d84ce | 2004-04-02 05:33:06 +0000 | [diff] [blame] | 360 | } |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 65f6279 | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 362 | std::string Error; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 363 | bool FilesDifferent = false; |
Chris Lattner | a328c51 | 2005-01-23 03:45:26 +0000 | [diff] [blame] | 364 | if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile), |
| 365 | sys::Path(Output.toString()), |
| 366 | AbsTolerance, RelTolerance, &Error)) { |
| 367 | if (Diff == 2) { |
Misha Brukman | eed80e2 | 2004-07-23 01:30:49 +0000 | [diff] [blame] | 368 | std::cerr << "While diffing output: " << Error << '\n'; |
Chris Lattner | 65f6279 | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 369 | exit(1); |
| 370 | } |
| 371 | FilesDifferent = true; |
| 372 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 1a28a2b | 2003-10-18 21:02:51 +0000 | [diff] [blame] | 374 | // Remove the generated output. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 375 | Output.eraseFromDisk(); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 1a28a2b | 2003-10-18 21:02:51 +0000 | [diff] [blame] | 377 | // Remove the bytecode file if we are supposed to. |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 378 | if (RemoveBytecode) |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 379 | sys::Path(BytecodeFile).eraseFromDisk(); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 380 | return FilesDifferent; |
| 381 | } |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 382 | |
| 383 | bool BugDriver::isExecutingJIT() { |
| 384 | return InterpreterSel == RunJIT; |
| 385 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 386 | |