blob: 8589727753106b11723494c7df90633a0cd80b29 [file] [log] [blame]
Chris Lattnerffac2862006-06-06 22:30:59 +00001//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerffac2862006-06-06 22:30:59 +00007//
8//===----------------------------------------------------------------------===//
9//
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
13// engines.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef BUGPOINT_TOOLRUNNER_H
18#define BUGPOINT_TOOLRUNNER_H
19
Daniel Dunbar8575a602009-08-18 03:35:57 +000020#include "llvm/ADT/Triple.h"
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000021#include "llvm/Support/CommandLine.h"
Nick Lewycky6ba630b2010-04-12 05:08:25 +000022#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000023#include "llvm/Support/Path.h"
Rafael Espindolad1fcac92013-06-11 20:00:56 +000024#include "llvm/Support/PathV1.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000025#include "llvm/Support/SystemUtils.h"
Chris Lattnerffac2862006-06-06 22:30:59 +000026#include <exception>
27#include <vector>
28
29namespace llvm {
30
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000031extern cl::opt<bool> SaveTemps;
Daniel Dunbar8575a602009-08-18 03:35:57 +000032extern Triple TargetTriple;
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000033
Chris Lattnerffac2862006-06-06 22:30:59 +000034class CBE;
35class LLC;
36
Chris Lattnerffac2862006-06-06 22:30:59 +000037//===---------------------------------------------------------------------===//
38// GCC abstraction
39//
40class GCC {
Rafael Espindolae7a629f2013-06-13 16:22:26 +000041 std::string GCCPath; // The path to the gcc executable.
42 std::string RemoteClientPath; // The path to the rsh / ssh executable.
Bill Wendlingbf5d8272009-03-02 23:13:18 +000043 std::vector<std::string> gccArgs; // GCC-specific arguments.
Rafael Espindolae7a629f2013-06-13 16:22:26 +000044 GCC(StringRef gccPath, StringRef RemotePath,
Bill Wendlingbf5d8272009-03-02 23:13:18 +000045 const std::vector<std::string> *GCCArgs)
46 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
47 if (GCCArgs) gccArgs = *GCCArgs;
48 }
Chris Lattnerffac2862006-06-06 22:30:59 +000049public:
Chris Lattnerfd381322010-03-16 06:41:47 +000050 enum FileType { AsmFile, ObjectFile, CFile };
Chris Lattnerffac2862006-06-06 22:30:59 +000051
Dan Gohman46ffffa2009-08-05 20:21:17 +000052 static GCC *create(std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +000053 const std::string &GCCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +000054 const std::vector<std::string> *Args);
Chris Lattnerffac2862006-06-06 22:30:59 +000055
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.
62 ///
63 int ExecuteProgram(const std::string &ProgramFile,
64 const std::vector<std::string> &Args,
65 FileType fileType,
66 const std::string &InputFile,
67 const std::string &OutputFile,
Duncan Sands41b4a6b2010-07-12 08:16:59 +000068 std::string *Error = 0,
Chris Lattnerffac2862006-06-06 22:30:59 +000069 const std::vector<std::string> &GCCArgs =
Andrew Trick69a963e2011-02-08 18:07:10 +000070 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +000071 unsigned Timeout = 0,
72 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +000073
74 /// MakeSharedObject - This compiles the specified file (which is either a .c
75 /// file or a .s file) into a shared object.
76 ///
77 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattnercc21fa72006-06-27 20:35:36 +000078 std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +000079 const std::vector<std::string> &ArgsForGCC,
80 std::string &Error);
Chris Lattnerffac2862006-06-06 22:30:59 +000081};
82
83
84//===---------------------------------------------------------------------===//
85/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif0e535c3c2007-07-04 21:55:50 +000086/// LLVM bitcode in a variety of ways. This abstract interface hides this
Chris Lattnerffac2862006-06-06 22:30:59 +000087/// complexity behind a simple interface.
88///
89class AbstractInterpreter {
David Blaikiea379b1812011-12-20 02:50:00 +000090 virtual void anchor();
Chris Lattnerffac2862006-06-06 22:30:59 +000091public:
Dan Gohman46ffffa2009-08-05 20:21:17 +000092 static CBE *createCBE(const char *Argv0, std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +000093 const std::string &GCCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +000094 const std::vector<std::string> *Args = 0,
95 const std::vector<std::string> *GCCArgs = 0);
Dan Gohman46ffffa2009-08-05 20:21:17 +000096 static LLC *createLLC(const char *Argv0, std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +000097 const std::string &GCCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +000098 const std::vector<std::string> *Args = 0,
Chris Lattnerfd381322010-03-16 06:41:47 +000099 const std::vector<std::string> *GCCArgs = 0,
100 bool UseIntegratedAssembler = false);
Chris Lattnerffac2862006-06-06 22:30:59 +0000101
Dan Gohman46ffffa2009-08-05 20:21:17 +0000102 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
Chris Lattnerffac2862006-06-06 22:30:59 +0000103 const std::vector<std::string> *Args=0);
104
Dan Gohman46ffffa2009-08-05 20:21:17 +0000105 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
Chris Lattnerffac2862006-06-06 22:30:59 +0000106 const std::vector<std::string> *Args=0);
107
Andrew Trick8665d592011-02-08 18:20:48 +0000108 static AbstractInterpreter*
109 createCustomCompiler(std::string &Message,
110 const std::string &CompileCommandLine);
111
112 static AbstractInterpreter*
113 createCustomExecutor(std::string &Message,
114 const std::string &ExecCommandLine);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000115
Chris Lattnerffac2862006-06-06 22:30:59 +0000116
117 virtual ~AbstractInterpreter() {}
118
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000119 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000120 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000121 /// the code generator. It returns false if the code generator fails.
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000122 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
123 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
Chris Lattnerffac2862006-06-06 22:30:59 +0000124
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000125 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattner634bc042006-09-15 21:29:15 +0000126 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000127 /// fails, it sets Error, otherwise, this function returns the type of code
128 /// emitted.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000129 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000130 sys::Path &OutFile, std::string &Error,
131 unsigned Timeout = 0,
132 unsigned MemoryLimit = 0) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000133 Error = "OutputCode not supported by this AbstractInterpreter!";
134 return GCC::AsmFile;
Chris Lattner634bc042006-09-15 21:29:15 +0000135 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000136
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000137 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000138 /// 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
140 /// the program.
Chris Lattnerffac2862006-06-06 22:30:59 +0000141 ///
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000142 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000143 const std::vector<std::string> &Args,
144 const std::string &InputFile,
145 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000146 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000147 const std::vector<std::string> &GCCArgs =
148 std::vector<std::string>(),
149 const std::vector<std::string> &SharedLibs =
150 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000151 unsigned Timeout = 0,
152 unsigned MemoryLimit = 0) = 0;
Chris Lattnerffac2862006-06-06 22:30:59 +0000153};
154
155//===---------------------------------------------------------------------===//
156// CBE Implementation of AbstractIntepreter interface
157//
158class CBE : public AbstractInterpreter {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000159 sys::Path LLCPath; // The path to the `llc' executable.
160 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Chris Lattnerffac2862006-06-06 22:30:59 +0000161 GCC *gcc;
162public:
163 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000164 const std::vector<std::string> *Args)
165 : LLCPath(llcPath), gcc(Gcc) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000166 ToolArgs.clear ();
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000167 if (Args) ToolArgs = *Args;
Chris Lattnerffac2862006-06-06 22:30:59 +0000168 }
169 ~CBE() { delete gcc; }
170
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000171 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000172 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000173 /// the code generator. Returns false if the code generator fails.
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000174 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
175 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000176
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000177 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000178 const std::vector<std::string> &Args,
179 const std::string &InputFile,
180 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000181 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000182 const std::vector<std::string> &GCCArgs =
183 std::vector<std::string>(),
184 const std::vector<std::string> &SharedLibs =
185 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000186 unsigned Timeout = 0,
187 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000188
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000189 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattner634bc042006-09-15 21:29:15 +0000190 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000191 /// fails, it sets Error, otherwise, this function returns the type of code
192 /// emitted.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000193 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000194 sys::Path &OutFile, std::string &Error,
195 unsigned Timeout = 0,
196 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000197};
198
199
200//===---------------------------------------------------------------------===//
201// LLC Implementation of AbstractIntepreter interface
202//
203class LLC : public AbstractInterpreter {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000204 std::string LLCPath; // The path to the LLC executable.
205 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Chris Lattnerffac2862006-06-06 22:30:59 +0000206 GCC *gcc;
Chris Lattnerfd381322010-03-16 06:41:47 +0000207 bool UseIntegratedAssembler;
Chris Lattnerffac2862006-06-06 22:30:59 +0000208public:
209 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000210 const std::vector<std::string> *Args,
Chris Lattnerfd381322010-03-16 06:41:47 +0000211 bool useIntegratedAssembler)
212 : LLCPath(llcPath), gcc(Gcc),
213 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000214 ToolArgs.clear();
215 if (Args) ToolArgs = *Args;
Chris Lattnerffac2862006-06-06 22:30:59 +0000216 }
217 ~LLC() { delete gcc; }
218
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000219 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000220 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000221 /// the code generator. Returns false if the code generator fails.
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000222 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
223 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000224
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000225 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000226 const std::vector<std::string> &Args,
227 const std::string &InputFile,
228 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000229 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000230 const std::vector<std::string> &GCCArgs =
231 std::vector<std::string>(),
232 const std::vector<std::string> &SharedLibs =
233 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000234 unsigned Timeout = 0,
235 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000236
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000237 /// 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
240 /// emitted.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000241 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000242 sys::Path &OutFile, std::string &Error,
243 unsigned Timeout = 0,
244 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000245};
246
247} // End llvm namespace
248
249#endif