blob: 0cc5426b083fa7ad914f4cf355742362a813a884 [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 {
Reid Spencer2418bf92004-12-19 17:59:45 +000046 sys::Path GCCPath; // The path to the gcc executable
Evan Cheng34e400e2007-05-03 18:36:15 +000047 sys::Path RSHPath; // The path to the rsh executable
48 GCC(const sys::Path &gccPath, const sys::Path &rshPath)
49 : GCCPath(gccPath), RSHPath(rshPath) { }
Chris Lattner7915a1e2003-10-14 21:34:11 +000050public:
51 enum FileType { AsmFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000052
Chris Lattner130e2a32006-06-27 20:35:36 +000053 static GCC *create(const std::string &ProgramPath, std::string &Message);
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,
Reid Spencer51ab5c82006-06-06 00:00:42 +000067 const std::vector<std::string> &GCCArgs =
68 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000069 unsigned Timeout = 0,
70 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000071
Chris Lattnereeed9832003-10-14 21:52:52 +000072 /// MakeSharedObject - This compiles the specified file (which is either a .c
73 /// file or a .s file) into a shared object.
74 ///
75 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +000076 std::string &OutputFile,
77 const std::vector<std::string> &ArgsForGCC);
Misha Brukman29afb642003-09-29 22:38:57 +000078};
79
Misha Brukman29afb642003-09-29 22:38:57 +000080
Chris Lattner7915a1e2003-10-14 21:34:11 +000081//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000082/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif8ff70c22007-07-04 21:55:50 +000083/// LLVM bitcode in a variety of ways. This abstract interface hides this
Misha Brukman29afb642003-09-29 22:38:57 +000084/// complexity behind a simple interface.
85///
Jeff Cohen83881952005-01-22 16:30:58 +000086class AbstractInterpreter {
87public:
Brian Gaeked11577b2004-05-04 21:09:01 +000088 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
89 const std::vector<std::string> *Args = 0);
90 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
91 const std::vector<std::string> *Args = 0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000092
93 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000094 std::string &Message,
95 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000096
97 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000098 std::string &Message,
99 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000100
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000101 static AbstractInterpreter* createCustom(const std::string &ProgramPath,
102 std::string &Message,
103 const std::string &ExecCommandLine);
104
Misha Brukman29afb642003-09-29 22:38:57 +0000105
106 virtual ~AbstractInterpreter() {}
107
Gabor Greif8ff70c22007-07-04 21:55:50 +0000108 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000109 /// code. This does not produce any output, it is only used when debugging
110 /// the code generator. If the code generator fails, an exception should be
111 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000112 virtual void compileProgram(const std::string &Bitcode) {}
Chris Lattnerf03715c2004-02-18 23:24:29 +0000113
Gabor Greif8ff70c22007-07-04 21:55:50 +0000114 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000115 /// understood by the GCC driver (either C or asm). If the code generator
116 /// fails, an exception should be thrown, otherwise, this function returns the
117 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000118 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000119 sys::Path &OutFile) {
120 throw std::string("OutputCode not supported by this AbstractInterpreter!");
121 }
122
Gabor Greif8ff70c22007-07-04 21:55:50 +0000123 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Misha Brukman29afb642003-09-29 22:38:57 +0000124 /// specified filename. This returns the exit code of the program.
125 ///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000126 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000127 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000128 const std::string &InputFile,
129 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000130 const std::vector<std::string> &GCCArgs =
131 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000132 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000133 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000134 unsigned Timeout = 0,
135 unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000136};
137
138//===---------------------------------------------------------------------===//
139// CBE Implementation of AbstractIntepreter interface
140//
141class CBE : public AbstractInterpreter {
Reid Spencer2418bf92004-12-19 17:59:45 +0000142 sys::Path LLCPath; // The path to the `llc' executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000143 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000144 GCC *gcc;
145public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000146 CBE(const sys::Path &llcPath, GCC *Gcc,
Brian Gaeked11577b2004-05-04 21:09:01 +0000147 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
148 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000149 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000150 }
Misha Brukman29afb642003-09-29 22:38:57 +0000151 ~CBE() { delete gcc; }
152
Gabor Greif8ff70c22007-07-04 21:55:50 +0000153 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000154 /// code. This does not produce any output, it is only used when debugging
155 /// the code generator. If the code generator fails, an exception should be
156 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000157 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000158
Gabor Greif8ff70c22007-07-04 21:55:50 +0000159 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000160 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000161 const std::string &InputFile,
162 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000163 const std::vector<std::string> &GCCArgs =
164 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000165 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000166 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000167 unsigned Timeout = 0,
168 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000169
Gabor Greif8ff70c22007-07-04 21:55:50 +0000170 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000171 /// understood by the GCC driver (either C or asm). If the code generator
172 /// fails, an exception should be thrown, otherwise, this function returns the
173 /// type of code emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000174 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000175 sys::Path &OutFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000176};
177
Misha Brukman29afb642003-09-29 22:38:57 +0000178
179//===---------------------------------------------------------------------===//
180// LLC Implementation of AbstractIntepreter interface
181//
182class LLC : public AbstractInterpreter {
183 std::string LLCPath; // The path to the LLC executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000184 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000185 GCC *gcc;
186public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000187 LLC(const std::string &llcPath, GCC *Gcc,
188 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
189 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000190 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000191 }
Misha Brukman29afb642003-09-29 22:38:57 +0000192 ~LLC() { delete gcc; }
193
Gabor Greif8ff70c22007-07-04 21:55:50 +0000194 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000195 /// code. This does not produce any output, it is only used when debugging
196 /// the code generator. If the code generator fails, an exception should be
197 /// thrown, otherwise, this function will just return.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000198 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000199
Gabor Greif8ff70c22007-07-04 21:55:50 +0000200 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000201 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000202 const std::string &InputFile,
203 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000204 const std::vector<std::string> &GCCArgs =
205 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000206 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000207 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000208 unsigned Timeout = 0,
209 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000210
Gabor Greif8ff70c22007-07-04 21:55:50 +0000211 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000212 sys::Path &OutFile);
213
Misha Brukman29afb642003-09-29 22:38:57 +0000214};
215
Brian Gaeked0fde302003-11-11 22:41:34 +0000216} // End llvm namespace
217
Misha Brukman29afb642003-09-29 22:38:57 +0000218#endif