ToolRunner.h revision 193323
1130803Smarcel//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===// 2130803Smarcel// 3130803Smarcel// The LLVM Compiler Infrastructure 4130803Smarcel// 5130803Smarcel// This file is distributed under the University of Illinois Open Source 6130803Smarcel// License. See LICENSE.TXT for details. 7130803Smarcel// 8130803Smarcel//===----------------------------------------------------------------------===// 9130803Smarcel// 10130803Smarcel// This file exposes an abstraction around a platform C compiler, used to 11130803Smarcel// compile C and assembly code. It also exposes an "AbstractIntepreter" 12130803Smarcel// interface, which is used to execute code using one of the LLVM execution 13130803Smarcel// engines. 14130803Smarcel// 15130803Smarcel//===----------------------------------------------------------------------===// 16130803Smarcel 17130803Smarcel#ifndef BUGPOINT_TOOLRUNNER_H 18130803Smarcel#define BUGPOINT_TOOLRUNNER_H 19130803Smarcel 20130803Smarcel#include "llvm/Support/SystemUtils.h" 21130803Smarcel#include <exception> 22130803Smarcel#include <vector> 23130803Smarcel 24130803Smarcelnamespace llvm { 25130803Smarcel 26130803Smarcelclass CBE; 27130803Smarcelclass LLC; 28130803Smarcel 29130803Smarcel/// ToolExecutionError - An instance of this class is thrown by the 30130803Smarcel/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC 31130803Smarcel/// crashes) which prevents execution of the program. 32130803Smarcel/// 33130803Smarcelclass ToolExecutionError : std::exception { 34130803Smarcel std::string Message; 35130803Smarcelpublic: 36130803Smarcel explicit ToolExecutionError(const std::string &M) : Message(M) {} 37130803Smarcel virtual ~ToolExecutionError() throw(); 38130803Smarcel virtual const char* what() const throw() { return Message.c_str(); } 39130803Smarcel}; 40130803Smarcel 41130803Smarcel 42130803Smarcel//===---------------------------------------------------------------------===// 43130803Smarcel// GCC abstraction 44130803Smarcel// 45130803Smarcelclass GCC { 46130803Smarcel sys::Path GCCPath; // The path to the gcc executable. 47130803Smarcel sys::Path RemoteClientPath; // The path to the rsh / ssh executable. 48130803Smarcel std::vector<std::string> gccArgs; // GCC-specific arguments. 49130803Smarcel GCC(const sys::Path &gccPath, const sys::Path &RemotePath, 50130803Smarcel const std::vector<std::string> *GCCArgs) 51130803Smarcel : GCCPath(gccPath), RemoteClientPath(RemotePath) { 52130803Smarcel if (GCCArgs) gccArgs = *GCCArgs; 53130803Smarcel } 54130803Smarcelpublic: 55130803Smarcel enum FileType { AsmFile, CFile }; 56130803Smarcel 57130803Smarcel static GCC *create(const std::string &ProgramPath, std::string &Message, 58130803Smarcel const std::vector<std::string> *Args); 59130803Smarcel 60130803Smarcel /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is 61130803Smarcel /// either a .s file, or a .c file, specified by FileType), with the specified 62130803Smarcel /// arguments. Standard input is specified with InputFile, and standard 63130803Smarcel /// Output is captured to the specified OutputFile location. The SharedLibs 64130803Smarcel /// option specifies optional native shared objects that can be loaded into 65130803Smarcel /// the program for execution. 66130803Smarcel /// 67130803Smarcel int ExecuteProgram(const std::string &ProgramFile, 68130803Smarcel const std::vector<std::string> &Args, 69130803Smarcel FileType fileType, 70130803Smarcel const std::string &InputFile, 71130803Smarcel const std::string &OutputFile, 72130803Smarcel const std::vector<std::string> &GCCArgs = 73130803Smarcel std::vector<std::string>(), 74130803Smarcel unsigned Timeout = 0, 75130803Smarcel unsigned MemoryLimit = 0); 76130803Smarcel 77130803Smarcel /// MakeSharedObject - This compiles the specified file (which is either a .c 78130803Smarcel /// file or a .s file) into a shared object. 79130803Smarcel /// 80130803Smarcel int MakeSharedObject(const std::string &InputFile, FileType fileType, 81130803Smarcel std::string &OutputFile, 82130803Smarcel const std::vector<std::string> &ArgsForGCC); 83130803Smarcel}; 84130803Smarcel 85130803Smarcel 86130803Smarcel//===---------------------------------------------------------------------===// 87130803Smarcel/// AbstractInterpreter Class - Subclasses of this class are used to execute 88130803Smarcel/// LLVM bitcode in a variety of ways. This abstract interface hides this 89130803Smarcel/// complexity behind a simple interface. 90130803Smarcel/// 91130803Smarcelclass AbstractInterpreter { 92130803Smarcelpublic: 93130803Smarcel static CBE *createCBE(const std::string &ProgramPath, std::string &Message, 94130803Smarcel const std::vector<std::string> *Args = 0, 95130803Smarcel const std::vector<std::string> *GCCArgs = 0); 96130803Smarcel static LLC *createLLC(const std::string &ProgramPath, std::string &Message, 97130803Smarcel const std::vector<std::string> *Args = 0, 98130803Smarcel const std::vector<std::string> *GCCArgs = 0); 99130803Smarcel 100130803Smarcel static AbstractInterpreter* createLLI(const std::string &ProgramPath, 101130803Smarcel std::string &Message, 102130803Smarcel const std::vector<std::string> *Args=0); 103130803Smarcel 104130803Smarcel static AbstractInterpreter* createJIT(const std::string &ProgramPath, 105130803Smarcel std::string &Message, 106130803Smarcel const std::vector<std::string> *Args=0); 107130803Smarcel 108130803Smarcel static AbstractInterpreter* createCustom(const std::string &ProgramPath, 109130803Smarcel std::string &Message, 110130803Smarcel const std::string &ExecCommandLine); 111130803Smarcel 112130803Smarcel 113130803Smarcel virtual ~AbstractInterpreter() {} 114130803Smarcel 115130803Smarcel /// compileProgram - Compile the specified program from bitcode to executable 116130803Smarcel /// code. This does not produce any output, it is only used when debugging 117130803Smarcel /// the code generator. If the code generator fails, an exception should be 118130803Smarcel /// thrown, otherwise, this function will just return. 119130803Smarcel virtual void compileProgram(const std::string &Bitcode) {} 120130803Smarcel 121130803Smarcel /// OutputCode - Compile the specified program from bitcode to code 122130803Smarcel /// understood by the GCC driver (either C or asm). If the code generator 123130803Smarcel /// fails, an exception should be thrown, otherwise, this function returns the 124130803Smarcel /// type of code emitted. 125130803Smarcel virtual GCC::FileType OutputCode(const std::string &Bitcode, 126130803Smarcel sys::Path &OutFile) { 127130803Smarcel throw std::string("OutputCode not supported by this AbstractInterpreter!"); 128130803Smarcel } 129130803Smarcel 130130803Smarcel /// ExecuteProgram - Run the specified bitcode file, emitting output to the 131130803Smarcel /// specified filename. This returns the exit code of the program. 132130803Smarcel /// 133130803Smarcel virtual int ExecuteProgram(const std::string &Bitcode, 134130803Smarcel const std::vector<std::string> &Args, 135130803Smarcel const std::string &InputFile, 136130803Smarcel const std::string &OutputFile, 137130803Smarcel const std::vector<std::string> &GCCArgs = 138130803Smarcel std::vector<std::string>(), 139130803Smarcel const std::vector<std::string> &SharedLibs = 140130803Smarcel std::vector<std::string>(), 141130803Smarcel unsigned Timeout = 0, 142130803Smarcel unsigned MemoryLimit = 0) = 0; 143130803Smarcel}; 144130803Smarcel 145130803Smarcel//===---------------------------------------------------------------------===// 146130803Smarcel// CBE Implementation of AbstractIntepreter interface 147130803Smarcel// 148130803Smarcelclass CBE : public AbstractInterpreter { 149130803Smarcel sys::Path LLCPath; // The path to the `llc' executable. 150130803Smarcel std::vector<std::string> ToolArgs; // Extra args to pass to LLC. 151130803Smarcel GCC *gcc; 152130803Smarcelpublic: 153130803Smarcel CBE(const sys::Path &llcPath, GCC *Gcc, 154130803Smarcel const std::vector<std::string> *Args) 155130803Smarcel : LLCPath(llcPath), gcc(Gcc) { 156130803Smarcel ToolArgs.clear (); 157130803Smarcel if (Args) ToolArgs = *Args; 158130803Smarcel } 159130803Smarcel ~CBE() { delete gcc; } 160130803Smarcel 161130803Smarcel /// compileProgram - Compile the specified program from bitcode to executable 162130803Smarcel /// code. This does not produce any output, it is only used when debugging 163130803Smarcel /// the code generator. If the code generator fails, an exception should be 164130803Smarcel /// thrown, otherwise, this function will just return. 165130803Smarcel virtual void compileProgram(const std::string &Bitcode); 166130803Smarcel 167130803Smarcel virtual int ExecuteProgram(const std::string &Bitcode, 168130803Smarcel const std::vector<std::string> &Args, 169130803Smarcel const std::string &InputFile, 170130803Smarcel const std::string &OutputFile, 171130803Smarcel const std::vector<std::string> &GCCArgs = 172130803Smarcel std::vector<std::string>(), 173130803Smarcel const std::vector<std::string> &SharedLibs = 174130803Smarcel std::vector<std::string>(), 175130803Smarcel unsigned Timeout = 0, 176130803Smarcel unsigned MemoryLimit = 0); 177130803Smarcel 178130803Smarcel /// OutputCode - Compile the specified program from bitcode to code 179130803Smarcel /// understood by the GCC driver (either C or asm). If the code generator 180130803Smarcel /// fails, an exception should be thrown, otherwise, this function returns the 181130803Smarcel /// type of code emitted. 182130803Smarcel virtual GCC::FileType OutputCode(const std::string &Bitcode, 183130803Smarcel sys::Path &OutFile); 184130803Smarcel}; 185130803Smarcel 186130803Smarcel 187130803Smarcel//===---------------------------------------------------------------------===// 188130803Smarcel// LLC Implementation of AbstractIntepreter interface 189130803Smarcel// 190130803Smarcelclass LLC : public AbstractInterpreter { 191130803Smarcel std::string LLCPath; // The path to the LLC executable. 192130803Smarcel std::vector<std::string> ToolArgs; // Extra args to pass to LLC. 193130803Smarcel std::vector<std::string> gccArgs; // Extra args to pass to GCC. 194130803Smarcel GCC *gcc; 195130803Smarcelpublic: 196130803Smarcel LLC(const std::string &llcPath, GCC *Gcc, 197130803Smarcel const std::vector<std::string> *Args, 198130803Smarcel const std::vector<std::string> *GCCArgs) 199130803Smarcel : LLCPath(llcPath), gcc(Gcc) { 200130803Smarcel ToolArgs.clear(); 201130803Smarcel if (Args) ToolArgs = *Args; 202130803Smarcel if (GCCArgs) gccArgs = *GCCArgs; 203130803Smarcel } 204130803Smarcel ~LLC() { delete gcc; } 205130803Smarcel 206130803Smarcel /// compileProgram - Compile the specified program from bitcode to executable 207130803Smarcel /// code. This does not produce any output, it is only used when debugging 208130803Smarcel /// the code generator. If the code generator fails, an exception should be 209130803Smarcel /// thrown, otherwise, this function will just return. 210130803Smarcel virtual void compileProgram(const std::string &Bitcode); 211130803Smarcel 212130803Smarcel virtual int ExecuteProgram(const std::string &Bitcode, 213130803Smarcel const std::vector<std::string> &Args, 214130803Smarcel const std::string &InputFile, 215130803Smarcel const std::string &OutputFile, 216130803Smarcel const std::vector<std::string> &GCCArgs = 217130803Smarcel std::vector<std::string>(), 218130803Smarcel const std::vector<std::string> &SharedLibs = 219130803Smarcel std::vector<std::string>(), 220130803Smarcel unsigned Timeout = 0, 221130803Smarcel unsigned MemoryLimit = 0); 222130803Smarcel 223130803Smarcel virtual GCC::FileType OutputCode(const std::string &Bitcode, 224130803Smarcel sys::Path &OutFile); 225130803Smarcel 226130803Smarcel}; 227130803Smarcel 228130803Smarcel} // End llvm namespace 229130803Smarcel 230130803Smarcel#endif 231130803Smarcel