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 BUGPOINT_TOOLRUNNER_H
18 #define BUGPOINT_TOOLRUNNER_H
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/PathV1.h"
25 #include "llvm/Support/SystemUtils.h"
31 extern cl::opt<bool> SaveTemps;
32 extern Triple TargetTriple;
37 //===---------------------------------------------------------------------===//
41 std::string GCCPath; // The path to the gcc executable.
42 std::string RemoteClientPath; // The path to the rsh / ssh executable.
43 std::vector<std::string> gccArgs; // GCC-specific arguments.
44 GCC(StringRef gccPath, StringRef RemotePath,
45 const std::vector<std::string> *GCCArgs)
46 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
47 if (GCCArgs) gccArgs = *GCCArgs;
50 enum FileType { AsmFile, ObjectFile, CFile };
52 static GCC *create(std::string &Message,
53 const std::string &GCCBinary,
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 int ExecuteProgram(const std::string &ProgramFile,
64 const std::vector<std::string> &Args,
66 const std::string &InputFile,
67 const std::string &OutputFile,
68 std::string *Error = 0,
69 const std::vector<std::string> &GCCArgs =
70 std::vector<std::string>(),
72 unsigned MemoryLimit = 0);
74 /// MakeSharedObject - This compiles the specified file (which is either a .c
75 /// file or a .s file) into a shared object.
77 int MakeSharedObject(const std::string &InputFile, FileType fileType,
78 std::string &OutputFile,
79 const std::vector<std::string> &ArgsForGCC,
84 //===---------------------------------------------------------------------===//
85 /// AbstractInterpreter Class - Subclasses of this class are used to execute
86 /// LLVM bitcode in a variety of ways. This abstract interface hides this
87 /// complexity behind a simple interface.
89 class AbstractInterpreter {
90 virtual void anchor();
92 static CBE *createCBE(const char *Argv0, std::string &Message,
93 const std::string &GCCBinary,
94 const std::vector<std::string> *Args = 0,
95 const std::vector<std::string> *GCCArgs = 0);
96 static LLC *createLLC(const char *Argv0, std::string &Message,
97 const std::string &GCCBinary,
98 const std::vector<std::string> *Args = 0,
99 const std::vector<std::string> *GCCArgs = 0,
100 bool UseIntegratedAssembler = false);
102 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
103 const std::vector<std::string> *Args=0);
105 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
106 const std::vector<std::string> *Args=0);
108 static AbstractInterpreter*
109 createCustomCompiler(std::string &Message,
110 const std::string &CompileCommandLine);
112 static AbstractInterpreter*
113 createCustomExecutor(std::string &Message,
114 const std::string &ExecCommandLine);
117 virtual ~AbstractInterpreter() {}
119 /// compileProgram - Compile the specified program from bitcode to executable
120 /// code. This does not produce any output, it is only used when debugging
121 /// the code generator. It returns false if the code generator fails.
122 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
123 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
125 /// OutputCode - Compile the specified program from bitcode to code
126 /// understood by the GCC driver (either C or asm). If the code generator
127 /// fails, it sets Error, otherwise, this function returns the type of code
129 virtual GCC::FileType OutputCode(const std::string &Bitcode,
130 sys::Path &OutFile, std::string &Error,
131 unsigned Timeout = 0,
132 unsigned MemoryLimit = 0) {
133 Error = "OutputCode not supported by this AbstractInterpreter!";
137 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
138 /// specified filename. This sets RetVal to the exit code of the program or
139 /// returns false if a problem was encountered that prevented execution of
142 virtual int ExecuteProgram(const std::string &Bitcode,
143 const std::vector<std::string> &Args,
144 const std::string &InputFile,
145 const std::string &OutputFile,
147 const std::vector<std::string> &GCCArgs =
148 std::vector<std::string>(),
149 const std::vector<std::string> &SharedLibs =
150 std::vector<std::string>(),
151 unsigned Timeout = 0,
152 unsigned MemoryLimit = 0) = 0;
155 //===---------------------------------------------------------------------===//
156 // CBE Implementation of AbstractIntepreter interface
158 class CBE : public AbstractInterpreter {
159 sys::Path LLCPath; // The path to the `llc' executable.
160 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
163 CBE(const sys::Path &llcPath, GCC *Gcc,
164 const std::vector<std::string> *Args)
165 : LLCPath(llcPath), gcc(Gcc) {
167 if (Args) ToolArgs = *Args;
169 ~CBE() { delete gcc; }
171 /// compileProgram - Compile the specified program from bitcode to executable
172 /// code. This does not produce any output, it is only used when debugging
173 /// the code generator. Returns false if the code generator fails.
174 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
175 unsigned Timeout = 0, unsigned MemoryLimit = 0);
177 virtual int ExecuteProgram(const std::string &Bitcode,
178 const std::vector<std::string> &Args,
179 const std::string &InputFile,
180 const std::string &OutputFile,
182 const std::vector<std::string> &GCCArgs =
183 std::vector<std::string>(),
184 const std::vector<std::string> &SharedLibs =
185 std::vector<std::string>(),
186 unsigned Timeout = 0,
187 unsigned MemoryLimit = 0);
189 /// OutputCode - Compile the specified program from bitcode to code
190 /// understood by the GCC driver (either C or asm). If the code generator
191 /// fails, it sets Error, otherwise, this function returns the type of code
193 virtual GCC::FileType OutputCode(const std::string &Bitcode,
194 sys::Path &OutFile, std::string &Error,
195 unsigned Timeout = 0,
196 unsigned MemoryLimit = 0);
200 //===---------------------------------------------------------------------===//
201 // LLC Implementation of AbstractIntepreter interface
203 class LLC : public AbstractInterpreter {
204 std::string LLCPath; // The path to the LLC executable.
205 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
207 bool UseIntegratedAssembler;
209 LLC(const std::string &llcPath, GCC *Gcc,
210 const std::vector<std::string> *Args,
211 bool useIntegratedAssembler)
212 : LLCPath(llcPath), gcc(Gcc),
213 UseIntegratedAssembler(useIntegratedAssembler) {
215 if (Args) ToolArgs = *Args;
217 ~LLC() { delete gcc; }
219 /// compileProgram - Compile the specified program from bitcode to executable
220 /// code. This does not produce any output, it is only used when debugging
221 /// the code generator. Returns false if the code generator fails.
222 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
223 unsigned Timeout = 0, unsigned MemoryLimit = 0);
225 virtual int ExecuteProgram(const std::string &Bitcode,
226 const std::vector<std::string> &Args,
227 const std::string &InputFile,
228 const std::string &OutputFile,
230 const std::vector<std::string> &GCCArgs =
231 std::vector<std::string>(),
232 const std::vector<std::string> &SharedLibs =
233 std::vector<std::string>(),
234 unsigned Timeout = 0,
235 unsigned MemoryLimit = 0);
237 /// OutputCode - Compile the specified program from bitcode to code
238 /// understood by the GCC driver (either C or asm). If the code generator
239 /// fails, it sets Error, otherwise, this function returns the type of code
241 virtual GCC::FileType OutputCode(const std::string &Bitcode,
242 sys::Path &OutFile, std::string &Error,
243 unsigned Timeout = 0,
244 unsigned MemoryLimit = 0);
247 } // End llvm namespace