ExecutionDriver.cpp revision 25d66473c620b6b01e301b9cfabdbedace96608e
1aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
2aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák//
3aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák// This file contains code used to execute the program utilizing one of the
4aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák// various ways of running LLVM bytecode.
5aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák//
6aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák//===----------------------------------------------------------------------===//
7aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák
8aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák/*
9aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek OlšákBUGPOINT NOTES:
10aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák
11aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák1. Bugpoint should not leave any files behind if the program works properly
12aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák2. There should be an option to specify the program name, which specifies a
13aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák   unique string to put into output files.  This allows operation in the
14aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák   SingleSource directory, e.g. default to the first input filename.
15aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák*/
16aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák
17aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák#include "BugDriver.h"
18aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák#include "Support/CommandLine.h"
19aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák#include "Support/Debug.h"
20aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák#include "Support/FileUtilities.h"
21aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák#include "Support/SystemUtils.h"
22aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák#include "llvm/Support/ToolRunner.h"
23aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák#include <fstream>
24aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák#include <iostream>
25aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák
26aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšáknamespace {
27aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  // OutputType - Allow the user to specify the way code should be run, to test
28aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  // for miscompilation.
29aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  //
30aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  enum OutputType {
31aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák    RunLLI, RunJIT, RunLLC, RunCBE
32aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  };
33aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák
34aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  cl::opt<OutputType>
35aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
36aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák                 cl::values(clEnumValN(RunLLI, "run-lli", "Execute with LLI"),
37aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák                            clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
38aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák                            clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
39aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák                            clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
40aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák                            0),
41aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák                 cl::init(RunCBE));
42aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák
43aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  cl::opt<std::string>
44aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  InputFile("input", cl::init("/dev/null"),
45aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák            cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
46aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák
47aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  cl::list<std::string>
48aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák  AdditionalSOs("additional-so",
49aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák                cl::desc("Additional shared objects to load "
50aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák                         "into executing programs"));
51aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák}
52aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák
53aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák// Anything specified after the --args option are taken as arguments to the
54aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák// program being debugged.
55aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšákcl::list<std::string>
56aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek OlšákInputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
57aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák          cl::ZeroOrMore);
58aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák
59aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák//===----------------------------------------------------------------------===//
60aea4ed41ed9b4d0442d1090ac1f01231a9859796Marek Olšák// BugDriver method implementation
61//
62
63/// initializeExecutionEnvironment - This method is used to set up the
64/// environment for executing LLVM programs.
65///
66bool BugDriver::initializeExecutionEnvironment() {
67  std::cout << "Initializing execution environment: ";
68
69  // FIXME: This should default to searching for the best interpreter to use on
70  // this platform, which would be JIT, then LLC, then CBE, then LLI.
71
72  // Create an instance of the AbstractInterpreter interface as specified on
73  // the command line
74  std::string Message;
75  switch (InterpreterSel) {
76  case RunLLI:
77    Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
78    break;
79  case RunLLC:
80    Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
81    break;
82  case RunJIT:
83    Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
84    break;
85  case RunCBE:
86    Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
87    break;
88  default:
89    Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
90    break;
91  }
92  std::cerr << Message;
93
94  // Initialize auxiliary tools for debugging
95  cbe = AbstractInterpreter::createCBE(getToolName(), Message);
96  if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
97  gcc = GCC::create(getToolName(), Message);
98  if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
99
100  // If there was an error creating the selected interpreter, quit with error.
101  return Interpreter == 0;
102}
103
104
105/// executeProgram - This method runs "Program", capturing the output of the
106/// program to a file, returning the filename of the file.  A recommended
107/// filename may be optionally specified.
108///
109std::string BugDriver::executeProgram(std::string OutputFile,
110                                      std::string BytecodeFile,
111                                      const std::string &SharedObj,
112                                      AbstractInterpreter *AI) {
113  if (AI == 0) AI = Interpreter;
114  assert(AI && "Interpreter should have been created already!");
115  bool CreatedBytecode = false;
116  if (BytecodeFile.empty()) {
117    // Emit the program to a bytecode file...
118    BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
119
120    if (writeProgramToFile(BytecodeFile, Program)) {
121      std::cerr << ToolName << ": Error emitting bytecode to file '"
122                << BytecodeFile << "'!\n";
123      exit(1);
124    }
125    CreatedBytecode = true;
126  }
127
128  if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
129
130  // Check to see if this is a valid output filename...
131  OutputFile = getUniqueFilename(OutputFile);
132
133  // Figure out which shared objects to run, if any.
134  std::vector<std::string> SharedObjs(AdditionalSOs);
135  if (!SharedObj.empty())
136    SharedObjs.push_back(SharedObj);
137
138  // Actually execute the program!
139  int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
140                                  OutputFile, SharedObjs);
141
142
143  // Remove the temporary bytecode file.
144  if (CreatedBytecode) removeFile(BytecodeFile);
145
146  // Return the filename we captured the output to.
147  return OutputFile;
148}
149
150
151std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
152  assert(Interpreter && "Interpreter should have been created already!");
153  std::string OutputCFile;
154
155  // Using CBE
156  cbe->OutputC(BytecodeFile, OutputCFile);
157
158#if 0 /* This is an alternative, as yet unimplemented */
159  // Using LLC
160  std::string Message;
161  LLC *llc = createLLCtool(Message);
162  if (llc->OutputAsm(BytecodeFile, OutputFile)) {
163    std::cerr << "Could not generate asm code with `llc', exiting.\n";
164    exit(1);
165  }
166#endif
167
168  std::string SharedObjectFile;
169  if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
170    exit(1);
171
172  // Remove the intermediate C file
173  removeFile(OutputCFile);
174
175  return SharedObjectFile;
176}
177
178
179/// diffProgram - This method executes the specified module and diffs the output
180/// against the file specified by ReferenceOutputFile.  If the output is
181/// different, true is returned.
182///
183bool BugDriver::diffProgram(const std::string &BytecodeFile,
184                            const std::string &SharedObject,
185                            bool RemoveBytecode) {
186  // Execute the program, generating an output file...
187  std::string Output = executeProgram("", BytecodeFile, SharedObject);
188
189  std::string Error;
190  bool FilesDifferent = false;
191  if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
192    if (!Error.empty()) {
193      std::cerr << "While diffing output: " << Error << "\n";
194      exit(1);
195    }
196    FilesDifferent = true;
197  }
198
199  if (RemoveBytecode) removeFile(BytecodeFile);
200  return FilesDifferent;
201}
202
203bool BugDriver::isExecutingJIT() {
204  return InterpreterSel == RunJIT;
205}
206