Fix the first FIXME in this file: automatically pick a "good"
[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
33 namespace {
34   // OutputType - Allow the user to specify the way code should be run, to test
35   // for miscompilation.
36   //
37   enum OutputType {
38     AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
39   };
40
41   cl::opt<OutputType>
42   InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
43                  cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
44                             clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
45                             clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
46                             clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
47                             clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
48                             0),
49                  cl::init(AutoPick));
50
51   cl::opt<std::string>
52   InputFile("input", cl::init("/dev/null"),
53             cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
54
55   cl::list<std::string>
56   AdditionalSOs("additional-so",
57                 cl::desc("Additional shared objects to load "
58                          "into executing programs"));
59 }
60
61 // Anything specified after the --args option are taken as arguments to the
62 // program being debugged.
63 cl::list<std::string>
64 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
65           cl::ZeroOrMore);
66
67 //===----------------------------------------------------------------------===//
68 // BugDriver method implementation
69 //
70
71 /// initializeExecutionEnvironment - This method is used to set up the
72 /// environment for executing LLVM programs.
73 ///
74 bool BugDriver::initializeExecutionEnvironment() {
75   std::cout << "Initializing execution environment: ";
76
77   // Create an instance of the AbstractInterpreter interface as specified on
78   // the command line
79   std::string Message;
80   switch (InterpreterSel) {
81   case AutoPick:
82     InterpreterSel = RunCBE;
83     Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
84     if (!Interpreter) {
85       InterpreterSel = RunJIT;
86       Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
87     }
88     if (!Interpreter) {
89       InterpreterSel = RunLLC;
90       Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
91     }
92     if (!Interpreter) {
93       InterpreterSel = RunLLI;
94       Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
95     }
96     if (!Interpreter) {
97       InterpreterSel = AutoPick;
98       Message = "Sorry, I can't automatically select an interpreter!\n";
99     }
100     break;
101   case RunLLI:
102     Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
103     break;
104   case RunLLC:
105     Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
106     break;
107   case RunJIT:
108     Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
109     break;
110   case RunCBE:
111     Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
112     break;
113   default:
114     Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
115     break;
116   }
117   std::cerr << Message;
118
119   // Initialize auxiliary tools for debugging
120   cbe = AbstractInterpreter::createCBE(getToolName(), Message);
121   if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
122   gcc = GCC::create(getToolName(), Message);
123   if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
124
125   // If there was an error creating the selected interpreter, quit with error.
126   return Interpreter == 0;
127 }
128
129
130 /// executeProgram - This method runs "Program", capturing the output of the
131 /// program to a file, returning the filename of the file.  A recommended
132 /// filename may be optionally specified.
133 ///
134 std::string BugDriver::executeProgram(std::string OutputFile,
135                                       std::string BytecodeFile,
136                                       const std::string &SharedObj,
137                                       AbstractInterpreter *AI) {
138   if (AI == 0) AI = Interpreter;
139   assert(AI && "Interpreter should have been created already!");
140   bool CreatedBytecode = false;
141   if (BytecodeFile.empty()) {
142     // Emit the program to a bytecode file...
143     BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
144
145     if (writeProgramToFile(BytecodeFile, Program)) {
146       std::cerr << ToolName << ": Error emitting bytecode to file '"
147                 << BytecodeFile << "'!\n";
148       exit(1);
149     }
150     CreatedBytecode = true;
151   }
152
153   if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
154
155   // Check to see if this is a valid output filename...
156   OutputFile = getUniqueFilename(OutputFile);
157
158   // Figure out which shared objects to run, if any.
159   std::vector<std::string> SharedObjs(AdditionalSOs);
160   if (!SharedObj.empty())
161     SharedObjs.push_back(SharedObj);
162
163   // Actually execute the program!
164   int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
165                                   OutputFile, SharedObjs);
166
167
168   // Remove the temporary bytecode file.
169   if (CreatedBytecode) removeFile(BytecodeFile);
170
171   // Return the filename we captured the output to.
172   return OutputFile;
173 }
174
175
176 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
177   assert(Interpreter && "Interpreter should have been created already!");
178   std::string OutputCFile;
179
180   // Using CBE
181   cbe->OutputC(BytecodeFile, OutputCFile);
182
183 #if 0 /* This is an alternative, as yet unimplemented */
184   // Using LLC
185   std::string Message;
186   LLC *llc = createLLCtool(Message);
187   if (llc->OutputAsm(BytecodeFile, OutputFile)) {
188     std::cerr << "Could not generate asm code with `llc', exiting.\n";
189     exit(1);
190   }
191 #endif
192
193   std::string SharedObjectFile;
194   if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
195     exit(1);
196
197   // Remove the intermediate C file
198   removeFile(OutputCFile);
199
200   return "./" + SharedObjectFile;
201 }
202
203
204 /// diffProgram - This method executes the specified module and diffs the output
205 /// against the file specified by ReferenceOutputFile.  If the output is
206 /// different, true is returned.
207 ///
208 bool BugDriver::diffProgram(const std::string &BytecodeFile,
209                             const std::string &SharedObject,
210                             bool RemoveBytecode) {
211   // Execute the program, generating an output file...
212   std::string Output = executeProgram("", BytecodeFile, SharedObject);
213
214   std::string Error;
215   bool FilesDifferent = false;
216   if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
217     if (!Error.empty()) {
218       std::cerr << "While diffing output: " << Error << "\n";
219       exit(1);
220     }
221     FilesDifferent = true;
222   }
223   
224   // Remove the generated output.
225   removeFile(Output);
226
227   // Remove the bytecode file if we are supposed to.
228   if (RemoveBytecode) removeFile(BytecodeFile);
229   return FilesDifferent;
230 }
231
232 bool BugDriver::isExecutingJIT() {
233   return InterpreterSel == RunJIT;
234 }