Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 1 | //===-- llvm/Support/ToolRunner.h -------------------------------*- C++ -*-===// |
John Criswell | 6fbcc26 | 2003-10-20 20:19:47 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 17 | #ifndef LLVM_SUPPORT_TOOLRUNNER_H |
| 18 | #define LLVM_SUPPORT_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 | |
| 30 | /// ToolExecutionError - An instance of this class is thrown by the |
| 31 | /// AbstractInterpreter instances if there is an error running a tool (e.g., LLC |
| 32 | /// crashes) which prevents execution of the program. |
| 33 | /// |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 34 | class ToolExecutionError : std::exception { |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 35 | std::string Message; |
| 36 | public: |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 37 | explicit ToolExecutionError(const std::string &M) : Message(M) {} |
| 38 | virtual ~ToolExecutionError() throw(); |
| 39 | virtual const char* what() const throw() { return Message.c_str(); } |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 43 | //===---------------------------------------------------------------------===// |
| 44 | // GCC abstraction |
| 45 | // |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 46 | class GCC { |
| 47 | std::string GCCPath; // The path to the gcc executable |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 48 | GCC(const std::string &gccPath) : GCCPath(gccPath) { } |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 49 | public: |
| 50 | enum FileType { AsmFile, CFile }; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 52 | static GCC* create(const std::string &ProgramPath, std::string &Message); |
| 53 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 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 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 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, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 66 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 67 | std::vector<std::string>(), 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, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 73 | std::string &OutputFile); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 77 | //===---------------------------------------------------------------------===// |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 78 | /// AbstractInterpreter Class - Subclasses of this class are used to execute |
| 79 | /// LLVM bytecode in a variety of ways. This abstract interface hides this |
| 80 | /// complexity behind a simple interface. |
| 81 | /// |
| 82 | struct AbstractInterpreter { |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 83 | static CBE *createCBE(const std::string &ProgramPath, std::string &Message, |
| 84 | const std::vector<std::string> *Args = 0); |
| 85 | static LLC *createLLC(const std::string &ProgramPath, std::string &Message, |
| 86 | const std::vector<std::string> *Args = 0); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 87 | |
| 88 | static AbstractInterpreter* createLLI(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 89 | std::string &Message, |
| 90 | const std::vector<std::string> *Args=0); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 91 | |
| 92 | static AbstractInterpreter* createJIT(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 93 | std::string &Message, |
| 94 | const std::vector<std::string> *Args=0); |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 95 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 96 | |
| 97 | virtual ~AbstractInterpreter() {} |
| 98 | |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 99 | /// compileProgram - Compile the specified program from bytecode to executable |
| 100 | /// code. This does not produce any output, it is only used when debugging |
| 101 | /// the code generator. If the code generator fails, an exception should be |
| 102 | /// thrown, otherwise, this function will just return. |
| 103 | virtual void compileProgram(const std::string &Bytecode) {} |
| 104 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 105 | /// ExecuteProgram - Run the specified bytecode file, emitting output to the |
| 106 | /// specified filename. This returns the exit code of the program. |
| 107 | /// |
| 108 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 109 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 110 | const std::string &InputFile, |
| 111 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 112 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 113 | std::vector<std::string>(), |
| 114 | unsigned Timeout = 0) = 0; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | //===---------------------------------------------------------------------===// |
| 118 | // CBE Implementation of AbstractIntepreter interface |
| 119 | // |
| 120 | class CBE : public AbstractInterpreter { |
Chris Lattner | df2cf41 | 2004-02-17 06:39:48 +0000 | [diff] [blame] | 121 | std::string LLCPath; // The path to the `llc' executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 122 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 123 | GCC *gcc; |
| 124 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 125 | CBE(const std::string &llcPath, GCC *Gcc, |
| 126 | const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) { |
| 127 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 128 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 129 | } |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 130 | ~CBE() { delete gcc; } |
| 131 | |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 132 | /// compileProgram - Compile the specified program from bytecode to executable |
| 133 | /// code. This does not produce any output, it is only used when debugging |
| 134 | /// the code generator. If the code generator fails, an exception should be |
| 135 | /// thrown, otherwise, this function will just return. |
| 136 | virtual void compileProgram(const std::string &Bytecode); |
| 137 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 138 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 139 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 140 | const std::string &InputFile, |
| 141 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 142 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 143 | std::vector<std::string>(), |
| 144 | unsigned Timeout = 0); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 146 | // Sometimes we just want to go half-way and only generate the .c file, not |
| 147 | // necessarily compile it with GCC and run the program. This throws an |
| 148 | // exception if LLC crashes. |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 149 | // |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 150 | virtual void OutputC(const std::string &Bytecode, std::string &OutputCFile); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 153 | |
| 154 | //===---------------------------------------------------------------------===// |
| 155 | // LLC Implementation of AbstractIntepreter interface |
| 156 | // |
| 157 | class LLC : public AbstractInterpreter { |
| 158 | std::string LLCPath; // The path to the LLC executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 159 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 160 | GCC *gcc; |
| 161 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 162 | LLC(const std::string &llcPath, GCC *Gcc, |
| 163 | const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) { |
| 164 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 165 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 166 | } |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 167 | ~LLC() { delete gcc; } |
| 168 | |
Chris Lattner | f03715c | 2004-02-18 23:24:29 +0000 | [diff] [blame] | 169 | /// compileProgram - Compile the specified program from bytecode to executable |
| 170 | /// code. This does not produce any output, it is only used when debugging |
| 171 | /// the code generator. If the code generator fails, an exception should be |
| 172 | /// thrown, otherwise, this function will just return. |
| 173 | virtual void compileProgram(const std::string &Bytecode); |
| 174 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 175 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 176 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 177 | const std::string &InputFile, |
| 178 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 179 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | 62c91fc | 2004-07-24 07:48:50 +0000 | [diff] [blame] | 180 | std::vector<std::string>(), |
| 181 | unsigned Timeout = 0); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 183 | // 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] | 184 | // not necessarily compile it all the way and run the program. This throws |
| 185 | // an exception if execution of LLC fails. |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 186 | // |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 187 | void OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 190 | } // End llvm namespace |
| 191 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 192 | #endif |