blob: 6693dead47af670a75fe9ca23238a0c550a4a029 [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"
Chris Lattnerb683ea42009-08-23 21:36:09 +000024#include "llvm/System/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,
Nick Lewycky22ff7482010-04-12 05:08:25 +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.
115 virtual void compileProgram(const std::string &Bitcode, std::string *Error) {}
Chris Lattnerf03715c2004-02-18 23:24:29 +0000116
Gabor Greif8ff70c22007-07-04 21:55:50 +0000117 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000118 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000119 /// fails, it sets Error, otherwise, this function returns the type of code
120 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000121 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000122 sys::Path &OutFile, std::string &Error) {
123 Error = "OutputCode not supported by this AbstractInterpreter!";
124 return GCC::AsmFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000125 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000126
Gabor Greif8ff70c22007-07-04 21:55:50 +0000127 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky22ff7482010-04-12 05:08:25 +0000128 /// specified filename. This sets RetVal to the exit code of the program or
129 /// returns false if a problem was encountered that prevented execution of
130 /// the program.
Misha Brukman29afb642003-09-29 22:38:57 +0000131 ///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000132 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000133 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000134 const std::string &InputFile,
135 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000136 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000137 const std::vector<std::string> &GCCArgs =
138 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000139 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000140 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000141 unsigned Timeout = 0,
142 unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000143};
144
145//===---------------------------------------------------------------------===//
146// CBE Implementation of AbstractIntepreter interface
147//
148class CBE : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000149 sys::Path LLCPath; // The path to the `llc' executable.
150 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Misha Brukman29afb642003-09-29 22:38:57 +0000151 GCC *gcc;
152public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000153 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000154 const std::vector<std::string> *Args)
155 : LLCPath(llcPath), gcc(Gcc) {
Brian Gaeked11577b2004-05-04 21:09:01 +0000156 ToolArgs.clear ();
Bill Wendling38efa382009-03-02 23:13:18 +0000157 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000158 }
Misha Brukman29afb642003-09-29 22:38:57 +0000159 ~CBE() { delete gcc; }
160
Gabor Greif8ff70c22007-07-04 21:55:50 +0000161 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000162 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000163 /// the code generator. Returns false if the code generator fails.
164 virtual void compileProgram(const std::string &Bitcode, std::string *Error);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000165
Gabor Greif8ff70c22007-07-04 21:55:50 +0000166 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000167 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000168 const std::string &InputFile,
169 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000170 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000171 const std::vector<std::string> &GCCArgs =
172 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000173 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000174 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000175 unsigned Timeout = 0,
176 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000177
Gabor Greif8ff70c22007-07-04 21:55:50 +0000178 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000179 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000180 /// fails, it sets Error, otherwise, this function returns the type of code
181 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000182 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000183 sys::Path &OutFile, std::string &Error);
Misha Brukman29afb642003-09-29 22:38:57 +0000184};
185
Misha Brukman29afb642003-09-29 22:38:57 +0000186
187//===---------------------------------------------------------------------===//
188// LLC Implementation of AbstractIntepreter interface
189//
190class LLC : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000191 std::string LLCPath; // The path to the LLC executable.
192 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Misha Brukman29afb642003-09-29 22:38:57 +0000193 GCC *gcc;
Chris Lattner50010422010-03-16 06:41:47 +0000194 bool UseIntegratedAssembler;
Misha Brukman29afb642003-09-29 22:38:57 +0000195public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000196 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000197 const std::vector<std::string> *Args,
Chris Lattner50010422010-03-16 06:41:47 +0000198 bool useIntegratedAssembler)
199 : LLCPath(llcPath), gcc(Gcc),
200 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendling38efa382009-03-02 23:13:18 +0000201 ToolArgs.clear();
202 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000203 }
Misha Brukman29afb642003-09-29 22:38:57 +0000204 ~LLC() { delete gcc; }
205
Gabor Greif8ff70c22007-07-04 21:55:50 +0000206 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000207 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000208 /// the code generator. Returns false if the code generator fails.
209 virtual void compileProgram(const std::string &Bitcode, std::string *Error);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000210
Gabor Greif8ff70c22007-07-04 21:55:50 +0000211 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000212 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000213 const std::string &InputFile,
214 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000215 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000216 const std::vector<std::string> &GCCArgs =
217 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000218 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000219 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000220 unsigned Timeout = 0,
221 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000222
Nick Lewycky22ff7482010-04-12 05:08:25 +0000223 /// OutputCode - Compile the specified program from bitcode to code
224 /// understood by the GCC driver (either C or asm). If the code generator
225 /// fails, it sets Error, otherwise, this function returns the type of code
226 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000227 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000228 sys::Path &OutFile, std::string &Error);
Misha Brukman29afb642003-09-29 22:38:57 +0000229};
230
Brian Gaeked0fde302003-11-11 22:41:34 +0000231} // End llvm namespace
232
Misha Brukman29afb642003-09-29 22:38:57 +0000233#endif