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