blob: 96341116fcc6118155a211b7a1cc2307495cf9cb [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 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000167 if (!Interpreter)
168 std::cerr << Message;
169 else // Display informational messages on stdout instead of stderr
170 std::cout << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000171
Misha Brukmana259c9b2003-07-24 21:59:10 +0000172 // Initialize auxiliary tools for debugging
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000173 if (InterpreterSel == RunCBE) {
174 // We already created a CBE, reuse it.
175 cbe = Interpreter;
Chris Lattnercd6f46e2006-11-09 05:57:53 +0000176 } else if (InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe) {
177 // We want to debug the CBE itself or LLC is known-good. Use LLC as the
178 // 'known-good' compiler.
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000179 std::vector<std::string> ToolArgs;
180 ToolArgs.push_back("--relocation-model=pic");
181 cbe = AbstractInterpreter::createLLC(getToolName(), Message, &ToolArgs);
182 } else {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000183 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Chris Lattner7bb11542004-02-18 20:52:02 +0000184 }
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000185 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
186
Chris Lattner769f1fe2003-10-14 21:59:36 +0000187 gcc = GCC::create(getToolName(), Message);
Misha Brukmana259c9b2003-07-24 21:59:10 +0000188 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
189
Chris Lattner4a106452002-12-23 23:50:16 +0000190 // If there was an error creating the selected interpreter, quit with error.
191 return Interpreter == 0;
192}
193
Chris Lattnerea9212c2004-02-18 23:25:22 +0000194/// compileProgram - Try to compile the specified module, throwing an exception
195/// if an error occurs, or returning normally if not. This is used for code
196/// generation crash testing.
197///
198void BugDriver::compileProgram(Module *M) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000199 // Emit the program to a bitcode file...
200 sys::Path BitcodeFile ("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000201 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000202 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
Reid Spencer51c5a282006-08-23 20:34:57 +0000203 std::cerr << ToolName << ": Error making unique filename: " << ErrMsg
204 << "\n";
205 exit(1);
206 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000207 if (writeProgramToFile(BitcodeFile.toString(), M)) {
208 std::cerr << ToolName << ": Error emitting bitcode to file '"
209 << BitcodeFile << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000210 exit(1);
211 }
212
Gabor Greif8ff70c22007-07-04 21:55:50 +0000213 // Remove the temporary bitcode file when we are done.
214 FileRemover BitcodeFileRemover(BitcodeFile);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000215
216 // Actually compile the program!
Gabor Greif8ff70c22007-07-04 21:55:50 +0000217 Interpreter->compileProgram(BitcodeFile.toString());
Chris Lattnerea9212c2004-02-18 23:25:22 +0000218}
219
Chris Lattner4a106452002-12-23 23:50:16 +0000220
221/// executeProgram - This method runs "Program", capturing the output of the
222/// program to a file, returning the filename of the file. A recommended
223/// filename may be optionally specified.
224///
225std::string BugDriver::executeProgram(std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000226 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000227 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000228 AbstractInterpreter *AI,
229 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000230 if (AI == 0) AI = Interpreter;
231 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000232 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000233 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000234 if (BitcodeFile.empty()) {
235 // Emit the program to a bitcode file...
Reid Spencer97182982004-12-15 01:53:08 +0000236 sys::Path uniqueFilename("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000237 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
238 std::cerr << ToolName << ": Error making unique filename: "
239 << ErrMsg << "!\n";
240 exit(1);
241 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000242 BitcodeFile = uniqueFilename.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000243
Gabor Greif8ff70c22007-07-04 21:55:50 +0000244 if (writeProgramToFile(BitcodeFile, Program)) {
245 std::cerr << ToolName << ": Error emitting bitcode to file '"
246 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000247 exit(1);
248 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000249 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000250 }
251
Gabor Greif8ff70c22007-07-04 21:55:50 +0000252 // Remove the temporary bitcode file when we are done.
253 sys::Path BitcodePath (BitcodeFile);
254 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode);
Chris Lattner97092722004-02-18 22:01:21 +0000255
Chris Lattner4a106452002-12-23 23:50:16 +0000256 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000257
Chris Lattner4a106452002-12-23 23:50:16 +0000258 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000259 sys::Path uniqueFile(OutputFile);
Reid Spencer51c5a282006-08-23 20:34:57 +0000260 if (uniqueFile.makeUnique(true, &ErrMsg)) {
261 std::cerr << ToolName << ": Error making unique filename: "
262 << ErrMsg << "\n";
263 exit(1);
264 }
Reid Spencer97182982004-12-15 01:53:08 +0000265 OutputFile = uniqueFile.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000266
Chris Lattner769f1fe2003-10-14 21:59:36 +0000267 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000268 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000269 if (!SharedObj.empty())
270 SharedObjs.push_back(SharedObj);
271
Reid Spencer51ab5c82006-06-06 00:00:42 +0000272
273 // If this is an LLC or CBE run, then the GCC compiler might get run to
274 // compile the program. If so, we should pass the user's -Xlinker options
275 // as the GCCArgs.
276 int RetVal = 0;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000277 if (InterpreterSel == RunLLC || InterpreterSel == RunCBE ||
Lauro Ramos Venancio497391a2007-06-06 23:10:56 +0000278 InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe)
Gabor Greif8ff70c22007-07-04 21:55:50 +0000279 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000280 OutputFile, AdditionalLinkerArgs, SharedObjs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000281 Timeout, MemoryLimit);
Reid Spencer51ab5c82006-06-06 00:00:42 +0000282 else
Gabor Greif8ff70c22007-07-04 21:55:50 +0000283 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000284 OutputFile, std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000285 SharedObjs, Timeout, MemoryLimit);
Chris Lattner7d91e492004-07-24 07:53:26 +0000286
287 if (RetVal == -1) {
288 std::cerr << "<timeout>";
289 static bool FirstTimeout = true;
290 if (FirstTimeout) {
291 std::cout << "\n"
292 "*** Program execution timed out! This mechanism is designed to handle\n"
293 " programs stuck in infinite loops gracefully. The -timeout option\n"
294 " can be used to change the timeout threshold or disable it completely\n"
295 " (with -timeout=0). This message is only displayed once.\n";
296 FirstTimeout = false;
297 }
298 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000299
Reid Spencer5e1452c2006-11-28 07:04:10 +0000300 if (AppendProgramExitCode) {
301 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
302 outFile << "exit " << RetVal << '\n';
303 outFile.close();
304 }
305
Brian Gaekec5cad212004-02-11 18:37:32 +0000306 if (ProgramExitedNonzero != 0)
307 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000308
Chris Lattner4a106452002-12-23 23:50:16 +0000309 // Return the filename we captured the output to.
310 return OutputFile;
311}
312
Brian Gaekec5cad212004-02-11 18:37:32 +0000313/// executeProgramWithCBE - Used to create reference output with the C
314/// backend, if reference output is not provided.
315///
316std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
317 bool ProgramExitedNonzero;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000318 std::string outFN = executeProgram(OutputFile, "", "", cbe,
Brian Gaekec5cad212004-02-11 18:37:32 +0000319 &ProgramExitedNonzero);
320 if (ProgramExitedNonzero) {
321 std::cerr
322 << "Warning: While generating reference output, program exited with\n"
323 << "non-zero exit code. This will NOT be treated as a failure.\n";
324 CheckProgramExitCode = false;
325 }
326 return outFN;
327}
Misha Brukman50733362003-07-24 18:17:43 +0000328
Gabor Greif8ff70c22007-07-04 21:55:50 +0000329std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000330 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000331 sys::Path OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000332
333 // Using CBE
Gabor Greif8ff70c22007-07-04 21:55:50 +0000334 GCC::FileType FT = cbe->OutputCode(BitcodeFile, OutputFile);
Misha Brukman50733362003-07-24 18:17:43 +0000335
Chris Lattnera0f5b152003-10-14 21:09:11 +0000336 std::string SharedObjectFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000337 if (gcc->MakeSharedObject(OutputFile.toString(), FT,
Chris Lattner130e2a32006-06-27 20:35:36 +0000338 SharedObjectFile, AdditionalLinkerArgs))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000339 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000340
341 // Remove the intermediate C file
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000342 OutputFile.eraseFromDisk();
Misha Brukman50733362003-07-24 18:17:43 +0000343
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000344 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000345}
346
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000347/// createReferenceFile - calls compileProgram and then records the output
348/// into ReferenceOutputFile. Returns true if reference file created, false
349/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
350/// this function.
351///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000352bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000353 try {
354 compileProgram(Program);
Jeff Cohend41b30d2006-11-05 19:31:28 +0000355 } catch (ToolExecutionError &) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000356 return false;
357 }
358 try {
359 ReferenceOutputFile = executeProgramWithCBE(Filename);
360 std::cout << "Reference output is: " << ReferenceOutputFile << "\n\n";
361 } catch (ToolExecutionError &TEE) {
362 std::cerr << TEE.what();
363 if (Interpreter != cbe) {
364 std::cerr << "*** There is a bug running the C backend. Either debug"
365 << " it (use the -run-cbe bugpoint option), or fix the error"
366 << " some other way.\n";
367 }
368 return false;
369 }
370 return true;
371}
Misha Brukman50733362003-07-24 18:17:43 +0000372
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000373/// diffProgram - This method executes the specified module and diffs the
374/// output against the file specified by ReferenceOutputFile. If the output
375/// is different, true is returned. If there is a problem with the code
376/// generator (e.g., llc crashes), this will throw an exception.
Chris Lattner4a106452002-12-23 23:50:16 +0000377///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000378bool BugDriver::diffProgram(const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000379 const std::string &SharedObject,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000380 bool RemoveBitcode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000381 bool ProgramExitedNonzero;
382
Chris Lattner4a106452002-12-23 23:50:16 +0000383 // Execute the program, generating an output file...
Gabor Greif8ff70c22007-07-04 21:55:50 +0000384 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0,
Reid Spencer5f767602004-12-16 23:04:20 +0000385 &ProgramExitedNonzero));
Brian Gaekec5cad212004-02-11 18:37:32 +0000386
387 // If we're checking the program exit code, assume anything nonzero is bad.
Chris Lattner58d84ce2004-04-02 05:33:06 +0000388 if (CheckProgramExitCode && ProgramExitedNonzero) {
Reid Spencera229c5c2005-07-08 03:08:58 +0000389 Output.eraseFromDisk();
Gabor Greif8ff70c22007-07-04 21:55:50 +0000390 if (RemoveBitcode)
391 sys::Path(BitcodeFile).eraseFromDisk();
Brian Gaekec5cad212004-02-11 18:37:32 +0000392 return true;
Chris Lattner58d84ce2004-04-02 05:33:06 +0000393 }
Chris Lattner4a106452002-12-23 23:50:16 +0000394
Chris Lattner65f62792003-08-01 20:29:45 +0000395 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000396 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000397 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
398 sys::Path(Output.toString()),
399 AbsTolerance, RelTolerance, &Error)) {
400 if (Diff == 2) {
Misha Brukmaneed80e22004-07-23 01:30:49 +0000401 std::cerr << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000402 exit(1);
403 }
404 FilesDifferent = true;
405 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000406
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000407 // Remove the generated output.
Reid Spencera229c5c2005-07-08 03:08:58 +0000408 Output.eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000409
Gabor Greif8ff70c22007-07-04 21:55:50 +0000410 // Remove the bitcode file if we are supposed to.
411 if (RemoveBitcode)
412 sys::Path(BitcodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000413 return FilesDifferent;
414}
Misha Brukman91eabc12003-07-28 19:16:14 +0000415
416bool BugDriver::isExecutingJIT() {
417 return InterpreterSel == RunJIT;
418}
Brian Gaeked0fde302003-11-11 22:41:34 +0000419