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