1 //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
17 #ifndef BUGPOINT_TOOLRUNNER_H
18 #define BUGPOINT_TOOLRUNNER_H
20 #include "llvm/Support/SystemUtils.h"
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.
33 class ToolExecutionError : std::exception {
36 explicit ToolExecutionError(const std::string &M) : Message(M) {}
37 virtual ~ToolExecutionError() throw();
38 virtual const char* what() const throw() { return Message.c_str(); }
42 //===---------------------------------------------------------------------===//
46 sys::Path GCCPath; // The path to the gcc executable
47 GCC(const sys::Path &gccPath) : GCCPath(gccPath) { }
49 enum FileType { AsmFile, CFile };
51 static GCC *create(const std::string &ProgramPath, std::string &Message);
53 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
54 /// either a .s file, or a .c file, specified by FileType), with the specified
55 /// arguments. Standard input is specified with InputFile, and standard
56 /// Output is captured to the specified OutputFile location. The SharedLibs
57 /// option specifies optional native shared objects that can be loaded into
58 /// the program for execution.
60 int ExecuteProgram(const std::string &ProgramFile,
61 const std::vector<std::string> &Args,
63 const std::string &InputFile,
64 const std::string &OutputFile,
65 const std::vector<std::string> &GCCArgs =
66 std::vector<std::string>(),
68 unsigned MemoryLimit = 0);
70 /// MakeSharedObject - This compiles the specified file (which is either a .c
71 /// file or a .s file) into a shared object.
73 int MakeSharedObject(const std::string &InputFile, FileType fileType,
74 std::string &OutputFile,
75 const std::vector<std::string> &ArgsForGCC);
79 //===---------------------------------------------------------------------===//
80 /// AbstractInterpreter Class - Subclasses of this class are used to execute
81 /// LLVM bytecode in a variety of ways. This abstract interface hides this
82 /// complexity behind a simple interface.
84 class AbstractInterpreter {
86 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
87 const std::vector<std::string> *Args = 0);
88 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
89 const std::vector<std::string> *Args = 0);
91 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
93 const std::vector<std::string> *Args=0);
95 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
97 const std::vector<std::string> *Args=0);
100 virtual ~AbstractInterpreter() {}
102 /// compileProgram - Compile the specified program from bytecode to executable
103 /// code. This does not produce any output, it is only used when debugging
104 /// the code generator. If the code generator fails, an exception should be
105 /// thrown, otherwise, this function will just return.
106 virtual void compileProgram(const std::string &Bytecode) {}
108 /// OutputCode - Compile the specified program from bytecode to code
109 /// understood by the GCC driver (either C or asm). If the code generator
110 /// fails, an exception should be thrown, otherwise, this function returns the
111 /// type of code emitted.
112 virtual GCC::FileType OutputCode(const std::string &Bytecode,
113 sys::Path &OutFile) {
114 throw std::string("OutputCode not supported by this AbstractInterpreter!");
117 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
118 /// specified filename. This returns the exit code of the program.
120 virtual int ExecuteProgram(const std::string &Bytecode,
121 const std::vector<std::string> &Args,
122 const std::string &InputFile,
123 const std::string &OutputFile,
124 const std::vector<std::string> &GCCArgs =
125 std::vector<std::string>(),
126 const std::vector<std::string> &SharedLibs =
127 std::vector<std::string>(),
128 unsigned Timeout = 0,
129 unsigned MemoryLimit = 0) = 0;
132 //===---------------------------------------------------------------------===//
133 // CBE Implementation of AbstractIntepreter interface
135 class CBE : public AbstractInterpreter {
136 sys::Path LLCPath; // The path to the `llc' executable
137 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
140 CBE(const sys::Path &llcPath, GCC *Gcc,
141 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
143 if (Args) { ToolArgs = *Args; }
145 ~CBE() { delete gcc; }
147 /// compileProgram - Compile the specified program from bytecode to executable
148 /// code. This does not produce any output, it is only used when debugging
149 /// the code generator. If the code generator fails, an exception should be
150 /// thrown, otherwise, this function will just return.
151 virtual void compileProgram(const std::string &Bytecode);
153 virtual int ExecuteProgram(const std::string &Bytecode,
154 const std::vector<std::string> &Args,
155 const std::string &InputFile,
156 const std::string &OutputFile,
157 const std::vector<std::string> &GCCArgs =
158 std::vector<std::string>(),
159 const std::vector<std::string> &SharedLibs =
160 std::vector<std::string>(),
161 unsigned Timeout = 0,
162 unsigned MemoryLimit = 0);
164 /// OutputCode - Compile the specified program from bytecode to code
165 /// understood by the GCC driver (either C or asm). If the code generator
166 /// fails, an exception should be thrown, otherwise, this function returns the
167 /// type of code emitted.
168 virtual GCC::FileType OutputCode(const std::string &Bytecode,
173 //===---------------------------------------------------------------------===//
174 // LLC Implementation of AbstractIntepreter interface
176 class LLC : public AbstractInterpreter {
177 std::string LLCPath; // The path to the LLC executable
178 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
181 LLC(const std::string &llcPath, GCC *Gcc,
182 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
184 if (Args) { ToolArgs = *Args; }
186 ~LLC() { delete gcc; }
188 /// compileProgram - Compile the specified program from bytecode to executable
189 /// code. This does not produce any output, it is only used when debugging
190 /// the code generator. If the code generator fails, an exception should be
191 /// thrown, otherwise, this function will just return.
192 virtual void compileProgram(const std::string &Bytecode);
194 virtual int ExecuteProgram(const std::string &Bytecode,
195 const std::vector<std::string> &Args,
196 const std::string &InputFile,
197 const std::string &OutputFile,
198 const std::vector<std::string> &GCCArgs =
199 std::vector<std::string>(),
200 const std::vector<std::string> &SharedLibs =
201 std::vector<std::string>(),
202 unsigned Timeout = 0,
203 unsigned MemoryLimit = 0);
205 virtual GCC::FileType OutputCode(const std::string &Bytecode,
210 } // End llvm namespace