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 | //===---------------------------------------------------------------------===// |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 36 | // CC abstraction |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 37 | // |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 38 | class CC { |
Justin Bogner | 8d0a081 | 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 | ab25621 | 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 | 8d0a081 | 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 | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 47 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 48 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 49 | public: |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 50 | enum FileType { AsmFile, ObjectFile, CFile }; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 51 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 52 | static CC *create(std::string &Message, const std::string &CCBinary, |
| 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 | /// |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 62 | int ExecuteProgram( |
| 63 | const std::string &ProgramFile, const std::vector<std::string> &Args, |
| 64 | FileType fileType, const std::string &InputFile, |
| 65 | const std::string &OutputFile, std::string *Error = nullptr, |
| 66 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
| 67 | unsigned Timeout = 0, unsigned MemoryLimit = 0); |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 68 | |
| 69 | /// MakeSharedObject - This compiles the specified file (which is either a .c |
| 70 | /// file or a .s file) into a shared object. |
| 71 | /// |
| 72 | int MakeSharedObject(const std::string &InputFile, FileType fileType, |
Chris Lattner | cc21fa7 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 73 | std::string &OutputFile, |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 74 | const std::vector<std::string> &ArgsForCC, |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 75 | std::string &Error); |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 78 | //===---------------------------------------------------------------------===// |
| 79 | /// AbstractInterpreter Class - Subclasses of this class are used to execute |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 80 | /// LLVM bitcode in a variety of ways. This abstract interface hides this |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 81 | /// complexity behind a simple interface. |
| 82 | /// |
| 83 | class AbstractInterpreter { |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 84 | virtual void anchor(); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 85 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 86 | public: |
Dan Gohman | 46ffffa | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 87 | static LLC *createLLC(const char *Argv0, std::string &Message, |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 88 | const std::string &CCBinary, |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 89 | const std::vector<std::string> *Args = nullptr, |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 90 | const std::vector<std::string> *CCArgs = nullptr, |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 91 | bool UseIntegratedAssembler = false); |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 92 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 93 | static AbstractInterpreter * |
Craig Topper | e73658d | 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 | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 96 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 97 | static AbstractInterpreter * |
Craig Topper | e73658d | 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 | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 100 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 101 | static AbstractInterpreter * |
Andrew Trick | 8665d59 | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 102 | createCustomCompiler(std::string &Message, |
| 103 | const std::string &CompileCommandLine); |
| 104 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 105 | static AbstractInterpreter * |
Andrew Trick | 8665d59 | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 106 | createCustomExecutor(std::string &Message, |
| 107 | const std::string &ExecCommandLine); |
Anton Korobeynikov | c53565c | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 108 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 109 | virtual ~AbstractInterpreter() {} |
| 110 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 111 | /// compileProgram - Compile the specified program from bitcode to executable |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 112 | /// 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] | 113 | /// the code generator. It returns false if the code generator fails. |
Duncan Sands | e9cd6d0 | 2010-05-24 07:49:55 +0000 | [diff] [blame] | 114 | virtual void compileProgram(const std::string &Bitcode, std::string *Error, |
| 115 | unsigned Timeout = 0, unsigned MemoryLimit = 0) {} |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 116 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 117 | /// OutputCode - Compile the specified program from bitcode to code |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 118 | /// understood by the CC driver (either C or asm). If the code generator |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 119 | /// fails, it sets Error, otherwise, this function returns the type of code |
| 120 | /// emitted. |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 121 | virtual CC::FileType OutputCode(const std::string &Bitcode, |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 122 | std::string &OutFile, std::string &Error, |
| 123 | unsigned Timeout = 0, |
| 124 | unsigned MemoryLimit = 0) { |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 125 | Error = "OutputCode not supported by this AbstractInterpreter!"; |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 126 | return CC::AsmFile; |
Chris Lattner | 634bc04 | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 127 | } |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 128 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 129 | /// ExecuteProgram - Run the specified bitcode file, emitting output to the |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 130 | /// specified filename. This sets RetVal to the exit code of the program or |
| 131 | /// returns false if a problem was encountered that prevented execution of |
| 132 | /// the program. |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 133 | /// |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 134 | virtual int ExecuteProgram( |
| 135 | const std::string &Bitcode, const std::vector<std::string> &Args, |
| 136 | const std::string &InputFile, const std::string &OutputFile, |
| 137 | std::string *Error, |
| 138 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
| 139 | const std::vector<std::string> &SharedLibs = std::vector<std::string>(), |
| 140 | unsigned Timeout = 0, unsigned MemoryLimit = 0) = 0; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | //===---------------------------------------------------------------------===// |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 144 | // LLC Implementation of AbstractIntepreter interface |
| 145 | // |
| 146 | class LLC : public AbstractInterpreter { |
Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 147 | std::string LLCPath; // The path to the LLC executable. |
| 148 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC. |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 149 | CC *cc; |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 150 | bool UseIntegratedAssembler; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 151 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 152 | public: |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 153 | LLC(const std::string &llcPath, CC *cc, const std::vector<std::string> *Args, |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 154 | bool useIntegratedAssembler) |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 155 | : LLCPath(llcPath), cc(cc), |
| 156 | UseIntegratedAssembler(useIntegratedAssembler) { |
Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 157 | ToolArgs.clear(); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 158 | if (Args) |
| 159 | ToolArgs = *Args; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 160 | } |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 161 | ~LLC() override { delete cc; } |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 162 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 163 | /// compileProgram - Compile the specified program from bitcode to executable |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 164 | /// 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] | 165 | /// the code generator. Returns false if the code generator fails. |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 166 | void compileProgram(const std::string &Bitcode, std::string *Error, |
| 167 | unsigned Timeout = 0, unsigned MemoryLimit = 0) override; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 168 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 169 | int ExecuteProgram( |
| 170 | const std::string &Bitcode, const std::vector<std::string> &Args, |
| 171 | const std::string &InputFile, const std::string &OutputFile, |
| 172 | std::string *Error, |
| 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; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 176 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 177 | /// OutputCode - Compile the specified program from bitcode to code |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 178 | /// understood by the CC driver (either C or asm). If the code generator |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 179 | /// fails, it sets Error, otherwise, this function returns the type of code |
| 180 | /// emitted. |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame^] | 181 | CC::FileType OutputCode(const std::string &Bitcode, std::string &OutFile, |
| 182 | std::string &Error, unsigned Timeout = 0, |
| 183 | unsigned MemoryLimit = 0) override; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | } // End llvm namespace |
| 187 | |
| 188 | #endif |