blob: 4d5c78368f77183a988b3b63c68415c96efa6c69 [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
15/*
16BUGPOINT NOTES:
17
181. Bugpoint should not leave any files behind if the program works properly
192. There should be an option to specify the program name, which specifies a
20 unique string to put into output files. This allows operation in the
Misha Brukmand792c9b2003-07-24 18:17:43 +000021 SingleSource directory, e.g. default to the first input filename.
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000022*/
23
24#include "BugDriver.h"
Misha Brukman8a32c6d2004-04-19 03:12:35 +000025#include "llvm/Support/ToolRunner.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000026#include "Support/CommandLine.h"
Chris Lattnerf0c69642003-08-01 22:13:59 +000027#include "Support/Debug.h"
Chris Lattneraa997fb2003-08-01 20:29:45 +000028#include "Support/FileUtilities.h"
Misha Brukman0c2305b2003-08-07 21:19:30 +000029#include "Support/SystemUtils.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000030#include <fstream>
Chris Lattner7c0f8622002-12-24 00:44:34 +000031#include <iostream>
Brian Gaeke960707c2003-11-11 22:41:34 +000032using namespace llvm;
33
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000034namespace {
35 // OutputType - Allow the user to specify the way code should be run, to test
36 // for miscompilation.
37 //
38 enum OutputType {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000039 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000040 };
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000041
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000042 cl::opt<OutputType>
43 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000044 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukman8a32c6d2004-04-19 03:12:35 +000045 clEnumValN(RunLLI, "run-int",
46 "Execute with the interpreter"),
Misha Brukmand792c9b2003-07-24 18:17:43 +000047 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
48 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
49 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattner16d36a12003-10-18 20:18:20 +000050 0),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000051 cl::init(AutoPick));
Chris Lattner68efaa72003-04-23 20:31:37 +000052
Brian Gaeke35145be2004-02-11 18:37:32 +000053 cl::opt<bool>
54 CheckProgramExitCode("check-exit-code",
55 cl::desc("Assume nonzero exit code is failure (default on)"),
56 cl::init(true));
57
Chris Lattner68efaa72003-04-23 20:31:37 +000058 cl::opt<std::string>
59 InputFile("input", cl::init("/dev/null"),
60 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattnerdc92fa62003-10-14 22:24:31 +000061
62 cl::list<std::string>
63 AdditionalSOs("additional-so",
64 cl::desc("Additional shared objects to load "
65 "into executing programs"));
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000066}
67
Brian Gaeke960707c2003-11-11 22:41:34 +000068namespace llvm {
Chris Lattner2f1aa112004-01-14 03:38:37 +000069 // Anything specified after the --args option are taken as arguments to the
70 // program being debugged.
71 cl::list<std::string>
72 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
73 cl::ZeroOrMore);
74}
Misha Brukman40feb362003-07-30 20:15:44 +000075
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000076//===----------------------------------------------------------------------===//
77// BugDriver method implementation
78//
79
80/// initializeExecutionEnvironment - This method is used to set up the
81/// environment for executing LLVM programs.
82///
83bool BugDriver::initializeExecutionEnvironment() {
84 std::cout << "Initializing execution environment: ";
85
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000086 // Create an instance of the AbstractInterpreter interface as specified on
87 // the command line
Chris Lattner898de4a2004-02-18 20:52:02 +000088 cbe = 0;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000089 std::string Message;
Chris Lattner7709ec52003-05-03 03:19:41 +000090 switch (InterpreterSel) {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000091 case AutoPick:
92 InterpreterSel = RunCBE;
Chris Lattner898de4a2004-02-18 20:52:02 +000093 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000094 if (!Interpreter) {
95 InterpreterSel = RunJIT;
96 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
97 }
98 if (!Interpreter) {
99 InterpreterSel = RunLLC;
100 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
101 }
102 if (!Interpreter) {
103 InterpreterSel = RunLLI;
104 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
105 }
106 if (!Interpreter) {
107 InterpreterSel = AutoPick;
108 Message = "Sorry, I can't automatically select an interpreter!\n";
109 }
110 break;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000111 case RunLLI:
112 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
113 break;
114 case RunLLC:
115 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
116 break;
117 case RunJIT:
118 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
119 break;
120 case RunCBE:
Chris Lattner898de4a2004-02-18 20:52:02 +0000121 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000122 break;
Chris Lattner7709ec52003-05-03 03:19:41 +0000123 default:
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000124 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattner7709ec52003-05-03 03:19:41 +0000125 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000126 }
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000127 std::cerr << Message;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000128
Misha Brukman0fd31722003-07-24 21:59:10 +0000129 // Initialize auxiliary tools for debugging
Chris Lattner898de4a2004-02-18 20:52:02 +0000130 if (!cbe) {
131 cbe = AbstractInterpreter::createCBE(getToolName(), Message);
132 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
133 }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000134 gcc = GCC::create(getToolName(), Message);
Misha Brukman0fd31722003-07-24 21:59:10 +0000135 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
136
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000137 // If there was an error creating the selected interpreter, quit with error.
138 return Interpreter == 0;
139}
140
Chris Lattner96d41dd2004-02-18 23:25:22 +0000141/// compileProgram - Try to compile the specified module, throwing an exception
142/// if an error occurs, or returning normally if not. This is used for code
143/// generation crash testing.
144///
145void BugDriver::compileProgram(Module *M) {
146 // Emit the program to a bytecode file...
147 std::string BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
148 if (writeProgramToFile(BytecodeFile, M)) {
149 std::cerr << ToolName << ": Error emitting bytecode to file '"
150 << BytecodeFile << "'!\n";
151 exit(1);
152 }
153
154 // Remove the temporary bytecode file when we are done.
155 FileRemover BytecodeFileRemover(BytecodeFile);
156
157 // Actually compile the program!
158 Interpreter->compileProgram(BytecodeFile);
159}
160
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000161
162/// executeProgram - This method runs "Program", capturing the output of the
163/// program to a file, returning the filename of the file. A recommended
164/// filename may be optionally specified.
165///
166std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000167 std::string BytecodeFile,
Chris Lattner3f6e5222003-10-14 21:59:36 +0000168 const std::string &SharedObj,
Brian Gaeke35145be2004-02-11 18:37:32 +0000169 AbstractInterpreter *AI,
170 bool *ProgramExitedNonzero) {
Chris Lattner3f6e5222003-10-14 21:59:36 +0000171 if (AI == 0) AI = Interpreter;
172 assert(AI && "Interpreter should have been created already!");
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000173 bool CreatedBytecode = false;
174 if (BytecodeFile.empty()) {
175 // Emit the program to a bytecode file...
176 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
177
178 if (writeProgramToFile(BytecodeFile, Program)) {
179 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukmand792c9b2003-07-24 18:17:43 +0000180 << BytecodeFile << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000181 exit(1);
182 }
183 CreatedBytecode = true;
184 }
185
Chris Lattner1f80a922004-02-18 22:01:21 +0000186 // Remove the temporary bytecode file when we are done.
187 FileRemover BytecodeFileRemover(BytecodeFile, CreatedBytecode);
188
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000189 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000190
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000191 // Check to see if this is a valid output filename...
192 OutputFile = getUniqueFilename(OutputFile);
193
Chris Lattner3f6e5222003-10-14 21:59:36 +0000194 // Figure out which shared objects to run, if any.
Chris Lattnerdc92fa62003-10-14 22:24:31 +0000195 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000196 if (!SharedObj.empty())
197 SharedObjs.push_back(SharedObj);
198
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000199 // Actually execute the program!
Chris Lattner3f6e5222003-10-14 21:59:36 +0000200 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
201 OutputFile, SharedObjs);
202
Brian Gaeke35145be2004-02-11 18:37:32 +0000203 if (ProgramExitedNonzero != 0)
204 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000205
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000206 // Return the filename we captured the output to.
207 return OutputFile;
208}
209
Brian Gaeke35145be2004-02-11 18:37:32 +0000210/// executeProgramWithCBE - Used to create reference output with the C
211/// backend, if reference output is not provided.
212///
213std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
214 bool ProgramExitedNonzero;
215 std::string outFN = executeProgram(OutputFile, "", "",
216 (AbstractInterpreter*)cbe,
217 &ProgramExitedNonzero);
218 if (ProgramExitedNonzero) {
219 std::cerr
220 << "Warning: While generating reference output, program exited with\n"
221 << "non-zero exit code. This will NOT be treated as a failure.\n";
222 CheckProgramExitCode = false;
223 }
224 return outFN;
225}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000226
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000227std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000228 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000229 std::string OutputCFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000230
231 // Using CBE
Misha Brukmand792c9b2003-07-24 18:17:43 +0000232 cbe->OutputC(BytecodeFile, OutputCFile);
233
234#if 0 /* This is an alternative, as yet unimplemented */
235 // Using LLC
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000236 std::string Message;
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000237 LLC *llc = createLLCtool(Message);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000238 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
239 std::cerr << "Could not generate asm code with `llc', exiting.\n";
240 exit(1);
241 }
242#endif
243
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000244 std::string SharedObjectFile;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000245 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000246 exit(1);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000247
248 // Remove the intermediate C file
249 removeFile(OutputCFile);
250
Chris Lattner2b97d6e2003-10-19 21:54:13 +0000251 return "./" + SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000252}
253
254
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000255/// diffProgram - This method executes the specified module and diffs the output
256/// against the file specified by ReferenceOutputFile. If the output is
257/// different, true is returned.
258///
Misha Brukmand792c9b2003-07-24 18:17:43 +0000259bool BugDriver::diffProgram(const std::string &BytecodeFile,
260 const std::string &SharedObject,
Chris Lattner16a41312003-04-24 17:02:17 +0000261 bool RemoveBytecode) {
Brian Gaeke35145be2004-02-11 18:37:32 +0000262 bool ProgramExitedNonzero;
263
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000264 // Execute the program, generating an output file...
Brian Gaeke35145be2004-02-11 18:37:32 +0000265 std::string Output = executeProgram("", BytecodeFile, SharedObject, 0,
266 &ProgramExitedNonzero);
267
268 // If we're checking the program exit code, assume anything nonzero is bad.
Chris Lattner73c141a2004-04-02 05:33:06 +0000269 if (CheckProgramExitCode && ProgramExitedNonzero) {
270 removeFile(Output);
271 if (RemoveBytecode) removeFile(BytecodeFile);
Brian Gaeke35145be2004-02-11 18:37:32 +0000272 return true;
Chris Lattner73c141a2004-04-02 05:33:06 +0000273 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000274
Chris Lattneraa997fb2003-08-01 20:29:45 +0000275 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000276 bool FilesDifferent = false;
Chris Lattneraa997fb2003-08-01 20:29:45 +0000277 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
278 if (!Error.empty()) {
279 std::cerr << "While diffing output: " << Error << "\n";
280 exit(1);
281 }
282 FilesDifferent = true;
283 }
Chris Lattner759b9932003-10-18 21:02:51 +0000284
285 // Remove the generated output.
286 removeFile(Output);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000287
Chris Lattner759b9932003-10-18 21:02:51 +0000288 // Remove the bytecode file if we are supposed to.
Chris Lattner16a41312003-04-24 17:02:17 +0000289 if (RemoveBytecode) removeFile(BytecodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000290 return FilesDifferent;
291}
Misha Brukman539f9592003-07-28 19:16:14 +0000292
293bool BugDriver::isExecutingJIT() {
294 return InterpreterSel == RunJIT;
295}
Brian Gaeke960707c2003-11-11 22:41:34 +0000296