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