blob: 0f75e99db4be06e39dfc0492077dca985582ec05 [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/SystemUtils.h"
Chris Lattnerb683ea42009-08-23 21:36:09 +000023#include "llvm/System/Path.h"
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000024#include <exception>
Misha Brukman29afb642003-09-29 22:38:57 +000025#include <vector>
26
Brian Gaeked0fde302003-11-11 22:41:34 +000027namespace llvm {
28
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000029extern cl::opt<bool> SaveTemps;
Daniel Dunbarca740962009-08-18 03:35:57 +000030extern Triple TargetTriple;
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000031
Chris Lattner7915a1e2003-10-14 21:34:11 +000032class CBE;
33class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000034
Chris Lattner8c56be52004-02-18 20:21:57 +000035/// ToolExecutionError - An instance of this class is thrown by the
36/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
37/// crashes) which prevents execution of the program.
38///
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000039class ToolExecutionError : std::exception {
Chris Lattner8c56be52004-02-18 20:21:57 +000040 std::string Message;
41public:
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000042 explicit ToolExecutionError(const std::string &M) : Message(M) {}
43 virtual ~ToolExecutionError() throw();
44 virtual const char* what() const throw() { return Message.c_str(); }
Chris Lattner8c56be52004-02-18 20:21:57 +000045};
46
47
Misha Brukman29afb642003-09-29 22:38:57 +000048//===---------------------------------------------------------------------===//
49// GCC abstraction
50//
Misha Brukman29afb642003-09-29 22:38:57 +000051class GCC {
Bill Wendling38efa382009-03-02 23:13:18 +000052 sys::Path GCCPath; // The path to the gcc executable.
53 sys::Path RemoteClientPath; // The path to the rsh / ssh executable.
54 std::vector<std::string> gccArgs; // GCC-specific arguments.
55 GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
56 const std::vector<std::string> *GCCArgs)
57 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
58 if (GCCArgs) gccArgs = *GCCArgs;
59 }
Chris Lattner7915a1e2003-10-14 21:34:11 +000060public:
Chris Lattner50010422010-03-16 06:41:47 +000061 enum FileType { AsmFile, ObjectFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000062
Dan Gohman197f7282009-08-05 20:21:17 +000063 static GCC *create(std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +000064 const std::vector<std::string> *Args);
Chris Lattner7915a1e2003-10-14 21:34:11 +000065
Chris Lattnereeed9832003-10-14 21:52:52 +000066 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
67 /// either a .s file, or a .c file, specified by FileType), with the specified
68 /// arguments. Standard input is specified with InputFile, and standard
69 /// Output is captured to the specified OutputFile location. The SharedLibs
70 /// option specifies optional native shared objects that can be loaded into
71 /// the program for execution.
72 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000073 int ExecuteProgram(const std::string &ProgramFile,
74 const std::vector<std::string> &Args,
75 FileType fileType,
76 const std::string &InputFile,
77 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +000078 const std::vector<std::string> &GCCArgs =
79 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000080 unsigned Timeout = 0,
81 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000082
Chris Lattnereeed9832003-10-14 21:52:52 +000083 /// MakeSharedObject - This compiles the specified file (which is either a .c
84 /// file or a .s file) into a shared object.
85 ///
86 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +000087 std::string &OutputFile,
88 const std::vector<std::string> &ArgsForGCC);
Misha Brukman29afb642003-09-29 22:38:57 +000089};
90
Misha Brukman29afb642003-09-29 22:38:57 +000091
Chris Lattner7915a1e2003-10-14 21:34:11 +000092//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000093/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif8ff70c22007-07-04 21:55:50 +000094/// LLVM bitcode in a variety of ways. This abstract interface hides this
Misha Brukman29afb642003-09-29 22:38:57 +000095/// complexity behind a simple interface.
96///
Jeff Cohen83881952005-01-22 16:30:58 +000097class AbstractInterpreter {
98public:
Dan Gohman197f7282009-08-05 20:21:17 +000099 static CBE *createCBE(const char *Argv0, std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000100 const std::vector<std::string> *Args = 0,
101 const std::vector<std::string> *GCCArgs = 0);
Dan Gohman197f7282009-08-05 20:21:17 +0000102 static LLC *createLLC(const char *Argv0, std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000103 const std::vector<std::string> *Args = 0,
Chris Lattner50010422010-03-16 06:41:47 +0000104 const std::vector<std::string> *GCCArgs = 0,
105 bool UseIntegratedAssembler = false);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000106
Dan Gohman197f7282009-08-05 20:21:17 +0000107 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +0000108 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000109
Dan Gohman197f7282009-08-05 20:21:17 +0000110 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +0000111 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000112
Dan Gohman197f7282009-08-05 20:21:17 +0000113 static AbstractInterpreter* createCustom(std::string &Message,
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000114 const std::string &ExecCommandLine);
115
Misha Brukman29afb642003-09-29 22:38:57 +0000116
117 virtual ~AbstractInterpreter() {}
118
Gabor Greif8ff70c22007-07-04 21:55:50 +0000119 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000120 /// code. This does not produce any output, it is only used when debugging
121 /// the code generator. If the code generator fails, an exception should be
122 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000123 virtual void compileProgram(const std::string &Bitcode) {}
Chris Lattnerf03715c2004-02-18 23:24:29 +0000124
Gabor Greif8ff70c22007-07-04 21:55:50 +0000125 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000126 /// understood by the GCC driver (either C or asm). If the code generator
127 /// fails, an exception should be thrown, otherwise, this function returns the
128 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000129 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000130 sys::Path &OutFile) {
131 throw std::string("OutputCode not supported by this AbstractInterpreter!");
132 }
133
Gabor Greif8ff70c22007-07-04 21:55:50 +0000134 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Misha Brukman29afb642003-09-29 22:38:57 +0000135 /// specified filename. This returns the exit code of the program.
136 ///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000137 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000138 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000139 const std::string &InputFile,
140 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000141 const std::vector<std::string> &GCCArgs =
142 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000143 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000144 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000145 unsigned Timeout = 0,
146 unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000147};
148
149//===---------------------------------------------------------------------===//
150// CBE Implementation of AbstractIntepreter interface
151//
152class CBE : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000153 sys::Path LLCPath; // The path to the `llc' executable.
154 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Misha Brukman29afb642003-09-29 22:38:57 +0000155 GCC *gcc;
156public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000157 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000158 const std::vector<std::string> *Args)
159 : LLCPath(llcPath), gcc(Gcc) {
Brian Gaeked11577b2004-05-04 21:09:01 +0000160 ToolArgs.clear ();
Bill Wendling38efa382009-03-02 23:13:18 +0000161 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000162 }
Misha Brukman29afb642003-09-29 22:38:57 +0000163 ~CBE() { delete gcc; }
164
Gabor Greif8ff70c22007-07-04 21:55:50 +0000165 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000166 /// code. This does not produce any output, it is only used when debugging
167 /// the code generator. If the code generator fails, an exception should be
168 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000169 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000170
Gabor Greif8ff70c22007-07-04 21:55:50 +0000171 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000172 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000173 const std::string &InputFile,
174 const std::string &OutputFile,
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
184 /// fails, an exception should be thrown, otherwise, this function returns the
185 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000186 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000187 sys::Path &OutFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000188};
189
Misha Brukman29afb642003-09-29 22:38:57 +0000190
191//===---------------------------------------------------------------------===//
192// LLC Implementation of AbstractIntepreter interface
193//
194class LLC : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000195 std::string LLCPath; // The path to the LLC executable.
196 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
197 std::vector<std::string> gccArgs; // Extra args to pass to GCC.
Misha Brukman29afb642003-09-29 22:38:57 +0000198 GCC *gcc;
Chris Lattner50010422010-03-16 06:41:47 +0000199 bool UseIntegratedAssembler;
Misha Brukman29afb642003-09-29 22:38:57 +0000200public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000201 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000202 const std::vector<std::string> *Args,
Chris Lattner50010422010-03-16 06:41:47 +0000203 const std::vector<std::string> *GCCArgs,
204 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;
209 if (GCCArgs) gccArgs = *GCCArgs;
Brian Gaeked11577b2004-05-04 21:09:01 +0000210 }
Misha Brukman29afb642003-09-29 22:38:57 +0000211 ~LLC() { delete gcc; }
212
Gabor Greif8ff70c22007-07-04 21:55:50 +0000213 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000214 /// code. This does not produce any output, it is only used when debugging
215 /// the code generator. If the code generator fails, an exception should be
216 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000217 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000218
Gabor Greif8ff70c22007-07-04 21:55:50 +0000219 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000220 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000221 const std::string &InputFile,
222 const std::string &OutputFile,
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
Gabor Greif8ff70c22007-07-04 21:55:50 +0000230 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000231 sys::Path &OutFile);
232
Misha Brukman29afb642003-09-29 22:38:57 +0000233};
234
Brian Gaeked0fde302003-11-11 22:41:34 +0000235} // End llvm namespace
236
Misha Brukman29afb642003-09-29 22:38:57 +0000237#endif