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 sys::Path GCCPath; // The path to the gcc executable 00048 GCC(const sys::Path &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 class AbstractInterpreter { 00083 public: 00084 static CBE *createCBE(const std::string &ProgramPath, std::string &Message, 00085 const std::vector<std::string> *Args = 0); 00086 static LLC *createLLC(const std::string &ProgramPath, std::string &Message, 00087 const std::vector<std::string> *Args = 0); 00088 00089 static AbstractInterpreter* createLLI(const std::string &ProgramPath, 00090 std::string &Message, 00091 const std::vector<std::string> *Args=0); 00092 00093 static AbstractInterpreter* createJIT(const std::string &ProgramPath, 00094 std::string &Message, 00095 const std::vector<std::string> *Args=0); 00096 00097 00098 virtual ~AbstractInterpreter() {} 00099 00100 /// compileProgram - Compile the specified program from bytecode to executable 00101 /// code. This does not produce any output, it is only used when debugging 00102 /// the code generator. If the code generator fails, an exception should be 00103 /// thrown, otherwise, this function will just return. 00104 virtual void compileProgram(const std::string &Bytecode) {} 00105 00106 /// ExecuteProgram - Run the specified bytecode file, emitting output to the 00107 /// specified filename. This returns the exit code of the program. 00108 /// 00109 virtual int ExecuteProgram(const std::string &Bytecode, 00110 const std::vector<std::string> &Args, 00111 const std::string &InputFile, 00112 const std::string &OutputFile, 00113 const std::vector<std::string> &SharedLibs = 00114 std::vector<std::string>(), 00115 unsigned Timeout = 0) = 0; 00116 }; 00117 00118 //===---------------------------------------------------------------------===// 00119 // CBE Implementation of AbstractIntepreter interface 00120 // 00121 class CBE : public AbstractInterpreter { 00122 sys::Path LLCPath; // The path to the `llc' executable 00123 std::vector<std::string> ToolArgs; // Extra args to pass to LLC 00124 GCC *gcc; 00125 public: 00126 CBE(const sys::Path &llcPath, GCC *Gcc, 00127 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) { 00128 ToolArgs.clear (); 00129 if (Args) { ToolArgs = *Args; } 00130 } 00131 ~CBE() { delete gcc; } 00132 00133 /// compileProgram - Compile the specified program from bytecode to executable 00134 /// code. This does not produce any output, it is only used when debugging 00135 /// the code generator. If the code generator fails, an exception should be 00136 /// thrown, otherwise, this function will just return. 00137 virtual void compileProgram(const std::string &Bytecode); 00138 00139 virtual int ExecuteProgram(const std::string &Bytecode, 00140 const std::vector<std::string> &Args, 00141 const std::string &InputFile, 00142 const std::string &OutputFile, 00143 const std::vector<std::string> &SharedLibs = 00144 std::vector<std::string>(), 00145 unsigned Timeout = 0); 00146 00147 // Sometimes we just want to go half-way and only generate the .c file, not 00148 // necessarily compile it with GCC and run the program. This throws an 00149 // exception if LLC crashes. 00150 // 00151 virtual void OutputC(const std::string &Bytecode, sys::Path& OutputCFile); 00152 }; 00153 00154 00155 //===---------------------------------------------------------------------===// 00156 // LLC Implementation of AbstractIntepreter interface 00157 // 00158 class LLC : public AbstractInterpreter { 00159 std::string LLCPath; // The path to the LLC executable 00160 std::vector<std::string> ToolArgs; // Extra args to pass to LLC 00161 GCC *gcc; 00162 public: 00163 LLC(const std::string &llcPath, GCC *Gcc, 00164 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) { 00165 ToolArgs.clear (); 00166 if (Args) { ToolArgs = *Args; } 00167 } 00168 ~LLC() { delete gcc; } 00169 00170 /// compileProgram - Compile the specified program from bytecode to executable 00171 /// code. This does not produce any output, it is only used when debugging 00172 /// the code generator. If the code generator fails, an exception should be 00173 /// thrown, otherwise, this function will just return. 00174 virtual void compileProgram(const std::string &Bytecode); 00175 00176 virtual int ExecuteProgram(const std::string &Bytecode, 00177 const std::vector<std::string> &Args, 00178 const std::string &InputFile, 00179 const std::string &OutputFile, 00180 const std::vector<std::string> &SharedLibs = 00181 std::vector<std::string>(), 00182 unsigned Timeout = 0); 00183 00184 // Sometimes we just want to go half-way and only generate the .s file, 00185 // not necessarily compile it all the way and run the program. This throws 00186 // an exception if execution of LLC fails. 00187 // 00188 void OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile); 00189 }; 00190 00191 } // End llvm namespace 00192 00193 #endif