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