blob: 3e3d7d5e5acf136b5184ad1d414d4d743b951208 [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"
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000023#include <exception>
Misha Brukman29afb642003-09-29 22:38:57 +000024#include <vector>
25
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
27
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000028extern cl::opt<bool> SaveTemps;
Daniel Dunbarca740962009-08-18 03:35:57 +000029extern Triple TargetTriple;
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000030
Chris Lattner7915a1e2003-10-14 21:34:11 +000031class CBE;
32class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000033
Chris Lattner8c56be52004-02-18 20:21:57 +000034/// ToolExecutionError - An instance of this class is thrown by the
35/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
36/// crashes) which prevents execution of the program.
37///
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000038class ToolExecutionError : std::exception {
Chris Lattner8c56be52004-02-18 20:21:57 +000039 std::string Message;
40public:
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000041 explicit ToolExecutionError(const std::string &M) : Message(M) {}
42 virtual ~ToolExecutionError() throw();
43 virtual const char* what() const throw() { return Message.c_str(); }
Chris Lattner8c56be52004-02-18 20:21:57 +000044};
45
46
Misha Brukman29afb642003-09-29 22:38:57 +000047//===---------------------------------------------------------------------===//
48// GCC abstraction
49//
Misha Brukman29afb642003-09-29 22:38:57 +000050class GCC {
Bill Wendling38efa382009-03-02 23:13:18 +000051 sys::Path GCCPath; // The path to the gcc executable.
52 sys::Path RemoteClientPath; // The path to the rsh / ssh executable.
53 std::vector<std::string> gccArgs; // GCC-specific arguments.
54 GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
55 const std::vector<std::string> *GCCArgs)
56 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
57 if (GCCArgs) gccArgs = *GCCArgs;
58 }
Chris Lattner7915a1e2003-10-14 21:34:11 +000059public:
60 enum FileType { AsmFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000061
Dan Gohman197f7282009-08-05 20:21:17 +000062 static GCC *create(std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +000063 const std::vector<std::string> *Args);
Chris Lattner7915a1e2003-10-14 21:34:11 +000064
Chris Lattnereeed9832003-10-14 21:52:52 +000065 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
66 /// either a .s file, or a .c file, specified by FileType), with the specified
67 /// arguments. Standard input is specified with InputFile, and standard
68 /// Output is captured to the specified OutputFile location. The SharedLibs
69 /// option specifies optional native shared objects that can be loaded into
70 /// the program for execution.
71 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000072 int ExecuteProgram(const std::string &ProgramFile,
73 const std::vector<std::string> &Args,
74 FileType fileType,
75 const std::string &InputFile,
76 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +000077 const std::vector<std::string> &GCCArgs =
78 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000079 unsigned Timeout = 0,
80 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000081
Chris Lattnereeed9832003-10-14 21:52:52 +000082 /// MakeSharedObject - This compiles the specified file (which is either a .c
83 /// file or a .s file) into a shared object.
84 ///
85 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +000086 std::string &OutputFile,
87 const std::vector<std::string> &ArgsForGCC);
Misha Brukman29afb642003-09-29 22:38:57 +000088};
89
Misha Brukman29afb642003-09-29 22:38:57 +000090
Chris Lattner7915a1e2003-10-14 21:34:11 +000091//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000092/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif8ff70c22007-07-04 21:55:50 +000093/// LLVM bitcode in a variety of ways. This abstract interface hides this
Misha Brukman29afb642003-09-29 22:38:57 +000094/// complexity behind a simple interface.
95///
Jeff Cohen83881952005-01-22 16:30:58 +000096class AbstractInterpreter {
97public:
Dan Gohman197f7282009-08-05 20:21:17 +000098 static CBE *createCBE(const char *Argv0, std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +000099 const std::vector<std::string> *Args = 0,
100 const std::vector<std::string> *GCCArgs = 0);
Dan Gohman197f7282009-08-05 20:21:17 +0000101 static LLC *createLLC(const char *Argv0, std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000102 const std::vector<std::string> *Args = 0,
103 const std::vector<std::string> *GCCArgs = 0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000104
Dan Gohman197f7282009-08-05 20:21:17 +0000105 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +0000106 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000107
Dan Gohman197f7282009-08-05 20:21:17 +0000108 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +0000109 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000110
Dan Gohman197f7282009-08-05 20:21:17 +0000111 static AbstractInterpreter* createCustom(std::string &Message,
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000112 const std::string &ExecCommandLine);
113
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
119 /// the code generator. If the code generator fails, an exception should be
120 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000121 virtual void compileProgram(const std::string &Bitcode) {}
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
125 /// fails, an exception should be thrown, otherwise, this function returns the
126 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000127 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000128 sys::Path &OutFile) {
129 throw std::string("OutputCode not supported by this AbstractInterpreter!");
130 }
131
Gabor Greif8ff70c22007-07-04 21:55:50 +0000132 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Misha Brukman29afb642003-09-29 22:38:57 +0000133 /// specified filename. This returns the exit code of the program.
134 ///
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,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000139 const std::vector<std::string> &GCCArgs =
140 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000141 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000142 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000143 unsigned Timeout = 0,
144 unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000145};
146
147//===---------------------------------------------------------------------===//
148// CBE Implementation of AbstractIntepreter interface
149//
150class CBE : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000151 sys::Path LLCPath; // The path to the `llc' executable.
152 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Misha Brukman29afb642003-09-29 22:38:57 +0000153 GCC *gcc;
154public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000155 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000156 const std::vector<std::string> *Args)
157 : LLCPath(llcPath), gcc(Gcc) {
Brian Gaeked11577b2004-05-04 21:09:01 +0000158 ToolArgs.clear ();
Bill Wendling38efa382009-03-02 23:13:18 +0000159 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000160 }
Misha Brukman29afb642003-09-29 22:38:57 +0000161 ~CBE() { delete gcc; }
162
Gabor Greif8ff70c22007-07-04 21:55:50 +0000163 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000164 /// code. This does not produce any output, it is only used when debugging
165 /// the code generator. If the code generator fails, an exception should be
166 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000167 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000168
Gabor Greif8ff70c22007-07-04 21:55:50 +0000169 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000170 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000171 const std::string &InputFile,
172 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000173 const std::vector<std::string> &GCCArgs =
174 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000175 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000176 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000177 unsigned Timeout = 0,
178 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000179
Gabor Greif8ff70c22007-07-04 21:55:50 +0000180 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000181 /// understood by the GCC driver (either C or asm). If the code generator
182 /// fails, an exception should be thrown, otherwise, this function returns the
183 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000184 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000185 sys::Path &OutFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000186};
187
Misha Brukman29afb642003-09-29 22:38:57 +0000188
189//===---------------------------------------------------------------------===//
190// LLC Implementation of AbstractIntepreter interface
191//
192class LLC : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000193 std::string LLCPath; // The path to the LLC executable.
194 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
195 std::vector<std::string> gccArgs; // Extra args to pass to GCC.
Misha Brukman29afb642003-09-29 22:38:57 +0000196 GCC *gcc;
197public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000198 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000199 const std::vector<std::string> *Args,
200 const std::vector<std::string> *GCCArgs)
201 : LLCPath(llcPath), gcc(Gcc) {
202 ToolArgs.clear();
203 if (Args) ToolArgs = *Args;
204 if (GCCArgs) gccArgs = *GCCArgs;
Brian Gaeked11577b2004-05-04 21:09:01 +0000205 }
Misha Brukman29afb642003-09-29 22:38:57 +0000206 ~LLC() { delete gcc; }
207
Gabor Greif8ff70c22007-07-04 21:55:50 +0000208 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000209 /// code. This does not produce any output, it is only used when debugging
210 /// the code generator. If the code generator fails, an exception should be
211 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000212 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000213
Gabor Greif8ff70c22007-07-04 21:55:50 +0000214 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000215 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000216 const std::string &InputFile,
217 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000218 const std::vector<std::string> &GCCArgs =
219 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000220 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000221 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000222 unsigned Timeout = 0,
223 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000224
Gabor Greif8ff70c22007-07-04 21:55:50 +0000225 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000226 sys::Path &OutFile);
227
Misha Brukman29afb642003-09-29 22:38:57 +0000228};
229
Brian Gaeked0fde302003-11-11 22:41:34 +0000230} // End llvm namespace
231
Misha Brukman29afb642003-09-29 22:38:57 +0000232#endif