Include PathV1.h in files that use it.
[oota-llvm.git] / tools / bugpoint / ToolRunner.h
1 //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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
17 #ifndef BUGPOINT_TOOLRUNNER_H
18 #define BUGPOINT_TOOLRUNNER_H
19
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/PathV1.h"
25 #include "llvm/Support/SystemUtils.h"
26 #include <exception>
27 #include <vector>
28
29 namespace llvm {
30
31 extern cl::opt<bool> SaveTemps;
32 extern Triple TargetTriple;
33
34 class CBE;
35 class LLC;
36
37 //===---------------------------------------------------------------------===//
38 // GCC abstraction
39 //
40 class GCC {
41   sys::Path GCCPath;                // The path to the gcc executable.
42   sys::Path RemoteClientPath;       // The path to the rsh / ssh executable.
43   std::vector<std::string> gccArgs; // GCC-specific arguments.
44   GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
45       const std::vector<std::string> *GCCArgs)
46     : GCCPath(gccPath), RemoteClientPath(RemotePath) {
47     if (GCCArgs) gccArgs = *GCCArgs;
48   }
49 public:
50   enum FileType { AsmFile, ObjectFile, CFile };
51
52   static GCC *create(std::string &Message,
53                      const std::string &GCCBinary,
54                      const std::vector<std::string> *Args);
55
56   /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
57   /// either a .s file, or a .c file, specified by FileType), with the specified
58   /// arguments.  Standard input is specified with InputFile, and standard
59   /// Output is captured to the specified OutputFile location.  The SharedLibs
60   /// option specifies optional native shared objects that can be loaded into
61   /// the program for execution.
62   ///
63   int ExecuteProgram(const std::string &ProgramFile,
64                      const std::vector<std::string> &Args,
65                      FileType fileType,
66                      const std::string &InputFile,
67                      const std::string &OutputFile,
68                      std::string *Error = 0,
69                      const std::vector<std::string> &GCCArgs =
70                          std::vector<std::string>(),
71                      unsigned Timeout = 0,
72                      unsigned MemoryLimit = 0);
73
74   /// MakeSharedObject - This compiles the specified file (which is either a .c
75   /// file or a .s file) into a shared object.
76   ///
77   int MakeSharedObject(const std::string &InputFile, FileType fileType,
78                        std::string &OutputFile,
79                        const std::vector<std::string> &ArgsForGCC,
80                        std::string &Error);
81 };
82
83
84 //===---------------------------------------------------------------------===//
85 /// AbstractInterpreter Class - Subclasses of this class are used to execute
86 /// LLVM bitcode in a variety of ways.  This abstract interface hides this
87 /// complexity behind a simple interface.
88 ///
89 class AbstractInterpreter {
90   virtual void anchor();
91 public:
92   static CBE *createCBE(const char *Argv0, std::string &Message,
93                         const std::string              &GCCBinary,
94                         const std::vector<std::string> *Args = 0,
95                         const std::vector<std::string> *GCCArgs = 0);
96   static LLC *createLLC(const char *Argv0, std::string &Message,
97                         const std::string              &GCCBinary,
98                         const std::vector<std::string> *Args = 0,
99                         const std::vector<std::string> *GCCArgs = 0,
100                         bool UseIntegratedAssembler = false);
101
102   static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
103                                         const std::vector<std::string> *Args=0);
104
105   static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
106                                         const std::vector<std::string> *Args=0);
107
108   static AbstractInterpreter*
109   createCustomCompiler(std::string &Message,
110                        const std::string &CompileCommandLine);
111
112   static AbstractInterpreter*
113   createCustomExecutor(std::string &Message,
114                        const std::string &ExecCommandLine);
115
116
117   virtual ~AbstractInterpreter() {}
118
119   /// compileProgram - Compile the specified program from bitcode to executable
120   /// code.  This does not produce any output, it is only used when debugging
121   /// the code generator.  It returns false if the code generator fails.
122   virtual void compileProgram(const std::string &Bitcode, std::string *Error,
123                               unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
124
125   /// OutputCode - Compile the specified program from bitcode to code
126   /// understood by the GCC driver (either C or asm).  If the code generator
127   /// fails, it sets Error, otherwise, this function returns the type of code
128   /// emitted.
129   virtual GCC::FileType OutputCode(const std::string &Bitcode,
130                                    sys::Path &OutFile, std::string &Error,
131                                    unsigned Timeout = 0,
132                                    unsigned MemoryLimit = 0) {
133     Error = "OutputCode not supported by this AbstractInterpreter!";
134     return GCC::AsmFile;
135   }
136
137   /// ExecuteProgram - Run the specified bitcode file, emitting output to the
138   /// specified filename.  This sets RetVal to the exit code of the program or
139   /// returns false if a problem was encountered that prevented execution of
140   /// the program.
141   ///
142   virtual int ExecuteProgram(const std::string &Bitcode,
143                              const std::vector<std::string> &Args,
144                              const std::string &InputFile,
145                              const std::string &OutputFile,
146                              std::string *Error,
147                              const std::vector<std::string> &GCCArgs =
148                                std::vector<std::string>(),
149                              const std::vector<std::string> &SharedLibs =
150                                std::vector<std::string>(),
151                              unsigned Timeout = 0,
152                              unsigned MemoryLimit = 0) = 0;
153 };
154
155 //===---------------------------------------------------------------------===//
156 // CBE Implementation of AbstractIntepreter interface
157 //
158 class CBE : public AbstractInterpreter {
159   sys::Path LLCPath;                 // The path to the `llc' executable.
160   std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
161   GCC *gcc;
162 public:
163   CBE(const sys::Path &llcPath, GCC *Gcc,
164       const std::vector<std::string> *Args)
165     : LLCPath(llcPath), gcc(Gcc) {
166     ToolArgs.clear ();
167     if (Args) ToolArgs = *Args;
168   }
169   ~CBE() { delete gcc; }
170
171   /// compileProgram - Compile the specified program from bitcode to executable
172   /// code.  This does not produce any output, it is only used when debugging
173   /// the code generator.  Returns false if the code generator fails.
174   virtual void compileProgram(const std::string &Bitcode, std::string *Error,
175                               unsigned Timeout = 0, unsigned MemoryLimit = 0);
176
177   virtual int ExecuteProgram(const std::string &Bitcode,
178                              const std::vector<std::string> &Args,
179                              const std::string &InputFile,
180                              const std::string &OutputFile,
181                              std::string *Error,
182                              const std::vector<std::string> &GCCArgs =
183                                std::vector<std::string>(),
184                              const std::vector<std::string> &SharedLibs =
185                                std::vector<std::string>(),
186                              unsigned Timeout = 0,
187                              unsigned MemoryLimit = 0);
188
189   /// OutputCode - Compile the specified program from bitcode to code
190   /// understood by the GCC driver (either C or asm).  If the code generator
191   /// fails, it sets Error, otherwise, this function returns the type of code
192   /// emitted.
193   virtual GCC::FileType OutputCode(const std::string &Bitcode,
194                                    sys::Path &OutFile, std::string &Error,
195                                    unsigned Timeout = 0,
196                                    unsigned MemoryLimit = 0);
197 };
198
199
200 //===---------------------------------------------------------------------===//
201 // LLC Implementation of AbstractIntepreter interface
202 //
203 class LLC : public AbstractInterpreter {
204   std::string LLCPath;               // The path to the LLC executable.
205   std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
206   GCC *gcc;
207   bool UseIntegratedAssembler;
208 public:
209   LLC(const std::string &llcPath, GCC *Gcc,
210       const std::vector<std::string> *Args,
211       bool useIntegratedAssembler)
212     : LLCPath(llcPath), gcc(Gcc),
213       UseIntegratedAssembler(useIntegratedAssembler) {
214     ToolArgs.clear();
215     if (Args) ToolArgs = *Args;
216   }
217   ~LLC() { delete gcc; }
218
219   /// compileProgram - Compile the specified program from bitcode to executable
220   /// code.  This does not produce any output, it is only used when debugging
221   /// the code generator.  Returns false if the code generator fails.
222   virtual void compileProgram(const std::string &Bitcode, std::string *Error,
223                               unsigned Timeout = 0, unsigned MemoryLimit = 0);
224
225   virtual int ExecuteProgram(const std::string &Bitcode,
226                              const std::vector<std::string> &Args,
227                              const std::string &InputFile,
228                              const std::string &OutputFile,
229                              std::string *Error,
230                              const std::vector<std::string> &GCCArgs =
231                                std::vector<std::string>(),
232                              const std::vector<std::string> &SharedLibs =
233                                 std::vector<std::string>(),
234                              unsigned Timeout = 0,
235                              unsigned MemoryLimit = 0);
236
237   /// OutputCode - Compile the specified program from bitcode to code
238   /// understood by the GCC driver (either C or asm).  If the code generator
239   /// fails, it sets Error, otherwise, this function returns the type of code
240   /// emitted.
241   virtual GCC::FileType OutputCode(const std::string &Bitcode,
242                                    sys::Path &OutFile, std::string &Error,
243                                    unsigned Timeout = 0,
244                                    unsigned MemoryLimit = 0);
245 };
246
247 } // End llvm namespace
248
249 #endif