blob: 2eded1788d955c59d61c69a5b5eea938ebe19e88 [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 {
Chris Lattner50010422010-03-16 06:41:47 +000031 AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, 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"),
Chris Lattner50010422010-03-16 06:41:47 +000048 clEnumValN(RunLLCIA, "run-llc-ia",
49 "Compile with LLC with integrated assembler"),
Misha Brukman50733362003-07-24 18:17:43 +000050 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattnerc600f3c2006-09-15 21:29:15 +000051 clEnumValN(CBE_bug,"cbe-bug", "Find CBE bugs"),
Chris Lattnercd6f46e2006-11-09 05:57:53 +000052 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
Anton Korobeynikov9ef74252008-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."),
Chris Lattner4d143ee2004-07-16 00:08:28 +000056 clEnumValEnd),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000057 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000058
Dan Gohman70ef4492008-12-08 04:02:47 +000059 cl::opt<OutputType>
60 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
Evan Cheng49419e22009-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 Gohman70ef4492008-12-08 04:02:47 +000068 cl::init(AutoPick));
69
70 cl::opt<std::string>
71 SafeInterpreterPath("safe-path",
Evan Cheng49419e22009-07-21 19:25:09 +000072 cl::desc("Specify the path to the \"safe\" backend program"),
73 cl::init(""));
Dan Gohman70ef4492008-12-08 04:02:47 +000074
Brian Gaekec5cad212004-02-11 18:37:32 +000075 cl::opt<bool>
Reid Spencer5e1452c2006-11-28 07:04:10 +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
Chris Lattner3c053a02003-04-23 20:31:37 +000080 cl::opt<std::string>
81 InputFile("input", cl::init("/dev/null"),
82 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000083
84 cl::list<std::string>
85 AdditionalSOs("additional-so",
86 cl::desc("Additional shared objects to load "
87 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000088
Reid Spencer51ab5c82006-06-06 00:00:42 +000089 cl::list<std::string>
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000090 AdditionalLinkerArgs("Xlinker",
Reid Spencer51ab5c82006-06-06 00:00:42 +000091 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikov9ef74252008-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)"));
Chris Lattner4a106452002-12-23 23:50:16 +000097}
98
Brian Gaeked0fde302003-11-11 22:41:34 +000099namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +0000100 // 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>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000104 cl::ZeroOrMore, cl::PositionalEatsArgs);
Daniel Dunbar68ccdaa2009-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 Gohman70ef4492008-12-08 04:02:47 +0000109}
Brian Gaeke636df3d2004-05-04 21:09:16 +0000110
Dan Gohman70ef4492008-12-08 04:02:47 +0000111namespace {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000112 cl::list<std::string>
113 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000114 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman70ef4492008-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 Wendling38efa382009-03-02 23:13:18 +0000120
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000121 cl::opt<std::string>
122 GCCBinary("gcc", cl::init("gcc"),
123 cl::desc("The gcc binary to use. (default 'gcc')"));
124
Bill Wendling38efa382009-03-02 23:13:18 +0000125 cl::list<std::string>
126 GCCToolArgv("gcc-tool-args", cl::Positional,
127 cl::desc("<gcc-tool arguments>..."),
128 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +0000129}
Misha Brukman9d679cb2003-07-30 20:15:44 +0000130
Chris Lattner4a106452002-12-23 23:50:16 +0000131//===----------------------------------------------------------------------===//
132// BugDriver method implementation
133//
134
135/// initializeExecutionEnvironment - This method is used to set up the
136/// environment for executing LLVM programs.
137///
138bool BugDriver::initializeExecutionEnvironment() {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000139 outs() << "Initializing execution environment: ";
Chris Lattner4a106452002-12-23 23:50:16 +0000140
Misha Brukman41485562003-09-29 22:40:52 +0000141 // Create an instance of the AbstractInterpreter interface as specified on
142 // the command line
Dan Gohman70ef4492008-12-08 04:02:47 +0000143 SafeInterpreter = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000144 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000145
Chris Lattnercc876a72003-05-03 03:19:41 +0000146 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000147 case AutoPick:
148 InterpreterSel = RunCBE;
Dan Gohman70ef4492008-12-08 04:02:47 +0000149 Interpreter =
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000150 AbstractInterpreter::createCBE(getToolName(), Message, GCCBinary,
151 &ToolArgv, &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000152 if (!Interpreter) {
153 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000154 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
155 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000156 }
157 if (!Interpreter) {
158 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000159 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000160 GCCBinary, &ToolArgv,
161 &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000162 }
163 if (!Interpreter) {
164 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000165 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
166 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000167 }
168 if (!Interpreter) {
169 InterpreterSel = AutoPick;
170 Message = "Sorry, I can't automatically select an interpreter!\n";
171 }
172 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000173 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000174 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
175 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000176 break;
177 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000178 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000179 case LLC_Safe:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000180 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000181 GCCBinary, &ToolArgv,
182 &GCCToolArgv,
Chris Lattner50010422010-03-16 06:41:47 +0000183 InterpreterSel == RunLLCIA);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000184 break;
185 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000186 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
187 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000188 break;
189 case RunCBE:
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000190 case CBE_bug:
191 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000192 GCCBinary, &ToolArgv,
193 &GCCToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000194 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000195 case Custom:
Dan Gohman197f7282009-08-05 20:21:17 +0000196 Interpreter = AbstractInterpreter::createCustom(Message, CustomExecCommand);
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000197 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000198 default:
Misha Brukman41485562003-09-29 22:40:52 +0000199 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000200 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000201 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000202 if (!Interpreter)
Dan Gohman65f57c22009-07-15 16:35:29 +0000203 errs() << Message;
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000204 else // Display informational messages on stdout instead of stderr
Dan Gohmanac95cc72009-07-16 15:30:09 +0000205 outs() << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000206
Dan Gohman70ef4492008-12-08 04:02:47 +0000207 std::string Path = SafeInterpreterPath;
208 if (Path.empty())
209 Path = getToolName();
210 std::vector<std::string> SafeToolArgs = SafeToolArgv;
211 switch (SafeInterpreterSel) {
212 case AutoPick:
213 // In "cbe-bug" mode, default to using LLC as the "safe" backend.
214 if (!SafeInterpreter &&
215 InterpreterSel == CBE_bug) {
216 SafeInterpreterSel = RunLLC;
217 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000218 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000219 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000220 &SafeToolArgs,
221 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000222 }
223
224 // In "llc-safe" mode, default to using LLC as the "safe" backend.
225 if (!SafeInterpreter &&
226 InterpreterSel == LLC_Safe) {
227 SafeInterpreterSel = RunLLC;
228 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000229 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000230 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000231 &SafeToolArgs,
232 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000233 }
234
235 // Pick a backend that's different from the test backend. The JIT and
236 // LLC backends share a lot of code, so prefer to use the CBE as the
237 // safe back-end when testing them.
238 if (!SafeInterpreter &&
239 InterpreterSel != RunCBE) {
240 SafeInterpreterSel = RunCBE;
Dan Gohman197f7282009-08-05 20:21:17 +0000241 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000242 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000243 &SafeToolArgs,
244 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000245 }
246 if (!SafeInterpreter &&
247 InterpreterSel != RunLLC &&
248 InterpreterSel != RunJIT) {
249 SafeInterpreterSel = RunLLC;
250 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000251 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000252 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000253 &SafeToolArgs,
254 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000255 }
256 if (!SafeInterpreter) {
257 SafeInterpreterSel = AutoPick;
258 Message = "Sorry, I can't automatically select an interpreter!\n";
259 }
260 break;
261 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000262 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000263 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000264 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000265 GCCBinary, &SafeToolArgs,
Chris Lattner50010422010-03-16 06:41:47 +0000266 &GCCToolArgv,
267 SafeInterpreterSel == RunLLCIA);
Dan Gohman70ef4492008-12-08 04:02:47 +0000268 break;
269 case RunCBE:
Dan Gohman197f7282009-08-05 20:21:17 +0000270 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000271 GCCBinary, &SafeToolArgs,
Bill Wendling38efa382009-03-02 23:13:18 +0000272 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000273 break;
274 case Custom:
Dan Gohman197f7282009-08-05 20:21:17 +0000275 SafeInterpreter = AbstractInterpreter::createCustom(Message,
Dan Gohman70ef4492008-12-08 04:02:47 +0000276 CustomExecCommand);
277 break;
278 default:
279 Message = "Sorry, this back-end is not supported by bugpoint as the "
280 "\"safe\" backend right now!\n";
281 break;
Chris Lattner7bb11542004-02-18 20:52:02 +0000282 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000283 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000284
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000285 gcc = GCC::create(Message, GCCBinary, &GCCToolArgv);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000286 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000287
Chris Lattner4a106452002-12-23 23:50:16 +0000288 // If there was an error creating the selected interpreter, quit with error.
289 return Interpreter == 0;
290}
291
Nick Lewycky22ff7482010-04-12 05:08:25 +0000292/// compileProgram - Try to compile the specified module, returning false and
293/// setting Error if an error occurs. This is used for code generation
294/// crash testing.
Chris Lattnerea9212c2004-02-18 23:25:22 +0000295///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000296void BugDriver::compileProgram(Module *M, std::string *Error) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000297 // Emit the program to a bitcode file...
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000298 sys::Path BitcodeFile (OutputPrefix + "-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000299 std::string ErrMsg;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000300 if (BitcodeFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000301 errs() << ToolName << ": Error making unique filename: " << ErrMsg
302 << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000303 exit(1);
304 }
Chris Lattner74382b72009-08-23 22:45:37 +0000305 if (writeProgramToFile(BitcodeFile.str(), M)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000306 errs() << ToolName << ": Error emitting bitcode to file '"
Chris Lattner74382b72009-08-23 22:45:37 +0000307 << BitcodeFile.str() << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000308 exit(1);
309 }
310
Nick Lewycky22ff7482010-04-12 05:08:25 +0000311 // Remove the temporary bitcode file when we are done.
Anton Korobeynikov86c006a2009-08-05 09:32:10 +0000312 FileRemover BitcodeFileRemover(BitcodeFile, !SaveTemps);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000313
314 // Actually compile the program!
Nick Lewycky22ff7482010-04-12 05:08:25 +0000315 Interpreter->compileProgram(BitcodeFile.str(), Error);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000316}
317
Chris Lattner4a106452002-12-23 23:50:16 +0000318
319/// executeProgram - This method runs "Program", capturing the output of the
320/// program to a file, returning the filename of the file. A recommended
321/// filename may be optionally specified.
322///
323std::string BugDriver::executeProgram(std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000324 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000325 const std::string &SharedObj,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000326 AbstractInterpreter *AI,
327 std::string *Error) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000328 if (AI == 0) AI = Interpreter;
329 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000330 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000331 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000332 if (BitcodeFile.empty()) {
333 // Emit the program to a bitcode file...
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000334 sys::Path uniqueFilename(OutputPrefix + "-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000335 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000336 errs() << ToolName << ": Error making unique filename: "
337 << ErrMsg << "!\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000338 exit(1);
339 }
Chris Lattner74382b72009-08-23 22:45:37 +0000340 BitcodeFile = uniqueFilename.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000341
Gabor Greif8ff70c22007-07-04 21:55:50 +0000342 if (writeProgramToFile(BitcodeFile, Program)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000343 errs() << ToolName << ": Error emitting bitcode to file '"
344 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000345 exit(1);
346 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000347 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000348 }
349
Gabor Greif8ff70c22007-07-04 21:55:50 +0000350 // Remove the temporary bitcode file when we are done.
Nick Lewycky16350f82010-04-10 23:18:13 +0000351 sys::Path BitcodePath(BitcodeFile);
Anton Korobeynikov86c006a2009-08-05 09:32:10 +0000352 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
Chris Lattner97092722004-02-18 22:01:21 +0000353
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000354 if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000355
Chris Lattner4a106452002-12-23 23:50:16 +0000356 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000357 sys::Path uniqueFile(OutputFile);
Reid Spencer51c5a282006-08-23 20:34:57 +0000358 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000359 errs() << ToolName << ": Error making unique filename: "
360 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000361 exit(1);
362 }
Chris Lattner74382b72009-08-23 22:45:37 +0000363 OutputFile = uniqueFile.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000364
Chris Lattner769f1fe2003-10-14 21:59:36 +0000365 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000366 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000367 if (!SharedObj.empty())
368 SharedObjs.push_back(SharedObj);
369
Nick Lewycky22ff7482010-04-12 05:08:25 +0000370 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
371 Error, AdditionalLinkerArgs, SharedObjs,
Dan Gohman70ef4492008-12-08 04:02:47 +0000372 Timeout, MemoryLimit);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000373 if (!Error->empty())
374 return OutputFile;
Chris Lattner7d91e492004-07-24 07:53:26 +0000375
376 if (RetVal == -1) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000377 errs() << "<timeout>";
Chris Lattner7d91e492004-07-24 07:53:26 +0000378 static bool FirstTimeout = true;
379 if (FirstTimeout) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000380 outs() << "\n"
Chris Lattner7d91e492004-07-24 07:53:26 +0000381 "*** Program execution timed out! This mechanism is designed to handle\n"
382 " programs stuck in infinite loops gracefully. The -timeout option\n"
383 " can be used to change the timeout threshold or disable it completely\n"
384 " (with -timeout=0). This message is only displayed once.\n";
385 FirstTimeout = false;
386 }
387 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000388
Reid Spencer5e1452c2006-11-28 07:04:10 +0000389 if (AppendProgramExitCode) {
390 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
391 outFile << "exit " << RetVal << '\n';
392 outFile.close();
393 }
394
Chris Lattner4a106452002-12-23 23:50:16 +0000395 // Return the filename we captured the output to.
396 return OutputFile;
397}
398
Dan Gohman70ef4492008-12-08 04:02:47 +0000399/// executeProgramSafely - Used to create reference output with the "safe"
Brian Gaekec5cad212004-02-11 18:37:32 +0000400/// backend, if reference output is not provided.
401///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000402std::string BugDriver::executeProgramSafely(std::string OutputFile,
403 std::string *Error) {
404 return executeProgram(OutputFile, "", "", SafeInterpreter, Error);
Brian Gaekec5cad212004-02-11 18:37:32 +0000405}
Misha Brukman50733362003-07-24 18:17:43 +0000406
Nick Lewycky22ff7482010-04-12 05:08:25 +0000407std::string BugDriver::compileSharedObject(const std::string &BitcodeFile,
408 std::string &Error) {
Misha Brukman50733362003-07-24 18:17:43 +0000409 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000410 sys::Path OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000411
Dan Gohman70ef4492008-12-08 04:02:47 +0000412 // Using the known-good backend.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000413 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile,
414 Error);
415 if (!Error.empty())
416 return "";
Misha Brukman50733362003-07-24 18:17:43 +0000417
Chris Lattnera0f5b152003-10-14 21:09:11 +0000418 std::string SharedObjectFile;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000419 bool Failure = gcc->MakeSharedObject(OutputFile.str(), FT, SharedObjectFile,
420 AdditionalLinkerArgs, Error);
421 if (!Error.empty())
422 return "";
423 if (Failure)
Chris Lattnera0f5b152003-10-14 21:09:11 +0000424 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000425
426 // Remove the intermediate C file
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000427 OutputFile.eraseFromDisk();
Misha Brukman50733362003-07-24 18:17:43 +0000428
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000429 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000430}
431
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000432/// createReferenceFile - calls compileProgram and then records the output
433/// into ReferenceOutputFile. Returns true if reference file created, false
434/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
435/// this function.
436///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000437bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000438 std::string Error;
439 compileProgram(Program, &Error);
440 if (!Error.empty())
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000441 return false;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000442
443 ReferenceOutputFile = executeProgramSafely(Filename, &Error);
444 if (!Error.empty()) {
445 errs() << Error;
Dan Gohman70ef4492008-12-08 04:02:47 +0000446 if (Interpreter != SafeInterpreter) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000447 errs() << "*** There is a bug running the \"safe\" backend. Either"
448 << " debug it (for example with the -run-cbe bugpoint option,"
449 << " if CBE is being used as the \"safe\" backend), or fix the"
450 << " error some other way.\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000451 }
452 return false;
453 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000454 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000455 return true;
456}
Misha Brukman50733362003-07-24 18:17:43 +0000457
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000458/// diffProgram - This method executes the specified module and diffs the
459/// output against the file specified by ReferenceOutputFile. If the output
Nick Lewycky22ff7482010-04-12 05:08:25 +0000460/// is different, 1 is returned. If there is a problem with the code
461/// generator (e.g., llc crashes), this will return -1 and set Error.
Chris Lattner4a106452002-12-23 23:50:16 +0000462///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000463bool BugDriver::diffProgram(const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000464 const std::string &SharedObject,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000465 bool RemoveBitcode,
466 std::string *ErrMsg) {
Chris Lattner4a106452002-12-23 23:50:16 +0000467 // Execute the program, generating an output file...
Nick Lewycky22ff7482010-04-12 05:08:25 +0000468 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0, ErrMsg));
469 if (!ErrMsg->empty())
470 return false;
Brian Gaekec5cad212004-02-11 18:37:32 +0000471
Chris Lattner65f62792003-08-01 20:29:45 +0000472 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000473 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000474 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
Chris Lattner74382b72009-08-23 22:45:37 +0000475 sys::Path(Output.str()),
Chris Lattnera328c512005-01-23 03:45:26 +0000476 AbsTolerance, RelTolerance, &Error)) {
477 if (Diff == 2) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000478 errs() << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000479 exit(1);
480 }
481 FilesDifferent = true;
482 }
David Goodwin80becf12009-07-10 21:39:28 +0000483 else {
484 // Remove the generated output if there are no differences.
485 Output.eraseFromDisk();
486 }
Chris Lattner4a106452002-12-23 23:50:16 +0000487
Gabor Greif8ff70c22007-07-04 21:55:50 +0000488 // Remove the bitcode file if we are supposed to.
489 if (RemoveBitcode)
490 sys::Path(BitcodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000491 return FilesDifferent;
492}
Misha Brukman91eabc12003-07-28 19:16:14 +0000493
494bool BugDriver::isExecutingJIT() {
495 return InterpreterSel == RunJIT;
496}
Brian Gaeked0fde302003-11-11 22:41:34 +0000497