blob: 721f66c126cced43f2d887ac000c543657db1f13 [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
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/SystemUtils.h"
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000021#include <exception>
Misha Brukman29afb642003-09-29 22:38:57 +000022#include <vector>
23
Brian Gaeked0fde302003-11-11 22:41:34 +000024namespace llvm {
25
Chris Lattner7915a1e2003-10-14 21:34:11 +000026class CBE;
27class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000028
Chris Lattner8c56be52004-02-18 20:21:57 +000029/// ToolExecutionError - An instance of this class is thrown by the
30/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
31/// crashes) which prevents execution of the program.
32///
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000033class ToolExecutionError : std::exception {
Chris Lattner8c56be52004-02-18 20:21:57 +000034 std::string Message;
35public:
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000036 explicit ToolExecutionError(const std::string &M) : Message(M) {}
37 virtual ~ToolExecutionError() throw();
38 virtual const char* what() const throw() { return Message.c_str(); }
Chris Lattner8c56be52004-02-18 20:21:57 +000039};
40
41
Misha Brukman29afb642003-09-29 22:38:57 +000042//===---------------------------------------------------------------------===//
43// GCC abstraction
44//
Misha Brukman29afb642003-09-29 22:38:57 +000045class GCC {
Bill Wendling38efa382009-03-02 23:13:18 +000046 sys::Path GCCPath; // The path to the gcc executable.
47 sys::Path RemoteClientPath; // The path to the rsh / ssh executable.
48 std::vector<std::string> gccArgs; // GCC-specific arguments.
49 GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
50 const std::vector<std::string> *GCCArgs)
51 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
52 if (GCCArgs) gccArgs = *GCCArgs;
53 }
Chris Lattner7915a1e2003-10-14 21:34:11 +000054public:
55 enum FileType { AsmFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000056
Bill Wendling38efa382009-03-02 23:13:18 +000057 static GCC *create(const std::string &ProgramPath, std::string &Message,
58 const std::vector<std::string> *Args);
Chris Lattner7915a1e2003-10-14 21:34:11 +000059
Chris Lattnereeed9832003-10-14 21:52:52 +000060 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
61 /// either a .s file, or a .c file, specified by FileType), with the specified
62 /// arguments. Standard input is specified with InputFile, and standard
63 /// Output is captured to the specified OutputFile location. The SharedLibs
64 /// option specifies optional native shared objects that can be loaded into
65 /// the program for execution.
66 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000067 int ExecuteProgram(const std::string &ProgramFile,
68 const std::vector<std::string> &Args,
69 FileType fileType,
70 const std::string &InputFile,
71 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +000072 const std::vector<std::string> &GCCArgs =
73 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000074 unsigned Timeout = 0,
75 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000076
Chris Lattnereeed9832003-10-14 21:52:52 +000077 /// MakeSharedObject - This compiles the specified file (which is either a .c
78 /// file or a .s file) into a shared object.
79 ///
80 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +000081 std::string &OutputFile,
82 const std::vector<std::string> &ArgsForGCC);
Misha Brukman29afb642003-09-29 22:38:57 +000083};
84
Misha Brukman29afb642003-09-29 22:38:57 +000085
Chris Lattner7915a1e2003-10-14 21:34:11 +000086//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000087/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif8ff70c22007-07-04 21:55:50 +000088/// LLVM bitcode in a variety of ways. This abstract interface hides this
Misha Brukman29afb642003-09-29 22:38:57 +000089/// complexity behind a simple interface.
90///
Jeff Cohen83881952005-01-22 16:30:58 +000091class AbstractInterpreter {
92public:
Brian Gaeked11577b2004-05-04 21:09:01 +000093 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +000094 const std::vector<std::string> *Args = 0,
95 const std::vector<std::string> *GCCArgs = 0);
Brian Gaeked11577b2004-05-04 21:09:01 +000096 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +000097 const std::vector<std::string> *Args = 0,
98 const std::vector<std::string> *GCCArgs = 0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000099
100 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000101 std::string &Message,
102 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000103
104 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000105 std::string &Message,
106 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000107
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000108 static AbstractInterpreter* createCustom(const std::string &ProgramPath,
109 std::string &Message,
110 const std::string &ExecCommandLine);
111
Misha Brukman29afb642003-09-29 22:38:57 +0000112
113 virtual ~AbstractInterpreter() {}
114
Gabor Greif8ff70c22007-07-04 21:55:50 +0000115 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000116 /// code. This does not produce any output, it is only used when debugging
117 /// the code generator. If the code generator fails, an exception should be
118 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000119 virtual void compileProgram(const std::string &Bitcode) {}
Chris Lattnerf03715c2004-02-18 23:24:29 +0000120
Gabor Greif8ff70c22007-07-04 21:55:50 +0000121 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000122 /// understood by the GCC driver (either C or asm). If the code generator
123 /// fails, an exception should be thrown, otherwise, this function returns the
124 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000125 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000126 sys::Path &OutFile) {
127 throw std::string("OutputCode not supported by this AbstractInterpreter!");
128 }
129
Gabor Greif8ff70c22007-07-04 21:55:50 +0000130 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Misha Brukman29afb642003-09-29 22:38:57 +0000131 /// specified filename. This returns the exit code of the program.
132 ///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000133 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000134 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000135 const std::string &InputFile,
136 const std::string &OutputFile,
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
163 /// the code generator. If the code generator fails, an exception should be
164 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000165 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000166
Gabor Greif8ff70c22007-07-04 21:55:50 +0000167 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000168 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000169 const std::string &InputFile,
170 const std::string &OutputFile,
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
180 /// fails, an exception should be thrown, otherwise, this function returns the
181 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000182 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000183 sys::Path &OutFile);
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.
193 std::vector<std::string> gccArgs; // Extra args to pass to GCC.
Misha Brukman29afb642003-09-29 22:38:57 +0000194 GCC *gcc;
195public:
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,
198 const std::vector<std::string> *GCCArgs)
199 : LLCPath(llcPath), gcc(Gcc) {
200 ToolArgs.clear();
201 if (Args) ToolArgs = *Args;
202 if (GCCArgs) gccArgs = *GCCArgs;
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
208 /// the code generator. If the code generator fails, an exception should be
209 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000210 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000211
Gabor Greif8ff70c22007-07-04 21:55:50 +0000212 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000213 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000214 const std::string &InputFile,
215 const std::string &OutputFile,
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
Gabor Greif8ff70c22007-07-04 21:55:50 +0000223 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000224 sys::Path &OutFile);
225
Misha Brukman29afb642003-09-29 22:38:57 +0000226};
227
Brian Gaeked0fde302003-11-11 22:41:34 +0000228} // End llvm namespace
229
Misha Brukman29afb642003-09-29 22:38:57 +0000230#endif