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