| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 1 | //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| Chris Lattner | 345353d | 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. |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +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 | 8575a60 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
| Anton Korobeynikov | 7cbff91 | 2009-08-05 09:32:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/SystemUtils.h" |
| Chris Lattner | b9f25f9 | 2009-08-23 21:36:09 +0000 | [diff] [blame] | 23 | #include "llvm/System/Path.h" |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 24 | #include <exception> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
| Anton Korobeynikov | 7cbff91 | 2009-08-05 09:32:10 +0000 | [diff] [blame] | 29 | extern cl::opt<bool> SaveTemps; |
| Daniel Dunbar | 8575a60 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 30 | extern Triple TargetTriple; |
| Anton Korobeynikov | 7cbff91 | 2009-08-05 09:32:10 +0000 | [diff] [blame] | 31 | |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +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 | bf5d827 | 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 | } |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 60 | public: |
| Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 61 | enum FileType { AsmFile, ObjectFile, CFile }; |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 62 | |
| Dan Gohman | 46ffffa | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 63 | static GCC *create(std::string &Message, |
| Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 64 | const std::vector<std::string> *Args); |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +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>(), |
| Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 80 | unsigned Timeout = 0, |
| 81 | unsigned MemoryLimit = 0); |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 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, |
| Chris Lattner | cc21fa7 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 87 | std::string &OutputFile, |
| 88 | const std::vector<std::string> &ArgsForGCC); |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | |
| 92 | //===---------------------------------------------------------------------===// |
| 93 | /// AbstractInterpreter Class - Subclasses of this class are used to execute |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 94 | /// LLVM bitcode in a variety of ways. This abstract interface hides this |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 95 | /// complexity behind a simple interface. |
| 96 | /// |
| 97 | class AbstractInterpreter { |
| 98 | public: |
| Dan Gohman | 46ffffa | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 99 | static CBE *createCBE(const char *Argv0, std::string &Message, |
| Bill Wendling | bf5d827 | 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 | 46ffffa | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 102 | static LLC *createLLC(const char *Argv0, std::string &Message, |
| Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 103 | const std::vector<std::string> *Args = 0, |
| Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 104 | const std::vector<std::string> *GCCArgs = 0, |
| 105 | bool UseIntegratedAssembler = false); |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 106 | |
| Dan Gohman | 46ffffa | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 107 | static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message, |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 108 | const std::vector<std::string> *Args=0); |
| 109 | |
| Dan Gohman | 46ffffa | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 110 | static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message, |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 111 | const std::vector<std::string> *Args=0); |
| 112 | |
| Dan Gohman | 46ffffa | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 113 | static AbstractInterpreter* createCustom(std::string &Message, |
| Anton Korobeynikov | c53565c | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 114 | const std::string &ExecCommandLine); |
| 115 | |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 116 | |
| 117 | virtual ~AbstractInterpreter() {} |
| 118 | |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 119 | /// compileProgram - Compile the specified program from bitcode to executable |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 120 | /// code. This does not produce any output, it is only used when debugging |
| 121 | /// the code generator. If the code generator fails, an exception should be |
| 122 | /// thrown, otherwise, this function will just return. |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 123 | virtual void compileProgram(const std::string &Bitcode) {} |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 124 | |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 125 | /// OutputCode - Compile the specified program from bitcode to code |
| Chris Lattner | 634bc04 | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 126 | /// understood by the GCC driver (either C or asm). If the code generator |
| 127 | /// fails, an exception should be thrown, otherwise, this function returns the |
| 128 | /// type of code emitted. |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 129 | virtual GCC::FileType OutputCode(const std::string &Bitcode, |
| Chris Lattner | 634bc04 | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 130 | sys::Path &OutFile) { |
| 131 | throw std::string("OutputCode not supported by this AbstractInterpreter!"); |
| 132 | } |
| 133 | |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 134 | /// ExecuteProgram - Run the specified bitcode file, emitting output to the |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 135 | /// specified filename. This returns the exit code of the program. |
| 136 | /// |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 137 | virtual int ExecuteProgram(const std::string &Bitcode, |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 138 | const std::vector<std::string> &Args, |
| 139 | const std::string &InputFile, |
| 140 | const std::string &OutputFile, |
| 141 | const std::vector<std::string> &GCCArgs = |
| 142 | std::vector<std::string>(), |
| 143 | const std::vector<std::string> &SharedLibs = |
| 144 | std::vector<std::string>(), |
| Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 145 | unsigned Timeout = 0, |
| 146 | unsigned MemoryLimit = 0) = 0; |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | //===---------------------------------------------------------------------===// |
| 150 | // CBE Implementation of AbstractIntepreter interface |
| 151 | // |
| 152 | class CBE : public AbstractInterpreter { |
| Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 153 | sys::Path LLCPath; // The path to the `llc' executable. |
| 154 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC. |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 155 | GCC *gcc; |
| 156 | public: |
| 157 | CBE(const sys::Path &llcPath, GCC *Gcc, |
| Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 158 | const std::vector<std::string> *Args) |
| 159 | : LLCPath(llcPath), gcc(Gcc) { |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 160 | ToolArgs.clear (); |
| Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 161 | if (Args) ToolArgs = *Args; |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 162 | } |
| 163 | ~CBE() { delete gcc; } |
| 164 | |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 165 | /// compileProgram - Compile the specified program from bitcode to executable |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 166 | /// code. This does not produce any output, it is only used when debugging |
| 167 | /// the code generator. If the code generator fails, an exception should be |
| 168 | /// thrown, otherwise, this function will just return. |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 169 | virtual void compileProgram(const std::string &Bitcode); |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 170 | |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 171 | virtual int ExecuteProgram(const std::string &Bitcode, |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 172 | const std::vector<std::string> &Args, |
| 173 | const std::string &InputFile, |
| 174 | const std::string &OutputFile, |
| 175 | const std::vector<std::string> &GCCArgs = |
| 176 | std::vector<std::string>(), |
| 177 | const std::vector<std::string> &SharedLibs = |
| 178 | std::vector<std::string>(), |
| Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 179 | unsigned Timeout = 0, |
| 180 | unsigned MemoryLimit = 0); |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 181 | |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 182 | /// OutputCode - Compile the specified program from bitcode to code |
| Chris Lattner | 634bc04 | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 183 | /// understood by the GCC driver (either C or asm). If the code generator |
| 184 | /// fails, an exception should be thrown, otherwise, this function returns the |
| 185 | /// type of code emitted. |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 186 | virtual GCC::FileType OutputCode(const std::string &Bitcode, |
| Chris Lattner | 634bc04 | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 187 | sys::Path &OutFile); |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | |
| 191 | //===---------------------------------------------------------------------===// |
| 192 | // LLC Implementation of AbstractIntepreter interface |
| 193 | // |
| 194 | class LLC : public AbstractInterpreter { |
| Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 195 | std::string LLCPath; // The path to the LLC executable. |
| 196 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC. |
| 197 | std::vector<std::string> gccArgs; // Extra args to pass to GCC. |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 198 | GCC *gcc; |
| Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 199 | bool UseIntegratedAssembler; |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 200 | public: |
| 201 | LLC(const std::string &llcPath, GCC *Gcc, |
| Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 202 | const std::vector<std::string> *Args, |
| Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 203 | const std::vector<std::string> *GCCArgs, |
| 204 | bool useIntegratedAssembler) |
| 205 | : LLCPath(llcPath), gcc(Gcc), |
| 206 | UseIntegratedAssembler(useIntegratedAssembler) { |
| Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 207 | ToolArgs.clear(); |
| 208 | if (Args) ToolArgs = *Args; |
| 209 | if (GCCArgs) gccArgs = *GCCArgs; |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 210 | } |
| 211 | ~LLC() { delete gcc; } |
| 212 | |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 213 | /// compileProgram - Compile the specified program from bitcode to executable |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 214 | /// code. This does not produce any output, it is only used when debugging |
| 215 | /// the code generator. If the code generator fails, an exception should be |
| 216 | /// thrown, otherwise, this function will just return. |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 217 | virtual void compileProgram(const std::string &Bitcode); |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 218 | |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 219 | virtual int ExecuteProgram(const std::string &Bitcode, |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 220 | const std::vector<std::string> &Args, |
| 221 | const std::string &InputFile, |
| 222 | const std::string &OutputFile, |
| 223 | const std::vector<std::string> &GCCArgs = |
| 224 | std::vector<std::string>(), |
| 225 | const std::vector<std::string> &SharedLibs = |
| 226 | std::vector<std::string>(), |
| Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 227 | unsigned Timeout = 0, |
| 228 | unsigned MemoryLimit = 0); |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 229 | |
| Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 230 | virtual GCC::FileType OutputCode(const std::string &Bitcode, |
| Chris Lattner | 634bc04 | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 231 | sys::Path &OutFile); |
| 232 | |
| Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | } // End llvm namespace |
| 236 | |
| 237 | #endif |