ToolRunner.h revision 41396302126c24f1d5231e191852ebf2ff37fe23
1//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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/ADT/Triple.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/SystemUtils.h"
24#include "llvm/System/Path.h"
25#include <exception>
26#include <vector>
27
28namespace llvm {
29
30extern cl::opt<bool> SaveTemps;
31extern Triple TargetTriple;
32
33class CBE;
34class LLC;
35
36//===---------------------------------------------------------------------===//
37// GCC abstraction
38//
39class GCC {
40  sys::Path GCCPath;                // The path to the gcc executable.
41  sys::Path RemoteClientPath;       // The path to the rsh / ssh executable.
42  std::vector<std::string> gccArgs; // GCC-specific arguments.
43  GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
44      const std::vector<std::string> *GCCArgs)
45    : GCCPath(gccPath), RemoteClientPath(RemotePath) {
46    if (GCCArgs) gccArgs = *GCCArgs;
47  }
48public:
49  enum FileType { AsmFile, ObjectFile, CFile };
50
51  static GCC *create(std::string &Message,
52                     const std::string &GCCBinary,
53                     const std::vector<std::string> *Args);
54
55  /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
56  /// either a .s file, or a .c file, specified by FileType), with the specified
57  /// arguments.  Standard input is specified with InputFile, and standard
58  /// Output is captured to the specified OutputFile location.  The SharedLibs
59  /// option specifies optional native shared objects that can be loaded into
60  /// the program for execution.
61  ///
62  int ExecuteProgram(const std::string &ProgramFile,
63                     const std::vector<std::string> &Args,
64                     FileType fileType,
65                     const std::string &InputFile,
66                     const std::string &OutputFile,
67		     std::string *Error = 0,
68                     const std::vector<std::string> &GCCArgs =
69                         std::vector<std::string>(),
70                     unsigned Timeout = 0,
71                     unsigned MemoryLimit = 0);
72
73  /// MakeSharedObject - This compiles the specified file (which is either a .c
74  /// file or a .s file) into a shared object.
75  ///
76  int MakeSharedObject(const std::string &InputFile, FileType fileType,
77                       std::string &OutputFile,
78                       const std::vector<std::string> &ArgsForGCC,
79                       std::string &Error);
80};
81
82
83//===---------------------------------------------------------------------===//
84/// AbstractInterpreter Class - Subclasses of this class are used to execute
85/// LLVM bitcode in a variety of ways.  This abstract interface hides this
86/// complexity behind a simple interface.
87///
88class AbstractInterpreter {
89public:
90  static CBE *createCBE(const char *Argv0, std::string &Message,
91                        const std::string              &GCCBinary,
92                        const std::vector<std::string> *Args = 0,
93                        const std::vector<std::string> *GCCArgs = 0);
94  static LLC *createLLC(const char *Argv0, std::string &Message,
95                        const std::string              &GCCBinary,
96                        const std::vector<std::string> *Args = 0,
97                        const std::vector<std::string> *GCCArgs = 0,
98                        bool UseIntegratedAssembler = false);
99
100  static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
101                                        const std::vector<std::string> *Args=0);
102
103  static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
104                                        const std::vector<std::string> *Args=0);
105
106  static AbstractInterpreter* createCustom(std::string &Message,
107                                           const std::string &ExecCommandLine);
108
109
110  virtual ~AbstractInterpreter() {}
111
112  /// compileProgram - Compile the specified program from bitcode to executable
113  /// code.  This does not produce any output, it is only used when debugging
114  /// the code generator.  It returns false if the code generator fails.
115  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
116                              unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
117
118  /// OutputCode - Compile the specified program from bitcode to code
119  /// understood by the GCC driver (either C or asm).  If the code generator
120  /// fails, it sets Error, otherwise, this function returns the type of code
121  /// emitted.
122  virtual GCC::FileType OutputCode(const std::string &Bitcode,
123                                   sys::Path &OutFile, std::string &Error,
124                                   unsigned Timeout = 0,
125                                   unsigned MemoryLimit = 0) {
126    Error = "OutputCode not supported by this AbstractInterpreter!";
127    return GCC::AsmFile;
128  }
129
130  /// ExecuteProgram - Run the specified bitcode file, emitting output to the
131  /// specified filename.  This sets RetVal to the exit code of the program or
132  /// returns false if a problem was encountered that prevented execution of
133  /// the program.
134  ///
135  virtual int ExecuteProgram(const std::string &Bitcode,
136                             const std::vector<std::string> &Args,
137                             const std::string &InputFile,
138                             const std::string &OutputFile,
139                             std::string *Error,
140                             const std::vector<std::string> &GCCArgs =
141                               std::vector<std::string>(),
142                             const std::vector<std::string> &SharedLibs =
143                               std::vector<std::string>(),
144                             unsigned Timeout = 0,
145                             unsigned MemoryLimit = 0) = 0;
146};
147
148//===---------------------------------------------------------------------===//
149// CBE Implementation of AbstractIntepreter interface
150//
151class CBE : public AbstractInterpreter {
152  sys::Path LLCPath;                 // The path to the `llc' executable.
153  std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
154  GCC *gcc;
155public:
156  CBE(const sys::Path &llcPath, GCC *Gcc,
157      const std::vector<std::string> *Args)
158    : LLCPath(llcPath), gcc(Gcc) {
159    ToolArgs.clear ();
160    if (Args) ToolArgs = *Args;
161  }
162  ~CBE() { delete gcc; }
163
164  /// compileProgram - Compile the specified program from bitcode to executable
165  /// code.  This does not produce any output, it is only used when debugging
166  /// the code generator.  Returns false if the code generator fails.
167  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
168                              unsigned Timeout = 0, unsigned MemoryLimit = 0);
169
170  virtual int ExecuteProgram(const std::string &Bitcode,
171                             const std::vector<std::string> &Args,
172                             const std::string &InputFile,
173                             const std::string &OutputFile,
174                             std::string *Error,
175                             const std::vector<std::string> &GCCArgs =
176                               std::vector<std::string>(),
177                             const std::vector<std::string> &SharedLibs =
178                               std::vector<std::string>(),
179                             unsigned Timeout = 0,
180                             unsigned MemoryLimit = 0);
181
182  /// OutputCode - Compile the specified program from bitcode to code
183  /// understood by the GCC driver (either C or asm).  If the code generator
184  /// fails, it sets Error, otherwise, this function returns the type of code
185  /// emitted.
186  virtual GCC::FileType OutputCode(const std::string &Bitcode,
187                                   sys::Path &OutFile, std::string &Error,
188                                   unsigned Timeout = 0,
189                                   unsigned MemoryLimit = 0);
190};
191
192
193//===---------------------------------------------------------------------===//
194// LLC Implementation of AbstractIntepreter interface
195//
196class LLC : public AbstractInterpreter {
197  std::string LLCPath;               // The path to the LLC executable.
198  std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
199  GCC *gcc;
200  bool UseIntegratedAssembler;
201public:
202  LLC(const std::string &llcPath, GCC *Gcc,
203      const std::vector<std::string> *Args,
204      bool useIntegratedAssembler)
205    : LLCPath(llcPath), gcc(Gcc),
206      UseIntegratedAssembler(useIntegratedAssembler) {
207    ToolArgs.clear();
208    if (Args) ToolArgs = *Args;
209  }
210  ~LLC() { delete gcc; }
211
212  /// compileProgram - Compile the specified program from bitcode to executable
213  /// code.  This does not produce any output, it is only used when debugging
214  /// the code generator.  Returns false if the code generator fails.
215  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
216                              unsigned Timeout = 0, unsigned MemoryLimit = 0);
217
218  virtual int ExecuteProgram(const std::string &Bitcode,
219                             const std::vector<std::string> &Args,
220                             const std::string &InputFile,
221                             const std::string &OutputFile,
222                             std::string *Error,
223                             const std::vector<std::string> &GCCArgs =
224                               std::vector<std::string>(),
225                             const std::vector<std::string> &SharedLibs =
226                                std::vector<std::string>(),
227                             unsigned Timeout = 0,
228                             unsigned MemoryLimit = 0);
229
230  /// OutputCode - Compile the specified program from bitcode to code
231  /// understood by the GCC driver (either C or asm).  If the code generator
232  /// fails, it sets Error, otherwise, this function returns the type of code
233  /// emitted.
234  virtual GCC::FileType OutputCode(const std::string &Bitcode,
235                                   sys::Path &OutFile, std::string &Error,
236                                   unsigned Timeout = 0,
237                                   unsigned MemoryLimit = 0);
238};
239
240} // End llvm namespace
241
242#endif
243