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"
23249423Sdim#include "llvm/Support/Path.h"
24193323Sed#include "llvm/Support/SystemUtils.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 =
69218885Sdim                         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 {
89234353Sdim  virtual void anchor();
90193323Sedpublic:
91198090Srdivacky  static CBE *createCBE(const char *Argv0, std::string &Message,
92208599Srdivacky                        const std::string              &GCCBinary,
93193323Sed                        const std::vector<std::string> *Args = 0,
94193323Sed                        const std::vector<std::string> *GCCArgs = 0);
95198090Srdivacky  static LLC *createLLC(const char *Argv0, std::string &Message,
96208599Srdivacky                        const std::string              &GCCBinary,
97193323Sed                        const std::vector<std::string> *Args = 0,
98205218Srdivacky                        const std::vector<std::string> *GCCArgs = 0,
99205218Srdivacky                        bool UseIntegratedAssembler = false);
100193323Sed
101198090Srdivacky  static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
102193323Sed                                        const std::vector<std::string> *Args=0);
103193323Sed
104198090Srdivacky  static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
105193323Sed                                        const std::vector<std::string> *Args=0);
106193323Sed
107218885Sdim  static AbstractInterpreter*
108218885Sdim  createCustomCompiler(std::string &Message,
109218885Sdim                       const std::string &CompileCommandLine);
110193323Sed
111218885Sdim  static AbstractInterpreter*
112218885Sdim  createCustomExecutor(std::string &Message,
113218885Sdim                       const std::string &ExecCommandLine);
114193323Sed
115218885Sdim
116193323Sed  virtual ~AbstractInterpreter() {}
117193323Sed
118193323Sed  /// compileProgram - Compile the specified program from bitcode to executable
119193323Sed  /// code.  This does not produce any output, it is only used when debugging
120207618Srdivacky  /// the code generator.  It returns false if the code generator fails.
121208599Srdivacky  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
122208599Srdivacky                              unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
123193323Sed
124193323Sed  /// OutputCode - Compile the specified program from bitcode to code
125193323Sed  /// understood by the GCC driver (either C or asm).  If the code generator
126207618Srdivacky  /// fails, it sets Error, otherwise, this function returns the type of code
127207618Srdivacky  /// emitted.
128193323Sed  virtual GCC::FileType OutputCode(const std::string &Bitcode,
129208599Srdivacky                                   sys::Path &OutFile, std::string &Error,
130208599Srdivacky                                   unsigned Timeout = 0,
131208599Srdivacky                                   unsigned MemoryLimit = 0) {
132207618Srdivacky    Error = "OutputCode not supported by this AbstractInterpreter!";
133207618Srdivacky    return GCC::AsmFile;
134193323Sed  }
135207618Srdivacky
136193323Sed  /// ExecuteProgram - Run the specified bitcode file, emitting output to the
137207618Srdivacky  /// specified filename.  This sets RetVal to the exit code of the program or
138207618Srdivacky  /// returns false if a problem was encountered that prevented execution of
139207618Srdivacky  /// the program.
140193323Sed  ///
141193323Sed  virtual int ExecuteProgram(const std::string &Bitcode,
142193323Sed                             const std::vector<std::string> &Args,
143193323Sed                             const std::string &InputFile,
144193323Sed                             const std::string &OutputFile,
145207618Srdivacky                             std::string *Error,
146193323Sed                             const std::vector<std::string> &GCCArgs =
147193323Sed                               std::vector<std::string>(),
148193323Sed                             const std::vector<std::string> &SharedLibs =
149193323Sed                               std::vector<std::string>(),
150193323Sed                             unsigned Timeout = 0,
151193323Sed                             unsigned MemoryLimit = 0) = 0;
152193323Sed};
153193323Sed
154193323Sed//===---------------------------------------------------------------------===//
155193323Sed// CBE Implementation of AbstractIntepreter interface
156193323Sed//
157193323Sedclass CBE : public AbstractInterpreter {
158193323Sed  sys::Path LLCPath;                 // The path to the `llc' executable.
159193323Sed  std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
160193323Sed  GCC *gcc;
161193323Sedpublic:
162193323Sed  CBE(const sys::Path &llcPath, GCC *Gcc,
163193323Sed      const std::vector<std::string> *Args)
164193323Sed    : LLCPath(llcPath), gcc(Gcc) {
165193323Sed    ToolArgs.clear ();
166193323Sed    if (Args) ToolArgs = *Args;
167193323Sed  }
168193323Sed  ~CBE() { delete gcc; }
169193323Sed
170193323Sed  /// compileProgram - Compile the specified program from bitcode to executable
171193323Sed  /// code.  This does not produce any output, it is only used when debugging
172207618Srdivacky  /// the code generator.  Returns false if the code generator fails.
173208599Srdivacky  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
174208599Srdivacky                              unsigned Timeout = 0, unsigned MemoryLimit = 0);
175193323Sed
176193323Sed  virtual int ExecuteProgram(const std::string &Bitcode,
177193323Sed                             const std::vector<std::string> &Args,
178193323Sed                             const std::string &InputFile,
179193323Sed                             const std::string &OutputFile,
180207618Srdivacky                             std::string *Error,
181193323Sed                             const std::vector<std::string> &GCCArgs =
182193323Sed                               std::vector<std::string>(),
183193323Sed                             const std::vector<std::string> &SharedLibs =
184193323Sed                               std::vector<std::string>(),
185193323Sed                             unsigned Timeout = 0,
186193323Sed                             unsigned MemoryLimit = 0);
187193323Sed
188193323Sed  /// OutputCode - Compile the specified program from bitcode to code
189193323Sed  /// understood by the GCC driver (either C or asm).  If the code generator
190207618Srdivacky  /// fails, it sets Error, otherwise, this function returns the type of code
191207618Srdivacky  /// emitted.
192193323Sed  virtual GCC::FileType OutputCode(const std::string &Bitcode,
193208599Srdivacky                                   sys::Path &OutFile, std::string &Error,
194208599Srdivacky                                   unsigned Timeout = 0,
195208599Srdivacky                                   unsigned MemoryLimit = 0);
196193323Sed};
197193323Sed
198193323Sed
199193323Sed//===---------------------------------------------------------------------===//
200193323Sed// LLC Implementation of AbstractIntepreter interface
201193323Sed//
202193323Sedclass LLC : public AbstractInterpreter {
203193323Sed  std::string LLCPath;               // The path to the LLC executable.
204193323Sed  std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
205193323Sed  GCC *gcc;
206205218Srdivacky  bool UseIntegratedAssembler;
207193323Sedpublic:
208193323Sed  LLC(const std::string &llcPath, GCC *Gcc,
209193323Sed      const std::vector<std::string> *Args,
210205218Srdivacky      bool useIntegratedAssembler)
211205218Srdivacky    : LLCPath(llcPath), gcc(Gcc),
212205218Srdivacky      UseIntegratedAssembler(useIntegratedAssembler) {
213193323Sed    ToolArgs.clear();
214193323Sed    if (Args) ToolArgs = *Args;
215193323Sed  }
216193323Sed  ~LLC() { delete gcc; }
217193323Sed
218193323Sed  /// compileProgram - Compile the specified program from bitcode to executable
219193323Sed  /// code.  This does not produce any output, it is only used when debugging
220207618Srdivacky  /// the code generator.  Returns false if the code generator fails.
221208599Srdivacky  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
222208599Srdivacky                              unsigned Timeout = 0, unsigned MemoryLimit = 0);
223193323Sed
224193323Sed  virtual int ExecuteProgram(const std::string &Bitcode,
225193323Sed                             const std::vector<std::string> &Args,
226193323Sed                             const std::string &InputFile,
227193323Sed                             const std::string &OutputFile,
228207618Srdivacky                             std::string *Error,
229193323Sed                             const std::vector<std::string> &GCCArgs =
230193323Sed                               std::vector<std::string>(),
231193323Sed                             const std::vector<std::string> &SharedLibs =
232193323Sed                                std::vector<std::string>(),
233193323Sed                             unsigned Timeout = 0,
234193323Sed                             unsigned MemoryLimit = 0);
235193323Sed
236207618Srdivacky  /// OutputCode - Compile the specified program from bitcode to code
237207618Srdivacky  /// understood by the GCC driver (either C or asm).  If the code generator
238207618Srdivacky  /// fails, it sets Error, otherwise, this function returns the type of code
239207618Srdivacky  /// emitted.
240193323Sed  virtual GCC::FileType OutputCode(const std::string &Bitcode,
241208599Srdivacky                                   sys::Path &OutFile, std::string &Error,
242208599Srdivacky                                   unsigned Timeout = 0,
243208599Srdivacky                                   unsigned MemoryLimit = 0);
244193323Sed};
245193323Sed
246193323Sed} // End llvm namespace
247193323Sed
248193323Sed#endif
249