blob: 3b93a1a3224b0f51d6e88e290999d49774fdcbcd [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains code used to execute the program utilizing one of the
11// various ways of running LLVM bitcode.
12//
13//===----------------------------------------------------------------------===//
14
15#include "BugDriver.h"
16#include "ToolRunner.h"
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 Lattnerb1aa85b2009-08-23 22:45:37 +000021#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include <fstream>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023
24using namespace llvm;
25
26namespace {
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
29 //
30 enum OutputType {
Chris Lattner51ff0582010-03-16 06:41:47 +000031 AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, RunCBE, CBE_bug, LLC_Safe,Custom
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 };
33
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
41 cl::opt<OutputType>
Dan Gohman7fb02ed2008-12-08 04:02:47 +000042 InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
44 clEnumValN(RunLLI, "run-int",
45 "Execute with the interpreter"),
46 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
47 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
Chris Lattner51ff0582010-03-16 06:41:47 +000048 clEnumValN(RunLLCIA, "run-llc-ia",
49 "Compile with LLC with integrated assembler"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
51 clEnumValN(CBE_bug,"cbe-bug", "Find CBE bugs"),
52 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +000053 clEnumValN(Custom, "run-custom",
54 "Use -exec-command to define a command to execute "
55 "the bitcode. Useful for cross-compilation."),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 clEnumValEnd),
57 cl::init(AutoPick));
58
Dan Gohman7fb02ed2008-12-08 04:02:47 +000059 cl::opt<OutputType>
60 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
Evan Cheng899dcd62009-07-21 19:25:09 +000061 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
62 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
63 clEnumValN(RunCBE, "safe-run-cbe", "Compile with CBE"),
64 clEnumValN(Custom, "safe-run-custom",
65 "Use -exec-command to define a command to execute "
66 "the bitcode. Useful for cross-compilation."),
67 clEnumValEnd),
Dan Gohman7fb02ed2008-12-08 04:02:47 +000068 cl::init(AutoPick));
69
70 cl::opt<std::string>
71 SafeInterpreterPath("safe-path",
Evan Cheng899dcd62009-07-21 19:25:09 +000072 cl::desc("Specify the path to the \"safe\" backend program"),
73 cl::init(""));
Dan Gohman7fb02ed2008-12-08 04:02:47 +000074
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 AppendProgramExitCode("append-exit-code",
77 cl::desc("Append the exit code to the output so it gets diff'd too"),
78 cl::init(false));
79
80 cl::opt<std::string>
81 InputFile("input", cl::init("/dev/null"),
82 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
83
84 cl::list<std::string>
85 AdditionalSOs("additional-so",
86 cl::desc("Additional shared objects to load "
87 "into executing programs"));
88
89 cl::list<std::string>
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +000090 AdditionalLinkerArgs("Xlinker",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +000092
93 cl::opt<std::string>
94 CustomExecCommand("exec-command", cl::init("simulate"),
95 cl::desc("Command to execute the bitcode (use with -run-custom) "
96 "(default: simulate)"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097}
98
99namespace llvm {
100 // Anything specified after the --args option are taken as arguments to the
101 // program being debugged.
102 cl::list<std::string>
103 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
104 cl::ZeroOrMore, cl::PositionalEatsArgs);
Daniel Dunbar377b5a32009-09-07 19:26:11 +0000105
106 cl::opt<std::string>
107 OutputPrefix("output-prefix", cl::init("bugpoint"),
108 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000109}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000111namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 cl::list<std::string>
113 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
114 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000115
116 cl::list<std::string>
117 SafeToolArgv("safe-tool-args", cl::Positional,
118 cl::desc("<safe-tool arguments>..."),
119 cl::ZeroOrMore, cl::PositionalEatsArgs);
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000120
121 cl::list<std::string>
122 GCCToolArgv("gcc-tool-args", cl::Positional,
123 cl::desc("<gcc-tool arguments>..."),
124 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125}
126
127//===----------------------------------------------------------------------===//
128// BugDriver method implementation
129//
130
131/// initializeExecutionEnvironment - This method is used to set up the
132/// environment for executing LLVM programs.
133///
134bool BugDriver::initializeExecutionEnvironment() {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000135 outs() << "Initializing execution environment: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136
137 // Create an instance of the AbstractInterpreter interface as specified on
138 // the command line
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000139 SafeInterpreter = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 std::string Message;
141
142 switch (InterpreterSel) {
143 case AutoPick:
144 InterpreterSel = RunCBE;
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000145 Interpreter =
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000146 AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv,
147 &GCCToolArgv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 if (!Interpreter) {
149 InterpreterSel = RunJIT;
150 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
151 &ToolArgv);
152 }
153 if (!Interpreter) {
154 InterpreterSel = RunLLC;
155 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000156 &ToolArgv, &GCCToolArgv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 }
158 if (!Interpreter) {
159 InterpreterSel = RunLLI;
160 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
161 &ToolArgv);
162 }
163 if (!Interpreter) {
164 InterpreterSel = AutoPick;
165 Message = "Sorry, I can't automatically select an interpreter!\n";
166 }
167 break;
168 case RunLLI:
169 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
170 &ToolArgv);
171 break;
172 case RunLLC:
Chris Lattner51ff0582010-03-16 06:41:47 +0000173 case RunLLCIA:
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000174 case LLC_Safe:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Chris Lattner51ff0582010-03-16 06:41:47 +0000176 &ToolArgv, &GCCToolArgv,
177 InterpreterSel == RunLLCIA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 break;
179 case RunJIT:
180 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
181 &ToolArgv);
182 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 case RunCBE:
184 case CBE_bug:
185 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000186 &ToolArgv, &GCCToolArgv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 break;
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +0000188 case Custom:
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000189 Interpreter = AbstractInterpreter::createCustom(Message, CustomExecCommand);
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +0000190 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 default:
192 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
193 break;
194 }
Matthijs Kooijmanfa2277b2008-06-12 13:09:43 +0000195 if (!Interpreter)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000196 errs() << Message;
Matthijs Kooijmanfa2277b2008-06-12 13:09:43 +0000197 else // Display informational messages on stdout instead of stderr
Dan Gohmanb714fab2009-07-16 15:30:09 +0000198 outs() << Message;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000200 std::string Path = SafeInterpreterPath;
201 if (Path.empty())
202 Path = getToolName();
203 std::vector<std::string> SafeToolArgs = SafeToolArgv;
204 switch (SafeInterpreterSel) {
205 case AutoPick:
206 // In "cbe-bug" mode, default to using LLC as the "safe" backend.
207 if (!SafeInterpreter &&
208 InterpreterSel == CBE_bug) {
209 SafeInterpreterSel = RunLLC;
210 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000211 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000212 &SafeToolArgs,
213 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000214 }
215
216 // In "llc-safe" mode, default to using LLC as the "safe" backend.
217 if (!SafeInterpreter &&
218 InterpreterSel == LLC_Safe) {
219 SafeInterpreterSel = RunLLC;
220 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000221 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000222 &SafeToolArgs,
223 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000224 }
225
226 // Pick a backend that's different from the test backend. The JIT and
227 // LLC backends share a lot of code, so prefer to use the CBE as the
228 // safe back-end when testing them.
229 if (!SafeInterpreter &&
230 InterpreterSel != RunCBE) {
231 SafeInterpreterSel = RunCBE;
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000232 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000233 &SafeToolArgs,
234 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000235 }
236 if (!SafeInterpreter &&
237 InterpreterSel != RunLLC &&
238 InterpreterSel != RunJIT) {
239 SafeInterpreterSel = RunLLC;
240 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000241 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000242 &SafeToolArgs,
243 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000244 }
245 if (!SafeInterpreter) {
246 SafeInterpreterSel = AutoPick;
247 Message = "Sorry, I can't automatically select an interpreter!\n";
248 }
249 break;
250 case RunLLC:
Chris Lattner51ff0582010-03-16 06:41:47 +0000251 case RunLLCIA:
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000252 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000253 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000254 &SafeToolArgs,
Chris Lattner51ff0582010-03-16 06:41:47 +0000255 &GCCToolArgv,
256 SafeInterpreterSel == RunLLCIA);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000257 break;
258 case RunCBE:
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000259 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000260 &SafeToolArgs,
261 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000262 break;
263 case Custom:
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000264 SafeInterpreter = AbstractInterpreter::createCustom(Message,
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000265 CustomExecCommand);
266 break;
267 default:
268 Message = "Sorry, this back-end is not supported by bugpoint as the "
269 "\"safe\" backend right now!\n";
270 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 }
Dan Gohmanb714fab2009-07-16 15:30:09 +0000272 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000274 gcc = GCC::create(Message, &GCCToolArgv);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000275 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276
277 // If there was an error creating the selected interpreter, quit with error.
278 return Interpreter == 0;
279}
280
281/// compileProgram - Try to compile the specified module, throwing an exception
282/// if an error occurs, or returning normally if not. This is used for code
283/// generation crash testing.
284///
285void BugDriver::compileProgram(Module *M) {
286 // Emit the program to a bitcode file...
Daniel Dunbar377b5a32009-09-07 19:26:11 +0000287 sys::Path BitcodeFile (OutputPrefix + "-test-program.bc");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 std::string ErrMsg;
289 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000290 errs() << ToolName << ": Error making unique filename: " << ErrMsg
291 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 exit(1);
293 }
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000294 if (writeProgramToFile(BitcodeFile.str(), M)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000295 errs() << ToolName << ": Error emitting bitcode to file '"
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000296 << BitcodeFile.str() << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 exit(1);
298 }
299
300 // Remove the temporary bitcode file when we are done.
Anton Korobeynikov43cf4f12009-08-05 09:32:10 +0000301 FileRemover BitcodeFileRemover(BitcodeFile, !SaveTemps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302
303 // Actually compile the program!
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000304 Interpreter->compileProgram(BitcodeFile.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305}
306
307
308/// executeProgram - This method runs "Program", capturing the output of the
309/// program to a file, returning the filename of the file. A recommended
310/// filename may be optionally specified.
311///
312std::string BugDriver::executeProgram(std::string OutputFile,
313 std::string BitcodeFile,
314 const std::string &SharedObj,
Nick Lewycky65349c42010-04-10 23:18:13 +0000315 AbstractInterpreter *AI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 if (AI == 0) AI = Interpreter;
317 assert(AI && "Interpreter should have been created already!");
318 bool CreatedBitcode = false;
319 std::string ErrMsg;
320 if (BitcodeFile.empty()) {
321 // Emit the program to a bitcode file...
Daniel Dunbar377b5a32009-09-07 19:26:11 +0000322 sys::Path uniqueFilename(OutputPrefix + "-test-program.bc");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000324 errs() << ToolName << ": Error making unique filename: "
325 << ErrMsg << "!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 exit(1);
327 }
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000328 BitcodeFile = uniqueFilename.str();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329
330 if (writeProgramToFile(BitcodeFile, Program)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000331 errs() << ToolName << ": Error emitting bitcode to file '"
332 << BitcodeFile << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 exit(1);
334 }
335 CreatedBitcode = true;
336 }
337
338 // Remove the temporary bitcode file when we are done.
Nick Lewycky65349c42010-04-10 23:18:13 +0000339 sys::Path BitcodePath(BitcodeFile);
Anton Korobeynikov43cf4f12009-08-05 09:32:10 +0000340 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341
Daniel Dunbar377b5a32009-09-07 19:26:11 +0000342 if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343
344 // Check to see if this is a valid output filename...
345 sys::Path uniqueFile(OutputFile);
346 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000347 errs() << ToolName << ": Error making unique filename: "
348 << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 exit(1);
350 }
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000351 OutputFile = uniqueFile.str();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352
353 // Figure out which shared objects to run, if any.
354 std::vector<std::string> SharedObjs(AdditionalSOs);
355 if (!SharedObj.empty())
356 SharedObjs.push_back(SharedObj);
357
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000358 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
359 OutputFile, AdditionalLinkerArgs, SharedObjs,
360 Timeout, MemoryLimit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361
362 if (RetVal == -1) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000363 errs() << "<timeout>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 static bool FirstTimeout = true;
365 if (FirstTimeout) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000366 outs() << "\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 "*** Program execution timed out! This mechanism is designed to handle\n"
368 " programs stuck in infinite loops gracefully. The -timeout option\n"
369 " can be used to change the timeout threshold or disable it completely\n"
370 " (with -timeout=0). This message is only displayed once.\n";
371 FirstTimeout = false;
372 }
373 }
374
375 if (AppendProgramExitCode) {
376 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
377 outFile << "exit " << RetVal << '\n';
378 outFile.close();
379 }
380
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 // Return the filename we captured the output to.
382 return OutputFile;
383}
384
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000385/// executeProgramSafely - Used to create reference output with the "safe"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386/// backend, if reference output is not provided.
387///
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000388std::string BugDriver::executeProgramSafely(std::string OutputFile) {
Nick Lewycky65349c42010-04-10 23:18:13 +0000389 std::string outFN = executeProgram(OutputFile, "", "", SafeInterpreter);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 return outFN;
391}
392
393std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
394 assert(Interpreter && "Interpreter should have been created already!");
395 sys::Path OutputFile;
396
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000397 // Using the known-good backend.
398 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399
400 std::string SharedObjectFile;
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000401 if (gcc->MakeSharedObject(OutputFile.str(), FT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 SharedObjectFile, AdditionalLinkerArgs))
403 exit(1);
404
405 // Remove the intermediate C file
406 OutputFile.eraseFromDisk();
407
408 return "./" + SharedObjectFile;
409}
410
411/// createReferenceFile - calls compileProgram and then records the output
412/// into ReferenceOutputFile. Returns true if reference file created, false
413/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
414/// this function.
415///
416bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
417 try {
418 compileProgram(Program);
419 } catch (ToolExecutionError &) {
420 return false;
421 }
422 try {
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000423 ReferenceOutputFile = executeProgramSafely(Filename);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000424 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 } catch (ToolExecutionError &TEE) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000426 errs() << TEE.what();
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000427 if (Interpreter != SafeInterpreter) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000428 errs() << "*** There is a bug running the \"safe\" backend. Either"
429 << " debug it (for example with the -run-cbe bugpoint option,"
430 << " if CBE is being used as the \"safe\" backend), or fix the"
431 << " error some other way.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 }
433 return false;
434 }
435 return true;
436}
437
438/// diffProgram - This method executes the specified module and diffs the
439/// output against the file specified by ReferenceOutputFile. If the output
440/// is different, true is returned. If there is a problem with the code
441/// generator (e.g., llc crashes), this will throw an exception.
442///
443bool BugDriver::diffProgram(const std::string &BitcodeFile,
444 const std::string &SharedObject,
445 bool RemoveBitcode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 // Execute the program, generating an output file...
Nick Lewycky65349c42010-04-10 23:18:13 +0000447 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 std::string Error;
450 bool FilesDifferent = false;
451 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000452 sys::Path(Output.str()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 AbsTolerance, RelTolerance, &Error)) {
454 if (Diff == 2) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000455 errs() << "While diffing output: " << Error << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 exit(1);
457 }
458 FilesDifferent = true;
459 }
David Goodwinec1edba2009-07-10 21:39:28 +0000460 else {
461 // Remove the generated output if there are no differences.
462 Output.eraseFromDisk();
463 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464
465 // Remove the bitcode file if we are supposed to.
466 if (RemoveBitcode)
467 sys::Path(BitcodeFile).eraseFromDisk();
468 return FilesDifferent;
469}
470
471bool BugDriver::isExecutingJIT() {
472 return InterpreterSel == RunJIT;
473}
474