blob: 3e3d7d5e5acf136b5184ad1d414d4d743b951208 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// 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.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef BUGPOINT_TOOLRUNNER_H
18#define BUGPOINT_TOOLRUNNER_H
19
Daniel Dunbard8590af2009-08-18 03:35:57 +000020#include "llvm/ADT/Triple.h"
Anton Korobeynikov43cf4f12009-08-05 09:32:10 +000021#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Support/SystemUtils.h"
23#include <exception>
24#include <vector>
25
26namespace llvm {
27
Anton Korobeynikov43cf4f12009-08-05 09:32:10 +000028extern cl::opt<bool> SaveTemps;
Daniel Dunbard8590af2009-08-18 03:35:57 +000029extern Triple TargetTriple;
Anton Korobeynikov43cf4f12009-08-05 09:32:10 +000030
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031class CBE;
32class LLC;
33
34/// 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///
38class ToolExecutionError : std::exception {
39 std::string Message;
40public:
41 explicit ToolExecutionError(const std::string &M) : Message(M) {}
42 virtual ~ToolExecutionError() throw();
43 virtual const char* what() const throw() { return Message.c_str(); }
44};
45
46
47//===---------------------------------------------------------------------===//
48// GCC abstraction
49//
50class GCC {
Bill Wendlingcd1389d2009-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 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059public:
60 enum FileType { AsmFile, CFile };
61
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +000062 static GCC *create(std::string &Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +000063 const std::vector<std::string> *Args);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064
65 /// 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 ///
72 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,
77 const std::vector<std::string> &GCCArgs =
78 std::vector<std::string>(),
79 unsigned Timeout = 0,
80 unsigned MemoryLimit = 0);
81
82 /// 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,
86 std::string &OutputFile,
87 const std::vector<std::string> &ArgsForGCC);
88};
89
90
91//===---------------------------------------------------------------------===//
92/// AbstractInterpreter Class - Subclasses of this class are used to execute
93/// LLVM bitcode in a variety of ways. This abstract interface hides this
94/// complexity behind a simple interface.
95///
96class AbstractInterpreter {
97public:
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +000098 static CBE *createCBE(const char *Argv0, std::string &Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +000099 const std::vector<std::string> *Args = 0,
100 const std::vector<std::string> *GCCArgs = 0);
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000101 static LLC *createLLC(const char *Argv0, std::string &Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000102 const std::vector<std::string> *Args = 0,
103 const std::vector<std::string> *GCCArgs = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000105 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 const std::vector<std::string> *Args=0);
107
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000108 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 const std::vector<std::string> *Args=0);
110
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000111 static AbstractInterpreter* createCustom(std::string &Message,
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +0000112 const std::string &ExecCommandLine);
113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
115 virtual ~AbstractInterpreter() {}
116
117 /// compileProgram - Compile the specified program from bitcode to executable
118 /// 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.
121 virtual void compileProgram(const std::string &Bitcode) {}
122
123 /// OutputCode - Compile the specified program from bitcode to code
124 /// 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.
127 virtual GCC::FileType OutputCode(const std::string &Bitcode,
128 sys::Path &OutFile) {
129 throw std::string("OutputCode not supported by this AbstractInterpreter!");
130 }
131
132 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
133 /// specified filename. This returns the exit code of the program.
134 ///
135 virtual int ExecuteProgram(const std::string &Bitcode,
136 const std::vector<std::string> &Args,
137 const std::string &InputFile,
138 const std::string &OutputFile,
139 const std::vector<std::string> &GCCArgs =
140 std::vector<std::string>(),
141 const std::vector<std::string> &SharedLibs =
142 std::vector<std::string>(),
143 unsigned Timeout = 0,
144 unsigned MemoryLimit = 0) = 0;
145};
146
147//===---------------------------------------------------------------------===//
148// CBE Implementation of AbstractIntepreter interface
149//
150class CBE : public AbstractInterpreter {
Bill Wendlingcd1389d2009-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 GCC *gcc;
154public:
155 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000156 const std::vector<std::string> *Args)
157 : LLCPath(llcPath), gcc(Gcc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 ToolArgs.clear ();
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000159 if (Args) ToolArgs = *Args;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 }
161 ~CBE() { delete gcc; }
162
163 /// compileProgram - Compile the specified program from bitcode to executable
164 /// 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.
167 virtual void compileProgram(const std::string &Bitcode);
168
169 virtual int ExecuteProgram(const std::string &Bitcode,
170 const std::vector<std::string> &Args,
171 const std::string &InputFile,
172 const std::string &OutputFile,
173 const std::vector<std::string> &GCCArgs =
174 std::vector<std::string>(),
175 const std::vector<std::string> &SharedLibs =
176 std::vector<std::string>(),
177 unsigned Timeout = 0,
178 unsigned MemoryLimit = 0);
179
180 /// OutputCode - Compile the specified program from bitcode to code
181 /// 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.
184 virtual GCC::FileType OutputCode(const std::string &Bitcode,
185 sys::Path &OutFile);
186};
187
188
189//===---------------------------------------------------------------------===//
190// LLC Implementation of AbstractIntepreter interface
191//
192class LLC : public AbstractInterpreter {
Bill Wendlingcd1389d2009-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 GCC *gcc;
197public:
198 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendlingcd1389d2009-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 }
206 ~LLC() { delete gcc; }
207
208 /// compileProgram - Compile the specified program from bitcode to executable
209 /// 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.
212 virtual void compileProgram(const std::string &Bitcode);
213
214 virtual int ExecuteProgram(const std::string &Bitcode,
215 const std::vector<std::string> &Args,
216 const std::string &InputFile,
217 const std::string &OutputFile,
218 const std::vector<std::string> &GCCArgs =
219 std::vector<std::string>(),
220 const std::vector<std::string> &SharedLibs =
221 std::vector<std::string>(),
222 unsigned Timeout = 0,
223 unsigned MemoryLimit = 0);
224
225 virtual GCC::FileType OutputCode(const std::string &Bitcode,
226 sys::Path &OutFile);
227
228};
229
230} // End llvm namespace
231
232#endif