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 LLC;
34193323Sed
35193323Sed//===---------------------------------------------------------------------===//
36193323Sed// GCC abstraction
37193323Sed//
38193323Sedclass GCC {
39263508Sdim  std::string GCCPath;                // The path to the gcc executable.
40263508Sdim  std::string RemoteClientPath;       // The path to the rsh / ssh executable.
41193323Sed  std::vector<std::string> gccArgs; // GCC-specific arguments.
42263508Sdim  GCC(StringRef gccPath, StringRef RemotePath,
43193323Sed      const std::vector<std::string> *GCCArgs)
44193323Sed    : GCCPath(gccPath), RemoteClientPath(RemotePath) {
45193323Sed    if (GCCArgs) gccArgs = *GCCArgs;
46193323Sed  }
47193323Sedpublic:
48205218Srdivacky  enum FileType { AsmFile, ObjectFile, CFile };
49193323Sed
50198090Srdivacky  static GCC *create(std::string &Message,
51208599Srdivacky                     const std::string &GCCBinary,
52193323Sed                     const std::vector<std::string> *Args);
53193323Sed
54193323Sed  /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
55193323Sed  /// either a .s file, or a .c file, specified by FileType), with the specified
56193323Sed  /// arguments.  Standard input is specified with InputFile, and standard
57193323Sed  /// Output is captured to the specified OutputFile location.  The SharedLibs
58193323Sed  /// option specifies optional native shared objects that can be loaded into
59193323Sed  /// the program for execution.
60193323Sed  ///
61193323Sed  int ExecuteProgram(const std::string &ProgramFile,
62193323Sed                     const std::vector<std::string> &Args,
63193323Sed                     FileType fileType,
64193323Sed                     const std::string &InputFile,
65193323Sed                     const std::string &OutputFile,
66210006Srdivacky                     std::string *Error = 0,
67193323Sed                     const std::vector<std::string> &GCCArgs =
68218885Sdim                         std::vector<std::string>(),
69193323Sed                     unsigned Timeout = 0,
70193323Sed                     unsigned MemoryLimit = 0);
71193323Sed
72193323Sed  /// MakeSharedObject - This compiles the specified file (which is either a .c
73193323Sed  /// file or a .s file) into a shared object.
74193323Sed  ///
75193323Sed  int MakeSharedObject(const std::string &InputFile, FileType fileType,
76193323Sed                       std::string &OutputFile,
77207618Srdivacky                       const std::vector<std::string> &ArgsForGCC,
78207618Srdivacky                       std::string &Error);
79193323Sed};
80193323Sed
81193323Sed
82193323Sed//===---------------------------------------------------------------------===//
83193323Sed/// AbstractInterpreter Class - Subclasses of this class are used to execute
84193323Sed/// LLVM bitcode in a variety of ways.  This abstract interface hides this
85193323Sed/// complexity behind a simple interface.
86193323Sed///
87193323Sedclass AbstractInterpreter {
88234353Sdim  virtual void anchor();
89193323Sedpublic:
90198090Srdivacky  static LLC *createLLC(const char *Argv0, std::string &Message,
91208599Srdivacky                        const std::string              &GCCBinary,
92193323Sed                        const std::vector<std::string> *Args = 0,
93205218Srdivacky                        const std::vector<std::string> *GCCArgs = 0,
94205218Srdivacky                        bool UseIntegratedAssembler = false);
95193323Sed
96198090Srdivacky  static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
97193323Sed                                        const std::vector<std::string> *Args=0);
98193323Sed
99198090Srdivacky  static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
100193323Sed                                        const std::vector<std::string> *Args=0);
101193323Sed
102218885Sdim  static AbstractInterpreter*
103218885Sdim  createCustomCompiler(std::string &Message,
104218885Sdim                       const std::string &CompileCommandLine);
105193323Sed
106218885Sdim  static AbstractInterpreter*
107218885Sdim  createCustomExecutor(std::string &Message,
108218885Sdim                       const std::string &ExecCommandLine);
109193323Sed
110218885Sdim
111193323Sed  virtual ~AbstractInterpreter() {}
112193323Sed
113193323Sed  /// compileProgram - Compile the specified program from bitcode to executable
114193323Sed  /// code.  This does not produce any output, it is only used when debugging
115207618Srdivacky  /// the code generator.  It returns false if the code generator fails.
116208599Srdivacky  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
117208599Srdivacky                              unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
118193323Sed
119193323Sed  /// OutputCode - Compile the specified program from bitcode to code
120193323Sed  /// understood by the GCC driver (either C or asm).  If the code generator
121207618Srdivacky  /// fails, it sets Error, otherwise, this function returns the type of code
122207618Srdivacky  /// emitted.
123193323Sed  virtual GCC::FileType OutputCode(const std::string &Bitcode,
124263508Sdim                                   std::string &OutFile, std::string &Error,
125208599Srdivacky                                   unsigned Timeout = 0,
126208599Srdivacky                                   unsigned MemoryLimit = 0) {
127207618Srdivacky    Error = "OutputCode not supported by this AbstractInterpreter!";
128207618Srdivacky    return GCC::AsmFile;
129193323Sed  }
130207618Srdivacky
131193323Sed  /// ExecuteProgram - Run the specified bitcode file, emitting output to the
132207618Srdivacky  /// specified filename.  This sets RetVal to the exit code of the program or
133207618Srdivacky  /// returns false if a problem was encountered that prevented execution of
134207618Srdivacky  /// the program.
135193323Sed  ///
136193323Sed  virtual int ExecuteProgram(const std::string &Bitcode,
137193323Sed                             const std::vector<std::string> &Args,
138193323Sed                             const std::string &InputFile,
139193323Sed                             const std::string &OutputFile,
140207618Srdivacky                             std::string *Error,
141193323Sed                             const std::vector<std::string> &GCCArgs =
142193323Sed                               std::vector<std::string>(),
143193323Sed                             const std::vector<std::string> &SharedLibs =
144193323Sed                               std::vector<std::string>(),
145193323Sed                             unsigned Timeout = 0,
146193323Sed                             unsigned MemoryLimit = 0) = 0;
147193323Sed};
148193323Sed
149193323Sed//===---------------------------------------------------------------------===//
150193323Sed// LLC Implementation of AbstractIntepreter interface
151193323Sed//
152193323Sedclass LLC : public AbstractInterpreter {
153193323Sed  std::string LLCPath;               // The path to the LLC executable.
154193323Sed  std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
155193323Sed  GCC *gcc;
156205218Srdivacky  bool UseIntegratedAssembler;
157193323Sedpublic:
158193323Sed  LLC(const std::string &llcPath, GCC *Gcc,
159193323Sed      const std::vector<std::string> *Args,
160205218Srdivacky      bool useIntegratedAssembler)
161205218Srdivacky    : LLCPath(llcPath), gcc(Gcc),
162205218Srdivacky      UseIntegratedAssembler(useIntegratedAssembler) {
163193323Sed    ToolArgs.clear();
164193323Sed    if (Args) ToolArgs = *Args;
165193323Sed  }
166193323Sed  ~LLC() { delete gcc; }
167193323Sed
168193323Sed  /// compileProgram - Compile the specified program from bitcode to executable
169193323Sed  /// code.  This does not produce any output, it is only used when debugging
170207618Srdivacky  /// the code generator.  Returns false if the code generator fails.
171208599Srdivacky  virtual void compileProgram(const std::string &Bitcode, std::string *Error,
172208599Srdivacky                              unsigned Timeout = 0, unsigned MemoryLimit = 0);
173193323Sed
174193323Sed  virtual int ExecuteProgram(const std::string &Bitcode,
175193323Sed                             const std::vector<std::string> &Args,
176193323Sed                             const std::string &InputFile,
177193323Sed                             const std::string &OutputFile,
178207618Srdivacky                             std::string *Error,
179193323Sed                             const std::vector<std::string> &GCCArgs =
180193323Sed                               std::vector<std::string>(),
181193323Sed                             const std::vector<std::string> &SharedLibs =
182193323Sed                                std::vector<std::string>(),
183193323Sed                             unsigned Timeout = 0,
184193323Sed                             unsigned MemoryLimit = 0);
185193323Sed
186207618Srdivacky  /// OutputCode - Compile the specified program from bitcode to code
187207618Srdivacky  /// understood by the GCC driver (either C or asm).  If the code generator
188207618Srdivacky  /// fails, it sets Error, otherwise, this function returns the type of code
189207618Srdivacky  /// emitted.
190193323Sed  virtual GCC::FileType OutputCode(const std::string &Bitcode,
191263508Sdim                                   std::string &OutFile, std::string &Error,
192208599Srdivacky                                   unsigned Timeout = 0,
193208599Srdivacky                                   unsigned MemoryLimit = 0);
194193323Sed};
195193323Sed
196193323Sed} // End llvm namespace
197193323Sed
198193323Sed#endif
199