LLVM API Documentation
00001 //===-- llvm/Support/ToolRunner.h -------------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file exposes an abstraction around a platform C compiler, used to 00011 // compile C and assembly code. It also exposes an "AbstractIntepreter" 00012 // interface, which is used to execute code using one of the LLVM execution 00013 // engines. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #ifndef LLVM_SUPPORT_TOOLRUNNER_H 00018 #define LLVM_SUPPORT_TOOLRUNNER_H 00019 00020 #include "llvm/Support/SystemUtils.h" 00021 #include <exception> 00022 #include <vector> 00023 00024 namespace llvm { 00025 00026 class CBE; 00027 class LLC; 00028 00029 00030 /// ToolExecutionError - An instance of this class is thrown by the 00031 /// AbstractInterpreter instances if there is an error running a tool (e.g., LLC 00032 /// crashes) which prevents execution of the program. 00033 /// 00034 class ToolExecutionError : std::exception { 00035 std::string Message; 00036 public: 00037 explicit ToolExecutionError(const std::string &M) : Message(M) {} 00038 virtual ~ToolExecutionError() throw(); 00039 virtual const char* what() const throw() { return Message.c_str(); } 00040 }; 00041 00042 00043 //===---------------------------------------------------------------------===// 00044 // GCC abstraction 00045 // 00046 class GCC { 00047 std::string GCCPath; // The path to the gcc executable 00048 GCC(const std::string &gccPath) : GCCPath(gccPath) { } 00049 public: 00050 enum FileType { AsmFile, CFile }; 00051 00052 static GCC* create(const std::string &ProgramPath, std::string &Message); 00053 00054 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is 00055 /// either a .s file, or a .c file, specified by FileType), with the specified 00056 /// arguments. Standard input is specified with InputFile, and standard 00057 /// Output is captured to the specified OutputFile location. The SharedLibs 00058 /// option specifies optional native shared objects that can be loaded into 00059 /// the program for execution. 00060 /// 00061 int ExecuteProgram(const std::string &ProgramFile, 00062 const std::vector<std::string> &Args, 00063 FileType fileType, 00064 const std::string &InputFile, 00065 const std::string &OutputFile, 00066 const std::vector<std::string> &SharedLibs = 00067 std::vector<std::string>(), unsigned Timeout = 0); 00068 00069 /// MakeSharedObject - This compiles the specified file (which is either a .c 00070 /// file or a .s file) into a shared object. 00071 /// 00072 int MakeSharedObject(const std::string &InputFile, FileType fileType, 00073 std::string &OutputFile); 00074 }; 00075 00076 00077 //===---------------------------------------------------------------------===// 00078 /// AbstractInterpreter Class - Subclasses of this class are used to execute 00079 /// LLVM bytecode in a variety of ways. This abstract interface hides this 00080 /// complexity behind a simple interface. 00081 /// 00082 struct AbstractInterpreter { 00083 static CBE *createCBE(const std::string &ProgramPath, std::string &Message, 00084 const std::vector<std::string> *Args = 0); 00085 static LLC *createLLC(const std::string &ProgramPath, std::string &Message, 00086 const std::vector<std::string> *Args = 0); 00087 00088 static AbstractInterpreter* createLLI(const std::string &ProgramPath, 00089 std::string &Message, 00090 const std::vector<std::string> *Args=0); 00091 00092 static AbstractInterpreter* createJIT(const std::string &ProgramPath, 00093 std::string &Message, 00094 const std::vector<std::string> *Args=0); 00095 00096 00097 virtual ~AbstractInterpreter() {} 00098 00099 /// compileProgram - Compile the specified program from bytecode to executable 00100 /// code. This does not produce any output, it is only used when debugging 00101 /// the code generator. If the code generator fails, an exception should be 00102 /// thrown, otherwise, this function will just return. 00103 virtual void compileProgram(const std::string &Bytecode) {} 00104 00105 /// ExecuteProgram - Run the specified bytecode file, emitting output to the 00106 /// specified filename. This returns the exit code of the program. 00107 /// 00108 virtual int ExecuteProgram(const std::string &Bytecode, 00109 const std::vector<std::string> &Args, 00110 const std::string &InputFile, 00111 const std::string &OutputFile, 00112 const std::vector<std::string> &SharedLibs = 00113 std::vector<std::string>(), 00114 unsigned Timeout = 0) = 0; 00115 }; 00116 00117 //===---------------------------------------------------------------------===// 00118 // CBE Implementation of AbstractIntepreter interface 00119 // 00120 class CBE : public AbstractInterpreter { 00121 std::string LLCPath; // The path to the `llc' executable 00122 std::vector<std::string> ToolArgs; // Extra args to pass to LLC 00123 GCC *gcc; 00124 public: 00125 CBE(const std::string &llcPath, GCC *Gcc, 00126 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) { 00127 ToolArgs.clear (); 00128 if (Args) { ToolArgs = *Args; } 00129 } 00130 ~CBE() { delete gcc; } 00131 00132 /// compileProgram - Compile the specified program from bytecode to executable 00133 /// code. This does not produce any output, it is only used when debugging 00134 /// the code generator. If the code generator fails, an exception should be 00135 /// thrown, otherwise, this function will just return. 00136 virtual void compileProgram(const std::string &Bytecode); 00137 00138 virtual int ExecuteProgram(const std::string &Bytecode, 00139 const std::vector<std::string> &Args, 00140 const std::string &InputFile, 00141 const std::string &OutputFile, 00142 const std::vector<std::string> &SharedLibs = 00143 std::vector<std::string>(), 00144 unsigned Timeout = 0); 00145 00146 // Sometimes we just want to go half-way and only generate the .c file, not 00147 // necessarily compile it with GCC and run the program. This throws an 00148 // exception if LLC crashes. 00149 // 00150 virtual void OutputC(const std::string &Bytecode, std::string &OutputCFile); 00151 }; 00152 00153 00154 //===---------------------------------------------------------------------===// 00155 // LLC Implementation of AbstractIntepreter interface 00156 // 00157 class LLC : public AbstractInterpreter { 00158 std::string LLCPath; // The path to the LLC executable 00159 std::vector<std::string> ToolArgs; // Extra args to pass to LLC 00160 GCC *gcc; 00161 public: 00162 LLC(const std::string &llcPath, GCC *Gcc, 00163 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) { 00164 ToolArgs.clear (); 00165 if (Args) { ToolArgs = *Args; } 00166 } 00167 ~LLC() { delete gcc; } 00168 00169 /// compileProgram - Compile the specified program from bytecode to executable 00170 /// code. This does not produce any output, it is only used when debugging 00171 /// the code generator. If the code generator fails, an exception should be 00172 /// thrown, otherwise, this function will just return. 00173 virtual void compileProgram(const std::string &Bytecode); 00174 00175 virtual int ExecuteProgram(const std::string &Bytecode, 00176 const std::vector<std::string> &Args, 00177 const std::string &InputFile, 00178 const std::string &OutputFile, 00179 const std::vector<std::string> &SharedLibs = 00180 std::vector<std::string>(), 00181 unsigned Timeout = 0); 00182 00183 // Sometimes we just want to go half-way and only generate the .s file, 00184 // not necessarily compile it all the way and run the program. This throws 00185 // an exception if execution of LLC fails. 00186 // 00187 void OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile); 00188 }; 00189 00190 } // End llvm namespace 00191 00192 #endif