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