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