3d4e61058ff37bead6831cdef56059ec6337c99d
[oota-llvm.git] / tools / bugpoint / ExecutionDriver.cpp
1 //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
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 /*
16 BUGPOINT NOTES:
17
18 1. Bugpoint should not leave any files behind if the program works properly
19 2. 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
21    SingleSource directory, e.g. default to the first input filename.
22 */
23
24 #include "BugDriver.h"
25 #include "Support/CommandLine.h"
26 #include "Support/Debug.h"
27 #include "Support/FileUtilities.h"
28 #include "Support/SystemUtils.h"
29 #include "llvm/Support/ToolRunner.h"
30 #include <fstream>
31 #include <iostream>
32 using namespace llvm;
33
34 namespace {
35   // OutputType - Allow the user to specify the way code should be run, to test
36   // for miscompilation.
37   //
38   enum OutputType {
39     AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
40   };
41
42   cl::opt<OutputType>
43   InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
44                  cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
45                             clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
46                             clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
47                             clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
48                             clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
49                             0),
50                  cl::init(AutoPick));
51
52   cl::opt<bool>
53   CheckProgramExitCode("check-exit-code",
54                        cl::desc("Assume nonzero exit code is failure (default on)"),
55                        cl::init(true));
56
57   cl::opt<std::string>
58   InputFile("input", cl::init("/dev/null"),
59             cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
60
61   cl::list<std::string>
62   AdditionalSOs("additional-so",
63                 cl::desc("Additional shared objects to load "
64                          "into executing programs"));
65 }
66
67 namespace llvm {
68   // Anything specified after the --args option are taken as arguments to the
69   // program being debugged.
70   cl::list<std::string>
71   InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
72             cl::ZeroOrMore);
73 }
74
75 //===----------------------------------------------------------------------===//
76 // BugDriver method implementation
77 //
78
79 /// initializeExecutionEnvironment - This method is used to set up the
80 /// environment for executing LLVM programs.
81 ///
82 bool BugDriver::initializeExecutionEnvironment() {
83   std::cout << "Initializing execution environment: ";
84
85   // Create an instance of the AbstractInterpreter interface as specified on
86   // the command line
87   cbe = 0;
88   std::string Message;
89   switch (InterpreterSel) {
90   case AutoPick:
91     InterpreterSel = RunCBE;
92     Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message);
93     if (!Interpreter) {
94       InterpreterSel = RunJIT;
95       Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
96     }
97     if (!Interpreter) {
98       InterpreterSel = RunLLC;
99       Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
100     }
101     if (!Interpreter) {
102       InterpreterSel = RunLLI;
103       Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
104     }
105     if (!Interpreter) {
106       InterpreterSel = AutoPick;
107       Message = "Sorry, I can't automatically select an interpreter!\n";
108     }
109     break;
110   case RunLLI:
111     Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
112     break;
113   case RunLLC:
114     Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
115     break;
116   case RunJIT:
117     Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
118     break;
119   case RunCBE:
120     Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message);
121     break;
122   default:
123     Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
124     break;
125   }
126   std::cerr << Message;
127
128   // Initialize auxiliary tools for debugging
129   if (!cbe) {
130     cbe = AbstractInterpreter::createCBE(getToolName(), Message);
131     if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
132   }
133   gcc = GCC::create(getToolName(), Message);
134   if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
135
136   // If there was an error creating the selected interpreter, quit with error.
137   return Interpreter == 0;
138 }
139
140 /// compileProgram - Try to compile the specified module, throwing an exception
141 /// if an error occurs, or returning normally if not.  This is used for code
142 /// generation crash testing.
143 ///
144 void BugDriver::compileProgram(Module *M) {
145   // Emit the program to a bytecode file...
146   std::string BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
147   if (writeProgramToFile(BytecodeFile, M)) {
148     std::cerr << ToolName << ": Error emitting bytecode to file '"
149               << BytecodeFile << "'!\n";
150     exit(1);
151   }
152
153     // Remove the temporary bytecode file when we are done.
154   FileRemover BytecodeFileRemover(BytecodeFile);
155
156   // Actually compile the program!
157   Interpreter->compileProgram(BytecodeFile);
158 }
159
160
161 /// executeProgram - This method runs "Program", capturing the output of the
162 /// program to a file, returning the filename of the file.  A recommended
163 /// filename may be optionally specified.
164 ///
165 std::string BugDriver::executeProgram(std::string OutputFile,
166                                       std::string BytecodeFile,
167                                       const std::string &SharedObj,
168                                       AbstractInterpreter *AI,
169                                       bool *ProgramExitedNonzero) {
170   if (AI == 0) AI = Interpreter;
171   assert(AI && "Interpreter should have been created already!");
172   bool CreatedBytecode = false;
173   if (BytecodeFile.empty()) {
174     // Emit the program to a bytecode file...
175     BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
176
177     if (writeProgramToFile(BytecodeFile, Program)) {
178       std::cerr << ToolName << ": Error emitting bytecode to file '"
179                 << BytecodeFile << "'!\n";
180       exit(1);
181     }
182     CreatedBytecode = true;
183   }
184
185   // Remove the temporary bytecode file when we are done.
186   FileRemover BytecodeFileRemover(BytecodeFile, CreatedBytecode);
187
188   if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
189
190   // Check to see if this is a valid output filename...
191   OutputFile = getUniqueFilename(OutputFile);
192
193   // Figure out which shared objects to run, if any.
194   std::vector<std::string> SharedObjs(AdditionalSOs);
195   if (!SharedObj.empty())
196     SharedObjs.push_back(SharedObj);
197
198   // Actually execute the program!
199   int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
200                                   OutputFile, SharedObjs);
201
202   if (ProgramExitedNonzero != 0)
203     *ProgramExitedNonzero = (RetVal != 0);
204
205   // Return the filename we captured the output to.
206   return OutputFile;
207 }
208
209 /// executeProgramWithCBE - Used to create reference output with the C
210 /// backend, if reference output is not provided.
211 ///
212 std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
213   bool ProgramExitedNonzero;
214   std::string outFN = executeProgram(OutputFile, "", "",
215                                      (AbstractInterpreter*)cbe,
216                                      &ProgramExitedNonzero);
217   if (ProgramExitedNonzero) {
218     std::cerr
219       << "Warning: While generating reference output, program exited with\n"
220       << "non-zero exit code. This will NOT be treated as a failure.\n";
221     CheckProgramExitCode = false;
222   }
223   return outFN;
224 }
225
226 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
227   assert(Interpreter && "Interpreter should have been created already!");
228   std::string OutputCFile;
229
230   // Using CBE
231   cbe->OutputC(BytecodeFile, OutputCFile);
232
233 #if 0 /* This is an alternative, as yet unimplemented */
234   // Using LLC
235   std::string Message;
236   LLC *llc = createLLCtool(Message);
237   if (llc->OutputAsm(BytecodeFile, OutputFile)) {
238     std::cerr << "Could not generate asm code with `llc', exiting.\n";
239     exit(1);
240   }
241 #endif
242
243   std::string SharedObjectFile;
244   if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
245     exit(1);
246
247   // Remove the intermediate C file
248   removeFile(OutputCFile);
249
250   return "./" + SharedObjectFile;
251 }
252
253
254 /// diffProgram - This method executes the specified module and diffs the output
255 /// against the file specified by ReferenceOutputFile.  If the output is
256 /// different, true is returned.
257 ///
258 bool BugDriver::diffProgram(const std::string &BytecodeFile,
259                             const std::string &SharedObject,
260                             bool RemoveBytecode) {
261   bool ProgramExitedNonzero;
262
263   // Execute the program, generating an output file...
264   std::string Output = executeProgram("", BytecodeFile, SharedObject, 0,
265                                       &ProgramExitedNonzero);
266
267   // If we're checking the program exit code, assume anything nonzero is bad.
268   if (CheckProgramExitCode && ProgramExitedNonzero)
269     return true;
270
271   std::string Error;
272   bool FilesDifferent = false;
273   if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
274     if (!Error.empty()) {
275       std::cerr << "While diffing output: " << Error << "\n";
276       exit(1);
277     }
278     FilesDifferent = true;
279   }
280   
281   // Remove the generated output.
282   removeFile(Output);
283
284   // Remove the bytecode file if we are supposed to.
285   if (RemoveBytecode) removeFile(BytecodeFile);
286   return FilesDifferent;
287 }
288
289 bool BugDriver::isExecutingJIT() {
290   return InterpreterSel == RunJIT;
291 }
292