Chris Lattner | f1b20d8 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 1 | //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===// |
Misha Brukman | 63b3afa | 2005-04-21 20:48:15 +0000 | [diff] [blame] | 2 | // |
John Criswell | 6fbcc26 | 2003-10-20 20:19:47 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 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. |
Misha Brukman | 63b3afa | 2005-04-21 20:48:15 +0000 | [diff] [blame] | 7 | // |
John Criswell | 6fbcc26 | 2003-10-20 20:19:47 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | f474449 | 2003-09-30 18:28:53 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 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. |
Chris Lattner | f474449 | 2003-09-30 18:28:53 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | f1b20d8 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 17 | #ifndef BUGPOINT_TOOLRUNNER_H |
| 18 | #define BUGPOINT_TOOLRUNNER_H |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 19 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/SystemUtils.h" |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 21 | #include <exception> |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 26 | class CBE; |
| 27 | class LLC; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 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 | /// |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 33 | class ToolExecutionError : std::exception { |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 34 | std::string Message; |
| 35 | public: |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 36 | explicit ToolExecutionError(const std::string &M) : Message(M) {} |
| 37 | virtual ~ToolExecutionError() throw(); |
| 38 | virtual const char* what() const throw() { return Message.c_str(); } |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 42 | //===---------------------------------------------------------------------===// |
| 43 | // GCC abstraction |
| 44 | // |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 45 | class GCC { |
Bill Wendling | 38efa38 | 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 | } |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 54 | public: |
| 55 | enum FileType { AsmFile, CFile }; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 56 | |
Bill Wendling | 38efa38 | 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); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 59 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 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 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 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, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 72 | const std::vector<std::string> &GCCArgs = |
| 73 | std::vector<std::string>(), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 74 | unsigned Timeout = 0, |
| 75 | unsigned MemoryLimit = 0); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 76 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 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, |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 81 | std::string &OutputFile, |
| 82 | const std::vector<std::string> &ArgsForGCC); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 86 | //===---------------------------------------------------------------------===// |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 87 | /// AbstractInterpreter Class - Subclasses of this class are used to execute |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 88 | /// LLVM bitcode in a variety of ways. This abstract interface hides this |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 89 | /// complexity behind a simple interface. |
| 90 | /// |
Jeff Cohen | 8388195 | 2005-01-22 16:30:58 +0000 | [diff] [blame] | 91 | class AbstractInterpreter { |
| 92 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 93 | static CBE *createCBE(const std::string &ProgramPath, std::string &Message, |
Bill Wendling | 38efa38 | 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); |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 96 | static LLC *createLLC(const std::string &ProgramPath, std::string &Message, |
Bill Wendling | 38efa38 | 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); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 99 | |
| 100 | static AbstractInterpreter* createLLI(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 101 | std::string &Message, |
| 102 | const std::vector<std::string> *Args=0); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 103 | |
| 104 | static AbstractInterpreter* createJIT(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 105 | std::string &Message, |
| 106 | const std::vector<std::string> *Args=0); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 107 | |
Anton Korobeynikov | 9ef7425 | 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 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 112 | |
| 113 | virtual ~AbstractInterpreter() {} |
| 114 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 115 | /// compileProgram - Compile the specified program from bitcode to executable |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 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. |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 119 | virtual void compileProgram(const std::string &Bitcode) {} |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 120 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 121 | /// OutputCode - Compile the specified program from bitcode to code |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 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. |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 125 | virtual GCC::FileType OutputCode(const std::string &Bitcode, |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 126 | sys::Path &OutFile) { |
| 127 | throw std::string("OutputCode not supported by this AbstractInterpreter!"); |
| 128 | } |
| 129 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 130 | /// ExecuteProgram - Run the specified bitcode file, emitting output to the |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 131 | /// specified filename. This returns the exit code of the program. |
| 132 | /// |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 133 | virtual int ExecuteProgram(const std::string &Bitcode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 134 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 135 | const std::string &InputFile, |
| 136 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 137 | const std::vector<std::string> &GCCArgs = |
| 138 | std::vector<std::string>(), |
Misha Brukman | 63b3afa | 2005-04-21 20:48:15 +0000 | [diff] [blame] | 139 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 140 | std::vector<std::string>(), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 141 | unsigned Timeout = 0, |
| 142 | unsigned MemoryLimit = 0) = 0; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | //===---------------------------------------------------------------------===// |
| 146 | // CBE Implementation of AbstractIntepreter interface |
| 147 | // |
| 148 | class CBE : public AbstractInterpreter { |
Bill Wendling | 38efa38 | 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. |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 151 | GCC *gcc; |
| 152 | public: |
Reid Spencer | 2418bf9 | 2004-12-19 17:59:45 +0000 | [diff] [blame] | 153 | CBE(const sys::Path &llcPath, GCC *Gcc, |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame^] | 154 | const std::vector<std::string> *Args) |
| 155 | : LLCPath(llcPath), gcc(Gcc) { |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 156 | ToolArgs.clear (); |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame^] | 157 | if (Args) ToolArgs = *Args; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 158 | } |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 159 | ~CBE() { delete gcc; } |
| 160 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 161 | /// compileProgram - Compile the specified program from bitcode to executable |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 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. |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 165 | virtual void compileProgram(const std::string &Bitcode); |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 166 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 167 | virtual int ExecuteProgram(const std::string &Bitcode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 168 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 169 | const std::string &InputFile, |
| 170 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 171 | const std::vector<std::string> &GCCArgs = |
| 172 | std::vector<std::string>(), |
Misha Brukman | 63b3afa | 2005-04-21 20:48:15 +0000 | [diff] [blame] | 173 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 174 | std::vector<std::string>(), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 175 | unsigned Timeout = 0, |
| 176 | unsigned MemoryLimit = 0); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 177 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 178 | /// OutputCode - Compile the specified program from bitcode to code |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 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. |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 182 | virtual GCC::FileType OutputCode(const std::string &Bitcode, |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 183 | sys::Path &OutFile); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 186 | |
| 187 | //===---------------------------------------------------------------------===// |
| 188 | // LLC Implementation of AbstractIntepreter interface |
| 189 | // |
| 190 | class LLC : public AbstractInterpreter { |
Bill Wendling | 38efa38 | 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. |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 194 | GCC *gcc; |
| 195 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 196 | LLC(const std::string &llcPath, GCC *Gcc, |
Bill Wendling | 38efa38 | 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; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 203 | } |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 204 | ~LLC() { delete gcc; } |
| 205 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 206 | /// compileProgram - Compile the specified program from bitcode to executable |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 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. |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 210 | virtual void compileProgram(const std::string &Bitcode); |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 211 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 212 | virtual int ExecuteProgram(const std::string &Bitcode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 213 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 214 | const std::string &InputFile, |
| 215 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 216 | const std::vector<std::string> &GCCArgs = |
| 217 | std::vector<std::string>(), |
Misha Brukman | 63b3afa | 2005-04-21 20:48:15 +0000 | [diff] [blame] | 218 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 219 | std::vector<std::string>(), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 220 | unsigned Timeout = 0, |
| 221 | unsigned MemoryLimit = 0); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 222 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 223 | virtual GCC::FileType OutputCode(const std::string &Bitcode, |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 224 | sys::Path &OutFile); |
| 225 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 228 | } // End llvm namespace |
| 229 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 230 | #endif |