d232cd2e97df84c42f6133ecbb9ba74d031f90c7
[oota-llvm.git] / tools / bugpoint / ToolRunner.h
1 //===-- llvm/Support/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 LLVM_SUPPORT_TOOLRUNNER_H
18 #define LLVM_SUPPORT_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
30 /// ToolExecutionError - An instance of this class is thrown by the
31 /// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
32 /// crashes) which prevents execution of the program.
33 ///
34 class ToolExecutionError : std::exception {
35   std::string Message;
36 public:
37   explicit ToolExecutionError(const std::string &M) : Message(M) {}
38   virtual ~ToolExecutionError() throw();
39   virtual const char* what() const throw() { return Message.c_str(); }
40 };
41
42
43 //===---------------------------------------------------------------------===//
44 // GCC abstraction
45 //
46 class GCC {
47   sys::Path GCCPath;          // The path to the gcc executable
48   GCC(const sys::Path &gccPath) : GCCPath(gccPath) { }
49 public:
50   enum FileType { AsmFile, CFile };
51
52   static GCC* create(const std::string &ProgramPath, std::string &Message);
53
54   /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
55   /// either a .s file, or a .c file, specified by FileType), with the specified
56   /// arguments.  Standard input is specified with InputFile, and standard
57   /// Output is captured to the specified OutputFile location.  The SharedLibs
58   /// option specifies optional native shared objects that can be loaded into
59   /// the program for execution.
60   ///
61   int ExecuteProgram(const std::string &ProgramFile,
62                      const std::vector<std::string> &Args,
63                      FileType fileType,
64                      const std::string &InputFile,
65                      const std::string &OutputFile,
66                      const std::vector<std::string> &GCCArgs =
67                          std::vector<std::string>(), 
68                      unsigned Timeout = 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 };
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   /// ExecuteProgram - Run the specified bytecode file, emitting output to the
108   /// specified filename.  This returns the exit code of the program.
109   ///
110   virtual int ExecuteProgram(const std::string &Bytecode,
111                              const std::vector<std::string> &Args,
112                              const std::string &InputFile,
113                              const std::string &OutputFile,
114                              const std::vector<std::string> &GCCArgs =
115                                std::vector<std::string>(),
116                              const std::vector<std::string> &SharedLibs =
117                                std::vector<std::string>(),
118                              unsigned Timeout = 0) = 0;
119 };
120
121 //===---------------------------------------------------------------------===//
122 // CBE Implementation of AbstractIntepreter interface
123 //
124 class CBE : public AbstractInterpreter {
125   sys::Path LLCPath;          // The path to the `llc' executable
126   std::vector<std::string> ToolArgs; // Extra args to pass to LLC
127   GCC *gcc;
128 public:
129   CBE(const sys::Path &llcPath, GCC *Gcc,
130       const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
131     ToolArgs.clear ();
132     if (Args) { ToolArgs = *Args; }
133   }
134   ~CBE() { delete gcc; }
135
136   /// compileProgram - Compile the specified program from bytecode to executable
137   /// code.  This does not produce any output, it is only used when debugging
138   /// the code generator.  If the code generator fails, an exception should be
139   /// thrown, otherwise, this function will just return.
140   virtual void compileProgram(const std::string &Bytecode);
141
142   virtual int ExecuteProgram(const std::string &Bytecode,
143                              const std::vector<std::string> &Args,
144                              const std::string &InputFile,
145                              const std::string &OutputFile,
146                              const std::vector<std::string> &GCCArgs =
147                                std::vector<std::string>(),
148                              const std::vector<std::string> &SharedLibs =
149                                std::vector<std::string>(),
150                              unsigned Timeout = 0);
151
152   // Sometimes we just want to go half-way and only generate the .c file, not
153   // necessarily compile it with GCC and run the program.  This throws an
154   // exception if LLC crashes.
155   //
156   virtual void OutputC(const std::string &Bytecode, sys::Path& OutputCFile);
157 };
158
159
160 //===---------------------------------------------------------------------===//
161 // LLC Implementation of AbstractIntepreter interface
162 //
163 class LLC : public AbstractInterpreter {
164   std::string LLCPath;          // The path to the LLC executable
165   std::vector<std::string> ToolArgs; // Extra args to pass to LLC
166   GCC *gcc;
167 public:
168   LLC(const std::string &llcPath, GCC *Gcc,
169     const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
170     ToolArgs.clear ();
171     if (Args) { ToolArgs = *Args; }
172   }
173   ~LLC() { delete gcc; }
174
175   /// compileProgram - Compile the specified program from bytecode to executable
176   /// code.  This does not produce any output, it is only used when debugging
177   /// the code generator.  If the code generator fails, an exception should be
178   /// thrown, otherwise, this function will just return.
179   virtual void compileProgram(const std::string &Bytecode);
180
181   virtual int ExecuteProgram(const std::string &Bytecode,
182                              const std::vector<std::string> &Args,
183                              const std::string &InputFile,
184                              const std::string &OutputFile,
185                              const std::vector<std::string> &GCCArgs =
186                                std::vector<std::string>(),
187                              const std::vector<std::string> &SharedLibs =
188                                 std::vector<std::string>(),
189                              unsigned Timeout = 0);
190
191   // Sometimes we just want to go half-way and only generate the .s file,
192   // not necessarily compile it all the way and run the program.  This throws
193   // an exception if execution of LLC fails.
194   //
195   void OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile);
196 };
197
198 } // End llvm namespace
199
200 #endif