blob: 49480efb63663c91324f7d0ae6991930c235a8c9 [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>
58 CheckProgramExitCode("check-exit-code",
Misha Brukmaneed80e22004-07-23 01:30:49 +000059 cl::desc("Assume nonzero exit code is failure (default on)"),
Brian Gaekec5cad212004-02-11 18:37:32 +000060 cl::init(true));
61
Reid Spencer5e1452c2006-11-28 07:04:10 +000062 cl::opt<bool>
63 AppendProgramExitCode("append-exit-code",
64 cl::desc("Append the exit code to the output so it gets diff'd too"),
65 cl::init(false));
66
Chris Lattner3c053a02003-04-23 20:31:37 +000067 cl::opt<std::string>
68 InputFile("input", cl::init("/dev/null"),
69 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000070
71 cl::list<std::string>
72 AdditionalSOs("additional-so",
73 cl::desc("Additional shared objects to load "
74 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000075
Reid Spencer51ab5c82006-06-06 00:00:42 +000076 cl::list<std::string>
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000077 AdditionalLinkerArgs("Xlinker",
Reid Spencer51ab5c82006-06-06 00:00:42 +000078 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000079
80 cl::opt<std::string>
81 CustomExecCommand("exec-command", cl::init("simulate"),
82 cl::desc("Command to execute the bitcode (use with -run-custom) "
83 "(default: simulate)"));
Chris Lattner4a106452002-12-23 23:50:16 +000084}
85
Brian Gaeked0fde302003-11-11 22:41:34 +000086namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +000087 // Anything specified after the --args option are taken as arguments to the
88 // program being debugged.
89 cl::list<std::string>
90 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000091 cl::ZeroOrMore, cl::PositionalEatsArgs);
Brian Gaeke636df3d2004-05-04 21:09:16 +000092
93 cl::list<std::string>
94 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000095 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +000096}
Misha Brukman9d679cb2003-07-30 20:15:44 +000097
Chris Lattner4a106452002-12-23 23:50:16 +000098//===----------------------------------------------------------------------===//
99// BugDriver method implementation
100//
101
102/// initializeExecutionEnvironment - This method is used to set up the
103/// environment for executing LLVM programs.
104///
105bool BugDriver::initializeExecutionEnvironment() {
106 std::cout << "Initializing execution environment: ";
107
Misha Brukman41485562003-09-29 22:40:52 +0000108 // Create an instance of the AbstractInterpreter interface as specified on
109 // the command line
Chris Lattner7bb11542004-02-18 20:52:02 +0000110 cbe = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000111 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000112
Chris Lattnercc876a72003-05-03 03:19:41 +0000113 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000114 case AutoPick:
115 InterpreterSel = RunCBE;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000116 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
117 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000118 if (!Interpreter) {
119 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000120 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
121 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000122 }
123 if (!Interpreter) {
124 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000125 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
126 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000127 }
128 if (!Interpreter) {
129 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000130 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
131 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000132 }
133 if (!Interpreter) {
134 InterpreterSel = AutoPick;
135 Message = "Sorry, I can't automatically select an interpreter!\n";
136 }
137 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000138 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000139 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
140 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000141 break;
142 case RunLLC:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000143 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
144 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000145 break;
146 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000147 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
148 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000149 break;
Chris Lattnercd6f46e2006-11-09 05:57:53 +0000150 case LLC_Safe:
151 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
152 &ToolArgv);
153 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000154 case RunCBE:
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000155 case CBE_bug:
156 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
157 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000158 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000159 case Custom:
160 Interpreter = AbstractInterpreter::createCustom(getToolName(), Message,
161 CustomExecCommand);
162 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000163 default:
Misha Brukman41485562003-09-29 22:40:52 +0000164 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000165 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000166 }
Misha Brukman41485562003-09-29 22:40:52 +0000167 std::cerr << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000168
Misha Brukmana259c9b2003-07-24 21:59:10 +0000169 // Initialize auxiliary tools for debugging
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000170 if (InterpreterSel == RunCBE) {
171 // We already created a CBE, reuse it.
172 cbe = Interpreter;
Chris Lattnercd6f46e2006-11-09 05:57:53 +0000173 } else if (InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe) {
174 // We want to debug the CBE itself or LLC is known-good. Use LLC as the
175 // 'known-good' compiler.
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000176 std::vector<std::string> ToolArgs;
177 ToolArgs.push_back("--relocation-model=pic");
178 cbe = AbstractInterpreter::createLLC(getToolName(), Message, &ToolArgs);
179 } else {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000180 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Chris Lattner7bb11542004-02-18 20:52:02 +0000181 }
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000182 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
183
Chris Lattner769f1fe2003-10-14 21:59:36 +0000184 gcc = GCC::create(getToolName(), Message);
Misha Brukmana259c9b2003-07-24 21:59:10 +0000185 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
186
Chris Lattner4a106452002-12-23 23:50:16 +0000187 // If there was an error creating the selected interpreter, quit with error.
188 return Interpreter == 0;
189}
190
Chris Lattnerea9212c2004-02-18 23:25:22 +0000191/// compileProgram - Try to compile the specified module, throwing an exception
192/// if an error occurs, or returning normally if not. This is used for code
193/// generation crash testing.
194///
195void BugDriver::compileProgram(Module *M) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000196 // Emit the program to a bitcode file...
197 sys::Path BitcodeFile ("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000198 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000199 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
Reid Spencer51c5a282006-08-23 20:34:57 +0000200 std::cerr << ToolName << ": Error making unique filename: " << ErrMsg
201 << "\n";
202 exit(1);
203 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000204 if (writeProgramToFile(BitcodeFile.toString(), M)) {
205 std::cerr << ToolName << ": Error emitting bitcode to file '"
206 << BitcodeFile << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000207 exit(1);
208 }
209
Gabor Greif8ff70c22007-07-04 21:55:50 +0000210 // Remove the temporary bitcode file when we are done.
211 FileRemover BitcodeFileRemover(BitcodeFile);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000212
213 // Actually compile the program!
Gabor Greif8ff70c22007-07-04 21:55:50 +0000214 Interpreter->compileProgram(BitcodeFile.toString());
Chris Lattnerea9212c2004-02-18 23:25:22 +0000215}
216
Chris Lattner4a106452002-12-23 23:50:16 +0000217
218/// executeProgram - This method runs "Program", capturing the output of the
219/// program to a file, returning the filename of the file. A recommended
220/// filename may be optionally specified.
221///
222std::string BugDriver::executeProgram(std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000223 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000224 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000225 AbstractInterpreter *AI,
226 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000227 if (AI == 0) AI = Interpreter;
228 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000229 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000230 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000231 if (BitcodeFile.empty()) {
232 // Emit the program to a bitcode file...
Reid Spencer97182982004-12-15 01:53:08 +0000233 sys::Path uniqueFilename("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000234 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
235 std::cerr << ToolName << ": Error making unique filename: "
236 << ErrMsg << "!\n";
237 exit(1);
238 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000239 BitcodeFile = uniqueFilename.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000240
Gabor Greif8ff70c22007-07-04 21:55:50 +0000241 if (writeProgramToFile(BitcodeFile, Program)) {
242 std::cerr << ToolName << ": Error emitting bitcode to file '"
243 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000244 exit(1);
245 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000246 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000247 }
248
Gabor Greif8ff70c22007-07-04 21:55:50 +0000249 // Remove the temporary bitcode file when we are done.
250 sys::Path BitcodePath (BitcodeFile);
251 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode);
Chris Lattner97092722004-02-18 22:01:21 +0000252
Chris Lattner4a106452002-12-23 23:50:16 +0000253 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000254
Chris Lattner4a106452002-12-23 23:50:16 +0000255 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000256 sys::Path uniqueFile(OutputFile);
Reid Spencer51c5a282006-08-23 20:34:57 +0000257 if (uniqueFile.makeUnique(true, &ErrMsg)) {
258 std::cerr << ToolName << ": Error making unique filename: "
259 << ErrMsg << "\n";
260 exit(1);
261 }
Reid Spencer97182982004-12-15 01:53:08 +0000262 OutputFile = uniqueFile.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000263
Chris Lattner769f1fe2003-10-14 21:59:36 +0000264 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000265 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000266 if (!SharedObj.empty())
267 SharedObjs.push_back(SharedObj);
268
Reid Spencer51ab5c82006-06-06 00:00:42 +0000269
270 // If this is an LLC or CBE run, then the GCC compiler might get run to
271 // compile the program. If so, we should pass the user's -Xlinker options
272 // as the GCCArgs.
273 int RetVal = 0;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000274 if (InterpreterSel == RunLLC || InterpreterSel == RunCBE ||
Lauro Ramos Venancio497391a2007-06-06 23:10:56 +0000275 InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe)
Gabor Greif8ff70c22007-07-04 21:55:50 +0000276 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000277 OutputFile, AdditionalLinkerArgs, SharedObjs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000278 Timeout, MemoryLimit);
Reid Spencer51ab5c82006-06-06 00:00:42 +0000279 else
Gabor Greif8ff70c22007-07-04 21:55:50 +0000280 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000281 OutputFile, std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000282 SharedObjs, Timeout, MemoryLimit);
Chris Lattner7d91e492004-07-24 07:53:26 +0000283
284 if (RetVal == -1) {
285 std::cerr << "<timeout>";
286 static bool FirstTimeout = true;
287 if (FirstTimeout) {
288 std::cout << "\n"
289 "*** Program execution timed out! This mechanism is designed to handle\n"
290 " programs stuck in infinite loops gracefully. The -timeout option\n"
291 " can be used to change the timeout threshold or disable it completely\n"
292 " (with -timeout=0). This message is only displayed once.\n";
293 FirstTimeout = false;
294 }
295 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000296
Reid Spencer5e1452c2006-11-28 07:04:10 +0000297 if (AppendProgramExitCode) {
298 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
299 outFile << "exit " << RetVal << '\n';
300 outFile.close();
301 }
302
Brian Gaekec5cad212004-02-11 18:37:32 +0000303 if (ProgramExitedNonzero != 0)
304 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000305
Chris Lattner4a106452002-12-23 23:50:16 +0000306 // Return the filename we captured the output to.
307 return OutputFile;
308}
309
Brian Gaekec5cad212004-02-11 18:37:32 +0000310/// executeProgramWithCBE - Used to create reference output with the C
311/// backend, if reference output is not provided.
312///
313std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
314 bool ProgramExitedNonzero;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000315 std::string outFN = executeProgram(OutputFile, "", "", cbe,
Brian Gaekec5cad212004-02-11 18:37:32 +0000316 &ProgramExitedNonzero);
317 if (ProgramExitedNonzero) {
318 std::cerr
319 << "Warning: While generating reference output, program exited with\n"
320 << "non-zero exit code. This will NOT be treated as a failure.\n";
321 CheckProgramExitCode = false;
322 }
323 return outFN;
324}
Misha Brukman50733362003-07-24 18:17:43 +0000325
Gabor Greif8ff70c22007-07-04 21:55:50 +0000326std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000327 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000328 sys::Path OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000329
330 // Using CBE
Gabor Greif8ff70c22007-07-04 21:55:50 +0000331 GCC::FileType FT = cbe->OutputCode(BitcodeFile, OutputFile);
Misha Brukman50733362003-07-24 18:17:43 +0000332
Chris Lattnera0f5b152003-10-14 21:09:11 +0000333 std::string SharedObjectFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000334 if (gcc->MakeSharedObject(OutputFile.toString(), FT,
Chris Lattner130e2a32006-06-27 20:35:36 +0000335 SharedObjectFile, AdditionalLinkerArgs))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000336 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000337
338 // Remove the intermediate C file
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000339 OutputFile.eraseFromDisk();
Misha Brukman50733362003-07-24 18:17:43 +0000340
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000341 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000342}
343
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000344/// createReferenceFile - calls compileProgram and then records the output
345/// into ReferenceOutputFile. Returns true if reference file created, false
346/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
347/// this function.
348///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000349bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000350 try {
351 compileProgram(Program);
Jeff Cohend41b30d2006-11-05 19:31:28 +0000352 } catch (ToolExecutionError &) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000353 return false;
354 }
355 try {
356 ReferenceOutputFile = executeProgramWithCBE(Filename);
357 std::cout << "Reference output is: " << ReferenceOutputFile << "\n\n";
358 } catch (ToolExecutionError &TEE) {
359 std::cerr << TEE.what();
360 if (Interpreter != cbe) {
361 std::cerr << "*** There is a bug running the C backend. Either debug"
362 << " it (use the -run-cbe bugpoint option), or fix the error"
363 << " some other way.\n";
364 }
365 return false;
366 }
367 return true;
368}
Misha Brukman50733362003-07-24 18:17:43 +0000369
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000370/// diffProgram - This method executes the specified module and diffs the
371/// output against the file specified by ReferenceOutputFile. If the output
372/// is different, true is returned. If there is a problem with the code
373/// generator (e.g., llc crashes), this will throw an exception.
Chris Lattner4a106452002-12-23 23:50:16 +0000374///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000375bool BugDriver::diffProgram(const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000376 const std::string &SharedObject,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000377 bool RemoveBitcode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000378 bool ProgramExitedNonzero;
379
Chris Lattner4a106452002-12-23 23:50:16 +0000380 // Execute the program, generating an output file...
Gabor Greif8ff70c22007-07-04 21:55:50 +0000381 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0,
Reid Spencer5f767602004-12-16 23:04:20 +0000382 &ProgramExitedNonzero));
Brian Gaekec5cad212004-02-11 18:37:32 +0000383
384 // If we're checking the program exit code, assume anything nonzero is bad.
Chris Lattner58d84ce2004-04-02 05:33:06 +0000385 if (CheckProgramExitCode && ProgramExitedNonzero) {
Reid Spencera229c5c2005-07-08 03:08:58 +0000386 Output.eraseFromDisk();
Gabor Greif8ff70c22007-07-04 21:55:50 +0000387 if (RemoveBitcode)
388 sys::Path(BitcodeFile).eraseFromDisk();
Brian Gaekec5cad212004-02-11 18:37:32 +0000389 return true;
Chris Lattner58d84ce2004-04-02 05:33:06 +0000390 }
Chris Lattner4a106452002-12-23 23:50:16 +0000391
Chris Lattner65f62792003-08-01 20:29:45 +0000392 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000393 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000394 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
395 sys::Path(Output.toString()),
396 AbsTolerance, RelTolerance, &Error)) {
397 if (Diff == 2) {
Misha Brukmaneed80e22004-07-23 01:30:49 +0000398 std::cerr << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000399 exit(1);
400 }
401 FilesDifferent = true;
402 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000403
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000404 // Remove the generated output.
Reid Spencera229c5c2005-07-08 03:08:58 +0000405 Output.eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000406
Gabor Greif8ff70c22007-07-04 21:55:50 +0000407 // Remove the bitcode file if we are supposed to.
408 if (RemoveBitcode)
409 sys::Path(BitcodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000410 return FilesDifferent;
411}
Misha Brukman91eabc12003-07-28 19:16:14 +0000412
413bool BugDriver::isExecutingJIT() {
414 return InterpreterSel == RunJIT;
415}
Brian Gaeked0fde302003-11-11 22:41:34 +0000416