ToolRunner.h revision 205218
1//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file exposes an abstraction around a platform C compiler, used to 11// compile C and assembly code. It also exposes an "AbstractIntepreter" 12// interface, which is used to execute code using one of the LLVM execution 13// engines. 14// 15//===----------------------------------------------------------------------===// 16 17#ifndef BUGPOINT_TOOLRUNNER_H 18#define BUGPOINT_TOOLRUNNER_H 19 20#include "llvm/ADT/Triple.h" 21#include "llvm/Support/CommandLine.h" 22#include "llvm/Support/SystemUtils.h" 23#include "llvm/System/Path.h" 24#include <exception> 25#include <vector> 26 27namespace llvm { 28 29extern cl::opt<bool> SaveTemps; 30extern Triple TargetTriple; 31 32class CBE; 33class LLC; 34 35/// ToolExecutionError - An instance of this class is thrown by the 36/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC 37/// crashes) which prevents execution of the program. 38/// 39class ToolExecutionError : std::exception { 40 std::string Message; 41public: 42 explicit ToolExecutionError(const std::string &M) : Message(M) {} 43 virtual ~ToolExecutionError() throw(); 44 virtual const char* what() const throw() { return Message.c_str(); } 45}; 46 47 48//===---------------------------------------------------------------------===// 49// GCC abstraction 50// 51class GCC { 52 sys::Path GCCPath; // The path to the gcc executable. 53 sys::Path RemoteClientPath; // The path to the rsh / ssh executable. 54 std::vector<std::string> gccArgs; // GCC-specific arguments. 55 GCC(const sys::Path &gccPath, const sys::Path &RemotePath, 56 const std::vector<std::string> *GCCArgs) 57 : GCCPath(gccPath), RemoteClientPath(RemotePath) { 58 if (GCCArgs) gccArgs = *GCCArgs; 59 } 60public: 61 enum FileType { AsmFile, ObjectFile, CFile }; 62 63 static GCC *create(std::string &Message, 64 const std::vector<std::string> *Args); 65 66 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is 67 /// either a .s file, or a .c file, specified by FileType), with the specified 68 /// arguments. Standard input is specified with InputFile, and standard 69 /// Output is captured to the specified OutputFile location. The SharedLibs 70 /// option specifies optional native shared objects that can be loaded into 71 /// the program for execution. 72 /// 73 int ExecuteProgram(const std::string &ProgramFile, 74 const std::vector<std::string> &Args, 75 FileType fileType, 76 const std::string &InputFile, 77 const std::string &OutputFile, 78 const std::vector<std::string> &GCCArgs = 79 std::vector<std::string>(), 80 unsigned Timeout = 0, 81 unsigned MemoryLimit = 0); 82 83 /// MakeSharedObject - This compiles the specified file (which is either a .c 84 /// file or a .s file) into a shared object. 85 /// 86 int MakeSharedObject(const std::string &InputFile, FileType fileType, 87 std::string &OutputFile, 88 const std::vector<std::string> &ArgsForGCC); 89}; 90 91 92//===---------------------------------------------------------------------===// 93/// AbstractInterpreter Class - Subclasses of this class are used to execute 94/// LLVM bitcode in a variety of ways. This abstract interface hides this 95/// complexity behind a simple interface. 96/// 97class AbstractInterpreter { 98public: 99 static CBE *createCBE(const char *Argv0, std::string &Message, 100 const std::vector<std::string> *Args = 0, 101 const std::vector<std::string> *GCCArgs = 0); 102 static LLC *createLLC(const char *Argv0, std::string &Message, 103 const std::vector<std::string> *Args = 0, 104 const std::vector<std::string> *GCCArgs = 0, 105 bool UseIntegratedAssembler = false); 106 107 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message, 108 const std::vector<std::string> *Args=0); 109 110 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message, 111 const std::vector<std::string> *Args=0); 112 113 static AbstractInterpreter* createCustom(std::string &Message, 114 const std::string &ExecCommandLine); 115 116 117 virtual ~AbstractInterpreter() {} 118 119 /// compileProgram - Compile the specified program from bitcode to executable 120 /// code. This does not produce any output, it is only used when debugging 121 /// the code generator. If the code generator fails, an exception should be 122 /// thrown, otherwise, this function will just return. 123 virtual void compileProgram(const std::string &Bitcode) {} 124 125 /// OutputCode - Compile the specified program from bitcode to code 126 /// understood by the GCC driver (either C or asm). If the code generator 127 /// fails, an exception should be thrown, otherwise, this function returns the 128 /// type of code emitted. 129 virtual GCC::FileType OutputCode(const std::string &Bitcode, 130 sys::Path &OutFile) { 131 throw std::string("OutputCode not supported by this AbstractInterpreter!"); 132 } 133 134 /// ExecuteProgram - Run the specified bitcode file, emitting output to the 135 /// specified filename. This returns the exit code of the program. 136 /// 137 virtual int ExecuteProgram(const std::string &Bitcode, 138 const std::vector<std::string> &Args, 139 const std::string &InputFile, 140 const std::string &OutputFile, 141 const std::vector<std::string> &GCCArgs = 142 std::vector<std::string>(), 143 const std::vector<std::string> &SharedLibs = 144 std::vector<std::string>(), 145 unsigned Timeout = 0, 146 unsigned MemoryLimit = 0) = 0; 147}; 148 149//===---------------------------------------------------------------------===// 150// CBE Implementation of AbstractIntepreter interface 151// 152class CBE : public AbstractInterpreter { 153 sys::Path LLCPath; // The path to the `llc' executable. 154 std::vector<std::string> ToolArgs; // Extra args to pass to LLC. 155 GCC *gcc; 156public: 157 CBE(const sys::Path &llcPath, GCC *Gcc, 158 const std::vector<std::string> *Args) 159 : LLCPath(llcPath), gcc(Gcc) { 160 ToolArgs.clear (); 161 if (Args) ToolArgs = *Args; 162 } 163 ~CBE() { delete gcc; } 164 165 /// compileProgram - Compile the specified program from bitcode to executable 166 /// code. This does not produce any output, it is only used when debugging 167 /// the code generator. If the code generator fails, an exception should be 168 /// thrown, otherwise, this function will just return. 169 virtual void compileProgram(const std::string &Bitcode); 170 171 virtual int ExecuteProgram(const std::string &Bitcode, 172 const std::vector<std::string> &Args, 173 const std::string &InputFile, 174 const std::string &OutputFile, 175 const std::vector<std::string> &GCCArgs = 176 std::vector<std::string>(), 177 const std::vector<std::string> &SharedLibs = 178 std::vector<std::string>(), 179 unsigned Timeout = 0, 180 unsigned MemoryLimit = 0); 181 182 /// OutputCode - Compile the specified program from bitcode to code 183 /// understood by the GCC driver (either C or asm). If the code generator 184 /// fails, an exception should be thrown, otherwise, this function returns the 185 /// type of code emitted. 186 virtual GCC::FileType OutputCode(const std::string &Bitcode, 187 sys::Path &OutFile); 188}; 189 190 191//===---------------------------------------------------------------------===// 192// LLC Implementation of AbstractIntepreter interface 193// 194class LLC : public AbstractInterpreter { 195 std::string LLCPath; // The path to the LLC executable. 196 std::vector<std::string> ToolArgs; // Extra args to pass to LLC. 197 std::vector<std::string> gccArgs; // Extra args to pass to GCC. 198 GCC *gcc; 199 bool UseIntegratedAssembler; 200public: 201 LLC(const std::string &llcPath, GCC *Gcc, 202 const std::vector<std::string> *Args, 203 const std::vector<std::string> *GCCArgs, 204 bool useIntegratedAssembler) 205 : LLCPath(llcPath), gcc(Gcc), 206 UseIntegratedAssembler(useIntegratedAssembler) { 207 ToolArgs.clear(); 208 if (Args) ToolArgs = *Args; 209 if (GCCArgs) gccArgs = *GCCArgs; 210 } 211 ~LLC() { delete gcc; } 212 213 /// compileProgram - Compile the specified program from bitcode to executable 214 /// code. This does not produce any output, it is only used when debugging 215 /// the code generator. If the code generator fails, an exception should be 216 /// thrown, otherwise, this function will just return. 217 virtual void compileProgram(const std::string &Bitcode); 218 219 virtual int ExecuteProgram(const std::string &Bitcode, 220 const std::vector<std::string> &Args, 221 const std::string &InputFile, 222 const std::string &OutputFile, 223 const std::vector<std::string> &GCCArgs = 224 std::vector<std::string>(), 225 const std::vector<std::string> &SharedLibs = 226 std::vector<std::string>(), 227 unsigned Timeout = 0, 228 unsigned MemoryLimit = 0); 229 230 virtual GCC::FileType OutputCode(const std::string &Bitcode, 231 sys::Path &OutFile); 232 233}; 234 235} // End llvm namespace 236 237#endif 238