Apply simplification suggested by Chris: why assign() when operator = will do?
[oota-llvm.git] / tools / bugpoint / ToolRunner.h
1 //===-- 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 TOOLRUNNER_H
18 #define TOOLRUNNER_H
19
20 #include "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   std::string GCCPath;          // The path to the gcc executable
48   GCC(const std::string &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> &SharedLibs = 
67                          std::vector<std::string>());
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 struct AbstractInterpreter {
83   static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
84                         const std::vector<std::string> *Args = 0);
85   static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
86                         const std::vector<std::string> *Args = 0);
87
88   static AbstractInterpreter* createLLI(const std::string &ProgramPath,
89                                         std::string &Message,
90                                         const std::vector<std::string> *Args=0);
91
92   static AbstractInterpreter* createJIT(const std::string &ProgramPath,
93                                         std::string &Message,
94                                         const std::vector<std::string> *Args=0);
95
96
97   virtual ~AbstractInterpreter() {}
98
99   /// compileProgram - Compile the specified program from bytecode to executable
100   /// code.  This does not produce any output, it is only used when debugging
101   /// the code generator.  If the code generator fails, an exception should be
102   /// thrown, otherwise, this function will just return.
103   virtual void compileProgram(const std::string &Bytecode) {}
104
105   /// ExecuteProgram - Run the specified bytecode file, emitting output to the
106   /// specified filename.  This returns the exit code of the program.
107   ///
108   virtual int ExecuteProgram(const std::string &Bytecode,
109                              const std::vector<std::string> &Args,
110                              const std::string &InputFile,
111                              const std::string &OutputFile,
112                              const std::vector<std::string> &SharedLibs = 
113                                std::vector<std::string>()) = 0;
114 };
115
116 //===---------------------------------------------------------------------===//
117 // CBE Implementation of AbstractIntepreter interface
118 //
119 class CBE : public AbstractInterpreter {
120   std::string LLCPath;          // The path to the `llc' executable
121   std::vector<std::string> ToolArgs; // Extra args to pass to LLC
122   GCC *gcc;
123 public:
124   CBE(const std::string &llcPath, GCC *Gcc,
125       const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
126     ToolArgs.clear ();
127     if (Args) { ToolArgs = *Args; }
128   }
129   ~CBE() { delete gcc; }
130
131   /// compileProgram - Compile the specified program from bytecode to executable
132   /// code.  This does not produce any output, it is only used when debugging
133   /// the code generator.  If the code generator fails, an exception should be
134   /// thrown, otherwise, this function will just return.
135   virtual void compileProgram(const std::string &Bytecode);
136
137   virtual int ExecuteProgram(const std::string &Bytecode,
138                              const std::vector<std::string> &Args,
139                              const std::string &InputFile,
140                              const std::string &OutputFile,
141                              const std::vector<std::string> &SharedLibs = 
142                                std::vector<std::string>());
143
144   // Sometimes we just want to go half-way and only generate the .c file, not
145   // necessarily compile it with GCC and run the program.  This throws an
146   // exception if LLC crashes.
147   //
148   virtual void OutputC(const std::string &Bytecode, std::string &OutputCFile);
149 };
150
151
152 //===---------------------------------------------------------------------===//
153 // LLC Implementation of AbstractIntepreter interface
154 //
155 class LLC : public AbstractInterpreter {
156   std::string LLCPath;          // The path to the LLC executable
157   std::vector<std::string> ToolArgs; // Extra args to pass to LLC
158   GCC *gcc;
159 public:
160   LLC(const std::string &llcPath, GCC *Gcc,
161     const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
162     ToolArgs.clear ();
163     if (Args) { ToolArgs = *Args; }
164   }
165   ~LLC() { delete gcc; }
166
167   /// compileProgram - Compile the specified program from bytecode to executable
168   /// code.  This does not produce any output, it is only used when debugging
169   /// the code generator.  If the code generator fails, an exception should be
170   /// thrown, otherwise, this function will just return.
171   virtual void compileProgram(const std::string &Bytecode);
172
173   virtual int ExecuteProgram(const std::string &Bytecode,
174                              const std::vector<std::string> &Args,
175                              const std::string &InputFile,
176                              const std::string &OutputFile,
177                              const std::vector<std::string> &SharedLibs = 
178                                 std::vector<std::string>());
179
180   // Sometimes we just want to go half-way and only generate the .s file,
181   // not necessarily compile it all the way and run the program.  This throws
182   // an exception if execution of LLC fails.
183   //
184   void OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
185 };
186
187 } // End llvm namespace
188
189 #endif