blob: cfa8acf6b24043ba74f573e66211e4d7d85b3365 [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"
Chris Lattnerffac2862006-06-06 22:30:59 +000023#include "llvm/Support/SystemUtils.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000024#include "llvm/Support/Path.h"
Chris Lattnerffac2862006-06-06 22:30:59 +000025#include <exception>
26#include <vector>
27
28namespace llvm {
29
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000030extern cl::opt<bool> SaveTemps;
Daniel Dunbar8575a602009-08-18 03:35:57 +000031extern Triple TargetTriple;
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000032
Chris Lattnerffac2862006-06-06 22:30:59 +000033class CBE;
34class LLC;
35
Chris Lattnerffac2862006-06-06 22:30:59 +000036//===---------------------------------------------------------------------===//
37// GCC abstraction
38//
39class GCC {
Bill Wendlingbf5d8272009-03-02 23:13:18 +000040 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;
47 }
Chris Lattnerffac2862006-06-06 22:30:59 +000048public:
Chris Lattnerfd381322010-03-16 06:41:47 +000049 enum FileType { AsmFile, ObjectFile, CFile };
Chris Lattnerffac2862006-06-06 22:30:59 +000050
Dan Gohman46ffffa2009-08-05 20:21:17 +000051 static GCC *create(std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +000052 const std::string &GCCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +000053 const std::vector<std::string> *Args);
Chris Lattnerffac2862006-06-06 22:30:59 +000054
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.
61 ///
62 int ExecuteProgram(const std::string &ProgramFile,
63 const std::vector<std::string> &Args,
64 FileType fileType,
65 const std::string &InputFile,
66 const std::string &OutputFile,
Duncan Sands41b4a6b2010-07-12 08:16:59 +000067 std::string *Error = 0,
Chris Lattnerffac2862006-06-06 22:30:59 +000068 const std::vector<std::string> &GCCArgs =
Andrew Trick69a963e2011-02-08 18:07:10 +000069 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +000070 unsigned Timeout = 0,
71 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +000072
73 /// MakeSharedObject - This compiles the specified file (which is either a .c
74 /// file or a .s file) into a shared object.
75 ///
76 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattnercc21fa72006-06-27 20:35:36 +000077 std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +000078 const std::vector<std::string> &ArgsForGCC,
79 std::string &Error);
Chris Lattnerffac2862006-06-06 22:30:59 +000080};
81
82
83//===---------------------------------------------------------------------===//
84/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif0e535c3c2007-07-04 21:55:50 +000085/// LLVM bitcode in a variety of ways. This abstract interface hides this
Chris Lattnerffac2862006-06-06 22:30:59 +000086/// complexity behind a simple interface.
87///
88class AbstractInterpreter {
89public:
Dan Gohman46ffffa2009-08-05 20:21:17 +000090 static CBE *createCBE(const char *Argv0, std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +000091 const std::string &GCCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +000092 const std::vector<std::string> *Args = 0,
93 const std::vector<std::string> *GCCArgs = 0);
Dan Gohman46ffffa2009-08-05 20:21:17 +000094 static LLC *createLLC(const char *Argv0, std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +000095 const std::string &GCCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +000096 const std::vector<std::string> *Args = 0,
Chris Lattnerfd381322010-03-16 06:41:47 +000097 const std::vector<std::string> *GCCArgs = 0,
98 bool UseIntegratedAssembler = false);
Chris Lattnerffac2862006-06-06 22:30:59 +000099
Dan Gohman46ffffa2009-08-05 20:21:17 +0000100 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
Chris Lattnerffac2862006-06-06 22:30:59 +0000101 const std::vector<std::string> *Args=0);
102
Dan Gohman46ffffa2009-08-05 20:21:17 +0000103 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
Chris Lattnerffac2862006-06-06 22:30:59 +0000104 const std::vector<std::string> *Args=0);
105
Andrew Trick8665d592011-02-08 18:20:48 +0000106 static AbstractInterpreter*
107 createCustomCompiler(std::string &Message,
108 const std::string &CompileCommandLine);
109
110 static AbstractInterpreter*
111 createCustomExecutor(std::string &Message,
112 const std::string &ExecCommandLine);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000113
Chris Lattnerffac2862006-06-06 22:30:59 +0000114
115 virtual ~AbstractInterpreter() {}
116
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000117 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000118 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000119 /// the code generator. It returns false if the code generator fails.
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000120 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
121 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
Chris Lattnerffac2862006-06-06 22:30:59 +0000122
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000123 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattner634bc042006-09-15 21:29:15 +0000124 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000125 /// fails, it sets Error, otherwise, this function returns the type of code
126 /// emitted.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000127 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000128 sys::Path &OutFile, std::string &Error,
129 unsigned Timeout = 0,
130 unsigned MemoryLimit = 0) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000131 Error = "OutputCode not supported by this AbstractInterpreter!";
132 return GCC::AsmFile;
Chris Lattner634bc042006-09-15 21:29:15 +0000133 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000134
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000135 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000136 /// specified filename. This sets RetVal to the exit code of the program or
137 /// returns false if a problem was encountered that prevented execution of
138 /// the program.
Chris Lattnerffac2862006-06-06 22:30:59 +0000139 ///
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000140 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000141 const std::vector<std::string> &Args,
142 const std::string &InputFile,
143 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000144 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000145 const std::vector<std::string> &GCCArgs =
146 std::vector<std::string>(),
147 const std::vector<std::string> &SharedLibs =
148 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000149 unsigned Timeout = 0,
150 unsigned MemoryLimit = 0) = 0;
Chris Lattnerffac2862006-06-06 22:30:59 +0000151};
152
153//===---------------------------------------------------------------------===//
154// CBE Implementation of AbstractIntepreter interface
155//
156class CBE : public AbstractInterpreter {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000157 sys::Path LLCPath; // The path to the `llc' executable.
158 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Chris Lattnerffac2862006-06-06 22:30:59 +0000159 GCC *gcc;
160public:
161 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000162 const std::vector<std::string> *Args)
163 : LLCPath(llcPath), gcc(Gcc) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000164 ToolArgs.clear ();
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000165 if (Args) ToolArgs = *Args;
Chris Lattnerffac2862006-06-06 22:30:59 +0000166 }
167 ~CBE() { delete gcc; }
168
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000169 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000170 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000171 /// the code generator. Returns false if the code generator fails.
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000172 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
173 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000174
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000175 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000176 const std::vector<std::string> &Args,
177 const std::string &InputFile,
178 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000179 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000180 const std::vector<std::string> &GCCArgs =
181 std::vector<std::string>(),
182 const std::vector<std::string> &SharedLibs =
183 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000184 unsigned Timeout = 0,
185 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000186
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000187 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattner634bc042006-09-15 21:29:15 +0000188 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000189 /// fails, it sets Error, otherwise, this function returns the type of code
190 /// emitted.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000191 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000192 sys::Path &OutFile, std::string &Error,
193 unsigned Timeout = 0,
194 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000195};
196
197
198//===---------------------------------------------------------------------===//
199// LLC Implementation of AbstractIntepreter interface
200//
201class LLC : public AbstractInterpreter {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000202 std::string LLCPath; // The path to the LLC executable.
203 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Chris Lattnerffac2862006-06-06 22:30:59 +0000204 GCC *gcc;
Chris Lattnerfd381322010-03-16 06:41:47 +0000205 bool UseIntegratedAssembler;
Chris Lattnerffac2862006-06-06 22:30:59 +0000206public:
207 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000208 const std::vector<std::string> *Args,
Chris Lattnerfd381322010-03-16 06:41:47 +0000209 bool useIntegratedAssembler)
210 : LLCPath(llcPath), gcc(Gcc),
211 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000212 ToolArgs.clear();
213 if (Args) ToolArgs = *Args;
Chris Lattnerffac2862006-06-06 22:30:59 +0000214 }
215 ~LLC() { delete gcc; }
216
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000217 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000218 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000219 /// the code generator. Returns false if the code generator fails.
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000220 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
221 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000222
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000223 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000224 const std::vector<std::string> &Args,
225 const std::string &InputFile,
226 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000227 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000228 const std::vector<std::string> &GCCArgs =
229 std::vector<std::string>(),
230 const std::vector<std::string> &SharedLibs =
231 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000232 unsigned Timeout = 0,
233 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000234
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000235 /// OutputCode - Compile the specified program from bitcode to code
236 /// understood by the GCC driver (either C or asm). If the code generator
237 /// fails, it sets Error, otherwise, this function returns the type of code
238 /// emitted.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000239 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000240 sys::Path &OutFile, std::string &Error,
241 unsigned Timeout = 0,
242 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000243};
244
245} // End llvm namespace
246
247#endif