1 //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
18 #define LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
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"
30 extern cl::opt<bool> SaveTemps;
31 extern Triple TargetTriple;
35 //===---------------------------------------------------------------------===//
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) {
50 enum FileType { AsmFile, ObjectFile, CFile };
52 static CC *create(const char *Argv0, std::string &Message,
53 const std::string &CCBinary,
54 const std::vector<std::string> *Args);
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.
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);
70 /// MakeSharedObject - This compiles the specified file (which is either a .c
71 /// file or a .s file) into a shared object.
73 Error MakeSharedObject(const std::string &InputFile, FileType fileType,
74 std::string &OutputFile,
75 const std::vector<std::string> &ArgsForCC);
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.
83 class AbstractInterpreter {
84 virtual void anchor();
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);
93 static AbstractInterpreter *
94 createLLI(const char *Argv0, std::string &Message,
95 const std::vector<std::string> *Args = nullptr);
97 static AbstractInterpreter *
98 createJIT(const char *Argv0, std::string &Message,
99 const std::vector<std::string> *Args = nullptr);
101 static AbstractInterpreter *
102 createCustomCompiler(const char *Argv0, std::string &Message,
103 const std::string &CompileCommandLine);
105 static AbstractInterpreter *
106 createCustomExecutor(const char *Argv0, std::string &Message,
107 const std::string &ExecCommandLine);
109 virtual ~AbstractInterpreter() {}
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();
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());
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
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;
144 //===---------------------------------------------------------------------===//
145 // LLC Implementation of AbstractIntepreter interface
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.
151 bool UseIntegratedAssembler;
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) {
162 ~LLC() override { delete cc; }
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;
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;
177 Expected<CC::FileType> OutputCode(const std::string &Bitcode,
178 std::string &OutFile, unsigned Timeout = 0,
179 unsigned MemoryLimit = 0) override;
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);
189 } // End llvm namespace