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 | |
Benjamin Kramer | 00e08fc | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 17 | #ifndef LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H |
| 18 | #define LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 19 | |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Anton Korobeynikov | 86c006a | 2009-08-05 09:32:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Error.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Path.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 24 | #include "llvm/Support/SystemUtils.h" |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 25 | #include <exception> |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | namespace llvm { |
| 29 | |
Anton Korobeynikov | 86c006a | 2009-08-05 09:32:10 +0000 | [diff] [blame] | 30 | extern cl::opt<bool> SaveTemps; |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 31 | extern Triple TargetTriple; |
Anton Korobeynikov | 86c006a | 2009-08-05 09:32:10 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 33 | class LLC; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 34 | |
| 35 | //===---------------------------------------------------------------------===// |
Davide Italiano | f7b2acb | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 36 | // CC abstraction |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 37 | // |
Davide Italiano | f7b2acb | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 38 | class CC { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 39 | std::string CCPath; // The path to the cc executable. |
| 40 | std::string RemoteClientPath; // The path to the rsh / ssh executable. |
Davide Italiano | f7b2acb | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 41 | std::vector<std::string> ccArgs; // CC-specific arguments. |
| 42 | CC(StringRef ccPath, StringRef RemotePath, |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 43 | const std::vector<std::string> *CCArgs) |
| 44 | : CCPath(ccPath), RemoteClientPath(RemotePath) { |
| 45 | if (CCArgs) |
| 46 | ccArgs = *CCArgs; |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 47 | } |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 49 | public: |
Chris Lattner | 5001042 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 50 | enum FileType { AsmFile, ObjectFile, CFile }; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 51 | |
Brian Gesiak | dcf592e | 2018-12-10 00:56:13 +0000 | [diff] [blame] | 52 | static CC *create(const char *Argv0, std::string &Message, |
| 53 | const std::string &CCBinary, |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 54 | const std::vector<std::string> *Args); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 55 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 56 | /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is |
| 57 | /// either a .s file, or a .c file, specified by FileType), with the specified |
| 58 | /// arguments. Standard input is specified with InputFile, and standard |
| 59 | /// Output is captured to the specified OutputFile location. The SharedLibs |
| 60 | /// option specifies optional native shared objects that can be loaded into |
| 61 | /// the program for execution. |
| 62 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 63 | Expected<int> ExecuteProgram( |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 64 | const std::string &ProgramFile, const std::vector<std::string> &Args, |
| 65 | FileType fileType, const std::string &InputFile, |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 66 | const std::string &OutputFile, |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 67 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
| 68 | unsigned Timeout = 0, unsigned MemoryLimit = 0); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 69 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 70 | /// MakeSharedObject - This compiles the specified file (which is either a .c |
| 71 | /// file or a .s file) into a shared object. |
| 72 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 73 | Error MakeSharedObject(const std::string &InputFile, FileType fileType, |
| 74 | std::string &OutputFile, |
| 75 | const std::vector<std::string> &ArgsForCC); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 78 | //===---------------------------------------------------------------------===// |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 79 | /// AbstractInterpreter Class - Subclasses of this class are used to execute |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 80 | /// LLVM bitcode in a variety of ways. This abstract interface hides this |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 81 | /// complexity behind a simple interface. |
| 82 | /// |
Jeff Cohen | 83881957 | 2005-01-22 16:30:58 +0000 | [diff] [blame] | 83 | class AbstractInterpreter { |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 84 | virtual void anchor(); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 85 | |
Jeff Cohen | 83881957 | 2005-01-22 16:30:58 +0000 | [diff] [blame] | 86 | public: |
Dan Gohman | 197f728 | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 87 | static LLC *createLLC(const char *Argv0, std::string &Message, |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 88 | const std::string &CCBinary, |
Craig Topper | c34a25d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 89 | const std::vector<std::string> *Args = nullptr, |
Davide Italiano | f7b2acb | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 90 | const std::vector<std::string> *CCArgs = nullptr, |
Chris Lattner | 5001042 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 91 | bool UseIntegratedAssembler = false); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 92 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 93 | static AbstractInterpreter * |
Craig Topper | c34a25d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 94 | createLLI(const char *Argv0, std::string &Message, |
| 95 | const std::vector<std::string> *Args = nullptr); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 96 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 97 | static AbstractInterpreter * |
Craig Topper | c34a25d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 98 | createJIT(const char *Argv0, std::string &Message, |
| 99 | const std::vector<std::string> *Args = nullptr); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 100 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 101 | static AbstractInterpreter * |
Brian Gesiak | dcf592e | 2018-12-10 00:56:13 +0000 | [diff] [blame] | 102 | createCustomCompiler(const char *Argv0, std::string &Message, |
Andrew Trick | f73311b | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 103 | const std::string &CompileCommandLine); |
| 104 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 105 | static AbstractInterpreter * |
Brian Gesiak | dcf592e | 2018-12-10 00:56:13 +0000 | [diff] [blame] | 106 | createCustomExecutor(const char *Argv0, std::string &Message, |
Andrew Trick | f73311b | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 107 | const std::string &ExecCommandLine); |
Anton Korobeynikov | 9ef7425 | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 108 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 109 | virtual ~AbstractInterpreter() {} |
| 110 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 111 | /// compileProgram - Compile the specified program from bitcode to executable |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 112 | /// code. This does not produce any output, it is only used when debugging |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 113 | /// the code generator. It returns false if the code generator fails. |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 114 | virtual Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0, |
| 115 | unsigned MemoryLimit = 0) { |
| 116 | return Error::success(); |
| 117 | } |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 118 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 119 | /// Compile the specified program from bitcode to code understood by the CC |
| 120 | /// driver (either C or asm). Returns an error if the code generator fails,, |
| 121 | /// otherwise, the type of code emitted. |
| 122 | virtual Expected<CC::FileType> OutputCode(const std::string &Bitcode, |
| 123 | std::string &OutFile, |
| 124 | unsigned Timeout = 0, |
| 125 | unsigned MemoryLimit = 0) { |
| 126 | return make_error<StringError>( |
| 127 | "OutputCode not supported by this AbstractInterpreter!", |
| 128 | inconvertibleErrorCode()); |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 129 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 130 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 131 | /// ExecuteProgram - Run the specified bitcode file, emitting output to the |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 132 | /// specified filename. This sets RetVal to the exit code of the program or |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 133 | /// returns an Error if a problem was encountered that prevented execution of |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 134 | /// the program. |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 135 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 136 | virtual Expected<int> ExecuteProgram( |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 137 | const std::string &Bitcode, const std::vector<std::string> &Args, |
| 138 | const std::string &InputFile, const std::string &OutputFile, |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 139 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
| 140 | const std::vector<std::string> &SharedLibs = std::vector<std::string>(), |
| 141 | unsigned Timeout = 0, unsigned MemoryLimit = 0) = 0; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | //===---------------------------------------------------------------------===// |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 145 | // LLC Implementation of AbstractIntepreter interface |
| 146 | // |
| 147 | class LLC : public AbstractInterpreter { |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 148 | std::string LLCPath; // The path to the LLC executable. |
| 149 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC. |
Davide Italiano | f7b2acb | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 150 | CC *cc; |
Chris Lattner | 5001042 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 151 | bool UseIntegratedAssembler; |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 152 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 153 | public: |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 154 | LLC(const std::string &llcPath, CC *cc, const std::vector<std::string> *Args, |
Chris Lattner | 5001042 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 155 | bool useIntegratedAssembler) |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 156 | : LLCPath(llcPath), cc(cc), |
| 157 | UseIntegratedAssembler(useIntegratedAssembler) { |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 158 | ToolArgs.clear(); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 159 | if (Args) |
| 160 | ToolArgs = *Args; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 161 | } |
Davide Italiano | f7b2acb | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 162 | ~LLC() override { delete cc; } |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 163 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 164 | /// compileProgram - Compile the specified program from bitcode to executable |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 165 | /// code. This does not produce any output, it is only used when debugging |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 166 | /// the code generator. Returns false if the code generator fails. |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 167 | Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0, |
| 168 | unsigned MemoryLimit = 0) override; |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 169 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 170 | Expected<int> ExecuteProgram( |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 171 | const std::string &Bitcode, const std::vector<std::string> &Args, |
| 172 | const std::string &InputFile, const std::string &OutputFile, |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 173 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
| 174 | const std::vector<std::string> &SharedLibs = std::vector<std::string>(), |
| 175 | unsigned Timeout = 0, unsigned MemoryLimit = 0) override; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 176 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 177 | Expected<CC::FileType> OutputCode(const std::string &Bitcode, |
| 178 | std::string &OutFile, unsigned Timeout = 0, |
| 179 | unsigned MemoryLimit = 0) override; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
Brian Gesiak | dcf592e | 2018-12-10 00:56:13 +0000 | [diff] [blame] | 182 | /// Find the first executable file \ExeName, either in the user's PATH or, |
| 183 | /// failing that, in the same directory as argv[0]. This allows us to find |
| 184 | /// another LLVM tool if it is built in the same directory. If no executable is |
| 185 | /// found, an error is returned. |
| 186 | ErrorOr<std::string> FindProgramByName(const std::string &ExeName, |
| 187 | const char *Argv0, void *MainAddr); |
| 188 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 189 | } // End llvm namespace |
| 190 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 191 | #endif |