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;
36 //===---------------------------------------------------------------------===//
40 std::string GCCPath; // The path to the gcc executable.
41 std::string RemoteClientPath; // The path to the rsh / ssh executable.
42 std::vector<std::string> gccArgs; // GCC-specific arguments.
43 GCC(StringRef gccPath, StringRef 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 LLC *createLLC(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 bool UseIntegratedAssembler = false);
97 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
98 const std::vector<std::string> *Args=0);
100 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
101 const std::vector<std::string> *Args=0);
103 static AbstractInterpreter*
104 createCustomCompiler(std::string &Message,
105 const std::string &CompileCommandLine);
107 static AbstractInterpreter*
108 createCustomExecutor(std::string &Message,
109 const std::string &ExecCommandLine);
112 virtual ~AbstractInterpreter() {}
114 /// compileProgram - Compile the specified program from bitcode to executable
115 /// code. This does not produce any output, it is only used when debugging
116 /// the code generator. It returns false if the code generator fails.
117 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
118 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
120 /// OutputCode - Compile the specified program from bitcode to code
121 /// understood by the GCC driver (either C or asm). If the code generator
122 /// fails, it sets Error, otherwise, this function returns the type of code
124 virtual GCC::FileType OutputCode(const std::string &Bitcode,
125 sys::Path &OutFile, std::string &Error,
126 unsigned Timeout = 0,
127 unsigned MemoryLimit = 0) {
128 Error = "OutputCode not supported by this AbstractInterpreter!";
132 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
133 /// specified filename. This sets RetVal to the exit code of the program or
134 /// returns false if a problem was encountered that prevented execution of
137 virtual int ExecuteProgram(const std::string &Bitcode,
138 const std::vector<std::string> &Args,
139 const std::string &InputFile,
140 const std::string &OutputFile,
142 const std::vector<std::string> &GCCArgs =
143 std::vector<std::string>(),
144 const std::vector<std::string> &SharedLibs =
145 std::vector<std::string>(),
146 unsigned Timeout = 0,
147 unsigned MemoryLimit = 0) = 0;
150 //===---------------------------------------------------------------------===//
151 // LLC Implementation of AbstractIntepreter interface
153 class LLC : public AbstractInterpreter {
154 std::string LLCPath; // The path to the LLC executable.
155 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
157 bool UseIntegratedAssembler;
159 LLC(const std::string &llcPath, GCC *Gcc,
160 const std::vector<std::string> *Args,
161 bool useIntegratedAssembler)
162 : LLCPath(llcPath), gcc(Gcc),
163 UseIntegratedAssembler(useIntegratedAssembler) {
165 if (Args) ToolArgs = *Args;
167 ~LLC() { delete gcc; }
169 /// compileProgram - Compile the specified program from bitcode to executable
170 /// code. This does not produce any output, it is only used when debugging
171 /// the code generator. Returns false if the code generator fails.
172 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
173 unsigned Timeout = 0, unsigned MemoryLimit = 0);
175 virtual int ExecuteProgram(const std::string &Bitcode,
176 const std::vector<std::string> &Args,
177 const std::string &InputFile,
178 const std::string &OutputFile,
180 const std::vector<std::string> &GCCArgs =
181 std::vector<std::string>(),
182 const std::vector<std::string> &SharedLibs =
183 std::vector<std::string>(),
184 unsigned Timeout = 0,
185 unsigned MemoryLimit = 0);
187 /// OutputCode - Compile the specified program from bitcode to code
188 /// understood by the GCC driver (either C or asm). If the code generator
189 /// fails, it sets Error, otherwise, this function returns the type of code
191 virtual GCC::FileType OutputCode(const std::string &Bitcode,
192 sys::Path &OutFile, std::string &Error,
193 unsigned Timeout = 0,
194 unsigned MemoryLimit = 0);
197 } // End llvm namespace