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/SystemUtils.h"
30 extern cl::opt<bool> SaveTemps;
31 extern Triple TargetTriple;
35 //===---------------------------------------------------------------------===//
39 std::string GCCPath; // The path to the gcc executable.
40 std::string RemoteClientPath; // The path to the rsh / ssh executable.
41 std::vector<std::string> gccArgs; // GCC-specific arguments.
42 GCC(StringRef gccPath, StringRef RemotePath,
43 const std::vector<std::string> *GCCArgs)
44 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
45 if (GCCArgs) gccArgs = *GCCArgs;
48 enum FileType { AsmFile, ObjectFile, CFile };
50 static GCC *create(std::string &Message,
51 const std::string &GCCBinary,
52 const std::vector<std::string> *Args);
54 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
55 /// either a .s file, or a .c file, specified by FileType), with the specified
56 /// arguments. Standard input is specified with InputFile, and standard
57 /// Output is captured to the specified OutputFile location. The SharedLibs
58 /// option specifies optional native shared objects that can be loaded into
59 /// the program for execution.
61 int ExecuteProgram(const std::string &ProgramFile,
62 const std::vector<std::string> &Args,
64 const std::string &InputFile,
65 const std::string &OutputFile,
66 std::string *Error = 0,
67 const std::vector<std::string> &GCCArgs =
68 std::vector<std::string>(),
70 unsigned MemoryLimit = 0);
72 /// MakeSharedObject - This compiles the specified file (which is either a .c
73 /// file or a .s file) into a shared object.
75 int MakeSharedObject(const std::string &InputFile, FileType fileType,
76 std::string &OutputFile,
77 const std::vector<std::string> &ArgsForGCC,
82 //===---------------------------------------------------------------------===//
83 /// AbstractInterpreter Class - Subclasses of this class are used to execute
84 /// LLVM bitcode in a variety of ways. This abstract interface hides this
85 /// complexity behind a simple interface.
87 class AbstractInterpreter {
88 virtual void anchor();
90 static LLC *createLLC(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 bool UseIntegratedAssembler = false);
96 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
97 const std::vector<std::string> *Args=0);
99 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
100 const std::vector<std::string> *Args=0);
102 static AbstractInterpreter*
103 createCustomCompiler(std::string &Message,
104 const std::string &CompileCommandLine);
106 static AbstractInterpreter*
107 createCustomExecutor(std::string &Message,
108 const std::string &ExecCommandLine);
111 virtual ~AbstractInterpreter() {}
113 /// compileProgram - Compile the specified program from bitcode to executable
114 /// code. This does not produce any output, it is only used when debugging
115 /// the code generator. It returns false if the code generator fails.
116 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
117 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
119 /// OutputCode - Compile the specified program from bitcode to code
120 /// understood by the GCC driver (either C or asm). If the code generator
121 /// fails, it sets Error, otherwise, this function returns the type of code
123 virtual GCC::FileType OutputCode(const std::string &Bitcode,
124 std::string &OutFile, std::string &Error,
125 unsigned Timeout = 0,
126 unsigned MemoryLimit = 0) {
127 Error = "OutputCode not supported by this AbstractInterpreter!";
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 false if a problem was encountered that prevented execution of
136 virtual int ExecuteProgram(const std::string &Bitcode,
137 const std::vector<std::string> &Args,
138 const std::string &InputFile,
139 const std::string &OutputFile,
141 const std::vector<std::string> &GCCArgs =
142 std::vector<std::string>(),
143 const std::vector<std::string> &SharedLibs =
144 std::vector<std::string>(),
145 unsigned Timeout = 0,
146 unsigned MemoryLimit = 0) = 0;
149 //===---------------------------------------------------------------------===//
150 // LLC Implementation of AbstractIntepreter interface
152 class LLC : public AbstractInterpreter {
153 std::string LLCPath; // The path to the LLC executable.
154 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
156 bool UseIntegratedAssembler;
158 LLC(const std::string &llcPath, GCC *Gcc,
159 const std::vector<std::string> *Args,
160 bool useIntegratedAssembler)
161 : LLCPath(llcPath), gcc(Gcc),
162 UseIntegratedAssembler(useIntegratedAssembler) {
164 if (Args) ToolArgs = *Args;
166 ~LLC() { delete gcc; }
168 /// compileProgram - Compile the specified program from bitcode to executable
169 /// code. This does not produce any output, it is only used when debugging
170 /// the code generator. Returns false if the code generator fails.
171 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
172 unsigned Timeout = 0, unsigned MemoryLimit = 0);
174 virtual int ExecuteProgram(const std::string &Bitcode,
175 const std::vector<std::string> &Args,
176 const std::string &InputFile,
177 const std::string &OutputFile,
179 const std::vector<std::string> &GCCArgs =
180 std::vector<std::string>(),
181 const std::vector<std::string> &SharedLibs =
182 std::vector<std::string>(),
183 unsigned Timeout = 0,
184 unsigned MemoryLimit = 0);
186 /// OutputCode - Compile the specified program from bitcode to code
187 /// understood by the GCC driver (either C or asm). If the code generator
188 /// fails, it sets Error, otherwise, this function returns the type of code
190 virtual GCC::FileType OutputCode(const std::string &Bitcode,
191 std::string &OutFile, std::string &Error,
192 unsigned Timeout = 0,
193 unsigned MemoryLimit = 0);
196 } // End llvm namespace