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/SystemUtils.h"
24 #include "llvm/Support/Path.h"
30 extern cl::opt<bool> SaveTemps;
31 extern Triple TargetTriple;
36 //===---------------------------------------------------------------------===//
40 sys::Path GCCPath; // The path to the gcc executable.
41 sys::Path RemoteClientPath; // The path to the rsh / ssh executable.
42 std::vector<std::string> gccArgs; // GCC-specific arguments.
43 GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
44 const std::vector<std::string> *GCCArgs)
45 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
46 if (GCCArgs) gccArgs = *GCCArgs;
49 enum FileType { AsmFile, ObjectFile, CFile };
51 static GCC *create(std::string &Message,
52 const std::string &GCCBinary,
53 const std::vector<std::string> *Args);
55 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
56 /// either a .s file, or a .c file, specified by FileType), with the specified
57 /// arguments. Standard input is specified with InputFile, and standard
58 /// Output is captured to the specified OutputFile location. The SharedLibs
59 /// option specifies optional native shared objects that can be loaded into
60 /// the program for execution.
62 int ExecuteProgram(const std::string &ProgramFile,
63 const std::vector<std::string> &Args,
65 const std::string &InputFile,
66 const std::string &OutputFile,
67 std::string *Error = 0,
68 const std::vector<std::string> &GCCArgs =
69 std::vector<std::string>(),
71 unsigned MemoryLimit = 0);
73 /// MakeSharedObject - This compiles the specified file (which is either a .c
74 /// file or a .s file) into a shared object.
76 int MakeSharedObject(const std::string &InputFile, FileType fileType,
77 std::string &OutputFile,
78 const std::vector<std::string> &ArgsForGCC,
83 //===---------------------------------------------------------------------===//
84 /// AbstractInterpreter Class - Subclasses of this class are used to execute
85 /// LLVM bitcode in a variety of ways. This abstract interface hides this
86 /// complexity behind a simple interface.
88 class AbstractInterpreter {
89 virtual void anchor();
91 static CBE *createCBE(const char *Argv0, std::string &Message,
92 const std::string &GCCBinary,
93 const std::vector<std::string> *Args = 0,
94 const std::vector<std::string> *GCCArgs = 0);
95 static LLC *createLLC(const char *Argv0, std::string &Message,
96 const std::string &GCCBinary,
97 const std::vector<std::string> *Args = 0,
98 const std::vector<std::string> *GCCArgs = 0,
99 bool UseIntegratedAssembler = false);
101 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
102 const std::vector<std::string> *Args=0);
104 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
105 const std::vector<std::string> *Args=0);
107 static AbstractInterpreter*
108 createCustomCompiler(std::string &Message,
109 const std::string &CompileCommandLine);
111 static AbstractInterpreter*
112 createCustomExecutor(std::string &Message,
113 const std::string &ExecCommandLine);
116 virtual ~AbstractInterpreter() {}
118 /// compileProgram - Compile the specified program from bitcode to executable
119 /// code. This does not produce any output, it is only used when debugging
120 /// the code generator. It returns false if the code generator fails.
121 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
122 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
124 /// OutputCode - Compile the specified program from bitcode to code
125 /// understood by the GCC driver (either C or asm). If the code generator
126 /// fails, it sets Error, otherwise, this function returns the type of code
128 virtual GCC::FileType OutputCode(const std::string &Bitcode,
129 sys::Path &OutFile, std::string &Error,
130 unsigned Timeout = 0,
131 unsigned MemoryLimit = 0) {
132 Error = "OutputCode not supported by this AbstractInterpreter!";
136 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
137 /// specified filename. This sets RetVal to the exit code of the program or
138 /// returns false if a problem was encountered that prevented execution of
141 virtual int ExecuteProgram(const std::string &Bitcode,
142 const std::vector<std::string> &Args,
143 const std::string &InputFile,
144 const std::string &OutputFile,
146 const std::vector<std::string> &GCCArgs =
147 std::vector<std::string>(),
148 const std::vector<std::string> &SharedLibs =
149 std::vector<std::string>(),
150 unsigned Timeout = 0,
151 unsigned MemoryLimit = 0) = 0;
154 //===---------------------------------------------------------------------===//
155 // CBE Implementation of AbstractIntepreter interface
157 class CBE : public AbstractInterpreter {
158 sys::Path LLCPath; // The path to the `llc' executable.
159 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
162 CBE(const sys::Path &llcPath, GCC *Gcc,
163 const std::vector<std::string> *Args)
164 : LLCPath(llcPath), gcc(Gcc) {
166 if (Args) ToolArgs = *Args;
168 ~CBE() { delete gcc; }
170 /// compileProgram - Compile the specified program from bitcode to executable
171 /// code. This does not produce any output, it is only used when debugging
172 /// the code generator. Returns false if the code generator fails.
173 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
174 unsigned Timeout = 0, unsigned MemoryLimit = 0);
176 virtual int ExecuteProgram(const std::string &Bitcode,
177 const std::vector<std::string> &Args,
178 const std::string &InputFile,
179 const std::string &OutputFile,
181 const std::vector<std::string> &GCCArgs =
182 std::vector<std::string>(),
183 const std::vector<std::string> &SharedLibs =
184 std::vector<std::string>(),
185 unsigned Timeout = 0,
186 unsigned MemoryLimit = 0);
188 /// OutputCode - Compile the specified program from bitcode to code
189 /// understood by the GCC driver (either C or asm). If the code generator
190 /// fails, it sets Error, otherwise, this function returns the type of code
192 virtual GCC::FileType OutputCode(const std::string &Bitcode,
193 sys::Path &OutFile, std::string &Error,
194 unsigned Timeout = 0,
195 unsigned MemoryLimit = 0);
199 //===---------------------------------------------------------------------===//
200 // LLC Implementation of AbstractIntepreter interface
202 class LLC : public AbstractInterpreter {
203 std::string LLCPath; // The path to the LLC executable.
204 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
206 bool UseIntegratedAssembler;
208 LLC(const std::string &llcPath, GCC *Gcc,
209 const std::vector<std::string> *Args,
210 bool useIntegratedAssembler)
211 : LLCPath(llcPath), gcc(Gcc),
212 UseIntegratedAssembler(useIntegratedAssembler) {
214 if (Args) ToolArgs = *Args;
216 ~LLC() { delete gcc; }
218 /// compileProgram - Compile the specified program from bitcode to executable
219 /// code. This does not produce any output, it is only used when debugging
220 /// the code generator. Returns false if the code generator fails.
221 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
222 unsigned Timeout = 0, unsigned MemoryLimit = 0);
224 virtual int ExecuteProgram(const std::string &Bitcode,
225 const std::vector<std::string> &Args,
226 const std::string &InputFile,
227 const std::string &OutputFile,
229 const std::vector<std::string> &GCCArgs =
230 std::vector<std::string>(),
231 const std::vector<std::string> &SharedLibs =
232 std::vector<std::string>(),
233 unsigned Timeout = 0,
234 unsigned MemoryLimit = 0);
236 /// OutputCode - Compile the specified program from bitcode to code
237 /// understood by the GCC driver (either C or asm). If the code generator
238 /// fails, it sets Error, otherwise, this function returns the type of code
240 virtual GCC::FileType OutputCode(const std::string &Bitcode,
241 sys::Path &OutFile, std::string &Error,
242 unsigned Timeout = 0,
243 unsigned MemoryLimit = 0);
246 } // End llvm namespace