blob: 7d6c4aaaae295f3694440d4dcab4547278e4da91 [file] [log] [blame]
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
John Criswell09344dc2003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00009//
10// This file contains code used to execute the program utilizing one of the
11// various ways of running LLVM bytecode.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000015#include "BugDriver.h"
Misha Brukman8a32c6d2004-04-19 03:12:35 +000016#include "llvm/Support/ToolRunner.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000017#include "Support/CommandLine.h"
Chris Lattnerf0c69642003-08-01 22:13:59 +000018#include "Support/Debug.h"
Chris Lattneraa997fb2003-08-01 20:29:45 +000019#include "Support/FileUtilities.h"
Misha Brukman0c2305b2003-08-07 21:19:30 +000020#include "Support/SystemUtils.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000021#include <fstream>
Brian Gaeke960707c2003-11-11 22:41:34 +000022using namespace llvm;
23
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000024namespace {
25 // OutputType - Allow the user to specify the way code should be run, to test
26 // for miscompilation.
27 //
28 enum OutputType {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000029 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000030 };
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000031
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000032 cl::opt<OutputType>
33 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000034 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukman8a32c6d2004-04-19 03:12:35 +000035 clEnumValN(RunLLI, "run-int",
36 "Execute with the interpreter"),
Misha Brukmand792c9b2003-07-24 18:17:43 +000037 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
38 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
39 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattner97161002004-07-16 00:08:28 +000040 clEnumValEnd),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000041 cl::init(AutoPick));
Chris Lattner68efaa72003-04-23 20:31:37 +000042
Brian Gaeke35145be2004-02-11 18:37:32 +000043 cl::opt<bool>
44 CheckProgramExitCode("check-exit-code",
Misha Brukman6aa3c832004-07-23 01:30:49 +000045 cl::desc("Assume nonzero exit code is failure (default on)"),
Brian Gaeke35145be2004-02-11 18:37:32 +000046 cl::init(true));
47
Chris Lattner68efaa72003-04-23 20:31:37 +000048 cl::opt<std::string>
49 InputFile("input", cl::init("/dev/null"),
50 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattnerdc92fa62003-10-14 22:24:31 +000051
52 cl::list<std::string>
53 AdditionalSOs("additional-so",
54 cl::desc("Additional shared objects to load "
55 "into executing programs"));
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000056}
57
Brian Gaeke960707c2003-11-11 22:41:34 +000058namespace llvm {
Chris Lattner2f1aa112004-01-14 03:38:37 +000059 // Anything specified after the --args option are taken as arguments to the
60 // program being debugged.
61 cl::list<std::string>
62 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner6aee736d2004-05-06 22:05:35 +000063 cl::ZeroOrMore, cl::PositionalEatsArgs);
Brian Gaeke4a278f02004-05-04 21:09:16 +000064
65 cl::list<std::string>
66 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner6aee736d2004-05-06 22:05:35 +000067 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattner2f1aa112004-01-14 03:38:37 +000068}
Misha Brukman40feb362003-07-30 20:15:44 +000069
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000070//===----------------------------------------------------------------------===//
71// BugDriver method implementation
72//
73
74/// initializeExecutionEnvironment - This method is used to set up the
75/// environment for executing LLVM programs.
76///
77bool BugDriver::initializeExecutionEnvironment() {
78 std::cout << "Initializing execution environment: ";
79
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000080 // Create an instance of the AbstractInterpreter interface as specified on
81 // the command line
Chris Lattner898de4a2004-02-18 20:52:02 +000082 cbe = 0;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000083 std::string Message;
Brian Gaeke4a278f02004-05-04 21:09:16 +000084
Chris Lattner7709ec52003-05-03 03:19:41 +000085 switch (InterpreterSel) {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000086 case AutoPick:
87 InterpreterSel = RunCBE;
Brian Gaeke4a278f02004-05-04 21:09:16 +000088 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
89 &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000090 if (!Interpreter) {
91 InterpreterSel = RunJIT;
Brian Gaeke4a278f02004-05-04 21:09:16 +000092 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
93 &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000094 }
95 if (!Interpreter) {
96 InterpreterSel = RunLLC;
Brian Gaeke4a278f02004-05-04 21:09:16 +000097 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
98 &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000099 }
100 if (!Interpreter) {
101 InterpreterSel = RunLLI;
Brian Gaeke4a278f02004-05-04 21:09:16 +0000102 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
103 &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000104 }
105 if (!Interpreter) {
106 InterpreterSel = AutoPick;
107 Message = "Sorry, I can't automatically select an interpreter!\n";
108 }
109 break;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000110 case RunLLI:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000111 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
112 &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000113 break;
114 case RunLLC:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000115 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
116 &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000117 break;
118 case RunJIT:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000119 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
120 &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000121 break;
122 case RunCBE:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000123 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
124 &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000125 break;
Chris Lattner7709ec52003-05-03 03:19:41 +0000126 default:
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000127 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattner7709ec52003-05-03 03:19:41 +0000128 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000129 }
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000130 std::cerr << Message;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000131
Misha Brukman0fd31722003-07-24 21:59:10 +0000132 // Initialize auxiliary tools for debugging
Chris Lattner898de4a2004-02-18 20:52:02 +0000133 if (!cbe) {
Brian Gaeke4a278f02004-05-04 21:09:16 +0000134 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Chris Lattner898de4a2004-02-18 20:52:02 +0000135 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
136 }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000137 gcc = GCC::create(getToolName(), Message);
Misha Brukman0fd31722003-07-24 21:59:10 +0000138 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
139
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000140 // If there was an error creating the selected interpreter, quit with error.
141 return Interpreter == 0;
142}
143
Chris Lattner96d41dd2004-02-18 23:25:22 +0000144/// compileProgram - Try to compile the specified module, throwing an exception
145/// if an error occurs, or returning normally if not. This is used for code
146/// generation crash testing.
147///
148void BugDriver::compileProgram(Module *M) {
149 // Emit the program to a bytecode file...
150 std::string BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
151 if (writeProgramToFile(BytecodeFile, M)) {
152 std::cerr << ToolName << ": Error emitting bytecode to file '"
153 << BytecodeFile << "'!\n";
154 exit(1);
155 }
156
157 // Remove the temporary bytecode file when we are done.
158 FileRemover BytecodeFileRemover(BytecodeFile);
159
160 // Actually compile the program!
161 Interpreter->compileProgram(BytecodeFile);
162}
163
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000164
165/// executeProgram - This method runs "Program", capturing the output of the
166/// program to a file, returning the filename of the file. A recommended
167/// filename may be optionally specified.
168///
169std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000170 std::string BytecodeFile,
Chris Lattner3f6e5222003-10-14 21:59:36 +0000171 const std::string &SharedObj,
Brian Gaeke35145be2004-02-11 18:37:32 +0000172 AbstractInterpreter *AI,
173 bool *ProgramExitedNonzero) {
Chris Lattner3f6e5222003-10-14 21:59:36 +0000174 if (AI == 0) AI = Interpreter;
175 assert(AI && "Interpreter should have been created already!");
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000176 bool CreatedBytecode = false;
177 if (BytecodeFile.empty()) {
178 // Emit the program to a bytecode file...
179 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
180
181 if (writeProgramToFile(BytecodeFile, Program)) {
182 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukmand792c9b2003-07-24 18:17:43 +0000183 << BytecodeFile << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000184 exit(1);
185 }
186 CreatedBytecode = true;
187 }
188
Chris Lattner1f80a922004-02-18 22:01:21 +0000189 // Remove the temporary bytecode file when we are done.
190 FileRemover BytecodeFileRemover(BytecodeFile, CreatedBytecode);
191
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000192 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000193
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000194 // Check to see if this is a valid output filename...
195 OutputFile = getUniqueFilename(OutputFile);
196
Chris Lattner3f6e5222003-10-14 21:59:36 +0000197 // Figure out which shared objects to run, if any.
Chris Lattnerdc92fa62003-10-14 22:24:31 +0000198 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000199 if (!SharedObj.empty())
200 SharedObjs.push_back(SharedObj);
201
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000202 // Actually execute the program!
Chris Lattner3f6e5222003-10-14 21:59:36 +0000203 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
204 OutputFile, SharedObjs);
205
Brian Gaeke35145be2004-02-11 18:37:32 +0000206 if (ProgramExitedNonzero != 0)
207 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000208
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000209 // Return the filename we captured the output to.
210 return OutputFile;
211}
212
Brian Gaeke35145be2004-02-11 18:37:32 +0000213/// executeProgramWithCBE - Used to create reference output with the C
214/// backend, if reference output is not provided.
215///
216std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
217 bool ProgramExitedNonzero;
218 std::string outFN = executeProgram(OutputFile, "", "",
219 (AbstractInterpreter*)cbe,
220 &ProgramExitedNonzero);
221 if (ProgramExitedNonzero) {
222 std::cerr
223 << "Warning: While generating reference output, program exited with\n"
224 << "non-zero exit code. This will NOT be treated as a failure.\n";
225 CheckProgramExitCode = false;
226 }
227 return outFN;
228}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000229
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000230std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000231 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000232 std::string OutputCFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000233
234 // Using CBE
Misha Brukmand792c9b2003-07-24 18:17:43 +0000235 cbe->OutputC(BytecodeFile, OutputCFile);
236
237#if 0 /* This is an alternative, as yet unimplemented */
238 // Using LLC
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000239 std::string Message;
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000240 LLC *llc = createLLCtool(Message);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000241 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
242 std::cerr << "Could not generate asm code with `llc', exiting.\n";
243 exit(1);
244 }
245#endif
246
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000247 std::string SharedObjectFile;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000248 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000249 exit(1);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000250
251 // Remove the intermediate C file
252 removeFile(OutputCFile);
253
Chris Lattner2b97d6e2003-10-19 21:54:13 +0000254 return "./" + SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000255}
256
257
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000258/// diffProgram - This method executes the specified module and diffs the output
259/// against the file specified by ReferenceOutputFile. If the output is
260/// different, true is returned.
261///
Misha Brukmand792c9b2003-07-24 18:17:43 +0000262bool BugDriver::diffProgram(const std::string &BytecodeFile,
263 const std::string &SharedObject,
Chris Lattner16a41312003-04-24 17:02:17 +0000264 bool RemoveBytecode) {
Brian Gaeke35145be2004-02-11 18:37:32 +0000265 bool ProgramExitedNonzero;
266
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000267 // Execute the program, generating an output file...
Brian Gaeke35145be2004-02-11 18:37:32 +0000268 std::string Output = executeProgram("", BytecodeFile, SharedObject, 0,
269 &ProgramExitedNonzero);
270
271 // If we're checking the program exit code, assume anything nonzero is bad.
Chris Lattner73c141a2004-04-02 05:33:06 +0000272 if (CheckProgramExitCode && ProgramExitedNonzero) {
273 removeFile(Output);
274 if (RemoveBytecode) removeFile(BytecodeFile);
Brian Gaeke35145be2004-02-11 18:37:32 +0000275 return true;
Chris Lattner73c141a2004-04-02 05:33:06 +0000276 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000277
Chris Lattneraa997fb2003-08-01 20:29:45 +0000278 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000279 bool FilesDifferent = false;
Chris Lattneraa997fb2003-08-01 20:29:45 +0000280 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
281 if (!Error.empty()) {
Misha Brukman6aa3c832004-07-23 01:30:49 +0000282 std::cerr << "While diffing output: " << Error << '\n';
Chris Lattneraa997fb2003-08-01 20:29:45 +0000283 exit(1);
284 }
285 FilesDifferent = true;
286 }
Chris Lattner759b9932003-10-18 21:02:51 +0000287
288 // Remove the generated output.
289 removeFile(Output);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000290
Chris Lattner759b9932003-10-18 21:02:51 +0000291 // Remove the bytecode file if we are supposed to.
Chris Lattner16a41312003-04-24 17:02:17 +0000292 if (RemoveBytecode) removeFile(BytecodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000293 return FilesDifferent;
294}
Misha Brukman539f9592003-07-28 19:16:14 +0000295
296bool BugDriver::isExecutingJIT() {
297 return InterpreterSel == RunJIT;
298}
Brian Gaeke960707c2003-11-11 22:41:34 +0000299