blob: cfa8acf6b24043ba74f573e66211e4d7d85b3365 [file] [log] [blame]
Chris Lattnerf1b20d82006-06-06 22:30:59 +00001//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
Misha Brukman63b3afa2005-04-21 20:48:15 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman63b3afa2005-04-21 20:48:15 +00007//
John Criswell6fbcc262003-10-20 20:19:47 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf4744492003-09-30 18:28:53 +00009//
Chris Lattner7915a1e2003-10-14 21:34:11 +000010// 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.
Chris Lattnerf4744492003-09-30 18:28:53 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerf1b20d82006-06-06 22:30:59 +000017#ifndef BUGPOINT_TOOLRUNNER_H
18#define BUGPOINT_TOOLRUNNER_H
Misha Brukman29afb642003-09-29 22:38:57 +000019
Daniel Dunbarca740962009-08-18 03:35:57 +000020#include "llvm/ADT/Triple.h"
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000021#include "llvm/Support/CommandLine.h"
Nick Lewycky22ff7482010-04-12 05:08:25 +000022#include "llvm/Support/ErrorHandling.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/SystemUtils.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000024#include "llvm/Support/Path.h"
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000025#include <exception>
Misha Brukman29afb642003-09-29 22:38:57 +000026#include <vector>
27
Brian Gaeked0fde302003-11-11 22:41:34 +000028namespace llvm {
29
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000030extern cl::opt<bool> SaveTemps;
Daniel Dunbarca740962009-08-18 03:35:57 +000031extern Triple TargetTriple;
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000032
Chris Lattner7915a1e2003-10-14 21:34:11 +000033class CBE;
34class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000035
36//===---------------------------------------------------------------------===//
37// GCC abstraction
38//
Misha Brukman29afb642003-09-29 22:38:57 +000039class GCC {
Bill Wendling38efa382009-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 Lattner7915a1e2003-10-14 21:34:11 +000048public:
Chris Lattner50010422010-03-16 06:41:47 +000049 enum FileType { AsmFile, ObjectFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000050
Dan Gohman197f7282009-08-05 20:21:17 +000051 static GCC *create(std::string &Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +000052 const std::string &GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +000053 const std::vector<std::string> *Args);
Chris Lattner7915a1e2003-10-14 21:34:11 +000054
Chris Lattnereeed9832003-10-14 21:52:52 +000055 /// 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 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000062 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 Sands34727662010-07-12 08:16:59 +000067 std::string *Error = 0,
Reid Spencer51ab5c82006-06-06 00:00:42 +000068 const std::vector<std::string> &GCCArgs =
Andrew Trickde86cbd2011-02-08 18:07:10 +000069 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000070 unsigned Timeout = 0,
71 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000072
Chris Lattnereeed9832003-10-14 21:52:52 +000073 /// 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 Lattner130e2a32006-06-27 20:35:36 +000077 std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +000078 const std::vector<std::string> &ArgsForGCC,
79 std::string &Error);
Misha Brukman29afb642003-09-29 22:38:57 +000080};
81
Misha Brukman29afb642003-09-29 22:38:57 +000082
Chris Lattner7915a1e2003-10-14 21:34:11 +000083//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000084/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif8ff70c22007-07-04 21:55:50 +000085/// LLVM bitcode in a variety of ways. This abstract interface hides this
Misha Brukman29afb642003-09-29 22:38:57 +000086/// complexity behind a simple interface.
87///
Jeff Cohen83881952005-01-22 16:30:58 +000088class AbstractInterpreter {
89public:
Dan Gohman197f7282009-08-05 20:21:17 +000090 static CBE *createCBE(const char *Argv0, std::string &Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +000091 const std::string &GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +000092 const std::vector<std::string> *Args = 0,
93 const std::vector<std::string> *GCCArgs = 0);
Dan Gohman197f7282009-08-05 20:21:17 +000094 static LLC *createLLC(const char *Argv0, std::string &Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +000095 const std::string &GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +000096 const std::vector<std::string> *Args = 0,
Chris Lattner50010422010-03-16 06:41:47 +000097 const std::vector<std::string> *GCCArgs = 0,
98 bool UseIntegratedAssembler = false);
Chris Lattner7915a1e2003-10-14 21:34:11 +000099
Dan Gohman197f7282009-08-05 20:21:17 +0000100 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +0000101 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000102
Dan Gohman197f7282009-08-05 20:21:17 +0000103 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +0000104 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000105
Andrew Trickf73311b2011-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 Korobeynikov9ef74252008-04-28 20:53:48 +0000113
Misha Brukman29afb642003-09-29 22:38:57 +0000114
115 virtual ~AbstractInterpreter() {}
116
Gabor Greif8ff70c22007-07-04 21:55:50 +0000117 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000118 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000119 /// the code generator. It returns false if the code generator fails.
Duncan Sands41396302010-05-24 07:49:55 +0000120 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
121 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
Chris Lattnerf03715c2004-02-18 23:24:29 +0000122
Gabor Greif8ff70c22007-07-04 21:55:50 +0000123 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000124 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000125 /// fails, it sets Error, otherwise, this function returns the type of code
126 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000127 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000128 sys::Path &OutFile, std::string &Error,
129 unsigned Timeout = 0,
130 unsigned MemoryLimit = 0) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000131 Error = "OutputCode not supported by this AbstractInterpreter!";
132 return GCC::AsmFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000133 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000134
Gabor Greif8ff70c22007-07-04 21:55:50 +0000135 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky22ff7482010-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.
Misha Brukman29afb642003-09-29 22:38:57 +0000139 ///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000140 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000141 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000142 const std::string &InputFile,
143 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000144 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000145 const std::vector<std::string> &GCCArgs =
146 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000147 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000148 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000149 unsigned Timeout = 0,
150 unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000151};
152
153//===---------------------------------------------------------------------===//
154// CBE Implementation of AbstractIntepreter interface
155//
156class CBE : public AbstractInterpreter {
Bill Wendling38efa382009-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.
Misha Brukman29afb642003-09-29 22:38:57 +0000159 GCC *gcc;
160public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000161 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000162 const std::vector<std::string> *Args)
163 : LLCPath(llcPath), gcc(Gcc) {
Brian Gaeked11577b2004-05-04 21:09:01 +0000164 ToolArgs.clear ();
Bill Wendling38efa382009-03-02 23:13:18 +0000165 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000166 }
Misha Brukman29afb642003-09-29 22:38:57 +0000167 ~CBE() { delete gcc; }
168
Gabor Greif8ff70c22007-07-04 21:55:50 +0000169 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000170 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000171 /// the code generator. Returns false if the code generator fails.
Duncan Sands41396302010-05-24 07:49:55 +0000172 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
173 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000174
Gabor Greif8ff70c22007-07-04 21:55:50 +0000175 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000176 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000177 const std::string &InputFile,
178 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000179 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000180 const std::vector<std::string> &GCCArgs =
181 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000182 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000183 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000184 unsigned Timeout = 0,
185 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000186
Gabor Greif8ff70c22007-07-04 21:55:50 +0000187 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000188 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000189 /// fails, it sets Error, otherwise, this function returns the type of code
190 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000191 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000192 sys::Path &OutFile, std::string &Error,
193 unsigned Timeout = 0,
194 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000195};
196
Misha Brukman29afb642003-09-29 22:38:57 +0000197
198//===---------------------------------------------------------------------===//
199// LLC Implementation of AbstractIntepreter interface
200//
201class LLC : public AbstractInterpreter {
Bill Wendling38efa382009-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.
Misha Brukman29afb642003-09-29 22:38:57 +0000204 GCC *gcc;
Chris Lattner50010422010-03-16 06:41:47 +0000205 bool UseIntegratedAssembler;
Misha Brukman29afb642003-09-29 22:38:57 +0000206public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000207 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000208 const std::vector<std::string> *Args,
Chris Lattner50010422010-03-16 06:41:47 +0000209 bool useIntegratedAssembler)
210 : LLCPath(llcPath), gcc(Gcc),
211 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendling38efa382009-03-02 23:13:18 +0000212 ToolArgs.clear();
213 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000214 }
Misha Brukman29afb642003-09-29 22:38:57 +0000215 ~LLC() { delete gcc; }
216
Gabor Greif8ff70c22007-07-04 21:55:50 +0000217 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000218 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000219 /// the code generator. Returns false if the code generator fails.
Duncan Sands41396302010-05-24 07:49:55 +0000220 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
221 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000222
Gabor Greif8ff70c22007-07-04 21:55:50 +0000223 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000224 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000225 const std::string &InputFile,
226 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000227 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000228 const std::vector<std::string> &GCCArgs =
229 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000230 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000231 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000232 unsigned Timeout = 0,
233 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000234
Nick Lewycky22ff7482010-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 Greif8ff70c22007-07-04 21:55:50 +0000239 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000240 sys::Path &OutFile, std::string &Error,
241 unsigned Timeout = 0,
242 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000243};
244
Brian Gaeked0fde302003-11-11 22:41:34 +0000245} // End llvm namespace
246
Misha Brukman29afb642003-09-29 22:38:57 +0000247#endif