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/ErrorHandling.h" 23 #include "llvm/Support/SystemUtils.h" 24 #include "llvm/Support/Path.h" 25 #include <exception> 26 #include <vector> 27 28 namespace llvm { 29 30 extern cl::opt<bool> SaveTemps; 31 extern Triple TargetTriple; 32 33 class CBE; 34 class LLC; 35 36 //===---------------------------------------------------------------------===// 37 // GCC abstraction 38 // 39 class GCC { 40 sys::Path GCCPath; // The path to the gcc executable. 41 sys::Path RemoteClientPath; // The path to the rsh / ssh executable. 42 std::vector<std::string> gccArgs; // GCC-specific arguments. 43 GCC(const sys::Path &gccPath, const sys::Path &RemotePath, 44 const std::vector<std::string> *GCCArgs) 45 : GCCPath(gccPath), RemoteClientPath(RemotePath) { 46 if (GCCArgs) gccArgs = *GCCArgs; 47 } 48 public: 49 enum FileType { AsmFile, ObjectFile, CFile }; 50 51 static GCC *create(std::string &Message, 52 const std::string &GCCBinary, 53 const std::vector<std::string> *Args); 54 55 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is 56 /// either a .s file, or a .c file, specified by FileType), with the specified 57 /// arguments. Standard input is specified with InputFile, and standard 58 /// Output is captured to the specified OutputFile location. The SharedLibs 59 /// option specifies optional native shared objects that can be loaded into 60 /// the program for execution. 61 /// 62 int ExecuteProgram(const std::string &ProgramFile, 63 const std::vector<std::string> &Args, 64 FileType fileType, 65 const std::string &InputFile, 66 const std::string &OutputFile, 67 std::string *Error = 0, 68 const std::vector<std::string> &GCCArgs = 69 std::vector<std::string>(), 70 unsigned Timeout = 0, 71 unsigned MemoryLimit = 0); 72 73 /// MakeSharedObject - This compiles the specified file (which is either a .c 74 /// file or a .s file) into a shared object. 75 /// 76 int MakeSharedObject(const std::string &InputFile, FileType fileType, 77 std::string &OutputFile, 78 const std::vector<std::string> &ArgsForGCC, 79 std::string &Error); 80 }; 81 82 83 //===---------------------------------------------------------------------===// 84 /// AbstractInterpreter Class - Subclasses of this class are used to execute 85 /// LLVM bitcode in a variety of ways. This abstract interface hides this 86 /// complexity behind a simple interface. 87 /// 88 class AbstractInterpreter { 89 public: 90 static CBE *createCBE(const char *Argv0, std::string &Message, 91 const std::string &GCCBinary, 92 const std::vector<std::string> *Args = 0, 93 const std::vector<std::string> *GCCArgs = 0); 94 static LLC *createLLC(const char *Argv0, std::string &Message, 95 const std::string &GCCBinary, 96 const std::vector<std::string> *Args = 0, 97 const std::vector<std::string> *GCCArgs = 0, 98 bool UseIntegratedAssembler = false); 99 100 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message, 101 const std::vector<std::string> *Args=0); 102 103 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message, 104 const std::vector<std::string> *Args=0); 105 106 static AbstractInterpreter* 107 createCustomCompiler(std::string &Message, 108 const std::string &CompileCommandLine); 109 110 static AbstractInterpreter* 111 createCustomExecutor(std::string &Message, 112 const std::string &ExecCommandLine); 113 114 115 virtual ~AbstractInterpreter() {} 116 117 /// compileProgram - Compile the specified program from bitcode to executable 118 /// code. This does not produce any output, it is only used when debugging 119 /// the code generator. It returns false if the code generator fails. 120 virtual void compileProgram(const std::string &Bitcode, std::string *Error, 121 unsigned Timeout = 0, unsigned MemoryLimit = 0) {} 122 123 /// OutputCode - Compile the specified program from bitcode to code 124 /// understood by the GCC driver (either C or asm). If the code generator 125 /// fails, it sets Error, otherwise, this function returns the type of code 126 /// emitted. 127 virtual GCC::FileType OutputCode(const std::string &Bitcode, 128 sys::Path &OutFile, std::string &Error, 129 unsigned Timeout = 0, 130 unsigned MemoryLimit = 0) { 131 Error = "OutputCode not supported by this AbstractInterpreter!"; 132 return GCC::AsmFile; 133 } 134 135 /// ExecuteProgram - Run the specified bitcode file, emitting output to the 136 /// specified filename. This sets RetVal to the exit code of the program or 137 /// returns false if a problem was encountered that prevented execution of 138 /// the program. 139 /// 140 virtual int ExecuteProgram(const std::string &Bitcode, 141 const std::vector<std::string> &Args, 142 const std::string &InputFile, 143 const std::string &OutputFile, 144 std::string *Error, 145 const std::vector<std::string> &GCCArgs = 146 std::vector<std::string>(), 147 const std::vector<std::string> &SharedLibs = 148 std::vector<std::string>(), 149 unsigned Timeout = 0, 150 unsigned MemoryLimit = 0) = 0; 151 }; 152 153 //===---------------------------------------------------------------------===// 154 // CBE Implementation of AbstractIntepreter interface 155 // 156 class CBE : public AbstractInterpreter { 157 sys::Path LLCPath; // The path to the `llc' executable. 158 std::vector<std::string> ToolArgs; // Extra args to pass to LLC. 159 GCC *gcc; 160 public: 161 CBE(const sys::Path &llcPath, GCC *Gcc, 162 const std::vector<std::string> *Args) 163 : LLCPath(llcPath), gcc(Gcc) { 164 ToolArgs.clear (); 165 if (Args) ToolArgs = *Args; 166 } 167 ~CBE() { delete gcc; } 168 169 /// compileProgram - Compile the specified program from bitcode to executable 170 /// code. This does not produce any output, it is only used when debugging 171 /// the code generator. Returns false if the code generator fails. 172 virtual void compileProgram(const std::string &Bitcode, std::string *Error, 173 unsigned Timeout = 0, unsigned MemoryLimit = 0); 174 175 virtual int ExecuteProgram(const std::string &Bitcode, 176 const std::vector<std::string> &Args, 177 const std::string &InputFile, 178 const std::string &OutputFile, 179 std::string *Error, 180 const std::vector<std::string> &GCCArgs = 181 std::vector<std::string>(), 182 const std::vector<std::string> &SharedLibs = 183 std::vector<std::string>(), 184 unsigned Timeout = 0, 185 unsigned MemoryLimit = 0); 186 187 /// OutputCode - Compile the specified program from bitcode to code 188 /// understood by the GCC driver (either C or asm). If the code generator 189 /// fails, it sets Error, otherwise, this function returns the type of code 190 /// emitted. 191 virtual GCC::FileType OutputCode(const std::string &Bitcode, 192 sys::Path &OutFile, std::string &Error, 193 unsigned Timeout = 0, 194 unsigned MemoryLimit = 0); 195 }; 196 197 198 //===---------------------------------------------------------------------===// 199 // LLC Implementation of AbstractIntepreter interface 200 // 201 class LLC : public AbstractInterpreter { 202 std::string LLCPath; // The path to the LLC executable. 203 std::vector<std::string> ToolArgs; // Extra args to pass to LLC. 204 GCC *gcc; 205 bool UseIntegratedAssembler; 206 public: 207 LLC(const std::string &llcPath, GCC *Gcc, 208 const std::vector<std::string> *Args, 209 bool useIntegratedAssembler) 210 : LLCPath(llcPath), gcc(Gcc), 211 UseIntegratedAssembler(useIntegratedAssembler) { 212 ToolArgs.clear(); 213 if (Args) ToolArgs = *Args; 214 } 215 ~LLC() { delete gcc; } 216 217 /// compileProgram - Compile the specified program from bitcode to executable 218 /// code. This does not produce any output, it is only used when debugging 219 /// the code generator. Returns false if the code generator fails. 220 virtual void compileProgram(const std::string &Bitcode, std::string *Error, 221 unsigned Timeout = 0, unsigned MemoryLimit = 0); 222 223 virtual int ExecuteProgram(const std::string &Bitcode, 224 const std::vector<std::string> &Args, 225 const std::string &InputFile, 226 const std::string &OutputFile, 227 std::string *Error, 228 const std::vector<std::string> &GCCArgs = 229 std::vector<std::string>(), 230 const std::vector<std::string> &SharedLibs = 231 std::vector<std::string>(), 232 unsigned Timeout = 0, 233 unsigned MemoryLimit = 0); 234 235 /// OutputCode - Compile the specified program from bitcode to code 236 /// understood by the GCC driver (either C or asm). If the code generator 237 /// fails, it sets Error, otherwise, this function returns the type of code 238 /// emitted. 239 virtual GCC::FileType OutputCode(const std::string &Bitcode, 240 sys::Path &OutFile, std::string &Error, 241 unsigned Timeout = 0, 242 unsigned MemoryLimit = 0); 243 }; 244 245 } // End llvm namespace 246 247 #endif 248