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