Drop 'const'
[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                      unsigned MemoryLimit = 0);
69
70   /// MakeSharedObject - This compiles the specified file (which is either a .c
71   /// file or a .s file) into a shared object.
72   ///
73   int MakeSharedObject(const std::string &InputFile, FileType fileType,
74                        std::string &OutputFile,
75                        const std::vector<std::string> &ArgsForGCC);
76 };
77
78
79 //===---------------------------------------------------------------------===//
80 /// AbstractInterpreter Class - Subclasses of this class are used to execute
81 /// LLVM bytecode in a variety of ways.  This abstract interface hides this
82 /// complexity behind a simple interface.
83 ///
84 class AbstractInterpreter {
85 public:
86   static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
87                         const std::vector<std::string> *Args = 0);
88   static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
89                         const std::vector<std::string> *Args = 0);
90
91   static AbstractInterpreter* createLLI(const std::string &ProgramPath,
92                                         std::string &Message,
93                                         const std::vector<std::string> *Args=0);
94
95   static AbstractInterpreter* createJIT(const std::string &ProgramPath,
96                                         std::string &Message,
97                                         const std::vector<std::string> *Args=0);
98
99
100   virtual ~AbstractInterpreter() {}
101
102   /// compileProgram - Compile the specified program from bytecode to executable
103   /// code.  This does not produce any output, it is only used when debugging
104   /// the code generator.  If the code generator fails, an exception should be
105   /// thrown, otherwise, this function will just return.
106   virtual void compileProgram(const std::string &Bytecode) {}
107
108   /// OutputCode - Compile the specified program from bytecode to code
109   /// understood by the GCC driver (either C or asm).  If the code generator
110   /// fails, an exception should be thrown, otherwise, this function returns the
111   /// type of code emitted.
112   virtual GCC::FileType OutputCode(const std::string &Bytecode,
113                                    sys::Path &OutFile) {
114     throw std::string("OutputCode not supported by this AbstractInterpreter!");
115   }
116   
117   /// ExecuteProgram - Run the specified bytecode file, emitting output to the
118   /// specified filename.  This returns the exit code of the program.
119   ///
120   virtual int ExecuteProgram(const std::string &Bytecode,
121                              const std::vector<std::string> &Args,
122                              const std::string &InputFile,
123                              const std::string &OutputFile,
124                              const std::vector<std::string> &GCCArgs =
125                                std::vector<std::string>(),
126                              const std::vector<std::string> &SharedLibs =
127                                std::vector<std::string>(),
128                              unsigned Timeout = 0,
129                              unsigned MemoryLimit = 0) = 0;
130 };
131
132 //===---------------------------------------------------------------------===//
133 // CBE Implementation of AbstractIntepreter interface
134 //
135 class CBE : public AbstractInterpreter {
136   sys::Path LLCPath;          // The path to the `llc' executable
137   std::vector<std::string> ToolArgs; // Extra args to pass to LLC
138   GCC *gcc;
139 public:
140   CBE(const sys::Path &llcPath, GCC *Gcc,
141       const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
142     ToolArgs.clear ();
143     if (Args) { ToolArgs = *Args; }
144   }
145   ~CBE() { delete gcc; }
146
147   /// compileProgram - Compile the specified program from bytecode to executable
148   /// code.  This does not produce any output, it is only used when debugging
149   /// the code generator.  If the code generator fails, an exception should be
150   /// thrown, otherwise, this function will just return.
151   virtual void compileProgram(const std::string &Bytecode);
152
153   virtual int ExecuteProgram(const std::string &Bytecode,
154                              const std::vector<std::string> &Args,
155                              const std::string &InputFile,
156                              const std::string &OutputFile,
157                              const std::vector<std::string> &GCCArgs =
158                                std::vector<std::string>(),
159                              const std::vector<std::string> &SharedLibs =
160                                std::vector<std::string>(),
161                              unsigned Timeout = 0,
162                              unsigned MemoryLimit = 0);
163
164   /// OutputCode - Compile the specified program from bytecode to code
165   /// understood by the GCC driver (either C or asm).  If the code generator
166   /// fails, an exception should be thrown, otherwise, this function returns the
167   /// type of code emitted.
168   virtual GCC::FileType OutputCode(const std::string &Bytecode,
169                                    sys::Path &OutFile);
170 };
171
172
173 //===---------------------------------------------------------------------===//
174 // LLC Implementation of AbstractIntepreter interface
175 //
176 class LLC : public AbstractInterpreter {
177   std::string LLCPath;          // The path to the LLC executable
178   std::vector<std::string> ToolArgs; // Extra args to pass to LLC
179   GCC *gcc;
180 public:
181   LLC(const std::string &llcPath, GCC *Gcc,
182     const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
183     ToolArgs.clear ();
184     if (Args) { ToolArgs = *Args; }
185   }
186   ~LLC() { delete gcc; }
187
188   /// compileProgram - Compile the specified program from bytecode to executable
189   /// code.  This does not produce any output, it is only used when debugging
190   /// the code generator.  If the code generator fails, an exception should be
191   /// thrown, otherwise, this function will just return.
192   virtual void compileProgram(const std::string &Bytecode);
193
194   virtual int ExecuteProgram(const std::string &Bytecode,
195                              const std::vector<std::string> &Args,
196                              const std::string &InputFile,
197                              const std::string &OutputFile,
198                              const std::vector<std::string> &GCCArgs =
199                                std::vector<std::string>(),
200                              const std::vector<std::string> &SharedLibs =
201                                 std::vector<std::string>(),
202                              unsigned Timeout = 0,
203                              unsigned MemoryLimit = 0);
204
205   virtual GCC::FileType OutputCode(const std::string &Bytecode,
206                                    sys::Path &OutFile);
207   
208 };
209
210 } // End llvm namespace
211
212 #endif