ToolRunner.h revision 210006
1193323Sed//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file exposes an abstraction around a platform C compiler, used to
11193323Sed// compile C and assembly code.  It also exposes an "AbstractIntepreter"
12193323Sed// interface, which is used to execute code using one of the LLVM execution
13193323Sed// engines.
14193323Sed//
15193323Sed//===----------------------------------------------------------------------===//
16193323Sed
17193323Sed#ifndef BUGPOINT_TOOLRUNNER_H
18193323Sed#define BUGPOINT_TOOLRUNNER_H
19193323Sed
20198090Srdivacky#include "llvm/ADT/Triple.h"
21198090Srdivacky#include "llvm/Support/CommandLine.h"
22207618Srdivacky#include "llvm/Support/ErrorHandling.h"
23193323Sed#include "llvm/Support/SystemUtils.h"
24198090Srdivacky#include "llvm/System/Path.h"
25193323Sed#include <exception>
26193323Sed#include <vector>
27193323Sed
28193323Sednamespace llvm {
29193323Sed
30198090Srdivackyextern cl::opt<bool> SaveTemps;
31198090Srdivackyextern Triple TargetTriple;
32198090Srdivacky
33193323Sedclass CBE;
34193323Sedclass LLC;
35193323Sed
36193323Sed//===---------------------------------------------------------------------===//
37193323Sed// GCC abstraction
38193323Sed//
39193323Sedclass GCC {
40193323Sed  sys::Path GCCPath;                // The path to the gcc executable.
41193323Sed  sys::Path RemoteClientPath;       // The path to the rsh / ssh executable.
42193323Sed  std::vector<std::string> gccArgs; // GCC-specific arguments.
43193323Sed  GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
44193323Sed      const std::vector<std::string> *GCCArgs)
45193323Sed    : GCCPath(gccPath), RemoteClientPath(RemotePath) {
46193323Sed    if (GCCArgs) gccArgs = *GCCArgs;
47193323Sed  }
48193323Sedpublic:
49205218Srdivacky  enum FileType { AsmFile, ObjectFile, CFile };
50193323Sed
51198090Srdivacky  static GCC *create(std::string &Message,
52208599Srdivacky                     const std::string &GCCBinary,
53193323Sed                     const std::vector<std::string> *Args);
54193323Sed
55193323Sed  /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
56193323Sed  /// either a .s file, or a .c file, specified by FileType), with the specified
57193323Sed  /// arguments.  Standard input is specified with InputFile, and standard
58193323Sed  /// Output is captured to the specified OutputFile location.  The SharedLibs
59193323Sed  /// option specifies optional native shared objects that can be loaded into
60193323Sed  /// the program for execution.
61193323Sed  ///
62193323Sed  int ExecuteProgram(const std::string &ProgramFile,
63193323Sed                     const std::vector<std::string> &Args,
64193323Sed                     FileType fileType,
65193323Sed                     const std::string &InputFile,
66193323Sed                     const std::string &OutputFile,
67210006Srdivacky                     std::string *Error = 0,
68193323Sed                     const std::vector<std::string> &GCCArgs =
69193323Sed                         std::vector<std::string>(),
70193323Sed                     unsigned Timeout = 0,
71193323Sed                     unsigned MemoryLimit = 0);
72193323Sed
73193323Sed  /// MakeSharedObject - This compiles the specified file (which is either a .c
74193323Sed  /// file or a .s file) into a shared object.
75193323Sed  ///
76193323Sed  int MakeSharedObject(const std::string &InputFile, FileType fileType,
77193323Sed                       std::string &OutputFile,
78207618Srdivacky                       const std::vector<std::string> &ArgsForGCC,
79207618Srdivacky                       std::string &Error);
80193323Sed};
81193323Sed
82193323Sed
83193323Sed//===---------------------------------------------------------------------===//
84193323Sed/// AbstractInterpreter Class - Subclasses of this class are used to execute
85193323Sed/// LLVM bitcode in a variety of ways.  This abstract interface hides this
86193323Sed/// complexity behind a simple interface.
87193323Sed///
88193323Sedclass AbstractInterpreter {
89193323Sedpublic:
90198090Srdivacky  static CBE *createCBE(const char *Argv0, std::string &Message,
91208599Srdivacky                        const std::string              &GCCBinary,
92193323Sed                        const std::vector<std::string> *Args = 0,
93193323Sed                        const std::vector<std::string> *GCCArgs = 0);
94198090Srdivacky  static LLC *createLLC(const char *Argv0, std::string &Message,
95208599Srdivacky                        const std::string              &GCCBinary,
96193323Sed                        const std::vector<std::string> *Args = 0,
97205218Srdivacky                        const std::vector<std::string> *GCCArgs = 0,
98205218Srdivacky                        bool UseIntegratedAssembler = false);
99193323Sed
100198090Srdivacky  static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
101193323Sed                                        const std::vector<std::string> *Args=0);
102193323Sed
103198090Srdivacky  static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
104193323Sed                                        const std::vector<std::string> *Args=0);
105193323Sed
106198090Srdivacky  static AbstractInterpreter* createCustom(std::string &Message,
107193323Sed                                           const std::string &ExecCommandLine);
108193323Sed
109193323Sed
110193323Sed  virtual ~AbstractInterpreter() {}
111193323Sed
112193323Sed  /// compileProgram - Compile the specified program from bitcode to executable
113193323Sed  /// code.  This does not produce any output, it is only used when debugging
114207618Srdivacky  /// the code generator.  It returns false if the code generator fails.
115208599Srdivacky  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
116208599Srdivacky                              unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
117193323Sed
118193323Sed  /// OutputCode - Compile the specified program from bitcode to code
119193323Sed  /// understood by the GCC driver (either C or asm).  If the code generator
120207618Srdivacky  /// fails, it sets Error, otherwise, this function returns the type of code
121207618Srdivacky  /// emitted.
122193323Sed  virtual GCC::FileType OutputCode(const std::string &Bitcode,
123208599Srdivacky                                   sys::Path &OutFile, std::string &Error,
124208599Srdivacky                                   unsigned Timeout = 0,
125208599Srdivacky                                   unsigned MemoryLimit = 0) {
126207618Srdivacky    Error = "OutputCode not supported by this AbstractInterpreter!";
127207618Srdivacky    return GCC::AsmFile;
128193323Sed  }
129207618Srdivacky
130193323Sed  /// ExecuteProgram - Run the specified bitcode file, emitting output to the
131207618Srdivacky  /// specified filename.  This sets RetVal to the exit code of the program or
132207618Srdivacky  /// returns false if a problem was encountered that prevented execution of
133207618Srdivacky  /// the program.
134193323Sed  ///
135193323Sed  virtual int ExecuteProgram(const std::string &Bitcode,
136193323Sed                             const std::vector<std::string> &Args,
137193323Sed                             const std::string &InputFile,
138193323Sed                             const std::string &OutputFile,
139207618Srdivacky                             std::string *Error,
140193323Sed                             const std::vector<std::string> &GCCArgs =
141193323Sed                               std::vector<std::string>(),
142193323Sed                             const std::vector<std::string> &SharedLibs =
143193323Sed                               std::vector<std::string>(),
144193323Sed                             unsigned Timeout = 0,
145193323Sed                             unsigned MemoryLimit = 0) = 0;
146193323Sed};
147193323Sed
148193323Sed//===---------------------------------------------------------------------===//
149193323Sed// CBE Implementation of AbstractIntepreter interface
150193323Sed//
151193323Sedclass CBE : public AbstractInterpreter {
152193323Sed  sys::Path LLCPath;                 // The path to the `llc' executable.
153193323Sed  std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
154193323Sed  GCC *gcc;
155193323Sedpublic:
156193323Sed  CBE(const sys::Path &llcPath, GCC *Gcc,
157193323Sed      const std::vector<std::string> *Args)
158193323Sed    : LLCPath(llcPath), gcc(Gcc) {
159193323Sed    ToolArgs.clear ();
160193323Sed    if (Args) ToolArgs = *Args;
161193323Sed  }
162193323Sed  ~CBE() { delete gcc; }
163193323Sed
164193323Sed  /// compileProgram - Compile the specified program from bitcode to executable
165193323Sed  /// code.  This does not produce any output, it is only used when debugging
166207618Srdivacky  /// the code generator.  Returns false if the code generator fails.
167208599Srdivacky  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
168208599Srdivacky                              unsigned Timeout = 0, unsigned MemoryLimit = 0);
169193323Sed
170193323Sed  virtual int ExecuteProgram(const std::string &Bitcode,
171193323Sed                             const std::vector<std::string> &Args,
172193323Sed                             const std::string &InputFile,
173193323Sed                             const std::string &OutputFile,
174207618Srdivacky                             std::string *Error,
175193323Sed                             const std::vector<std::string> &GCCArgs =
176193323Sed                               std::vector<std::string>(),
177193323Sed                             const std::vector<std::string> &SharedLibs =
178193323Sed                               std::vector<std::string>(),
179193323Sed                             unsigned Timeout = 0,
180193323Sed                             unsigned MemoryLimit = 0);
181193323Sed
182193323Sed  /// OutputCode - Compile the specified program from bitcode to code
183193323Sed  /// understood by the GCC driver (either C or asm).  If the code generator
184207618Srdivacky  /// fails, it sets Error, otherwise, this function returns the type of code
185207618Srdivacky  /// emitted.
186193323Sed  virtual GCC::FileType OutputCode(const std::string &Bitcode,
187208599Srdivacky                                   sys::Path &OutFile, std::string &Error,
188208599Srdivacky                                   unsigned Timeout = 0,
189208599Srdivacky                                   unsigned MemoryLimit = 0);
190193323Sed};
191193323Sed
192193323Sed
193193323Sed//===---------------------------------------------------------------------===//
194193323Sed// LLC Implementation of AbstractIntepreter interface
195193323Sed//
196193323Sedclass LLC : public AbstractInterpreter {
197193323Sed  std::string LLCPath;               // The path to the LLC executable.
198193323Sed  std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
199193323Sed  GCC *gcc;
200205218Srdivacky  bool UseIntegratedAssembler;
201193323Sedpublic:
202193323Sed  LLC(const std::string &llcPath, GCC *Gcc,
203193323Sed      const std::vector<std::string> *Args,
204205218Srdivacky      bool useIntegratedAssembler)
205205218Srdivacky    : LLCPath(llcPath), gcc(Gcc),
206205218Srdivacky      UseIntegratedAssembler(useIntegratedAssembler) {
207193323Sed    ToolArgs.clear();
208193323Sed    if (Args) ToolArgs = *Args;
209193323Sed  }
210193323Sed  ~LLC() { delete gcc; }
211193323Sed
212193323Sed  /// compileProgram - Compile the specified program from bitcode to executable
213193323Sed  /// code.  This does not produce any output, it is only used when debugging
214207618Srdivacky  /// the code generator.  Returns false if the code generator fails.
215208599Srdivacky  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
216208599Srdivacky                              unsigned Timeout = 0, unsigned MemoryLimit = 0);
217193323Sed
218193323Sed  virtual int ExecuteProgram(const std::string &Bitcode,
219193323Sed                             const std::vector<std::string> &Args,
220193323Sed                             const std::string &InputFile,
221193323Sed                             const std::string &OutputFile,
222207618Srdivacky                             std::string *Error,
223193323Sed                             const std::vector<std::string> &GCCArgs =
224193323Sed                               std::vector<std::string>(),
225193323Sed                             const std::vector<std::string> &SharedLibs =
226193323Sed                                std::vector<std::string>(),
227193323Sed                             unsigned Timeout = 0,
228193323Sed                             unsigned MemoryLimit = 0);
229193323Sed
230207618Srdivacky  /// OutputCode - Compile the specified program from bitcode to code
231207618Srdivacky  /// understood by the GCC driver (either C or asm).  If the code generator
232207618Srdivacky  /// fails, it sets Error, otherwise, this function returns the type of code
233207618Srdivacky  /// emitted.
234193323Sed  virtual GCC::FileType OutputCode(const std::string &Bitcode,
235208599Srdivacky                                   sys::Path &OutFile, std::string &Error,
236208599Srdivacky                                   unsigned Timeout = 0,
237208599Srdivacky                                   unsigned MemoryLimit = 0);
238193323Sed};
239193323Sed
240193323Sed} // End llvm namespace
241193323Sed
242193323Sed#endif
243