blob: 5cdd304f02e7c88d95a610a1f49d04670da617c7 [file] [log] [blame]
Chris Lattnerffac2862006-06-06 22:30:59 +00001//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
20#include "llvm/Support/SystemUtils.h"
21#include <exception>
22#include <vector>
23
24namespace llvm {
25
26class CBE;
27class LLC;
28
29/// 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///
33class ToolExecutionError : std::exception {
34 std::string Message;
35public:
36 explicit ToolExecutionError(const std::string &M) : Message(M) {}
37 virtual ~ToolExecutionError() throw();
38 virtual const char* what() const throw() { return Message.c_str(); }
39};
40
41
42//===---------------------------------------------------------------------===//
43// GCC abstraction
44//
45class GCC {
46 sys::Path GCCPath; // The path to the gcc executable
Evan Cheng779b52e2007-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 Lattnerffac2862006-06-06 22:30:59 +000050public:
51 enum FileType { AsmFile, CFile };
52
Chris Lattnercc21fa72006-06-27 20:35:36 +000053 static GCC *create(const std::string &ProgramPath, std::string &Message);
Chris Lattnerffac2862006-06-06 22:30:59 +000054
55 /// 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 ///
62 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,
67 const std::vector<std::string> &GCCArgs =
68 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +000069 unsigned Timeout = 0,
70 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +000071
72 /// 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 Lattnercc21fa72006-06-27 20:35:36 +000076 std::string &OutputFile,
77 const std::vector<std::string> &ArgsForGCC);
Chris Lattnerffac2862006-06-06 22:30:59 +000078};
79
80
81//===---------------------------------------------------------------------===//
82/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif0e535c3c2007-07-04 21:55:50 +000083/// LLVM bitcode in a variety of ways. This abstract interface hides this
Chris Lattnerffac2862006-06-06 22:30:59 +000084/// complexity behind a simple interface.
85///
86class AbstractInterpreter {
87public:
88 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);
92
93 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
94 std::string &Message,
95 const std::vector<std::string> *Args=0);
96
97 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
98 std::string &Message,
99 const std::vector<std::string> *Args=0);
100
101
102 virtual ~AbstractInterpreter() {}
103
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000104 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000105 /// code. This does not produce any output, it is only used when debugging
106 /// the code generator. If the code generator fails, an exception should be
107 /// thrown, otherwise, this function will just return.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000108 virtual void compileProgram(const std::string &Bitcode) {}
Chris Lattnerffac2862006-06-06 22:30:59 +0000109
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000110 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattner634bc042006-09-15 21:29:15 +0000111 /// understood by the GCC driver (either C or asm). If the code generator
112 /// fails, an exception should be thrown, otherwise, this function returns the
113 /// type of code emitted.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000114 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattner634bc042006-09-15 21:29:15 +0000115 sys::Path &OutFile) {
116 throw std::string("OutputCode not supported by this AbstractInterpreter!");
117 }
118
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000119 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Chris Lattnerffac2862006-06-06 22:30:59 +0000120 /// specified filename. This returns the exit code of the program.
121 ///
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000122 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000123 const std::vector<std::string> &Args,
124 const std::string &InputFile,
125 const std::string &OutputFile,
126 const std::vector<std::string> &GCCArgs =
127 std::vector<std::string>(),
128 const std::vector<std::string> &SharedLibs =
129 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000130 unsigned Timeout = 0,
131 unsigned MemoryLimit = 0) = 0;
Chris Lattnerffac2862006-06-06 22:30:59 +0000132};
133
134//===---------------------------------------------------------------------===//
135// CBE Implementation of AbstractIntepreter interface
136//
137class CBE : public AbstractInterpreter {
138 sys::Path LLCPath; // The path to the `llc' executable
139 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
140 GCC *gcc;
141public:
142 CBE(const sys::Path &llcPath, GCC *Gcc,
143 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
144 ToolArgs.clear ();
145 if (Args) { ToolArgs = *Args; }
146 }
147 ~CBE() { delete gcc; }
148
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000149 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000150 /// code. This does not produce any output, it is only used when debugging
151 /// the code generator. If the code generator fails, an exception should be
152 /// thrown, otherwise, this function will just return.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000153 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerffac2862006-06-06 22:30:59 +0000154
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000155 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000156 const std::vector<std::string> &Args,
157 const std::string &InputFile,
158 const std::string &OutputFile,
159 const std::vector<std::string> &GCCArgs =
160 std::vector<std::string>(),
161 const std::vector<std::string> &SharedLibs =
162 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000163 unsigned Timeout = 0,
164 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000165
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000166 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattner634bc042006-09-15 21:29:15 +0000167 /// understood by the GCC driver (either C or asm). If the code generator
168 /// fails, an exception should be thrown, otherwise, this function returns the
169 /// type of code emitted.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000170 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattner634bc042006-09-15 21:29:15 +0000171 sys::Path &OutFile);
Chris Lattnerffac2862006-06-06 22:30:59 +0000172};
173
174
175//===---------------------------------------------------------------------===//
176// LLC Implementation of AbstractIntepreter interface
177//
178class LLC : public AbstractInterpreter {
179 std::string LLCPath; // The path to the LLC executable
180 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
181 GCC *gcc;
182public:
183 LLC(const std::string &llcPath, GCC *Gcc,
184 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
185 ToolArgs.clear ();
186 if (Args) { ToolArgs = *Args; }
187 }
188 ~LLC() { delete gcc; }
189
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000190 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000191 /// code. This does not produce any output, it is only used when debugging
192 /// the code generator. If the code generator fails, an exception should be
193 /// thrown, otherwise, this function will just return.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000194 virtual void compileProgram(const std::string &Bitcode);
Chris Lattnerffac2862006-06-06 22:30:59 +0000195
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000196 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000197 const std::vector<std::string> &Args,
198 const std::string &InputFile,
199 const std::string &OutputFile,
200 const std::vector<std::string> &GCCArgs =
201 std::vector<std::string>(),
202 const std::vector<std::string> &SharedLibs =
203 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000204 unsigned Timeout = 0,
205 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000206
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000207 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Chris Lattner634bc042006-09-15 21:29:15 +0000208 sys::Path &OutFile);
209
Chris Lattnerffac2862006-06-06 22:30:59 +0000210};
211
212} // End llvm namespace
213
214#endif