ExecutionDriver.cpp revision 7dac658792425c10274594782d6fcf10208a16f0
197ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
297ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu//
397ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu// This file contains code used to execute the program utilizing one of the
497ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu// various ways of running LLVM bytecode.
597ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu//
697ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu//===----------------------------------------------------------------------===//
797ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
897ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu/*
997ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing XuBUGPOINT NOTES:
1097ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
1197ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu1. Bugpoint should not leave any files behind if the program works properly
1297ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu2. There should be an option to specify the program name, which specifies a
1397ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu   unique string to put into output files.  This allows operation in the
1497ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu   SingleSource directory, e.g. default to the first input filename.
1597ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu*/
1697ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
1797ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu#include "BugDriver.h"
1897ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu#include "Support/CommandLine.h"
1997ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu#include "Support/Debug.h"
2097ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu#include "Support/FileUtilities.h"
2187a05f1fe8ae14044f182b015b279e0a6f4cbdd1Mike Stump#include "Support/SystemUtils.h"
2297ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu#include "llvm/Support/ToolRunner.h"
2397ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu#include <fstream>
2497ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu#include <iostream>
2597ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
2697ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xunamespace {
2797ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  // OutputType - Allow the user to specify the way code should be run, to test
2897ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  // for miscompilation.
2997ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  //
3097ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  enum OutputType {
3197ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu    RunLLI, RunJIT, RunLLC, RunCBE
3297ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  };
3397ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
3497ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  cl::opt<OutputType>
3597ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
3697ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu                 cl::values(clEnumValN(RunLLI, "run-lli", "Execute with LLI"),
3787a05f1fe8ae14044f182b015b279e0a6f4cbdd1Mike Stump                            clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
3897ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu                            clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
3997ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu                            clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
4097ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu                            0));
4197ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
4297ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  cl::opt<std::string>
4397ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  InputFile("input", cl::init("/dev/null"),
4497ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu            cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
4597ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
4697ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  cl::list<std::string>
4797ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  AdditionalSOs("additional-so",
4897ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu                cl::desc("Additional shared objects to load "
4997ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu                         "into executing programs"));
5097ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu}
5197ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
5297ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu// Anything specified after the --args option are taken as arguments to the
5397ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu// program being debugged.
5497ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xucl::list<std::string>
5597ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing XuInputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
5697ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu          cl::ZeroOrMore);
5797ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
5897ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu//===----------------------------------------------------------------------===//
5997ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu// BugDriver method implementation
6097ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu//
6197ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
6297ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu/// initializeExecutionEnvironment - This method is used to set up the
6397ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu/// environment for executing LLVM programs.
6497ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu///
6597ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xubool BugDriver::initializeExecutionEnvironment() {
6697ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  std::cout << "Initializing execution environment: ";
6797ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
6897ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  // FIXME: This should default to searching for the best interpreter to use on
6997ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  // this platform, which would be JIT, then LLC, then CBE, then LLI.
7097ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu
7197ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  // Create an instance of the AbstractInterpreter interface as specified on
7297ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  // the command line
7397ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  std::string Message;
7497ab3941effe1f508c7113d9aa0c2887774f6fa8Zhongxing Xu  switch (InterpreterSel) {
7518c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  case RunLLI:
7618c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
7718c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    break;
7818c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  case RunLLC:
7918c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
8018c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    break;
8118c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  case RunJIT:
8218c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
8318c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    break;
8418c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  case RunCBE:
8518c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
8618c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    break;
8718c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  default:
8818c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
8918c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    break;
9018c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  }
9118c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  std::cerr << Message;
9218c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu
9318c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  // Initialize auxiliary tools for debugging
9418c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  cbe = AbstractInterpreter::createCBE(getToolName(), Message);
9518c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
9618c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  gcc = GCC::create(getToolName(), Message);
9718c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
9818c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu
9918c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  // If there was an error creating the selected interpreter, quit with error.
10018c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  return Interpreter == 0;
10118c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu}
10218c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu
10318c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu
10418c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu/// executeProgram - This method runs "Program", capturing the output of the
10518c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu/// program to a file, returning the filename of the file.  A recommended
10618c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu/// filename may be optionally specified.
10718c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu///
10818c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xustd::string BugDriver::executeProgram(std::string OutputFile,
10918c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu                                      std::string BytecodeFile,
11018c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu                                      const std::string &SharedObj,
11118c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu                                      AbstractInterpreter *AI) {
11218c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  if (AI == 0) AI = Interpreter;
11318c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  assert(AI && "Interpreter should have been created already!");
11418c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  bool CreatedBytecode = false;
11518c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu  if (BytecodeFile.empty()) {
11618c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    // Emit the program to a bytecode file...
11718c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
11818c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu
11918c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    if (writeProgramToFile(BytecodeFile, Program)) {
12018c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu      std::cerr << ToolName << ": Error emitting bytecode to file '"
12118c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu                << BytecodeFile << "'!\n";
12218c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu      exit(1);
12318c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    }
12418c7c06033cafe8c0cdcbe5759c802728688b49fZhongxing Xu    CreatedBytecode = true;
125  }
126
127  if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
128
129  // Check to see if this is a valid output filename...
130  OutputFile = getUniqueFilename(OutputFile);
131
132  // Figure out which shared objects to run, if any.
133  std::vector<std::string> SharedObjs(AdditionalSOs);
134  if (!SharedObj.empty())
135    SharedObjs.push_back(SharedObj);
136
137  // Actually execute the program!
138  int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
139                                  OutputFile, SharedObjs);
140
141
142  // Remove the temporary bytecode file.
143  if (CreatedBytecode) removeFile(BytecodeFile);
144
145  // Return the filename we captured the output to.
146  return OutputFile;
147}
148
149
150std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
151  assert(Interpreter && "Interpreter should have been created already!");
152  std::string OutputCFile;
153
154  // Using CBE
155  cbe->OutputC(BytecodeFile, OutputCFile);
156
157#if 0 /* This is an alternative, as yet unimplemented */
158  // Using LLC
159  std::string Message;
160  LLC *llc = createLLCtool(Message);
161  if (llc->OutputAsm(BytecodeFile, OutputFile)) {
162    std::cerr << "Could not generate asm code with `llc', exiting.\n";
163    exit(1);
164  }
165#endif
166
167  std::string SharedObjectFile;
168  if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
169    exit(1);
170
171  // Remove the intermediate C file
172  removeFile(OutputCFile);
173
174  return SharedObjectFile;
175}
176
177
178/// diffProgram - This method executes the specified module and diffs the output
179/// against the file specified by ReferenceOutputFile.  If the output is
180/// different, true is returned.
181///
182bool BugDriver::diffProgram(const std::string &BytecodeFile,
183                            const std::string &SharedObject,
184                            bool RemoveBytecode) {
185  // Execute the program, generating an output file...
186  std::string Output = executeProgram("", BytecodeFile, SharedObject);
187
188  std::string Error;
189  bool FilesDifferent = false;
190  if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
191    if (!Error.empty()) {
192      std::cerr << "While diffing output: " << Error << "\n";
193      exit(1);
194    }
195    FilesDifferent = true;
196  }
197
198  if (RemoveBytecode) removeFile(BytecodeFile);
199  return FilesDifferent;
200}
201
202bool BugDriver::isExecutingJIT() {
203  return InterpreterSel == RunJIT;
204}
205