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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
Chris Lattner | f1b20d8 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 17 | #ifndef BUGPOINT_TOOLRUNNER_H |
| 18 | #define BUGPOINT_TOOLRUNNER_H |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 19 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/SystemUtils.h" |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 21 | #include <exception> |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 26 | class CBE; |
| 27 | class LLC; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 29 | /// ToolExecutionError - An instance of this class is thrown by the |
| 30 | /// AbstractInterpreter instances if there is an error running a tool (e.g., LLC |
| 31 | /// crashes) which prevents execution of the program. |
| 32 | /// |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 33 | class ToolExecutionError : std::exception { |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 34 | std::string Message; |
| 35 | public: |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 36 | explicit ToolExecutionError(const std::string &M) : Message(M) {} |
| 37 | virtual ~ToolExecutionError() throw(); |
| 38 | virtual const char* what() const throw() { return Message.c_str(); } |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 42 | //===---------------------------------------------------------------------===// |
| 43 | // GCC abstraction |
| 44 | // |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 45 | class GCC { |
Reid Spencer | 2418bf9 | 2004-12-19 17:59:45 +0000 | [diff] [blame] | 46 | sys::Path GCCPath; // The path to the gcc executable |
| 47 | GCC(const sys::Path &gccPath) : GCCPath(gccPath) { } |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 48 | public: |
| 49 | enum FileType { AsmFile, CFile }; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame^] | 51 | static GCC *create(const std::string &ProgramPath, std::string &Message); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 52 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 53 | /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is |
| 54 | /// either a .s file, or a .c file, specified by FileType), with the specified |
| 55 | /// arguments. Standard input is specified with InputFile, and standard |
| 56 | /// Output is captured to the specified OutputFile location. The SharedLibs |
| 57 | /// option specifies optional native shared objects that can be loaded into |
| 58 | /// the program for execution. |
| 59 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 60 | int ExecuteProgram(const std::string &ProgramFile, |
| 61 | const std::vector<std::string> &Args, |
| 62 | FileType fileType, |
| 63 | const std::string &InputFile, |
| 64 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 65 | const std::vector<std::string> &GCCArgs = |
| 66 | std::vector<std::string>(), |
| 67 | unsigned Timeout = 0); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 68 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 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 | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame^] | 73 | std::string &OutputFile, |
| 74 | const std::vector<std::string> &ArgsForGCC); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 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 |
| 80 | /// LLVM bytecode in a variety of ways. This abstract interface hides this |
| 81 | /// complexity behind a simple interface. |
| 82 | /// |
Jeff Cohen | 8388195 | 2005-01-22 16:30:58 +0000 | [diff] [blame] | 83 | class AbstractInterpreter { |
| 84 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 85 | static CBE *createCBE(const std::string &ProgramPath, std::string &Message, |
| 86 | const std::vector<std::string> *Args = 0); |
| 87 | static LLC *createLLC(const std::string &ProgramPath, std::string &Message, |
| 88 | const std::vector<std::string> *Args = 0); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 89 | |
| 90 | static AbstractInterpreter* createLLI(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 91 | std::string &Message, |
| 92 | const std::vector<std::string> *Args=0); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 93 | |
| 94 | static AbstractInterpreter* createJIT(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 95 | std::string &Message, |
| 96 | const std::vector<std::string> *Args=0); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 97 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 98 | |
| 99 | virtual ~AbstractInterpreter() {} |
| 100 | |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 101 | /// compileProgram - Compile the specified program from bytecode to executable |
| 102 | /// code. This does not produce any output, it is only used when debugging |
| 103 | /// the code generator. If the code generator fails, an exception should be |
| 104 | /// thrown, otherwise, this function will just return. |
| 105 | virtual void compileProgram(const std::string &Bytecode) {} |
| 106 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 107 | /// ExecuteProgram - Run the specified bytecode file, emitting output to the |
| 108 | /// specified filename. This returns the exit code of the program. |
| 109 | /// |
| 110 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 111 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 112 | const std::string &InputFile, |
| 113 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 114 | const std::vector<std::string> &GCCArgs = |
| 115 | std::vector<std::string>(), |
Misha Brukman | 63b3afa | 2005-04-21 20:48:15 +0000 | [diff] [blame] | 116 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 117 | std::vector<std::string>(), |
| 118 | unsigned Timeout = 0) = 0; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | //===---------------------------------------------------------------------===// |
| 122 | // CBE Implementation of AbstractIntepreter interface |
| 123 | // |
| 124 | class CBE : public AbstractInterpreter { |
Reid Spencer | 2418bf9 | 2004-12-19 17:59:45 +0000 | [diff] [blame] | 125 | sys::Path LLCPath; // The path to the `llc' executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 126 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 127 | GCC *gcc; |
| 128 | public: |
Reid Spencer | 2418bf9 | 2004-12-19 17:59:45 +0000 | [diff] [blame] | 129 | CBE(const sys::Path &llcPath, GCC *Gcc, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 130 | const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) { |
| 131 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 132 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 133 | } |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 134 | ~CBE() { delete gcc; } |
| 135 | |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 136 | /// compileProgram - Compile the specified program from bytecode to executable |
| 137 | /// code. This does not produce any output, it is only used when debugging |
| 138 | /// the code generator. If the code generator fails, an exception should be |
| 139 | /// thrown, otherwise, this function will just return. |
| 140 | virtual void compileProgram(const std::string &Bytecode); |
| 141 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 142 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 143 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 144 | const std::string &InputFile, |
| 145 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 146 | const std::vector<std::string> &GCCArgs = |
| 147 | std::vector<std::string>(), |
Misha Brukman | 63b3afa | 2005-04-21 20:48:15 +0000 | [diff] [blame] | 148 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 149 | std::vector<std::string>(), |
| 150 | unsigned Timeout = 0); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 152 | // Sometimes we just want to go half-way and only generate the .c file, not |
| 153 | // necessarily compile it with GCC and run the program. This throws an |
| 154 | // exception if LLC crashes. |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 155 | // |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 156 | virtual void OutputC(const std::string &Bytecode, sys::Path& OutputCFile); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 159 | |
| 160 | //===---------------------------------------------------------------------===// |
| 161 | // LLC Implementation of AbstractIntepreter interface |
| 162 | // |
| 163 | class LLC : public AbstractInterpreter { |
| 164 | std::string LLCPath; // The path to the LLC executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 165 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 166 | GCC *gcc; |
| 167 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 168 | LLC(const std::string &llcPath, GCC *Gcc, |
| 169 | const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) { |
| 170 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 171 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 172 | } |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 173 | ~LLC() { delete gcc; } |
| 174 | |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 175 | /// compileProgram - Compile the specified program from bytecode to executable |
| 176 | /// code. This does not produce any output, it is only used when debugging |
| 177 | /// the code generator. If the code generator fails, an exception should be |
| 178 | /// thrown, otherwise, this function will just return. |
| 179 | virtual void compileProgram(const std::string &Bytecode); |
| 180 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 181 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 182 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 183 | const std::string &InputFile, |
| 184 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 185 | const std::vector<std::string> &GCCArgs = |
| 186 | std::vector<std::string>(), |
Misha Brukman | 63b3afa | 2005-04-21 20:48:15 +0000 | [diff] [blame] | 187 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 188 | std::vector<std::string>(), |
| 189 | unsigned Timeout = 0); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 191 | // Sometimes we just want to go half-way and only generate the .s file, |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 192 | // not necessarily compile it all the way and run the program. This throws |
| 193 | // an exception if execution of LLC fails. |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 194 | // |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 195 | void OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 198 | } // End llvm namespace |
| 199 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 200 | #endif |