blob: 58daef00a8c25fd728fcd216bfea0f0b7e24372b [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
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000020#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/SystemUtils.h"
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000022#include <exception>
Misha Brukman29afb642003-09-29 22:38:57 +000023#include <vector>
24
Brian Gaeked0fde302003-11-11 22:41:34 +000025namespace llvm {
26
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000027extern cl::opt<bool> SaveTemps;
28
Chris Lattner7915a1e2003-10-14 21:34:11 +000029class CBE;
30class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000031
Chris Lattner8c56be52004-02-18 20:21:57 +000032/// ToolExecutionError - An instance of this class is thrown by the
33/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
34/// crashes) which prevents execution of the program.
35///
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000036class ToolExecutionError : std::exception {
Chris Lattner8c56be52004-02-18 20:21:57 +000037 std::string Message;
38public:
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000039 explicit ToolExecutionError(const std::string &M) : Message(M) {}
40 virtual ~ToolExecutionError() throw();
41 virtual const char* what() const throw() { return Message.c_str(); }
Chris Lattner8c56be52004-02-18 20:21:57 +000042};
43
44
Misha Brukman29afb642003-09-29 22:38:57 +000045//===---------------------------------------------------------------------===//
46// GCC abstraction
47//
Misha Brukman29afb642003-09-29 22:38:57 +000048class GCC {
Bill Wendling38efa382009-03-02 23:13:18 +000049 sys::Path GCCPath; // The path to the gcc executable.
50 sys::Path RemoteClientPath; // The path to the rsh / ssh executable.
51 std::vector<std::string> gccArgs; // GCC-specific arguments.
52 GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
53 const std::vector<std::string> *GCCArgs)
54 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
55 if (GCCArgs) gccArgs = *GCCArgs;
56 }
Chris Lattner7915a1e2003-10-14 21:34:11 +000057public:
58 enum FileType { AsmFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000059
Bill Wendling38efa382009-03-02 23:13:18 +000060 static GCC *create(const std::string &ProgramPath, std::string &Message,
61 const std::vector<std::string> *Args);
Chris Lattner7915a1e2003-10-14 21:34:11 +000062
Chris Lattnereeed9832003-10-14 21:52:52 +000063 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
64 /// either a .s file, or a .c file, specified by FileType), with the specified
65 /// arguments. Standard input is specified with InputFile, and standard
66 /// Output is captured to the specified OutputFile location. The SharedLibs
67 /// option specifies optional native shared objects that can be loaded into
68 /// the program for execution.
69 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000070 int ExecuteProgram(const std::string &ProgramFile,
71 const std::vector<std::string> &Args,
72 FileType fileType,
73 const std::string &InputFile,
74 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +000075 const std::vector<std::string> &GCCArgs =
76 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000077 unsigned Timeout = 0,
78 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000079
Chris Lattnereeed9832003-10-14 21:52:52 +000080 /// MakeSharedObject - This compiles the specified file (which is either a .c
81 /// file or a .s file) into a shared object.
82 ///
83 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +000084 std::string &OutputFile,
85 const std::vector<std::string> &ArgsForGCC);
Misha Brukman29afb642003-09-29 22:38:57 +000086};
87
Misha Brukman29afb642003-09-29 22:38:57 +000088
Chris Lattner7915a1e2003-10-14 21:34:11 +000089//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000090/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif8ff70c22007-07-04 21:55:50 +000091/// LLVM bitcode in a variety of ways. This abstract interface hides this
Misha Brukman29afb642003-09-29 22:38:57 +000092/// complexity behind a simple interface.
93///
Jeff Cohen83881952005-01-22 16:30:58 +000094class AbstractInterpreter {
95public:
Brian Gaeked11577b2004-05-04 21:09:01 +000096 static CBE *createCBE(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);
Brian Gaeked11577b2004-05-04 21:09:01 +000099 static LLC *createLLC(const std::string &ProgramPath, 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);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000102
103 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000104 std::string &Message,
105 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000106
107 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000108 std::string &Message,
109 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000110
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000111 static AbstractInterpreter* createCustom(const std::string &ProgramPath,
112 std::string &Message,
113 const std::string &ExecCommandLine);
114
Misha Brukman29afb642003-09-29 22:38:57 +0000115
116 virtual ~AbstractInterpreter() {}
117
Gabor Greif8ff70c22007-07-04 21:55:50 +0000118 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000119 /// code. This does not produce any output, it is only used when debugging
120 /// the code generator. If the code generator fails, an exception should be
121 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000122 virtual void compileProgram(const std::string &Bitcode) {}
Chris Lattnerf03715c2004-02-18 23:24:29 +0000123
Gabor Greif8ff70c22007-07-04 21:55:50 +0000124 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000125 /// understood by the GCC driver (either C or asm). If the code generator
126 /// fails, an exception should be thrown, otherwise, this function returns the
127 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000128 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000129 sys::Path &OutFile) {
130 throw std::string("OutputCode not supported by this AbstractInterpreter!");
131 }
132
Gabor Greif8ff70c22007-07-04 21:55:50 +0000133 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Misha Brukman29afb642003-09-29 22:38:57 +0000134 /// specified filename. This returns the exit code of the program.
135 ///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000136 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000137 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000138 const std::string &InputFile,
139 const std::string &OutputFile,
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
166 /// the code generator. If the code generator fails, an exception should be
167 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000168 virtual void compileProgram(const std::string &Bitcode);
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,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000174 const std::vector<std::string> &GCCArgs =
175 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000176 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000177 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000178 unsigned Timeout = 0,
179 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000180
Gabor Greif8ff70c22007-07-04 21:55:50 +0000181 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000182 /// understood by the GCC driver (either C or asm). If the code generator
183 /// fails, an exception should be thrown, otherwise, this function returns the
184 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000185 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000186 sys::Path &OutFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000187};
188
Misha Brukman29afb642003-09-29 22:38:57 +0000189
190//===---------------------------------------------------------------------===//
191// LLC Implementation of AbstractIntepreter interface
192//
193class LLC : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000194 std::string LLCPath; // The path to the LLC executable.
195 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
196 std::vector<std::string> gccArgs; // Extra args to pass to GCC.
Misha Brukman29afb642003-09-29 22:38:57 +0000197 GCC *gcc;
198public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000199 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000200 const std::vector<std::string> *Args,
201 const std::vector<std::string> *GCCArgs)
202 : LLCPath(llcPath), gcc(Gcc) {
203 ToolArgs.clear();
204 if (Args) ToolArgs = *Args;
205 if (GCCArgs) gccArgs = *GCCArgs;
Brian Gaeked11577b2004-05-04 21:09:01 +0000206 }
Misha Brukman29afb642003-09-29 22:38:57 +0000207 ~LLC() { delete gcc; }
208
Gabor Greif8ff70c22007-07-04 21:55:50 +0000209 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000210 /// code. This does not produce any output, it is only used when debugging
211 /// the code generator. If the code generator fails, an exception should be
212 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000213 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000214
Gabor Greif8ff70c22007-07-04 21:55:50 +0000215 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000216 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000217 const std::string &InputFile,
218 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000219 const std::vector<std::string> &GCCArgs =
220 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000221 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000222 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000223 unsigned Timeout = 0,
224 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000225
Gabor Greif8ff70c22007-07-04 21:55:50 +0000226 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000227 sys::Path &OutFile);
228
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