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