blob: d4f9bc46f59e35328917b7bbf29f9b08ba346cf3 [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 =
69 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
Dan Gohman197f7282009-08-05 20:21:17 +0000106 static AbstractInterpreter* createCustom(std::string &Message,
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000107 const std::string &ExecCommandLine);
108
Misha Brukman29afb642003-09-29 22:38:57 +0000109
110 virtual ~AbstractInterpreter() {}
111
Gabor Greif8ff70c22007-07-04 21:55:50 +0000112 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000113 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000114 /// the code generator. It returns false if the code generator fails.
Duncan Sands41396302010-05-24 07:49:55 +0000115 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
116 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
Chris Lattnerf03715c2004-02-18 23:24:29 +0000117
Gabor Greif8ff70c22007-07-04 21:55:50 +0000118 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000119 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000120 /// fails, it sets Error, otherwise, this function returns the type of code
121 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000122 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000123 sys::Path &OutFile, std::string &Error,
124 unsigned Timeout = 0,
125 unsigned MemoryLimit = 0) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000126 Error = "OutputCode not supported by this AbstractInterpreter!";
127 return GCC::AsmFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000128 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000129
Gabor Greif8ff70c22007-07-04 21:55:50 +0000130 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky22ff7482010-04-12 05:08:25 +0000131 /// specified filename. This sets RetVal to the exit code of the program or
132 /// returns false if a problem was encountered that prevented execution of
133 /// the program.
Misha Brukman29afb642003-09-29 22:38:57 +0000134 ///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000135 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000136 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000137 const std::string &InputFile,
138 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000139 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000140 const std::vector<std::string> &GCCArgs =
141 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000142 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000143 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000144 unsigned Timeout = 0,
145 unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000146};
147
148//===---------------------------------------------------------------------===//
149// CBE Implementation of AbstractIntepreter interface
150//
151class CBE : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000152 sys::Path LLCPath; // The path to the `llc' executable.
153 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Misha Brukman29afb642003-09-29 22:38:57 +0000154 GCC *gcc;
155public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000156 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000157 const std::vector<std::string> *Args)
158 : LLCPath(llcPath), gcc(Gcc) {
Brian Gaeked11577b2004-05-04 21:09:01 +0000159 ToolArgs.clear ();
Bill Wendling38efa382009-03-02 23:13:18 +0000160 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000161 }
Misha Brukman29afb642003-09-29 22:38:57 +0000162 ~CBE() { delete gcc; }
163
Gabor Greif8ff70c22007-07-04 21:55:50 +0000164 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000165 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000166 /// the code generator. Returns false if the code generator fails.
Duncan Sands41396302010-05-24 07:49:55 +0000167 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
168 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000169
Gabor Greif8ff70c22007-07-04 21:55:50 +0000170 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000171 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000172 const std::string &InputFile,
173 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000174 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000175 const std::vector<std::string> &GCCArgs =
176 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000177 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000178 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000179 unsigned Timeout = 0,
180 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000181
Gabor Greif8ff70c22007-07-04 21:55:50 +0000182 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000183 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000184 /// fails, it sets Error, otherwise, this function returns the type of code
185 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000186 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000187 sys::Path &OutFile, std::string &Error,
188 unsigned Timeout = 0,
189 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000190};
191
Misha Brukman29afb642003-09-29 22:38:57 +0000192
193//===---------------------------------------------------------------------===//
194// LLC Implementation of AbstractIntepreter interface
195//
196class LLC : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000197 std::string LLCPath; // The path to the LLC executable.
198 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Misha Brukman29afb642003-09-29 22:38:57 +0000199 GCC *gcc;
Chris Lattner50010422010-03-16 06:41:47 +0000200 bool UseIntegratedAssembler;
Misha Brukman29afb642003-09-29 22:38:57 +0000201public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000202 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000203 const std::vector<std::string> *Args,
Chris Lattner50010422010-03-16 06:41:47 +0000204 bool useIntegratedAssembler)
205 : LLCPath(llcPath), gcc(Gcc),
206 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendling38efa382009-03-02 23:13:18 +0000207 ToolArgs.clear();
208 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000209 }
Misha Brukman29afb642003-09-29 22:38:57 +0000210 ~LLC() { delete gcc; }
211
Gabor Greif8ff70c22007-07-04 21:55:50 +0000212 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000213 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000214 /// the code generator. Returns false if the code generator fails.
Duncan Sands41396302010-05-24 07:49:55 +0000215 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
216 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000217
Gabor Greif8ff70c22007-07-04 21:55:50 +0000218 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000219 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000220 const std::string &InputFile,
221 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000222 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000223 const std::vector<std::string> &GCCArgs =
224 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000225 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000226 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000227 unsigned Timeout = 0,
228 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000229
Nick Lewycky22ff7482010-04-12 05:08:25 +0000230 /// OutputCode - Compile the specified program from bitcode to code
231 /// understood by the GCC driver (either C or asm). If the code generator
232 /// fails, it sets Error, otherwise, this function returns the type of code
233 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000234 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000235 sys::Path &OutFile, std::string &Error,
236 unsigned Timeout = 0,
237 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000238};
239
Brian Gaeked0fde302003-11-11 22:41:34 +0000240} // End llvm namespace
241
Misha Brukman29afb642003-09-29 22:38:57 +0000242#endif