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