Chris Lattner | f474449 | 2003-09-30 18:28:53 +0000 | [diff] [blame] | 1 | //===-- Support/ToolRunner.h ------------------------------------*- C++ -*-===// |
| 2 | // |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 3 | // This file exposes an abstraction around a platform C compiler, used to |
| 4 | // compile C and assembly code. It also exposes an "AbstractIntepreter" |
| 5 | // interface, which is used to execute code using one of the LLVM execution |
| 6 | // engines. |
Chris Lattner | f474449 | 2003-09-30 18:28:53 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 10 | #ifndef TOOLRUNNER_H |
| 11 | #define TOOLRUNNER_H |
| 12 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 13 | #include "Support/SystemUtils.h" |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 16 | class CBE; |
| 17 | class LLC; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 18 | |
| 19 | //===---------------------------------------------------------------------===// |
| 20 | // GCC abstraction |
| 21 | // |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 22 | class GCC { |
| 23 | std::string GCCPath; // The path to the gcc executable |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 24 | GCC(const std::string &gccPath) : GCCPath(gccPath) { } |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 25 | public: |
| 26 | enum FileType { AsmFile, CFile }; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 28 | static GCC* create(const std::string &ProgramPath, std::string &Message); |
| 29 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 30 | /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is |
| 31 | /// either a .s file, or a .c file, specified by FileType), with the specified |
| 32 | /// arguments. Standard input is specified with InputFile, and standard |
| 33 | /// Output is captured to the specified OutputFile location. The SharedLibs |
| 34 | /// option specifies optional native shared objects that can be loaded into |
| 35 | /// the program for execution. |
| 36 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 37 | int ExecuteProgram(const std::string &ProgramFile, |
| 38 | const std::vector<std::string> &Args, |
| 39 | FileType fileType, |
| 40 | const std::string &InputFile, |
| 41 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 42 | const std::vector<std::string> &SharedLibs = |
| 43 | std::vector<std::string>()); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 44 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 45 | /// MakeSharedObject - This compiles the specified file (which is either a .c |
| 46 | /// file or a .s file) into a shared object. |
| 47 | /// |
| 48 | int MakeSharedObject(const std::string &InputFile, FileType fileType, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 49 | std::string &OutputFile); |
| 50 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 51 | private: |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 52 | void ProcessFailure(const char **Args); |
| 53 | }; |
| 54 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 56 | //===---------------------------------------------------------------------===// |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 57 | /// AbstractInterpreter Class - Subclasses of this class are used to execute |
| 58 | /// LLVM bytecode in a variety of ways. This abstract interface hides this |
| 59 | /// complexity behind a simple interface. |
| 60 | /// |
| 61 | struct AbstractInterpreter { |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 62 | static CBE* createCBE(const std::string &ProgramPath, std::string &Message); |
| 63 | static LLC *createLLC(const std::string &ProgramPath, std::string &Message); |
| 64 | |
| 65 | static AbstractInterpreter* createLLI(const std::string &ProgramPath, |
| 66 | std::string &Message); |
| 67 | |
| 68 | static AbstractInterpreter* createJIT(const std::string &ProgramPath, |
| 69 | std::string &Message); |
| 70 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 71 | |
| 72 | virtual ~AbstractInterpreter() {} |
| 73 | |
| 74 | /// ExecuteProgram - Run the specified bytecode file, emitting output to the |
| 75 | /// specified filename. This returns the exit code of the program. |
| 76 | /// |
| 77 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 78 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 79 | const std::string &InputFile, |
| 80 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 81 | const std::vector<std::string> &SharedLibs = |
| 82 | std::vector<std::string>()) = 0; |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | //===---------------------------------------------------------------------===// |
| 86 | // CBE Implementation of AbstractIntepreter interface |
| 87 | // |
| 88 | class CBE : public AbstractInterpreter { |
| 89 | std::string DISPath; // The path to the `llvm-dis' executable |
| 90 | GCC *gcc; |
| 91 | public: |
| 92 | CBE(const std::string &disPath, GCC *Gcc) : DISPath(disPath), gcc(Gcc) { } |
| 93 | ~CBE() { delete gcc; } |
| 94 | |
| 95 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 96 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 97 | const std::string &InputFile, |
| 98 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 99 | const std::vector<std::string> &SharedLibs = |
| 100 | std::vector<std::string>()); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 102 | // Sometimes we just want to go half-way and only generate the .c file, |
| 103 | // not necessarily compile it with GCC and run the program. |
| 104 | // |
| 105 | virtual int OutputC(const std::string &Bytecode, std::string &OutputCFile); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 108 | |
| 109 | //===---------------------------------------------------------------------===// |
| 110 | // LLC Implementation of AbstractIntepreter interface |
| 111 | // |
| 112 | class LLC : public AbstractInterpreter { |
| 113 | std::string LLCPath; // The path to the LLC executable |
| 114 | GCC *gcc; |
| 115 | public: |
| 116 | LLC(const std::string &llcPath, GCC *Gcc) |
| 117 | : LLCPath(llcPath), gcc(Gcc) { } |
| 118 | ~LLC() { delete gcc; } |
| 119 | |
| 120 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 121 | const std::vector<std::string> &Args, |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 122 | const std::string &InputFile, |
| 123 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 124 | const std::vector<std::string> &SharedLibs = |
| 125 | std::vector<std::string>()); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 127 | // Sometimes we just want to go half-way and only generate the .s file, |
| 128 | // not necessarily compile it all the way and run the program. |
| 129 | // |
| 130 | int OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile); |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
Misha Brukman | 29afb64 | 2003-09-29 22:38:57 +0000 | [diff] [blame] | 133 | #endif |