blob: b3c802032d5a7d07206f42f5567329492b135383 [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-12-23 23:50:16 +00009//
10// This file contains code used to execute the program utilizing one of the
Gabor Greif8ff70c22007-07-04 21:55:50 +000011// various ways of running LLVM bitcode.
Chris Lattner4a106452002-12-23 23:50:16 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4a106452002-12-23 23:50:16 +000015#include "BugDriver.h"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000016#include "ToolRunner.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
20#include "llvm/Support/SystemUtils.h"
Chris Lattner4a106452002-12-23 23:50:16 +000021#include <fstream>
Chris Lattnere31a9cc2006-01-22 22:53:40 +000022#include <iostream>
Reid Spencer51ab5c82006-06-06 00:00:42 +000023
Brian Gaeked0fde302003-11-11 22:41:34 +000024using namespace llvm;
25
Chris Lattner4a106452002-12-23 23:50:16 +000026namespace {
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
29 //
30 enum OutputType {
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000031 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE, CBE_bug, LLC_Safe, Custom
Chris Lattner4a106452002-12-23 23:50:16 +000032 };
Misha Brukman41485562003-09-29 22:40:52 +000033
Chris Lattnera328c512005-01-23 03:45:26 +000034 cl::opt<double>
35 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
36 cl::init(0.0));
37 cl::opt<double>
38 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
39 cl::init(0.0));
40
Chris Lattner4a106452002-12-23 23:50:16 +000041 cl::opt<OutputType>
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:"),
59 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),
66 cl::init(AutoPick));
67
68 cl::opt<std::string>
69 SafeInterpreterPath("safe-path",
70 cl::desc("Specify the path to the \"safe\" backend program"),
71 cl::init(""));
72
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);
Dan Gohman70ef4492008-12-08 04:02:47 +0000103}
Brian Gaeke636df3d2004-05-04 21:09:16 +0000104
Dan Gohman70ef4492008-12-08 04:02:47 +0000105namespace {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000106 cl::list<std::string>
107 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000108 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman70ef4492008-12-08 04:02:47 +0000109
110 cl::list<std::string>
111 SafeToolArgv("safe-tool-args", cl::Positional,
112 cl::desc("<safe-tool arguments>..."),
113 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +0000114}
Misha Brukman9d679cb2003-07-30 20:15:44 +0000115
Chris Lattner4a106452002-12-23 23:50:16 +0000116//===----------------------------------------------------------------------===//
117// BugDriver method implementation
118//
119
120/// initializeExecutionEnvironment - This method is used to set up the
121/// environment for executing LLVM programs.
122///
123bool BugDriver::initializeExecutionEnvironment() {
124 std::cout << "Initializing execution environment: ";
125
Misha Brukman41485562003-09-29 22:40:52 +0000126 // Create an instance of the AbstractInterpreter interface as specified on
127 // the command line
Dan Gohman70ef4492008-12-08 04:02:47 +0000128 SafeInterpreter = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000129 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000130
Chris Lattnercc876a72003-05-03 03:19:41 +0000131 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000132 case AutoPick:
133 InterpreterSel = RunCBE;
Dan Gohman70ef4492008-12-08 04:02:47 +0000134 Interpreter =
135 AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000136 if (!Interpreter) {
137 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000138 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
139 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000140 }
141 if (!Interpreter) {
142 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000143 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
144 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000145 }
146 if (!Interpreter) {
147 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000148 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
149 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000150 }
151 if (!Interpreter) {
152 InterpreterSel = AutoPick;
153 Message = "Sorry, I can't automatically select an interpreter!\n";
154 }
155 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000156 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000157 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
158 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000159 break;
160 case RunLLC:
Dan Gohman70ef4492008-12-08 04:02:47 +0000161 case LLC_Safe:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000162 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
163 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000164 break;
165 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000166 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
167 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000168 break;
169 case RunCBE:
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000170 case CBE_bug:
171 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
172 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000173 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000174 case Custom:
175 Interpreter = AbstractInterpreter::createCustom(getToolName(), Message,
176 CustomExecCommand);
177 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000178 default:
Misha Brukman41485562003-09-29 22:40:52 +0000179 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000180 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000181 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000182 if (!Interpreter)
183 std::cerr << Message;
184 else // Display informational messages on stdout instead of stderr
185 std::cout << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000186
Dan Gohman70ef4492008-12-08 04:02:47 +0000187 std::string Path = SafeInterpreterPath;
188 if (Path.empty())
189 Path = getToolName();
190 std::vector<std::string> SafeToolArgs = SafeToolArgv;
191 switch (SafeInterpreterSel) {
192 case AutoPick:
193 // In "cbe-bug" mode, default to using LLC as the "safe" backend.
194 if (!SafeInterpreter &&
195 InterpreterSel == CBE_bug) {
196 SafeInterpreterSel = RunLLC;
197 SafeToolArgs.push_back("--relocation-model=pic");
198 SafeInterpreter = AbstractInterpreter::createLLC(Path, Message,
199 &SafeToolArgs);
200 }
201
202 // In "llc-safe" mode, default to using LLC as the "safe" backend.
203 if (!SafeInterpreter &&
204 InterpreterSel == LLC_Safe) {
205 SafeInterpreterSel = RunLLC;
206 SafeToolArgs.push_back("--relocation-model=pic");
207 SafeInterpreter = AbstractInterpreter::createLLC(Path, Message,
208 &SafeToolArgs);
209 }
210
211 // Pick a backend that's different from the test backend. The JIT and
212 // LLC backends share a lot of code, so prefer to use the CBE as the
213 // safe back-end when testing them.
214 if (!SafeInterpreter &&
215 InterpreterSel != RunCBE) {
216 SafeInterpreterSel = RunCBE;
217 SafeInterpreter = AbstractInterpreter::createCBE(Path, Message,
218 &SafeToolArgs);
219 }
220 if (!SafeInterpreter &&
221 InterpreterSel != RunLLC &&
222 InterpreterSel != RunJIT) {
223 SafeInterpreterSel = RunLLC;
224 SafeToolArgs.push_back("--relocation-model=pic");
225 SafeInterpreter = AbstractInterpreter::createLLC(Path, Message,
226 &SafeToolArgs);
227 }
228 if (!SafeInterpreter) {
229 SafeInterpreterSel = AutoPick;
230 Message = "Sorry, I can't automatically select an interpreter!\n";
231 }
232 break;
233 case RunLLC:
234 SafeToolArgs.push_back("--relocation-model=pic");
235 SafeInterpreter = AbstractInterpreter::createLLC(Path, Message,
236 &SafeToolArgs);
237 break;
238 case RunCBE:
239 SafeInterpreter = AbstractInterpreter::createCBE(Path, Message,
240 &SafeToolArgs);
241 break;
242 case Custom:
243 SafeInterpreter = AbstractInterpreter::createCustom(Path, Message,
244 CustomExecCommand);
245 break;
246 default:
247 Message = "Sorry, this back-end is not supported by bugpoint as the "
248 "\"safe\" backend right now!\n";
249 break;
Chris Lattner7bb11542004-02-18 20:52:02 +0000250 }
Dan Gohman70ef4492008-12-08 04:02:47 +0000251 if (!SafeInterpreter) { std::cout << Message << "\nExiting.\n"; exit(1); }
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000252
Chris Lattner769f1fe2003-10-14 21:59:36 +0000253 gcc = GCC::create(getToolName(), Message);
Misha Brukmana259c9b2003-07-24 21:59:10 +0000254 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
255
Chris Lattner4a106452002-12-23 23:50:16 +0000256 // If there was an error creating the selected interpreter, quit with error.
257 return Interpreter == 0;
258}
259
Chris Lattnerea9212c2004-02-18 23:25:22 +0000260/// compileProgram - Try to compile the specified module, throwing an exception
261/// if an error occurs, or returning normally if not. This is used for code
262/// generation crash testing.
263///
264void BugDriver::compileProgram(Module *M) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000265 // Emit the program to a bitcode file...
266 sys::Path BitcodeFile ("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000267 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000268 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
Reid Spencer51c5a282006-08-23 20:34:57 +0000269 std::cerr << ToolName << ": Error making unique filename: " << ErrMsg
270 << "\n";
271 exit(1);
272 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000273 if (writeProgramToFile(BitcodeFile.toString(), M)) {
274 std::cerr << ToolName << ": Error emitting bitcode to file '"
275 << BitcodeFile << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000276 exit(1);
277 }
278
Gabor Greif8ff70c22007-07-04 21:55:50 +0000279 // Remove the temporary bitcode file when we are done.
280 FileRemover BitcodeFileRemover(BitcodeFile);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000281
282 // Actually compile the program!
Gabor Greif8ff70c22007-07-04 21:55:50 +0000283 Interpreter->compileProgram(BitcodeFile.toString());
Chris Lattnerea9212c2004-02-18 23:25:22 +0000284}
285
Chris Lattner4a106452002-12-23 23:50:16 +0000286
287/// executeProgram - This method runs "Program", capturing the output of the
288/// program to a file, returning the filename of the file. A recommended
289/// filename may be optionally specified.
290///
291std::string BugDriver::executeProgram(std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000292 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000293 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000294 AbstractInterpreter *AI,
295 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000296 if (AI == 0) AI = Interpreter;
297 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000298 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000299 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000300 if (BitcodeFile.empty()) {
301 // Emit the program to a bitcode file...
Reid Spencer97182982004-12-15 01:53:08 +0000302 sys::Path uniqueFilename("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000303 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
304 std::cerr << ToolName << ": Error making unique filename: "
305 << ErrMsg << "!\n";
306 exit(1);
307 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000308 BitcodeFile = uniqueFilename.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000309
Gabor Greif8ff70c22007-07-04 21:55:50 +0000310 if (writeProgramToFile(BitcodeFile, Program)) {
311 std::cerr << ToolName << ": Error emitting bitcode to file '"
312 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000313 exit(1);
314 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000315 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000316 }
317
Gabor Greif8ff70c22007-07-04 21:55:50 +0000318 // Remove the temporary bitcode file when we are done.
319 sys::Path BitcodePath (BitcodeFile);
320 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode);
Chris Lattner97092722004-02-18 22:01:21 +0000321
Chris Lattner4a106452002-12-23 23:50:16 +0000322 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000323
Chris Lattner4a106452002-12-23 23:50:16 +0000324 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000325 sys::Path uniqueFile(OutputFile);
Reid Spencer51c5a282006-08-23 20:34:57 +0000326 if (uniqueFile.makeUnique(true, &ErrMsg)) {
327 std::cerr << ToolName << ": Error making unique filename: "
328 << ErrMsg << "\n";
329 exit(1);
330 }
Reid Spencer97182982004-12-15 01:53:08 +0000331 OutputFile = uniqueFile.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000332
Chris Lattner769f1fe2003-10-14 21:59:36 +0000333 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000334 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000335 if (!SharedObj.empty())
336 SharedObjs.push_back(SharedObj);
337
Dan Gohman70ef4492008-12-08 04:02:47 +0000338 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
339 OutputFile, AdditionalLinkerArgs, SharedObjs,
340 Timeout, MemoryLimit);
Chris Lattner7d91e492004-07-24 07:53:26 +0000341
342 if (RetVal == -1) {
343 std::cerr << "<timeout>";
344 static bool FirstTimeout = true;
345 if (FirstTimeout) {
346 std::cout << "\n"
347 "*** Program execution timed out! This mechanism is designed to handle\n"
348 " programs stuck in infinite loops gracefully. The -timeout option\n"
349 " can be used to change the timeout threshold or disable it completely\n"
350 " (with -timeout=0). This message is only displayed once.\n";
351 FirstTimeout = false;
352 }
353 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000354
Reid Spencer5e1452c2006-11-28 07:04:10 +0000355 if (AppendProgramExitCode) {
356 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
357 outFile << "exit " << RetVal << '\n';
358 outFile.close();
359 }
360
Brian Gaekec5cad212004-02-11 18:37:32 +0000361 if (ProgramExitedNonzero != 0)
362 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000363
Chris Lattner4a106452002-12-23 23:50:16 +0000364 // Return the filename we captured the output to.
365 return OutputFile;
366}
367
Dan Gohman70ef4492008-12-08 04:02:47 +0000368/// executeProgramSafely - Used to create reference output with the "safe"
Brian Gaekec5cad212004-02-11 18:37:32 +0000369/// backend, if reference output is not provided.
370///
Dan Gohman70ef4492008-12-08 04:02:47 +0000371std::string BugDriver::executeProgramSafely(std::string OutputFile) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000372 bool ProgramExitedNonzero;
Dan Gohman70ef4492008-12-08 04:02:47 +0000373 std::string outFN = executeProgram(OutputFile, "", "", SafeInterpreter,
Brian Gaekec5cad212004-02-11 18:37:32 +0000374 &ProgramExitedNonzero);
Brian Gaekec5cad212004-02-11 18:37:32 +0000375 return outFN;
376}
Misha Brukman50733362003-07-24 18:17:43 +0000377
Gabor Greif8ff70c22007-07-04 21:55:50 +0000378std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000379 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000380 sys::Path OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000381
Dan Gohman70ef4492008-12-08 04:02:47 +0000382 // Using the known-good backend.
383 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
Misha Brukman50733362003-07-24 18:17:43 +0000384
Chris Lattnera0f5b152003-10-14 21:09:11 +0000385 std::string SharedObjectFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000386 if (gcc->MakeSharedObject(OutputFile.toString(), FT,
Chris Lattner130e2a32006-06-27 20:35:36 +0000387 SharedObjectFile, AdditionalLinkerArgs))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000388 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000389
390 // Remove the intermediate C file
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000391 OutputFile.eraseFromDisk();
Misha Brukman50733362003-07-24 18:17:43 +0000392
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000393 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000394}
395
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000396/// createReferenceFile - calls compileProgram and then records the output
397/// into ReferenceOutputFile. Returns true if reference file created, false
398/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
399/// this function.
400///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000401bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000402 try {
403 compileProgram(Program);
Jeff Cohend41b30d2006-11-05 19:31:28 +0000404 } catch (ToolExecutionError &) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000405 return false;
406 }
407 try {
Dan Gohman70ef4492008-12-08 04:02:47 +0000408 ReferenceOutputFile = executeProgramSafely(Filename);
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000409 std::cout << "Reference output is: " << ReferenceOutputFile << "\n\n";
410 } catch (ToolExecutionError &TEE) {
411 std::cerr << TEE.what();
Dan Gohman70ef4492008-12-08 04:02:47 +0000412 if (Interpreter != SafeInterpreter) {
413 std::cerr << "*** There is a bug running the \"safe\" backend. Either"
414 << " debug it (for example with the -run-cbe bugpoint option,"
415 << " if CBE is being used as the \"safe\" backend), or fix the"
416 << " error some other way.\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000417 }
418 return false;
419 }
420 return true;
421}
Misha Brukman50733362003-07-24 18:17:43 +0000422
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000423/// diffProgram - This method executes the specified module and diffs the
424/// output against the file specified by ReferenceOutputFile. If the output
425/// is different, true is returned. If there is a problem with the code
426/// generator (e.g., llc crashes), this will throw an exception.
Chris Lattner4a106452002-12-23 23:50:16 +0000427///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000428bool BugDriver::diffProgram(const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000429 const std::string &SharedObject,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000430 bool RemoveBitcode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000431 bool ProgramExitedNonzero;
432
Chris Lattner4a106452002-12-23 23:50:16 +0000433 // Execute the program, generating an output file...
Gabor Greif8ff70c22007-07-04 21:55:50 +0000434 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0,
Reid Spencer5f767602004-12-16 23:04:20 +0000435 &ProgramExitedNonzero));
Brian Gaekec5cad212004-02-11 18:37:32 +0000436
Chris Lattner65f62792003-08-01 20:29:45 +0000437 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000438 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000439 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
440 sys::Path(Output.toString()),
441 AbsTolerance, RelTolerance, &Error)) {
442 if (Diff == 2) {
Misha Brukmaneed80e22004-07-23 01:30:49 +0000443 std::cerr << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000444 exit(1);
445 }
446 FilesDifferent = true;
447 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000448
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000449 // Remove the generated output.
Reid Spencera229c5c2005-07-08 03:08:58 +0000450 Output.eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000451
Gabor Greif8ff70c22007-07-04 21:55:50 +0000452 // Remove the bitcode file if we are supposed to.
453 if (RemoveBitcode)
454 sys::Path(BitcodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000455 return FilesDifferent;
456}
Misha Brukman91eabc12003-07-28 19:16:14 +0000457
458bool BugDriver::isExecutingJIT() {
459 return InterpreterSel == RunJIT;
460}
Brian Gaeked0fde302003-11-11 22:41:34 +0000461