blob: 7b93394fd8ca009d5cbc1c6de73503f4e702aa36 [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 {
David Blaikie2d24e2a2011-12-20 02:50:00 +000089 virtual void anchor();
Jeff Cohen83881952005-01-22 16:30:58 +000090public:
Dan Gohman197f7282009-08-05 20:21:17 +000091 static CBE *createCBE(const char *Argv0, std::string &Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +000092 const std::string &GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +000093 const std::vector<std::string> *Args = 0,
94 const std::vector<std::string> *GCCArgs = 0);
Dan Gohman197f7282009-08-05 20:21:17 +000095 static LLC *createLLC(const char *Argv0, std::string &Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +000096 const std::string &GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +000097 const std::vector<std::string> *Args = 0,
Chris Lattner50010422010-03-16 06:41:47 +000098 const std::vector<std::string> *GCCArgs = 0,
99 bool UseIntegratedAssembler = false);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000100
Dan Gohman197f7282009-08-05 20:21:17 +0000101 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +0000102 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000103
Dan Gohman197f7282009-08-05 20:21:17 +0000104 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +0000105 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000106
Andrew Trickf73311b2011-02-08 18:20:48 +0000107 static AbstractInterpreter*
108 createCustomCompiler(std::string &Message,
109 const std::string &CompileCommandLine);
110
111 static AbstractInterpreter*
112 createCustomExecutor(std::string &Message,
113 const std::string &ExecCommandLine);
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000114
Misha Brukman29afb642003-09-29 22:38:57 +0000115
116 virtual ~AbstractInterpreter() {}
117
Gabor Greif8ff70c22007-07-04 21:55:50 +0000118 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000119 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000120 /// the code generator. It returns false if the code generator fails.
Duncan Sands41396302010-05-24 07:49:55 +0000121 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
122 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
Chris Lattnerf03715c2004-02-18 23:24:29 +0000123
Gabor Greif8ff70c22007-07-04 21:55:50 +0000124 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000125 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000126 /// fails, it sets Error, otherwise, this function returns the type of code
127 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000128 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000129 sys::Path &OutFile, std::string &Error,
130 unsigned Timeout = 0,
131 unsigned MemoryLimit = 0) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000132 Error = "OutputCode not supported by this AbstractInterpreter!";
133 return GCC::AsmFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000134 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000135
Gabor Greif8ff70c22007-07-04 21:55:50 +0000136 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky22ff7482010-04-12 05:08:25 +0000137 /// specified filename. This sets RetVal to the exit code of the program or
138 /// returns false if a problem was encountered that prevented execution of
139 /// the program.
Misha Brukman29afb642003-09-29 22:38:57 +0000140 ///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000141 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000142 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000143 const std::string &InputFile,
144 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000145 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000146 const std::vector<std::string> &GCCArgs =
147 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000148 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000149 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000150 unsigned Timeout = 0,
151 unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000152};
153
154//===---------------------------------------------------------------------===//
155// CBE Implementation of AbstractIntepreter interface
156//
157class CBE : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000158 sys::Path LLCPath; // The path to the `llc' executable.
159 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Misha Brukman29afb642003-09-29 22:38:57 +0000160 GCC *gcc;
161public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000162 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000163 const std::vector<std::string> *Args)
164 : LLCPath(llcPath), gcc(Gcc) {
Brian Gaeked11577b2004-05-04 21:09:01 +0000165 ToolArgs.clear ();
Bill Wendling38efa382009-03-02 23:13:18 +0000166 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000167 }
Misha Brukman29afb642003-09-29 22:38:57 +0000168 ~CBE() { delete gcc; }
169
Gabor Greif8ff70c22007-07-04 21:55:50 +0000170 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000171 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000172 /// the code generator. Returns false if the code generator fails.
Duncan Sands41396302010-05-24 07:49:55 +0000173 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
174 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000175
Gabor Greif8ff70c22007-07-04 21:55:50 +0000176 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000177 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000178 const std::string &InputFile,
179 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000180 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000181 const std::vector<std::string> &GCCArgs =
182 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000183 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000184 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000185 unsigned Timeout = 0,
186 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000187
Gabor Greif8ff70c22007-07-04 21:55:50 +0000188 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000189 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000190 /// fails, it sets Error, otherwise, this function returns the type of code
191 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000192 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000193 sys::Path &OutFile, std::string &Error,
194 unsigned Timeout = 0,
195 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000196};
197
Misha Brukman29afb642003-09-29 22:38:57 +0000198
199//===---------------------------------------------------------------------===//
200// LLC Implementation of AbstractIntepreter interface
201//
202class LLC : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000203 std::string LLCPath; // The path to the LLC executable.
204 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Misha Brukman29afb642003-09-29 22:38:57 +0000205 GCC *gcc;
Chris Lattner50010422010-03-16 06:41:47 +0000206 bool UseIntegratedAssembler;
Misha Brukman29afb642003-09-29 22:38:57 +0000207public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000208 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000209 const std::vector<std::string> *Args,
Chris Lattner50010422010-03-16 06:41:47 +0000210 bool useIntegratedAssembler)
211 : LLCPath(llcPath), gcc(Gcc),
212 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendling38efa382009-03-02 23:13:18 +0000213 ToolArgs.clear();
214 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000215 }
Misha Brukman29afb642003-09-29 22:38:57 +0000216 ~LLC() { delete gcc; }
217
Gabor Greif8ff70c22007-07-04 21:55:50 +0000218 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000219 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000220 /// the code generator. Returns false if the code generator fails.
Duncan Sands41396302010-05-24 07:49:55 +0000221 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
222 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000223
Gabor Greif8ff70c22007-07-04 21:55:50 +0000224 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000225 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000226 const std::string &InputFile,
227 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000228 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000229 const std::vector<std::string> &GCCArgs =
230 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000231 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000232 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000233 unsigned Timeout = 0,
234 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000235
Nick Lewycky22ff7482010-04-12 05:08:25 +0000236 /// OutputCode - Compile the specified program from bitcode to code
237 /// understood by the GCC driver (either C or asm). If the code generator
238 /// fails, it sets Error, otherwise, this function returns the type of code
239 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000240 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000241 sys::Path &OutFile, std::string &Error,
242 unsigned Timeout = 0,
243 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000244};
245
Brian Gaeked0fde302003-11-11 22:41:34 +0000246} // End llvm namespace
247
Misha Brukman29afb642003-09-29 22:38:57 +0000248#endif