Move ToolRunner.(cpp|h) into the bugpoint directory
[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 };
75
76
77 //===---------------------------------------------------------------------===//
78 /// AbstractInterpreter Class - Subclasses of this class are used to execute
79 /// LLVM bytecode in a variety of ways.  This abstract interface hides this
80 /// complexity behind a simple interface.
81 ///
82 class AbstractInterpreter {
83 public:
84   static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
85                         const std::vector<std::string> *Args = 0);
86   static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
87                         const std::vector<std::string> *Args = 0);
88
89   static AbstractInterpreter* createLLI(const std::string &ProgramPath,
90                                         std::string &Message,
91                                         const std::vector<std::string> *Args=0);
92
93   static AbstractInterpreter* createJIT(const std::string &ProgramPath,
94                                         std::string &Message,
95                                         const std::vector<std::string> *Args=0);
96
97
98   virtual ~AbstractInterpreter() {}
99
100   /// compileProgram - Compile the specified program from bytecode to executable
101   /// code.  This does not produce any output, it is only used when debugging
102   /// the code generator.  If the code generator fails, an exception should be
103   /// thrown, otherwise, this function will just return.
104   virtual void compileProgram(const std::string &Bytecode) {}
105
106   /// ExecuteProgram - Run the specified bytecode file, emitting output to the
107   /// specified filename.  This returns the exit code of the program.
108   ///
109   virtual int ExecuteProgram(const std::string &Bytecode,
110                              const std::vector<std::string> &Args,
111                              const std::string &InputFile,
112                              const std::string &OutputFile,
113                              const std::vector<std::string> &GCCArgs =
114                                std::vector<std::string>(),
115                              const std::vector<std::string> &SharedLibs =
116                                std::vector<std::string>(),
117                              unsigned Timeout = 0) = 0;
118 };
119
120 //===---------------------------------------------------------------------===//
121 // CBE Implementation of AbstractIntepreter interface
122 //
123 class CBE : public AbstractInterpreter {
124   sys::Path LLCPath;          // The path to the `llc' executable
125   std::vector<std::string> ToolArgs; // Extra args to pass to LLC
126   GCC *gcc;
127 public:
128   CBE(const sys::Path &llcPath, GCC *Gcc,
129       const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
130     ToolArgs.clear ();
131     if (Args) { ToolArgs = *Args; }
132   }
133   ~CBE() { delete gcc; }
134
135   /// compileProgram - Compile the specified program from bytecode to executable
136   /// code.  This does not produce any output, it is only used when debugging
137   /// the code generator.  If the code generator fails, an exception should be
138   /// thrown, otherwise, this function will just return.
139   virtual void compileProgram(const std::string &Bytecode);
140
141   virtual int ExecuteProgram(const std::string &Bytecode,
142                              const std::vector<std::string> &Args,
143                              const std::string &InputFile,
144                              const std::string &OutputFile,
145                              const std::vector<std::string> &GCCArgs =
146                                std::vector<std::string>(),
147                              const std::vector<std::string> &SharedLibs =
148                                std::vector<std::string>(),
149                              unsigned Timeout = 0);
150
151   // Sometimes we just want to go half-way and only generate the .c file, not
152   // necessarily compile it with GCC and run the program.  This throws an
153   // exception if LLC crashes.
154   //
155   virtual void OutputC(const std::string &Bytecode, sys::Path& OutputCFile);
156 };
157
158
159 //===---------------------------------------------------------------------===//
160 // LLC Implementation of AbstractIntepreter interface
161 //
162 class LLC : public AbstractInterpreter {
163   std::string LLCPath;          // The path to the LLC executable
164   std::vector<std::string> ToolArgs; // Extra args to pass to LLC
165   GCC *gcc;
166 public:
167   LLC(const std::string &llcPath, GCC *Gcc,
168     const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
169     ToolArgs.clear ();
170     if (Args) { ToolArgs = *Args; }
171   }
172   ~LLC() { delete gcc; }
173
174   /// compileProgram - Compile the specified program from bytecode to executable
175   /// code.  This does not produce any output, it is only used when debugging
176   /// the code generator.  If the code generator fails, an exception should be
177   /// thrown, otherwise, this function will just return.
178   virtual void compileProgram(const std::string &Bytecode);
179
180   virtual int ExecuteProgram(const std::string &Bytecode,
181                              const std::vector<std::string> &Args,
182                              const std::string &InputFile,
183                              const std::string &OutputFile,
184                              const std::vector<std::string> &GCCArgs =
185                                std::vector<std::string>(),
186                              const std::vector<std::string> &SharedLibs =
187                                 std::vector<std::string>(),
188                              unsigned Timeout = 0);
189
190   // Sometimes we just want to go half-way and only generate the .s file,
191   // not necessarily compile it all the way and run the program.  This throws
192   // an exception if execution of LLC fails.
193   //
194   void OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile);
195 };
196
197 } // End llvm namespace
198
199 #endif