Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5f5a573 | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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/Support/SystemUtils.h" |
| 21 | #include <exception> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class CBE; |
| 27 | class LLC; |
| 28 | |
| 29 | /// ToolExecutionError - An instance of this class is thrown by the |
| 30 | /// AbstractInterpreter instances if there is an error running a tool (e.g., LLC |
| 31 | /// crashes) which prevents execution of the program. |
| 32 | /// |
| 33 | class ToolExecutionError : std::exception { |
| 34 | std::string Message; |
| 35 | public: |
| 36 | explicit ToolExecutionError(const std::string &M) : Message(M) {} |
| 37 | virtual ~ToolExecutionError() throw(); |
| 38 | virtual const char* what() const throw() { return Message.c_str(); } |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | //===---------------------------------------------------------------------===// |
| 43 | // GCC abstraction |
| 44 | // |
| 45 | class GCC { |
Bill Wendling | cd1389d | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 46 | sys::Path GCCPath; // The path to the gcc executable. |
| 47 | sys::Path RemoteClientPath; // The path to the rsh / ssh executable. |
| 48 | std::vector<std::string> gccArgs; // GCC-specific arguments. |
| 49 | GCC(const sys::Path &gccPath, const sys::Path &RemotePath, |
| 50 | const std::vector<std::string> *GCCArgs) |
| 51 | : GCCPath(gccPath), RemoteClientPath(RemotePath) { |
| 52 | if (GCCArgs) gccArgs = *GCCArgs; |
| 53 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | public: |
| 55 | enum FileType { AsmFile, CFile }; |
| 56 | |
Bill Wendling | cd1389d | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 57 | static GCC *create(const std::string &ProgramPath, std::string &Message, |
| 58 | const std::vector<std::string> *Args); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | |
| 60 | /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is |
| 61 | /// either a .s file, or a .c file, specified by FileType), with the specified |
| 62 | /// arguments. Standard input is specified with InputFile, and standard |
| 63 | /// Output is captured to the specified OutputFile location. The SharedLibs |
| 64 | /// option specifies optional native shared objects that can be loaded into |
| 65 | /// the program for execution. |
| 66 | /// |
| 67 | int ExecuteProgram(const std::string &ProgramFile, |
| 68 | const std::vector<std::string> &Args, |
| 69 | FileType fileType, |
| 70 | const std::string &InputFile, |
| 71 | const std::string &OutputFile, |
| 72 | const std::vector<std::string> &GCCArgs = |
| 73 | std::vector<std::string>(), |
| 74 | unsigned Timeout = 0, |
| 75 | unsigned MemoryLimit = 0); |
| 76 | |
| 77 | /// MakeSharedObject - This compiles the specified file (which is either a .c |
| 78 | /// file or a .s file) into a shared object. |
| 79 | /// |
| 80 | int MakeSharedObject(const std::string &InputFile, FileType fileType, |
| 81 | std::string &OutputFile, |
| 82 | const std::vector<std::string> &ArgsForGCC); |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | //===---------------------------------------------------------------------===// |
| 87 | /// AbstractInterpreter Class - Subclasses of this class are used to execute |
| 88 | /// LLVM bitcode in a variety of ways. This abstract interface hides this |
| 89 | /// complexity behind a simple interface. |
| 90 | /// |
| 91 | class AbstractInterpreter { |
| 92 | public: |
| 93 | static CBE *createCBE(const std::string &ProgramPath, std::string &Message, |
Bill Wendling | cd1389d | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 94 | const std::vector<std::string> *Args = 0, |
| 95 | const std::vector<std::string> *GCCArgs = 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | static LLC *createLLC(const std::string &ProgramPath, std::string &Message, |
Bill Wendling | cd1389d | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 97 | const std::vector<std::string> *Args = 0, |
| 98 | const std::vector<std::string> *GCCArgs = 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | |
| 100 | static AbstractInterpreter* createLLI(const std::string &ProgramPath, |
| 101 | std::string &Message, |
| 102 | const std::vector<std::string> *Args=0); |
| 103 | |
| 104 | static AbstractInterpreter* createJIT(const std::string &ProgramPath, |
| 105 | std::string &Message, |
| 106 | const std::vector<std::string> *Args=0); |
| 107 | |
Anton Korobeynikov | fc0116f | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 108 | static AbstractInterpreter* createCustom(const std::string &ProgramPath, |
| 109 | std::string &Message, |
| 110 | const std::string &ExecCommandLine); |
| 111 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | |
| 113 | virtual ~AbstractInterpreter() {} |
| 114 | |
| 115 | /// compileProgram - Compile the specified program from bitcode to executable |
| 116 | /// code. This does not produce any output, it is only used when debugging |
| 117 | /// the code generator. If the code generator fails, an exception should be |
| 118 | /// thrown, otherwise, this function will just return. |
| 119 | virtual void compileProgram(const std::string &Bitcode) {} |
| 120 | |
| 121 | /// OutputCode - Compile the specified program from bitcode to code |
| 122 | /// understood by the GCC driver (either C or asm). If the code generator |
| 123 | /// fails, an exception should be thrown, otherwise, this function returns the |
| 124 | /// type of code emitted. |
| 125 | virtual GCC::FileType OutputCode(const std::string &Bitcode, |
| 126 | sys::Path &OutFile) { |
| 127 | throw std::string("OutputCode not supported by this AbstractInterpreter!"); |
| 128 | } |
| 129 | |
| 130 | /// ExecuteProgram - Run the specified bitcode file, emitting output to the |
| 131 | /// specified filename. This returns the exit code of the program. |
| 132 | /// |
| 133 | virtual int ExecuteProgram(const std::string &Bitcode, |
| 134 | const std::vector<std::string> &Args, |
| 135 | const std::string &InputFile, |
| 136 | const std::string &OutputFile, |
| 137 | const std::vector<std::string> &GCCArgs = |
| 138 | std::vector<std::string>(), |
| 139 | const std::vector<std::string> &SharedLibs = |
| 140 | std::vector<std::string>(), |
| 141 | unsigned Timeout = 0, |
| 142 | unsigned MemoryLimit = 0) = 0; |
| 143 | }; |
| 144 | |
| 145 | //===---------------------------------------------------------------------===// |
| 146 | // CBE Implementation of AbstractIntepreter interface |
| 147 | // |
| 148 | class CBE : public AbstractInterpreter { |
Bill Wendling | cd1389d | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 149 | sys::Path LLCPath; // The path to the `llc' executable. |
| 150 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 151 | GCC *gcc; |
| 152 | public: |
| 153 | CBE(const sys::Path &llcPath, GCC *Gcc, |
Bill Wendling | cd1389d | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 154 | const std::vector<std::string> *Args) |
| 155 | : LLCPath(llcPath), gcc(Gcc) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 156 | ToolArgs.clear (); |
Bill Wendling | cd1389d | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 157 | if (Args) ToolArgs = *Args; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 158 | } |
| 159 | ~CBE() { delete gcc; } |
| 160 | |
| 161 | /// compileProgram - Compile the specified program from bitcode to executable |
| 162 | /// code. This does not produce any output, it is only used when debugging |
| 163 | /// the code generator. If the code generator fails, an exception should be |
| 164 | /// thrown, otherwise, this function will just return. |
| 165 | virtual void compileProgram(const std::string &Bitcode); |
| 166 | |
| 167 | virtual int ExecuteProgram(const std::string &Bitcode, |
| 168 | const std::vector<std::string> &Args, |
| 169 | const std::string &InputFile, |
| 170 | const std::string &OutputFile, |
| 171 | const std::vector<std::string> &GCCArgs = |
| 172 | std::vector<std::string>(), |
| 173 | const std::vector<std::string> &SharedLibs = |
| 174 | std::vector<std::string>(), |
| 175 | unsigned Timeout = 0, |
| 176 | unsigned MemoryLimit = 0); |
| 177 | |
| 178 | /// OutputCode - Compile the specified program from bitcode to code |
| 179 | /// understood by the GCC driver (either C or asm). If the code generator |
| 180 | /// fails, an exception should be thrown, otherwise, this function returns the |
| 181 | /// type of code emitted. |
| 182 | virtual GCC::FileType OutputCode(const std::string &Bitcode, |
| 183 | sys::Path &OutFile); |
| 184 | }; |
| 185 | |
| 186 | |
| 187 | //===---------------------------------------------------------------------===// |
| 188 | // LLC Implementation of AbstractIntepreter interface |
| 189 | // |
| 190 | class LLC : public AbstractInterpreter { |
Bill Wendling | cd1389d | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 191 | std::string LLCPath; // The path to the LLC executable. |
| 192 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC. |
| 193 | std::vector<std::string> gccArgs; // Extra args to pass to GCC. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 194 | GCC *gcc; |
| 195 | public: |
| 196 | LLC(const std::string &llcPath, GCC *Gcc, |
Bill Wendling | cd1389d | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 197 | const std::vector<std::string> *Args, |
| 198 | const std::vector<std::string> *GCCArgs) |
| 199 | : LLCPath(llcPath), gcc(Gcc) { |
| 200 | ToolArgs.clear(); |
| 201 | if (Args) ToolArgs = *Args; |
| 202 | if (GCCArgs) gccArgs = *GCCArgs; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. If the code generator fails, an exception should be |
| 209 | /// thrown, otherwise, this function will just return. |
| 210 | virtual void compileProgram(const std::string &Bitcode); |
| 211 | |
| 212 | virtual int ExecuteProgram(const std::string &Bitcode, |
| 213 | const std::vector<std::string> &Args, |
| 214 | const std::string &InputFile, |
| 215 | const std::string &OutputFile, |
| 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 | virtual GCC::FileType OutputCode(const std::string &Bitcode, |
| 224 | sys::Path &OutFile); |
| 225 | |
| 226 | }; |
| 227 | |
| 228 | } // End llvm namespace |
| 229 | |
| 230 | #endif |