]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/bugpoint/ToolRunner.h
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt, libc++,
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / bugpoint / ToolRunner.h
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 LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
18 #define LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
19
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/SystemUtils.h"
25 #include <exception>
26 #include <vector>
27
28 namespace llvm {
29
30 extern cl::opt<bool> SaveTemps;
31 extern Triple TargetTriple;
32
33 class LLC;
34
35 //===---------------------------------------------------------------------===//
36 // CC abstraction
37 //
38 class CC {
39   std::string CCPath;              // The path to the cc executable.
40   std::string RemoteClientPath;    // The path to the rsh / ssh executable.
41   std::vector<std::string> ccArgs; // CC-specific arguments.
42   CC(StringRef ccPath, StringRef RemotePath,
43      const std::vector<std::string> *CCArgs)
44       : CCPath(ccPath), RemoteClientPath(RemotePath) {
45     if (CCArgs)
46       ccArgs = *CCArgs;
47   }
48
49 public:
50   enum FileType { AsmFile, ObjectFile, CFile };
51
52   static CC *create(const char *Argv0, std::string &Message,
53                     const std::string &CCBinary,
54                     const std::vector<std::string> *Args);
55
56   /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
57   /// either a .s file, or a .c file, specified by FileType), with the specified
58   /// arguments.  Standard input is specified with InputFile, and standard
59   /// Output is captured to the specified OutputFile location.  The SharedLibs
60   /// option specifies optional native shared objects that can be loaded into
61   /// the program for execution.
62   ///
63   Expected<int> ExecuteProgram(
64       const std::string &ProgramFile, const std::vector<std::string> &Args,
65       FileType fileType, const std::string &InputFile,
66       const std::string &OutputFile,
67       const std::vector<std::string> &CCArgs = std::vector<std::string>(),
68       unsigned Timeout = 0, unsigned MemoryLimit = 0);
69
70   /// MakeSharedObject - This compiles the specified file (which is either a .c
71   /// file or a .s file) into a shared object.
72   ///
73   Error MakeSharedObject(const std::string &InputFile, FileType fileType,
74                          std::string &OutputFile,
75                          const std::vector<std::string> &ArgsForCC);
76 };
77
78 //===---------------------------------------------------------------------===//
79 /// AbstractInterpreter Class - Subclasses of this class are used to execute
80 /// LLVM bitcode in a variety of ways.  This abstract interface hides this
81 /// complexity behind a simple interface.
82 ///
83 class AbstractInterpreter {
84   virtual void anchor();
85
86 public:
87   static LLC *createLLC(const char *Argv0, std::string &Message,
88                         const std::string &CCBinary,
89                         const std::vector<std::string> *Args = nullptr,
90                         const std::vector<std::string> *CCArgs = nullptr,
91                         bool UseIntegratedAssembler = false);
92
93   static AbstractInterpreter *
94   createLLI(const char *Argv0, std::string &Message,
95             const std::vector<std::string> *Args = nullptr);
96
97   static AbstractInterpreter *
98   createJIT(const char *Argv0, std::string &Message,
99             const std::vector<std::string> *Args = nullptr);
100
101   static AbstractInterpreter *
102   createCustomCompiler(const char *Argv0, std::string &Message,
103                        const std::string &CompileCommandLine);
104
105   static AbstractInterpreter *
106   createCustomExecutor(const char *Argv0, std::string &Message,
107                        const std::string &ExecCommandLine);
108
109   virtual ~AbstractInterpreter() {}
110
111   /// compileProgram - Compile the specified program from bitcode to executable
112   /// code.  This does not produce any output, it is only used when debugging
113   /// the code generator.  It returns false if the code generator fails.
114   virtual Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
115                                unsigned MemoryLimit = 0) {
116     return Error::success();
117   }
118
119   /// Compile the specified program from bitcode to code understood by the CC
120   /// driver (either C or asm).  Returns an error if the code generator fails,,
121   /// otherwise, the type of code emitted.
122   virtual Expected<CC::FileType> OutputCode(const std::string &Bitcode,
123                                             std::string &OutFile,
124                                             unsigned Timeout = 0,
125                                             unsigned MemoryLimit = 0) {
126     return make_error<StringError>(
127         "OutputCode not supported by this AbstractInterpreter!",
128         inconvertibleErrorCode());
129   }
130
131   /// ExecuteProgram - Run the specified bitcode file, emitting output to the
132   /// specified filename.  This sets RetVal to the exit code of the program or
133   /// returns an Error if a problem was encountered that prevented execution of
134   /// the program.
135   ///
136   virtual Expected<int> ExecuteProgram(
137       const std::string &Bitcode, const std::vector<std::string> &Args,
138       const std::string &InputFile, const std::string &OutputFile,
139       const std::vector<std::string> &CCArgs = std::vector<std::string>(),
140       const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
141       unsigned Timeout = 0, unsigned MemoryLimit = 0) = 0;
142 };
143
144 //===---------------------------------------------------------------------===//
145 // LLC Implementation of AbstractIntepreter interface
146 //
147 class LLC : public AbstractInterpreter {
148   std::string LLCPath;               // The path to the LLC executable.
149   std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
150   CC *cc;
151   bool UseIntegratedAssembler;
152
153 public:
154   LLC(const std::string &llcPath, CC *cc, const std::vector<std::string> *Args,
155       bool useIntegratedAssembler)
156       : LLCPath(llcPath), cc(cc),
157         UseIntegratedAssembler(useIntegratedAssembler) {
158     ToolArgs.clear();
159     if (Args)
160       ToolArgs = *Args;
161   }
162   ~LLC() override { delete cc; }
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   Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
168                        unsigned MemoryLimit = 0) override;
169
170   Expected<int> ExecuteProgram(
171       const std::string &Bitcode, const std::vector<std::string> &Args,
172       const std::string &InputFile, const std::string &OutputFile,
173       const std::vector<std::string> &CCArgs = std::vector<std::string>(),
174       const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
175       unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
176
177   Expected<CC::FileType> OutputCode(const std::string &Bitcode,
178                                     std::string &OutFile, unsigned Timeout = 0,
179                                     unsigned MemoryLimit = 0) override;
180 };
181
182 /// Find the first executable file \ExeName, either in the user's PATH or,
183 /// failing that, in the same directory as argv[0]. This allows us to find
184 /// another LLVM tool if it is built in the same directory. If no executable is
185 /// found, an error is returned.
186 ErrorOr<std::string> FindProgramByName(const std::string &ExeName,
187                                        const char *Argv0, void *MainAddr);
188
189 } // End llvm namespace
190
191 #endif