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/System/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 {
90 static CBE *createCBE(const char *Argv0, std::string &Message,
91 const std::string &GCCBinary,
92 const std::vector<std::string> *Args = 0,
93 const std::vector<std::string> *GCCArgs = 0);
94 static LLC *createLLC(const char *Argv0, std::string &Message,
95 const std::string &GCCBinary,
96 const std::vector<std::string> *Args = 0,
97 const std::vector<std::string> *GCCArgs = 0,
98 bool UseIntegratedAssembler = false);
100 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
101 const std::vector<std::string> *Args=0);
103 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
104 const std::vector<std::string> *Args=0);
106 static AbstractInterpreter* createCustom(std::string &Message,
107 const std::string &ExecCommandLine);
110 virtual ~AbstractInterpreter() {}
112 /// compileProgram - Compile the specified program from bitcode to executable
113 /// code. This does not produce any output, it is only used when debugging
114 /// the code generator. It returns false if the code generator fails.
115 virtual void compileProgram(const std::string &Bitcode, std::string *Error) {}
117 /// OutputCode - Compile the specified program from bitcode to code
118 /// understood by the GCC driver (either C or asm). If the code generator
119 /// fails, it sets Error, otherwise, this function returns the type of code
121 virtual GCC::FileType OutputCode(const std::string &Bitcode,
122 sys::Path &OutFile, std::string &Error) {
123 Error = "OutputCode not supported by this AbstractInterpreter!";
127 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
128 /// specified filename. This sets RetVal to the exit code of the program or
129 /// returns false if a problem was encountered that prevented execution of
132 virtual int ExecuteProgram(const std::string &Bitcode,
133 const std::vector<std::string> &Args,
134 const std::string &InputFile,
135 const std::string &OutputFile,
137 const std::vector<std::string> &GCCArgs =
138 std::vector<std::string>(),
139 const std::vector<std::string> &SharedLibs =
140 std::vector<std::string>(),
141 unsigned Timeout = 0,
142 unsigned MemoryLimit = 0) = 0;
145 //===---------------------------------------------------------------------===//
146 // CBE Implementation of AbstractIntepreter interface
148 class CBE : public AbstractInterpreter {
149 sys::Path LLCPath; // The path to the `llc' executable.
150 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
153 CBE(const sys::Path &llcPath, GCC *Gcc,
154 const std::vector<std::string> *Args)
155 : LLCPath(llcPath), gcc(Gcc) {
157 if (Args) ToolArgs = *Args;
159 ~CBE() { delete gcc; }
161 /// compileProgram - Compile the specified program from bitcode to executable
162 /// code. This does not produce any output, it is only used when debugging
163 /// the code generator. Returns false if the code generator fails.
164 virtual void compileProgram(const std::string &Bitcode, std::string *Error);
166 virtual int ExecuteProgram(const std::string &Bitcode,
167 const std::vector<std::string> &Args,
168 const std::string &InputFile,
169 const std::string &OutputFile,
171 const std::vector<std::string> &GCCArgs =
172 std::vector<std::string>(),
173 const std::vector<std::string> &SharedLibs =
174 std::vector<std::string>(),
175 unsigned Timeout = 0,
176 unsigned MemoryLimit = 0);
178 /// OutputCode - Compile the specified program from bitcode to code
179 /// understood by the GCC driver (either C or asm). If the code generator
180 /// fails, it sets Error, otherwise, this function returns the type of code
182 virtual GCC::FileType OutputCode(const std::string &Bitcode,
183 sys::Path &OutFile, std::string &Error);
187 //===---------------------------------------------------------------------===//
188 // LLC Implementation of AbstractIntepreter interface
190 class LLC : public AbstractInterpreter {
191 std::string LLCPath; // The path to the LLC executable.
192 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
194 bool UseIntegratedAssembler;
196 LLC(const std::string &llcPath, GCC *Gcc,
197 const std::vector<std::string> *Args,
198 bool useIntegratedAssembler)
199 : LLCPath(llcPath), gcc(Gcc),
200 UseIntegratedAssembler(useIntegratedAssembler) {
202 if (Args) ToolArgs = *Args;
204 ~LLC() { delete gcc; }
206 /// compileProgram - Compile the specified program from bitcode to executable
207 /// code. This does not produce any output, it is only used when debugging
208 /// the code generator. Returns false if the code generator fails.
209 virtual void compileProgram(const std::string &Bitcode, std::string *Error);
211 virtual int ExecuteProgram(const std::string &Bitcode,
212 const std::vector<std::string> &Args,
213 const std::string &InputFile,
214 const std::string &OutputFile,
216 const std::vector<std::string> &GCCArgs =
217 std::vector<std::string>(),
218 const std::vector<std::string> &SharedLibs =
219 std::vector<std::string>(),
220 unsigned Timeout = 0,
221 unsigned MemoryLimit = 0);
223 /// OutputCode - Compile the specified program from bitcode to code
224 /// understood by the GCC driver (either C or asm). If the code generator
225 /// fails, it sets Error, otherwise, this function returns the type of code
227 virtual GCC::FileType OutputCode(const std::string &Bitcode,
228 sys::Path &OutFile, std::string &Error);
231 } // End llvm namespace