blob: feda331177b166a62536526ccf85c0e882a029ca [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 Lattner74382b72009-08-23 22:45:37 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner4a106452002-12-23 23:50:16 +000022#include <fstream>
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>
Dan Gohman70ef4492008-12-08 04:02:47 +000042 InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
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
Dan Gohman70ef4492008-12-08 04:02:47 +000057 cl::opt<OutputType>
58 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
Evan Cheng49419e22009-07-21 19:25:09 +000059 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
60 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
61 clEnumValN(RunCBE, "safe-run-cbe", "Compile with CBE"),
62 clEnumValN(Custom, "safe-run-custom",
63 "Use -exec-command to define a command to execute "
64 "the bitcode. Useful for cross-compilation."),
65 clEnumValEnd),
Dan Gohman70ef4492008-12-08 04:02:47 +000066 cl::init(AutoPick));
67
68 cl::opt<std::string>
69 SafeInterpreterPath("safe-path",
Evan Cheng49419e22009-07-21 19:25:09 +000070 cl::desc("Specify the path to the \"safe\" backend program"),
71 cl::init(""));
Dan Gohman70ef4492008-12-08 04:02:47 +000072
Brian Gaekec5cad212004-02-11 18:37:32 +000073 cl::opt<bool>
Reid Spencer5e1452c2006-11-28 07:04:10 +000074 AppendProgramExitCode("append-exit-code",
75 cl::desc("Append the exit code to the output so it gets diff'd too"),
76 cl::init(false));
77
Chris Lattner3c053a02003-04-23 20:31:37 +000078 cl::opt<std::string>
79 InputFile("input", cl::init("/dev/null"),
80 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000081
82 cl::list<std::string>
83 AdditionalSOs("additional-so",
84 cl::desc("Additional shared objects to load "
85 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000086
Reid Spencer51ab5c82006-06-06 00:00:42 +000087 cl::list<std::string>
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000088 AdditionalLinkerArgs("Xlinker",
Reid Spencer51ab5c82006-06-06 00:00:42 +000089 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000090
91 cl::opt<std::string>
92 CustomExecCommand("exec-command", cl::init("simulate"),
93 cl::desc("Command to execute the bitcode (use with -run-custom) "
94 "(default: simulate)"));
Chris Lattner4a106452002-12-23 23:50:16 +000095}
96
Brian Gaeked0fde302003-11-11 22:41:34 +000097namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +000098 // Anything specified after the --args option are taken as arguments to the
99 // program being debugged.
100 cl::list<std::string>
101 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000102 cl::ZeroOrMore, cl::PositionalEatsArgs);
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000103
104 cl::opt<std::string>
105 OutputPrefix("output-prefix", cl::init("bugpoint"),
106 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
Dan Gohman70ef4492008-12-08 04:02:47 +0000107}
Brian Gaeke636df3d2004-05-04 21:09:16 +0000108
Dan Gohman70ef4492008-12-08 04:02:47 +0000109namespace {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000110 cl::list<std::string>
111 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000112 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman70ef4492008-12-08 04:02:47 +0000113
114 cl::list<std::string>
115 SafeToolArgv("safe-tool-args", cl::Positional,
116 cl::desc("<safe-tool arguments>..."),
117 cl::ZeroOrMore, cl::PositionalEatsArgs);
Bill Wendling38efa382009-03-02 23:13:18 +0000118
119 cl::list<std::string>
120 GCCToolArgv("gcc-tool-args", cl::Positional,
121 cl::desc("<gcc-tool arguments>..."),
122 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +0000123}
Misha Brukman9d679cb2003-07-30 20:15:44 +0000124
Chris Lattner4a106452002-12-23 23:50:16 +0000125//===----------------------------------------------------------------------===//
126// BugDriver method implementation
127//
128
129/// initializeExecutionEnvironment - This method is used to set up the
130/// environment for executing LLVM programs.
131///
132bool BugDriver::initializeExecutionEnvironment() {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000133 outs() << "Initializing execution environment: ";
Chris Lattner4a106452002-12-23 23:50:16 +0000134
Misha Brukman41485562003-09-29 22:40:52 +0000135 // Create an instance of the AbstractInterpreter interface as specified on
136 // the command line
Dan Gohman70ef4492008-12-08 04:02:47 +0000137 SafeInterpreter = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000138 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000139
Chris Lattnercc876a72003-05-03 03:19:41 +0000140 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000141 case AutoPick:
142 InterpreterSel = RunCBE;
Dan Gohman70ef4492008-12-08 04:02:47 +0000143 Interpreter =
Bill Wendling38efa382009-03-02 23:13:18 +0000144 AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv,
145 &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000146 if (!Interpreter) {
147 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000148 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
149 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000150 }
151 if (!Interpreter) {
152 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000153 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000154 &ToolArgv, &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000155 }
156 if (!Interpreter) {
157 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000158 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
159 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000160 }
161 if (!Interpreter) {
162 InterpreterSel = AutoPick;
163 Message = "Sorry, I can't automatically select an interpreter!\n";
164 }
165 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000166 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000167 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
168 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000169 break;
170 case RunLLC:
Dan Gohman70ef4492008-12-08 04:02:47 +0000171 case LLC_Safe:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000172 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000173 &ToolArgv, &GCCToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000174 break;
175 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000176 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
177 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000178 break;
179 case RunCBE:
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000180 case CBE_bug:
181 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000182 &ToolArgv, &GCCToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000183 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000184 case Custom:
Dan Gohman197f7282009-08-05 20:21:17 +0000185 Interpreter = AbstractInterpreter::createCustom(Message, CustomExecCommand);
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000186 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000187 default:
Misha Brukman41485562003-09-29 22:40:52 +0000188 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000189 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000190 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000191 if (!Interpreter)
Dan Gohman65f57c22009-07-15 16:35:29 +0000192 errs() << Message;
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000193 else // Display informational messages on stdout instead of stderr
Dan Gohmanac95cc72009-07-16 15:30:09 +0000194 outs() << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000195
Dan Gohman70ef4492008-12-08 04:02:47 +0000196 std::string Path = SafeInterpreterPath;
197 if (Path.empty())
198 Path = getToolName();
199 std::vector<std::string> SafeToolArgs = SafeToolArgv;
200 switch (SafeInterpreterSel) {
201 case AutoPick:
202 // In "cbe-bug" mode, default to using LLC as the "safe" backend.
203 if (!SafeInterpreter &&
204 InterpreterSel == CBE_bug) {
205 SafeInterpreterSel = RunLLC;
206 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000207 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000208 &SafeToolArgs,
209 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000210 }
211
212 // In "llc-safe" mode, default to using LLC as the "safe" backend.
213 if (!SafeInterpreter &&
214 InterpreterSel == LLC_Safe) {
215 SafeInterpreterSel = RunLLC;
216 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000217 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000218 &SafeToolArgs,
219 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000220 }
221
222 // Pick a backend that's different from the test backend. The JIT and
223 // LLC backends share a lot of code, so prefer to use the CBE as the
224 // safe back-end when testing them.
225 if (!SafeInterpreter &&
226 InterpreterSel != RunCBE) {
227 SafeInterpreterSel = RunCBE;
Dan Gohman197f7282009-08-05 20:21:17 +0000228 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000229 &SafeToolArgs,
230 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000231 }
232 if (!SafeInterpreter &&
233 InterpreterSel != RunLLC &&
234 InterpreterSel != RunJIT) {
235 SafeInterpreterSel = RunLLC;
236 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000237 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000238 &SafeToolArgs,
239 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000240 }
241 if (!SafeInterpreter) {
242 SafeInterpreterSel = AutoPick;
243 Message = "Sorry, I can't automatically select an interpreter!\n";
244 }
245 break;
246 case RunLLC:
247 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000248 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000249 &SafeToolArgs,
250 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000251 break;
252 case RunCBE:
Dan Gohman197f7282009-08-05 20:21:17 +0000253 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000254 &SafeToolArgs,
255 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000256 break;
257 case Custom:
Dan Gohman197f7282009-08-05 20:21:17 +0000258 SafeInterpreter = AbstractInterpreter::createCustom(Message,
Dan Gohman70ef4492008-12-08 04:02:47 +0000259 CustomExecCommand);
260 break;
261 default:
262 Message = "Sorry, this back-end is not supported by bugpoint as the "
263 "\"safe\" backend right now!\n";
264 break;
Chris Lattner7bb11542004-02-18 20:52:02 +0000265 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000266 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000267
Dan Gohman197f7282009-08-05 20:21:17 +0000268 gcc = GCC::create(Message, &GCCToolArgv);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000269 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000270
Chris Lattner4a106452002-12-23 23:50:16 +0000271 // If there was an error creating the selected interpreter, quit with error.
272 return Interpreter == 0;
273}
274
Chris Lattnerea9212c2004-02-18 23:25:22 +0000275/// compileProgram - Try to compile the specified module, throwing an exception
276/// if an error occurs, or returning normally if not. This is used for code
277/// generation crash testing.
278///
279void BugDriver::compileProgram(Module *M) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000280 // Emit the program to a bitcode file...
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000281 sys::Path BitcodeFile (OutputPrefix + "-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000282 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000283 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000284 errs() << ToolName << ": Error making unique filename: " << ErrMsg
285 << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000286 exit(1);
287 }
Chris Lattner74382b72009-08-23 22:45:37 +0000288 if (writeProgramToFile(BitcodeFile.str(), M)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000289 errs() << ToolName << ": Error emitting bitcode to file '"
Chris Lattner74382b72009-08-23 22:45:37 +0000290 << BitcodeFile.str() << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000291 exit(1);
292 }
293
Gabor Greif8ff70c22007-07-04 21:55:50 +0000294 // Remove the temporary bitcode file when we are done.
Anton Korobeynikov86c006a2009-08-05 09:32:10 +0000295 FileRemover BitcodeFileRemover(BitcodeFile, !SaveTemps);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000296
297 // Actually compile the program!
Chris Lattner74382b72009-08-23 22:45:37 +0000298 Interpreter->compileProgram(BitcodeFile.str());
Chris Lattnerea9212c2004-02-18 23:25:22 +0000299}
300
Chris Lattner4a106452002-12-23 23:50:16 +0000301
302/// executeProgram - This method runs "Program", capturing the output of the
303/// program to a file, returning the filename of the file. A recommended
304/// filename may be optionally specified.
305///
306std::string BugDriver::executeProgram(std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000307 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000308 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000309 AbstractInterpreter *AI,
310 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000311 if (AI == 0) AI = Interpreter;
312 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000313 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000314 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000315 if (BitcodeFile.empty()) {
316 // Emit the program to a bitcode file...
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000317 sys::Path uniqueFilename(OutputPrefix + "-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000318 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000319 errs() << ToolName << ": Error making unique filename: "
320 << ErrMsg << "!\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000321 exit(1);
322 }
Chris Lattner74382b72009-08-23 22:45:37 +0000323 BitcodeFile = uniqueFilename.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000324
Gabor Greif8ff70c22007-07-04 21:55:50 +0000325 if (writeProgramToFile(BitcodeFile, Program)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000326 errs() << ToolName << ": Error emitting bitcode to file '"
327 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000328 exit(1);
329 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000330 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000331 }
332
Gabor Greif8ff70c22007-07-04 21:55:50 +0000333 // Remove the temporary bitcode file when we are done.
334 sys::Path BitcodePath (BitcodeFile);
Anton Korobeynikov86c006a2009-08-05 09:32:10 +0000335 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
Chris Lattner97092722004-02-18 22:01:21 +0000336
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000337 if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000338
Chris Lattner4a106452002-12-23 23:50:16 +0000339 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000340 sys::Path uniqueFile(OutputFile);
Reid Spencer51c5a282006-08-23 20:34:57 +0000341 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000342 errs() << ToolName << ": Error making unique filename: "
343 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000344 exit(1);
345 }
Chris Lattner74382b72009-08-23 22:45:37 +0000346 OutputFile = uniqueFile.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000347
Chris Lattner769f1fe2003-10-14 21:59:36 +0000348 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000349 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000350 if (!SharedObj.empty())
351 SharedObjs.push_back(SharedObj);
352
Dan Gohman70ef4492008-12-08 04:02:47 +0000353 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
354 OutputFile, AdditionalLinkerArgs, SharedObjs,
355 Timeout, MemoryLimit);
Chris Lattner7d91e492004-07-24 07:53:26 +0000356
357 if (RetVal == -1) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000358 errs() << "<timeout>";
Chris Lattner7d91e492004-07-24 07:53:26 +0000359 static bool FirstTimeout = true;
360 if (FirstTimeout) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000361 outs() << "\n"
Chris Lattner7d91e492004-07-24 07:53:26 +0000362 "*** Program execution timed out! This mechanism is designed to handle\n"
363 " programs stuck in infinite loops gracefully. The -timeout option\n"
364 " can be used to change the timeout threshold or disable it completely\n"
365 " (with -timeout=0). This message is only displayed once.\n";
366 FirstTimeout = false;
367 }
368 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000369
Reid Spencer5e1452c2006-11-28 07:04:10 +0000370 if (AppendProgramExitCode) {
371 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
372 outFile << "exit " << RetVal << '\n';
373 outFile.close();
374 }
375
Brian Gaekec5cad212004-02-11 18:37:32 +0000376 if (ProgramExitedNonzero != 0)
377 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000378
Chris Lattner4a106452002-12-23 23:50:16 +0000379 // Return the filename we captured the output to.
380 return OutputFile;
381}
382
Dan Gohman70ef4492008-12-08 04:02:47 +0000383/// executeProgramSafely - Used to create reference output with the "safe"
Brian Gaekec5cad212004-02-11 18:37:32 +0000384/// backend, if reference output is not provided.
385///
Dan Gohman70ef4492008-12-08 04:02:47 +0000386std::string BugDriver::executeProgramSafely(std::string OutputFile) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000387 bool ProgramExitedNonzero;
Dan Gohman70ef4492008-12-08 04:02:47 +0000388 std::string outFN = executeProgram(OutputFile, "", "", SafeInterpreter,
Brian Gaekec5cad212004-02-11 18:37:32 +0000389 &ProgramExitedNonzero);
Brian Gaekec5cad212004-02-11 18:37:32 +0000390 return outFN;
391}
Misha Brukman50733362003-07-24 18:17:43 +0000392
Gabor Greif8ff70c22007-07-04 21:55:50 +0000393std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000394 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000395 sys::Path OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000396
Dan Gohman70ef4492008-12-08 04:02:47 +0000397 // Using the known-good backend.
398 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
Misha Brukman50733362003-07-24 18:17:43 +0000399
Chris Lattnera0f5b152003-10-14 21:09:11 +0000400 std::string SharedObjectFile;
Chris Lattner74382b72009-08-23 22:45:37 +0000401 if (gcc->MakeSharedObject(OutputFile.str(), FT,
Chris Lattner130e2a32006-06-27 20:35:36 +0000402 SharedObjectFile, AdditionalLinkerArgs))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000403 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000404
405 // Remove the intermediate C file
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000406 OutputFile.eraseFromDisk();
Misha Brukman50733362003-07-24 18:17:43 +0000407
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000408 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000409}
410
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000411/// 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///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000416bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000417 try {
418 compileProgram(Program);
Jeff Cohend41b30d2006-11-05 19:31:28 +0000419 } catch (ToolExecutionError &) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000420 return false;
421 }
422 try {
Dan Gohman70ef4492008-12-08 04:02:47 +0000423 ReferenceOutputFile = executeProgramSafely(Filename);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000424 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000425 } catch (ToolExecutionError &TEE) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000426 errs() << TEE.what();
Dan Gohman70ef4492008-12-08 04:02:47 +0000427 if (Interpreter != SafeInterpreter) {
Dan Gohman65f57c22009-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";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000432 }
433 return false;
434 }
435 return true;
436}
Misha Brukman50733362003-07-24 18:17:43 +0000437
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000438/// 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.
Chris Lattner4a106452002-12-23 23:50:16 +0000442///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000443bool BugDriver::diffProgram(const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000444 const std::string &SharedObject,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000445 bool RemoveBitcode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000446 bool ProgramExitedNonzero;
447
Chris Lattner4a106452002-12-23 23:50:16 +0000448 // Execute the program, generating an output file...
Gabor Greif8ff70c22007-07-04 21:55:50 +0000449 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0,
Reid Spencer5f767602004-12-16 23:04:20 +0000450 &ProgramExitedNonzero));
Brian Gaekec5cad212004-02-11 18:37:32 +0000451
Chris Lattner65f62792003-08-01 20:29:45 +0000452 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000453 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000454 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
Chris Lattner74382b72009-08-23 22:45:37 +0000455 sys::Path(Output.str()),
Chris Lattnera328c512005-01-23 03:45:26 +0000456 AbsTolerance, RelTolerance, &Error)) {
457 if (Diff == 2) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000458 errs() << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000459 exit(1);
460 }
461 FilesDifferent = true;
462 }
David Goodwin80becf12009-07-10 21:39:28 +0000463 else {
464 // Remove the generated output if there are no differences.
465 Output.eraseFromDisk();
466 }
Chris Lattner4a106452002-12-23 23:50:16 +0000467
Gabor Greif8ff70c22007-07-04 21:55:50 +0000468 // Remove the bitcode file if we are supposed to.
469 if (RemoveBitcode)
470 sys::Path(BitcodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000471 return FilesDifferent;
472}
Misha Brukman91eabc12003-07-28 19:16:14 +0000473
474bool BugDriver::isExecutingJIT() {
475 return InterpreterSel == RunJIT;
476}
Brian Gaeked0fde302003-11-11 22:41:34 +0000477