blob: adf5587553efcce2e511cbfd502e3aecf45f739b [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 {
Andrew Trickf73311b2011-02-08 18:20:48 +000031 AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, RunCBE, CBE_bug, LLC_Safe,
32 CompileCustom, Custom
Chris Lattner4a106452002-12-23 23:50:16 +000033 };
Misha Brukman41485562003-09-29 22:40:52 +000034
Chris Lattnera328c512005-01-23 03:45:26 +000035 cl::opt<double>
36 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
37 cl::init(0.0));
38 cl::opt<double>
39 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
40 cl::init(0.0));
41
Chris Lattner4a106452002-12-23 23:50:16 +000042 cl::opt<OutputType>
Dan Gohman70ef4492008-12-08 04:02:47 +000043 InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000044 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukmanb687d822004-04-19 03:12:35 +000045 clEnumValN(RunLLI, "run-int",
46 "Execute with the interpreter"),
Misha Brukman50733362003-07-24 18:17:43 +000047 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
48 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
Chris Lattner50010422010-03-16 06:41:47 +000049 clEnumValN(RunLLCIA, "run-llc-ia",
50 "Compile with LLC with integrated assembler"),
Misha Brukman50733362003-07-24 18:17:43 +000051 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattnerc600f3c2006-09-15 21:29:15 +000052 clEnumValN(CBE_bug,"cbe-bug", "Find CBE bugs"),
Chris Lattnercd6f46e2006-11-09 05:57:53 +000053 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
Andrew Trickf73311b2011-02-08 18:20:48 +000054 clEnumValN(CompileCustom, "compile-custom",
55 "Use -compile-command to define a command to "
56 "compile the bitcode. Useful to avoid linking."),
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000057 clEnumValN(Custom, "run-custom",
58 "Use -exec-command to define a command to execute "
59 "the bitcode. Useful for cross-compilation."),
Chris Lattner4d143ee2004-07-16 00:08:28 +000060 clEnumValEnd),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000061 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000062
Dan Gohman70ef4492008-12-08 04:02:47 +000063 cl::opt<OutputType>
64 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
Evan Cheng49419e22009-07-21 19:25:09 +000065 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
66 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
67 clEnumValN(RunCBE, "safe-run-cbe", "Compile with CBE"),
68 clEnumValN(Custom, "safe-run-custom",
69 "Use -exec-command to define a command to execute "
70 "the bitcode. Useful for cross-compilation."),
71 clEnumValEnd),
Dan Gohman70ef4492008-12-08 04:02:47 +000072 cl::init(AutoPick));
73
74 cl::opt<std::string>
75 SafeInterpreterPath("safe-path",
Evan Cheng49419e22009-07-21 19:25:09 +000076 cl::desc("Specify the path to the \"safe\" backend program"),
77 cl::init(""));
Dan Gohman70ef4492008-12-08 04:02:47 +000078
Brian Gaekec5cad212004-02-11 18:37:32 +000079 cl::opt<bool>
Reid Spencer5e1452c2006-11-28 07:04:10 +000080 AppendProgramExitCode("append-exit-code",
81 cl::desc("Append the exit code to the output so it gets diff'd too"),
82 cl::init(false));
83
Chris Lattner3c053a02003-04-23 20:31:37 +000084 cl::opt<std::string>
85 InputFile("input", cl::init("/dev/null"),
86 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000087
88 cl::list<std::string>
89 AdditionalSOs("additional-so",
90 cl::desc("Additional shared objects to load "
91 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000092
Reid Spencer51ab5c82006-06-06 00:00:42 +000093 cl::list<std::string>
Andrew Trickde86cbd2011-02-08 18:07:10 +000094 AdditionalLinkerArgs("Xlinker",
Reid Spencer51ab5c82006-06-06 00:00:42 +000095 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000096
97 cl::opt<std::string>
Andrew Trickf73311b2011-02-08 18:20:48 +000098 CustomCompileCommand("compile-command", cl::init("llc"),
99 cl::desc("Command to compile the bitcode (use with -compile-custom) "
100 "(default: llc)"));
101
102 cl::opt<std::string>
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000103 CustomExecCommand("exec-command", cl::init("simulate"),
104 cl::desc("Command to execute the bitcode (use with -run-custom) "
105 "(default: simulate)"));
Chris Lattner4a106452002-12-23 23:50:16 +0000106}
107
Brian Gaeked0fde302003-11-11 22:41:34 +0000108namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +0000109 // Anything specified after the --args option are taken as arguments to the
110 // program being debugged.
111 cl::list<std::string>
112 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000113 cl::ZeroOrMore, cl::PositionalEatsArgs);
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000114
115 cl::opt<std::string>
116 OutputPrefix("output-prefix", cl::init("bugpoint"),
117 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
Dan Gohman70ef4492008-12-08 04:02:47 +0000118}
Brian Gaeke636df3d2004-05-04 21:09:16 +0000119
Dan Gohman70ef4492008-12-08 04:02:47 +0000120namespace {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000121 cl::list<std::string>
122 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000123 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman70ef4492008-12-08 04:02:47 +0000124
125 cl::list<std::string>
126 SafeToolArgv("safe-tool-args", cl::Positional,
127 cl::desc("<safe-tool arguments>..."),
128 cl::ZeroOrMore, cl::PositionalEatsArgs);
Bill Wendling38efa382009-03-02 23:13:18 +0000129
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000130 cl::opt<std::string>
Andrew Trickde86cbd2011-02-08 18:07:10 +0000131 GCCBinary("gcc", cl::init("gcc"),
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000132 cl::desc("The gcc binary to use. (default 'gcc')"));
133
Bill Wendling38efa382009-03-02 23:13:18 +0000134 cl::list<std::string>
135 GCCToolArgv("gcc-tool-args", cl::Positional,
136 cl::desc("<gcc-tool arguments>..."),
137 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +0000138}
Misha Brukman9d679cb2003-07-30 20:15:44 +0000139
Chris Lattner4a106452002-12-23 23:50:16 +0000140//===----------------------------------------------------------------------===//
141// BugDriver method implementation
142//
143
144/// initializeExecutionEnvironment - This method is used to set up the
145/// environment for executing LLVM programs.
146///
147bool BugDriver::initializeExecutionEnvironment() {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000148 outs() << "Initializing execution environment: ";
Chris Lattner4a106452002-12-23 23:50:16 +0000149
Misha Brukman41485562003-09-29 22:40:52 +0000150 // Create an instance of the AbstractInterpreter interface as specified on
151 // the command line
Dan Gohman70ef4492008-12-08 04:02:47 +0000152 SafeInterpreter = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000153 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000154
Chris Lattnercc876a72003-05-03 03:19:41 +0000155 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000156 case AutoPick:
157 InterpreterSel = RunCBE;
Dan Gohman70ef4492008-12-08 04:02:47 +0000158 Interpreter =
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000159 AbstractInterpreter::createCBE(getToolName(), Message, GCCBinary,
160 &ToolArgv, &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000161 if (!Interpreter) {
162 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000163 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
164 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000165 }
166 if (!Interpreter) {
167 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000168 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000169 GCCBinary, &ToolArgv,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000170 &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000171 }
172 if (!Interpreter) {
173 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000174 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
175 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000176 }
177 if (!Interpreter) {
178 InterpreterSel = AutoPick;
179 Message = "Sorry, I can't automatically select an interpreter!\n";
180 }
181 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000182 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000183 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
184 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000185 break;
186 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000187 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000188 case LLC_Safe:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000189 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000190 GCCBinary, &ToolArgv,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000191 &GCCToolArgv,
Chris Lattner50010422010-03-16 06:41:47 +0000192 InterpreterSel == RunLLCIA);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000193 break;
194 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000195 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
196 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000197 break;
198 case RunCBE:
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000199 case CBE_bug:
200 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000201 GCCBinary, &ToolArgv,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000202 &GCCToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000203 break;
Andrew Trickf73311b2011-02-08 18:20:48 +0000204 case CompileCustom:
205 Interpreter =
206 AbstractInterpreter::createCustomCompiler(Message, CustomCompileCommand);
207 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000208 case Custom:
Andrew Trickf73311b2011-02-08 18:20:48 +0000209 Interpreter =
210 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000211 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000212 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000213 if (!Interpreter)
Dan Gohman65f57c22009-07-15 16:35:29 +0000214 errs() << Message;
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000215 else // Display informational messages on stdout instead of stderr
Dan Gohmanac95cc72009-07-16 15:30:09 +0000216 outs() << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000217
Dan Gohman70ef4492008-12-08 04:02:47 +0000218 std::string Path = SafeInterpreterPath;
219 if (Path.empty())
220 Path = getToolName();
221 std::vector<std::string> SafeToolArgs = SafeToolArgv;
222 switch (SafeInterpreterSel) {
223 case AutoPick:
224 // In "cbe-bug" mode, default to using LLC as the "safe" backend.
225 if (!SafeInterpreter &&
226 InterpreterSel == CBE_bug) {
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,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000230 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000231 &SafeToolArgs,
232 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000233 }
234
235 // In "llc-safe" mode, default to using LLC as the "safe" backend.
236 if (!SafeInterpreter &&
237 InterpreterSel == LLC_Safe) {
238 SafeInterpreterSel = RunLLC;
239 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000240 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000241 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000242 &SafeToolArgs,
243 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000244 }
245
246 // Pick a backend that's different from the test backend. The JIT and
247 // LLC backends share a lot of code, so prefer to use the CBE as the
248 // safe back-end when testing them.
249 if (!SafeInterpreter &&
250 InterpreterSel != RunCBE) {
251 SafeInterpreterSel = RunCBE;
Dan Gohman197f7282009-08-05 20:21:17 +0000252 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000253 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000254 &SafeToolArgs,
255 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000256 }
257 if (!SafeInterpreter &&
258 InterpreterSel != RunLLC &&
259 InterpreterSel != RunJIT) {
260 SafeInterpreterSel = RunLLC;
261 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000262 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000263 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000264 &SafeToolArgs,
265 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000266 }
267 if (!SafeInterpreter) {
268 SafeInterpreterSel = AutoPick;
269 Message = "Sorry, I can't automatically select an interpreter!\n";
270 }
271 break;
272 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000273 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000274 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000275 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000276 GCCBinary, &SafeToolArgs,
Chris Lattner50010422010-03-16 06:41:47 +0000277 &GCCToolArgv,
278 SafeInterpreterSel == RunLLCIA);
Dan Gohman70ef4492008-12-08 04:02:47 +0000279 break;
280 case RunCBE:
Dan Gohman197f7282009-08-05 20:21:17 +0000281 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000282 GCCBinary, &SafeToolArgs,
Bill Wendling38efa382009-03-02 23:13:18 +0000283 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000284 break;
285 case Custom:
Andrew Trickf73311b2011-02-08 18:20:48 +0000286 SafeInterpreter =
287 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Dan Gohman70ef4492008-12-08 04:02:47 +0000288 break;
289 default:
290 Message = "Sorry, this back-end is not supported by bugpoint as the "
291 "\"safe\" backend right now!\n";
292 break;
Chris Lattner7bb11542004-02-18 20:52:02 +0000293 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000294 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
Andrew Trickde86cbd2011-02-08 18:07:10 +0000295
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000296 gcc = GCC::create(Message, GCCBinary, &GCCToolArgv);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000297 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000298
Chris Lattner4a106452002-12-23 23:50:16 +0000299 // If there was an error creating the selected interpreter, quit with error.
300 return Interpreter == 0;
301}
302
Nick Lewycky22ff7482010-04-12 05:08:25 +0000303/// compileProgram - Try to compile the specified module, returning false and
304/// setting Error if an error occurs. This is used for code generation
305/// crash testing.
Chris Lattnerea9212c2004-02-18 23:25:22 +0000306///
Rafael Espindola248d1c62010-08-05 03:00:22 +0000307void BugDriver::compileProgram(Module *M, std::string *Error) const {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000308 // Emit the program to a bitcode file...
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000309 sys::Path BitcodeFile (OutputPrefix + "-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000310 std::string ErrMsg;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000311 if (BitcodeFile.makeUnique(true, &ErrMsg)) {
Andrew Trickde86cbd2011-02-08 18:07:10 +0000312 errs() << ToolName << ": Error making unique filename: " << ErrMsg
Dan Gohman65f57c22009-07-15 16:35:29 +0000313 << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000314 exit(1);
315 }
Chris Lattner74382b72009-08-23 22:45:37 +0000316 if (writeProgramToFile(BitcodeFile.str(), M)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000317 errs() << ToolName << ": Error emitting bitcode to file '"
Chris Lattner74382b72009-08-23 22:45:37 +0000318 << BitcodeFile.str() << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000319 exit(1);
320 }
321
Nick Lewycky22ff7482010-04-12 05:08:25 +0000322 // Remove the temporary bitcode file when we are done.
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000323 FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000324
325 // Actually compile the program!
Duncan Sands41396302010-05-24 07:49:55 +0000326 Interpreter->compileProgram(BitcodeFile.str(), Error, Timeout, MemoryLimit);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000327}
328
Chris Lattner4a106452002-12-23 23:50:16 +0000329
330/// executeProgram - This method runs "Program", capturing the output of the
331/// program to a file, returning the filename of the file. A recommended
332/// filename may be optionally specified.
333///
Rafael Espindola10757dd2010-07-30 14:19:00 +0000334std::string BugDriver::executeProgram(const Module *Program,
335 std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000336 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000337 const std::string &SharedObj,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000338 AbstractInterpreter *AI,
Rafael Espindola13793262010-07-31 14:34:49 +0000339 std::string *Error) const {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000340 if (AI == 0) AI = Interpreter;
341 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000342 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000343 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000344 if (BitcodeFile.empty()) {
345 // Emit the program to a bitcode file...
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000346 sys::Path uniqueFilename(OutputPrefix + "-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000347 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000348 errs() << ToolName << ": Error making unique filename: "
349 << ErrMsg << "!\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000350 exit(1);
351 }
Chris Lattner74382b72009-08-23 22:45:37 +0000352 BitcodeFile = uniqueFilename.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000353
Gabor Greif8ff70c22007-07-04 21:55:50 +0000354 if (writeProgramToFile(BitcodeFile, Program)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000355 errs() << ToolName << ": Error emitting bitcode to file '"
356 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000357 exit(1);
358 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000359 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000360 }
361
Gabor Greif8ff70c22007-07-04 21:55:50 +0000362 // Remove the temporary bitcode file when we are done.
Nick Lewycky16350f82010-04-10 23:18:13 +0000363 sys::Path BitcodePath(BitcodeFile);
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000364 FileRemover BitcodeFileRemover(BitcodePath.str(),
365 CreatedBitcode && !SaveTemps);
Chris Lattner97092722004-02-18 22:01:21 +0000366
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000367 if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000368
Chris Lattner4a106452002-12-23 23:50:16 +0000369 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000370 sys::Path uniqueFile(OutputFile);
Reid Spencer51c5a282006-08-23 20:34:57 +0000371 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000372 errs() << ToolName << ": Error making unique filename: "
373 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000374 exit(1);
375 }
Chris Lattner74382b72009-08-23 22:45:37 +0000376 OutputFile = uniqueFile.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000377
Chris Lattner769f1fe2003-10-14 21:59:36 +0000378 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000379 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000380 if (!SharedObj.empty())
381 SharedObjs.push_back(SharedObj);
382
Nick Lewycky22ff7482010-04-12 05:08:25 +0000383 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
384 Error, AdditionalLinkerArgs, SharedObjs,
Dan Gohman70ef4492008-12-08 04:02:47 +0000385 Timeout, MemoryLimit);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000386 if (!Error->empty())
387 return OutputFile;
Chris Lattner7d91e492004-07-24 07:53:26 +0000388
389 if (RetVal == -1) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000390 errs() << "<timeout>";
Chris Lattner7d91e492004-07-24 07:53:26 +0000391 static bool FirstTimeout = true;
392 if (FirstTimeout) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000393 outs() << "\n"
Chris Lattner7d91e492004-07-24 07:53:26 +0000394 "*** Program execution timed out! This mechanism is designed to handle\n"
395 " programs stuck in infinite loops gracefully. The -timeout option\n"
396 " can be used to change the timeout threshold or disable it completely\n"
397 " (with -timeout=0). This message is only displayed once.\n";
398 FirstTimeout = false;
399 }
400 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000401
Reid Spencer5e1452c2006-11-28 07:04:10 +0000402 if (AppendProgramExitCode) {
403 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
404 outFile << "exit " << RetVal << '\n';
405 outFile.close();
406 }
407
Chris Lattner4a106452002-12-23 23:50:16 +0000408 // Return the filename we captured the output to.
409 return OutputFile;
410}
411
Dan Gohman70ef4492008-12-08 04:02:47 +0000412/// executeProgramSafely - Used to create reference output with the "safe"
Brian Gaekec5cad212004-02-11 18:37:32 +0000413/// backend, if reference output is not provided.
414///
Rafael Espindola10757dd2010-07-30 14:19:00 +0000415std::string BugDriver::executeProgramSafely(const Module *Program,
416 std::string OutputFile,
Rafael Espindola13793262010-07-31 14:34:49 +0000417 std::string *Error) const {
Rafael Espindola10757dd2010-07-30 14:19:00 +0000418 return executeProgram(Program, OutputFile, "", "", SafeInterpreter, Error);
Brian Gaekec5cad212004-02-11 18:37:32 +0000419}
Misha Brukman50733362003-07-24 18:17:43 +0000420
Nick Lewycky22ff7482010-04-12 05:08:25 +0000421std::string BugDriver::compileSharedObject(const std::string &BitcodeFile,
422 std::string &Error) {
Misha Brukman50733362003-07-24 18:17:43 +0000423 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000424 sys::Path OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000425
Dan Gohman70ef4492008-12-08 04:02:47 +0000426 // Using the known-good backend.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000427 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile,
428 Error);
429 if (!Error.empty())
430 return "";
Misha Brukman50733362003-07-24 18:17:43 +0000431
Chris Lattnera0f5b152003-10-14 21:09:11 +0000432 std::string SharedObjectFile;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000433 bool Failure = gcc->MakeSharedObject(OutputFile.str(), FT, SharedObjectFile,
434 AdditionalLinkerArgs, Error);
435 if (!Error.empty())
436 return "";
437 if (Failure)
Chris Lattnera0f5b152003-10-14 21:09:11 +0000438 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000439
440 // Remove the intermediate C file
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000441 OutputFile.eraseFromDisk();
Misha Brukman50733362003-07-24 18:17:43 +0000442
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000443 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000444}
445
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000446/// createReferenceFile - calls compileProgram and then records the output
Andrew Trickde86cbd2011-02-08 18:07:10 +0000447/// into ReferenceOutputFile. Returns true if reference file created, false
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000448/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
449/// this function.
450///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000451bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000452 std::string Error;
453 compileProgram(Program, &Error);
454 if (!Error.empty())
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000455 return false;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000456
Rafael Espindola10757dd2010-07-30 14:19:00 +0000457 ReferenceOutputFile = executeProgramSafely(Program, Filename, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000458 if (!Error.empty()) {
459 errs() << Error;
Dan Gohman70ef4492008-12-08 04:02:47 +0000460 if (Interpreter != SafeInterpreter) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000461 errs() << "*** There is a bug running the \"safe\" backend. Either"
462 << " debug it (for example with the -run-cbe bugpoint option,"
463 << " if CBE is being used as the \"safe\" backend), or fix the"
464 << " error some other way.\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000465 }
466 return false;
467 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000468 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000469 return true;
470}
Misha Brukman50733362003-07-24 18:17:43 +0000471
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000472/// diffProgram - This method executes the specified module and diffs the
473/// output against the file specified by ReferenceOutputFile. If the output
Nick Lewycky22ff7482010-04-12 05:08:25 +0000474/// is different, 1 is returned. If there is a problem with the code
Andrew Trick7c863eb2011-05-11 16:31:24 +0000475/// generator (e.g., llc crashes), this will set ErrMsg.
Chris Lattner4a106452002-12-23 23:50:16 +0000476///
Rafael Espindola10757dd2010-07-30 14:19:00 +0000477bool BugDriver::diffProgram(const Module *Program,
478 const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000479 const std::string &SharedObject,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000480 bool RemoveBitcode,
Rafael Espindola13793262010-07-31 14:34:49 +0000481 std::string *ErrMsg) const {
Chris Lattner4a106452002-12-23 23:50:16 +0000482 // Execute the program, generating an output file...
Rafael Espindola10757dd2010-07-30 14:19:00 +0000483 sys::Path Output(executeProgram(Program, "", BitcodeFile, SharedObject, 0,
484 ErrMsg));
Nick Lewycky22ff7482010-04-12 05:08:25 +0000485 if (!ErrMsg->empty())
486 return false;
Brian Gaekec5cad212004-02-11 18:37:32 +0000487
Chris Lattner65f62792003-08-01 20:29:45 +0000488 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000489 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000490 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
Chris Lattner74382b72009-08-23 22:45:37 +0000491 sys::Path(Output.str()),
Chris Lattnera328c512005-01-23 03:45:26 +0000492 AbsTolerance, RelTolerance, &Error)) {
493 if (Diff == 2) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000494 errs() << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000495 exit(1);
496 }
497 FilesDifferent = true;
498 }
David Goodwin80becf12009-07-10 21:39:28 +0000499 else {
500 // Remove the generated output if there are no differences.
501 Output.eraseFromDisk();
502 }
Chris Lattner4a106452002-12-23 23:50:16 +0000503
Gabor Greif8ff70c22007-07-04 21:55:50 +0000504 // Remove the bitcode file if we are supposed to.
505 if (RemoveBitcode)
506 sys::Path(BitcodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000507 return FilesDifferent;
508}
Misha Brukman91eabc12003-07-28 19:16:14 +0000509
510bool BugDriver::isExecutingJIT() {
511 return InterpreterSel == RunJIT;
512}
Brian Gaeked0fde302003-11-11 22:41:34 +0000513