blob: 821b842689e66dfaa5c6fce091129ed3a8210576 [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-12-23 23:50:16 +00009//
10// This file contains code used to execute the program utilizing one of the
Gabor Greif8ff70c22007-07-04 21:55:50 +000011// various ways of running LLVM bitcode.
Chris Lattner4a106452002-12-23 23:50:16 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4a106452002-12-23 23:50:16 +000015#include "BugDriver.h"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000016#include "ToolRunner.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
20#include "llvm/Support/SystemUtils.h"
Chris Lattner4a106452002-12-23 23:50:16 +000021#include <fstream>
Chris Lattnere31a9cc2006-01-22 22:53:40 +000022#include <iostream>
Reid Spencer51ab5c82006-06-06 00:00:42 +000023
Brian Gaeked0fde302003-11-11 22:41:34 +000024using namespace llvm;
25
Chris Lattner4a106452002-12-23 23:50:16 +000026namespace {
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
29 //
30 enum OutputType {
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000031 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE, CBE_bug, LLC_Safe, Custom
Chris Lattner4a106452002-12-23 23:50:16 +000032 };
Misha Brukman41485562003-09-29 22:40:52 +000033
Chris Lattnera328c512005-01-23 03:45:26 +000034 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 Lattner4a106452002-12-23 23:50:16 +000041 cl::opt<OutputType>
42 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000043 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukmanb687d822004-04-19 03:12:35 +000044 clEnumValN(RunLLI, "run-int",
45 "Execute with the interpreter"),
Misha Brukman50733362003-07-24 18:17:43 +000046 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
47 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
48 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattnerc600f3c2006-09-15 21:29:15 +000049 clEnumValN(CBE_bug,"cbe-bug", "Find CBE bugs"),
Chris Lattnercd6f46e2006-11-09 05:57:53 +000050 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000051 clEnumValN(Custom, "run-custom",
52 "Use -exec-command to define a command to execute "
53 "the bitcode. Useful for cross-compilation."),
Chris Lattner4d143ee2004-07-16 00:08:28 +000054 clEnumValEnd),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000055 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000056
Brian Gaekec5cad212004-02-11 18:37:32 +000057 cl::opt<bool>
Reid Spencer5e1452c2006-11-28 07:04:10 +000058 AppendProgramExitCode("append-exit-code",
59 cl::desc("Append the exit code to the output so it gets diff'd too"),
60 cl::init(false));
61
Chris Lattner3c053a02003-04-23 20:31:37 +000062 cl::opt<std::string>
63 InputFile("input", cl::init("/dev/null"),
64 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000065
66 cl::list<std::string>
67 AdditionalSOs("additional-so",
68 cl::desc("Additional shared objects to load "
69 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000070
Reid Spencer51ab5c82006-06-06 00:00:42 +000071 cl::list<std::string>
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000072 AdditionalLinkerArgs("Xlinker",
Reid Spencer51ab5c82006-06-06 00:00:42 +000073 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000074
75 cl::opt<std::string>
76 CustomExecCommand("exec-command", cl::init("simulate"),
77 cl::desc("Command to execute the bitcode (use with -run-custom) "
78 "(default: simulate)"));
Chris Lattner4a106452002-12-23 23:50:16 +000079}
80
Brian Gaeked0fde302003-11-11 22:41:34 +000081namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +000082 // Anything specified after the --args option are taken as arguments to the
83 // program being debugged.
84 cl::list<std::string>
85 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000086 cl::ZeroOrMore, cl::PositionalEatsArgs);
Brian Gaeke636df3d2004-05-04 21:09:16 +000087
88 cl::list<std::string>
89 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000090 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +000091}
Misha Brukman9d679cb2003-07-30 20:15:44 +000092
Chris Lattner4a106452002-12-23 23:50:16 +000093//===----------------------------------------------------------------------===//
94// BugDriver method implementation
95//
96
97/// initializeExecutionEnvironment - This method is used to set up the
98/// environment for executing LLVM programs.
99///
100bool BugDriver::initializeExecutionEnvironment() {
101 std::cout << "Initializing execution environment: ";
102
Misha Brukman41485562003-09-29 22:40:52 +0000103 // Create an instance of the AbstractInterpreter interface as specified on
104 // the command line
Chris Lattner7bb11542004-02-18 20:52:02 +0000105 cbe = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000106 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000107
Chris Lattnercc876a72003-05-03 03:19:41 +0000108 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000109 case AutoPick:
110 InterpreterSel = RunCBE;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000111 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
112 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000113 if (!Interpreter) {
114 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000115 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
116 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000117 }
118 if (!Interpreter) {
119 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000120 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
121 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000122 }
123 if (!Interpreter) {
124 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000125 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
126 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000127 }
128 if (!Interpreter) {
129 InterpreterSel = AutoPick;
130 Message = "Sorry, I can't automatically select an interpreter!\n";
131 }
132 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000133 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000134 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
135 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000136 break;
137 case RunLLC:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000138 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
139 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000140 break;
141 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000142 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
143 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000144 break;
Chris Lattnercd6f46e2006-11-09 05:57:53 +0000145 case LLC_Safe:
146 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
147 &ToolArgv);
148 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000149 case RunCBE:
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000150 case CBE_bug:
151 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
152 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000153 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000154 case Custom:
155 Interpreter = AbstractInterpreter::createCustom(getToolName(), Message,
156 CustomExecCommand);
157 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000158 default:
Misha Brukman41485562003-09-29 22:40:52 +0000159 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000160 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000161 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000162 if (!Interpreter)
163 std::cerr << Message;
164 else // Display informational messages on stdout instead of stderr
165 std::cout << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000166
Misha Brukmana259c9b2003-07-24 21:59:10 +0000167 // Initialize auxiliary tools for debugging
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000168 if (InterpreterSel == RunCBE) {
169 // We already created a CBE, reuse it.
170 cbe = Interpreter;
Chris Lattnercd6f46e2006-11-09 05:57:53 +0000171 } else if (InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe) {
172 // We want to debug the CBE itself or LLC is known-good. Use LLC as the
173 // 'known-good' compiler.
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000174 std::vector<std::string> ToolArgs;
175 ToolArgs.push_back("--relocation-model=pic");
176 cbe = AbstractInterpreter::createLLC(getToolName(), Message, &ToolArgs);
177 } else {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000178 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Chris Lattner7bb11542004-02-18 20:52:02 +0000179 }
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000180 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
181
Chris Lattner769f1fe2003-10-14 21:59:36 +0000182 gcc = GCC::create(getToolName(), Message);
Misha Brukmana259c9b2003-07-24 21:59:10 +0000183 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
184
Chris Lattner4a106452002-12-23 23:50:16 +0000185 // If there was an error creating the selected interpreter, quit with error.
186 return Interpreter == 0;
187}
188
Chris Lattnerea9212c2004-02-18 23:25:22 +0000189/// compileProgram - Try to compile the specified module, throwing an exception
190/// if an error occurs, or returning normally if not. This is used for code
191/// generation crash testing.
192///
193void BugDriver::compileProgram(Module *M) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000194 // Emit the program to a bitcode file...
195 sys::Path BitcodeFile ("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000196 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000197 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
Reid Spencer51c5a282006-08-23 20:34:57 +0000198 std::cerr << ToolName << ": Error making unique filename: " << ErrMsg
199 << "\n";
200 exit(1);
201 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000202 if (writeProgramToFile(BitcodeFile.toString(), M)) {
203 std::cerr << ToolName << ": Error emitting bitcode to file '"
204 << BitcodeFile << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000205 exit(1);
206 }
207
Gabor Greif8ff70c22007-07-04 21:55:50 +0000208 // Remove the temporary bitcode file when we are done.
209 FileRemover BitcodeFileRemover(BitcodeFile);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000210
211 // Actually compile the program!
Gabor Greif8ff70c22007-07-04 21:55:50 +0000212 Interpreter->compileProgram(BitcodeFile.toString());
Chris Lattnerea9212c2004-02-18 23:25:22 +0000213}
214
Chris Lattner4a106452002-12-23 23:50:16 +0000215
216/// executeProgram - This method runs "Program", capturing the output of the
217/// program to a file, returning the filename of the file. A recommended
218/// filename may be optionally specified.
219///
220std::string BugDriver::executeProgram(std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000221 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000222 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000223 AbstractInterpreter *AI,
224 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000225 if (AI == 0) AI = Interpreter;
226 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000227 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000228 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000229 if (BitcodeFile.empty()) {
230 // Emit the program to a bitcode file...
Reid Spencer97182982004-12-15 01:53:08 +0000231 sys::Path uniqueFilename("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000232 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
233 std::cerr << ToolName << ": Error making unique filename: "
234 << ErrMsg << "!\n";
235 exit(1);
236 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000237 BitcodeFile = uniqueFilename.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000238
Gabor Greif8ff70c22007-07-04 21:55:50 +0000239 if (writeProgramToFile(BitcodeFile, Program)) {
240 std::cerr << ToolName << ": Error emitting bitcode to file '"
241 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000242 exit(1);
243 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000244 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000245 }
246
Gabor Greif8ff70c22007-07-04 21:55:50 +0000247 // Remove the temporary bitcode file when we are done.
248 sys::Path BitcodePath (BitcodeFile);
249 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode);
Chris Lattner97092722004-02-18 22:01:21 +0000250
Chris Lattner4a106452002-12-23 23:50:16 +0000251 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000252
Chris Lattner4a106452002-12-23 23:50:16 +0000253 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000254 sys::Path uniqueFile(OutputFile);
Reid Spencer51c5a282006-08-23 20:34:57 +0000255 if (uniqueFile.makeUnique(true, &ErrMsg)) {
256 std::cerr << ToolName << ": Error making unique filename: "
257 << ErrMsg << "\n";
258 exit(1);
259 }
Reid Spencer97182982004-12-15 01:53:08 +0000260 OutputFile = uniqueFile.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000261
Chris Lattner769f1fe2003-10-14 21:59:36 +0000262 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000263 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000264 if (!SharedObj.empty())
265 SharedObjs.push_back(SharedObj);
266
Reid Spencer51ab5c82006-06-06 00:00:42 +0000267
268 // If this is an LLC or CBE run, then the GCC compiler might get run to
269 // compile the program. If so, we should pass the user's -Xlinker options
270 // as the GCCArgs.
271 int RetVal = 0;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000272 if (InterpreterSel == RunLLC || InterpreterSel == RunCBE ||
Lauro Ramos Venancio497391a2007-06-06 23:10:56 +0000273 InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe)
Gabor Greif8ff70c22007-07-04 21:55:50 +0000274 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000275 OutputFile, AdditionalLinkerArgs, SharedObjs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000276 Timeout, MemoryLimit);
Reid Spencer51ab5c82006-06-06 00:00:42 +0000277 else
Gabor Greif8ff70c22007-07-04 21:55:50 +0000278 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000279 OutputFile, std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000280 SharedObjs, Timeout, MemoryLimit);
Chris Lattner7d91e492004-07-24 07:53:26 +0000281
282 if (RetVal == -1) {
283 std::cerr << "<timeout>";
284 static bool FirstTimeout = true;
285 if (FirstTimeout) {
286 std::cout << "\n"
287 "*** Program execution timed out! This mechanism is designed to handle\n"
288 " programs stuck in infinite loops gracefully. The -timeout option\n"
289 " can be used to change the timeout threshold or disable it completely\n"
290 " (with -timeout=0). This message is only displayed once.\n";
291 FirstTimeout = false;
292 }
293 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000294
Reid Spencer5e1452c2006-11-28 07:04:10 +0000295 if (AppendProgramExitCode) {
296 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
297 outFile << "exit " << RetVal << '\n';
298 outFile.close();
299 }
300
Brian Gaekec5cad212004-02-11 18:37:32 +0000301 if (ProgramExitedNonzero != 0)
302 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000303
Chris Lattner4a106452002-12-23 23:50:16 +0000304 // Return the filename we captured the output to.
305 return OutputFile;
306}
307
Brian Gaekec5cad212004-02-11 18:37:32 +0000308/// executeProgramWithCBE - Used to create reference output with the C
309/// backend, if reference output is not provided.
310///
311std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
312 bool ProgramExitedNonzero;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000313 std::string outFN = executeProgram(OutputFile, "", "", cbe,
Brian Gaekec5cad212004-02-11 18:37:32 +0000314 &ProgramExitedNonzero);
Brian Gaekec5cad212004-02-11 18:37:32 +0000315 return outFN;
316}
Misha Brukman50733362003-07-24 18:17:43 +0000317
Gabor Greif8ff70c22007-07-04 21:55:50 +0000318std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000319 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000320 sys::Path OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000321
322 // Using CBE
Gabor Greif8ff70c22007-07-04 21:55:50 +0000323 GCC::FileType FT = cbe->OutputCode(BitcodeFile, OutputFile);
Misha Brukman50733362003-07-24 18:17:43 +0000324
Chris Lattnera0f5b152003-10-14 21:09:11 +0000325 std::string SharedObjectFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000326 if (gcc->MakeSharedObject(OutputFile.toString(), FT,
Chris Lattner130e2a32006-06-27 20:35:36 +0000327 SharedObjectFile, AdditionalLinkerArgs))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000328 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000329
330 // Remove the intermediate C file
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000331 OutputFile.eraseFromDisk();
Misha Brukman50733362003-07-24 18:17:43 +0000332
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000333 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000334}
335
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000336/// createReferenceFile - calls compileProgram and then records the output
337/// into ReferenceOutputFile. Returns true if reference file created, false
338/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
339/// this function.
340///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000341bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000342 try {
343 compileProgram(Program);
Jeff Cohend41b30d2006-11-05 19:31:28 +0000344 } catch (ToolExecutionError &) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000345 return false;
346 }
347 try {
348 ReferenceOutputFile = executeProgramWithCBE(Filename);
349 std::cout << "Reference output is: " << ReferenceOutputFile << "\n\n";
350 } catch (ToolExecutionError &TEE) {
351 std::cerr << TEE.what();
352 if (Interpreter != cbe) {
353 std::cerr << "*** There is a bug running the C backend. Either debug"
354 << " it (use the -run-cbe bugpoint option), or fix the error"
355 << " some other way.\n";
356 }
357 return false;
358 }
359 return true;
360}
Misha Brukman50733362003-07-24 18:17:43 +0000361
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000362/// diffProgram - This method executes the specified module and diffs the
363/// output against the file specified by ReferenceOutputFile. If the output
364/// is different, true is returned. If there is a problem with the code
365/// generator (e.g., llc crashes), this will throw an exception.
Chris Lattner4a106452002-12-23 23:50:16 +0000366///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000367bool BugDriver::diffProgram(const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000368 const std::string &SharedObject,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000369 bool RemoveBitcode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000370 bool ProgramExitedNonzero;
371
Chris Lattner4a106452002-12-23 23:50:16 +0000372 // Execute the program, generating an output file...
Gabor Greif8ff70c22007-07-04 21:55:50 +0000373 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0,
Reid Spencer5f767602004-12-16 23:04:20 +0000374 &ProgramExitedNonzero));
Brian Gaekec5cad212004-02-11 18:37:32 +0000375
Chris Lattner65f62792003-08-01 20:29:45 +0000376 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000377 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000378 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
379 sys::Path(Output.toString()),
380 AbsTolerance, RelTolerance, &Error)) {
381 if (Diff == 2) {
Misha Brukmaneed80e22004-07-23 01:30:49 +0000382 std::cerr << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000383 exit(1);
384 }
385 FilesDifferent = true;
386 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000387
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000388 // Remove the generated output.
Reid Spencera229c5c2005-07-08 03:08:58 +0000389 Output.eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000390
Gabor Greif8ff70c22007-07-04 21:55:50 +0000391 // Remove the bitcode file if we are supposed to.
392 if (RemoveBitcode)
393 sys::Path(BitcodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000394 return FilesDifferent;
395}
Misha Brukman91eabc12003-07-28 19:16:14 +0000396
397bool BugDriver::isExecutingJIT() {
398 return InterpreterSel == RunJIT;
399}
Brian Gaeked0fde302003-11-11 22:41:34 +0000400