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