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>(),
67 unsigned Timeout = 0);
69 /// MakeSharedObject - This compiles the specified file (which is either a .c
70 /// file or a .s file) into a shared object.
72 int MakeSharedObject(const std::string &InputFile, FileType fileType,
73 std::string &OutputFile,
74 const std::vector<std::string> &ArgsForGCC);
78 //===---------------------------------------------------------------------===//
79 /// AbstractInterpreter Class - Subclasses of this class are used to execute
80 /// LLVM bytecode in a variety of ways. This abstract interface hides this
81 /// complexity behind a simple interface.
83 class AbstractInterpreter {
85 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
86 const std::vector<std::string> *Args = 0);
87 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
88 const std::vector<std::string> *Args = 0);
90 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
92 const std::vector<std::string> *Args=0);
94 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
96 const std::vector<std::string> *Args=0);
99 virtual ~AbstractInterpreter() {}
101 /// compileProgram - Compile the specified program from bytecode to executable
102 /// code. This does not produce any output, it is only used when debugging
103 /// the code generator. If the code generator fails, an exception should be
104 /// thrown, otherwise, this function will just return.
105 virtual void compileProgram(const std::string &Bytecode) {}
107 /// OutputCode - Compile the specified program from bytecode to code
108 /// understood by the GCC driver (either C or asm). If the code generator
109 /// fails, an exception should be thrown, otherwise, this function returns the
110 /// type of code emitted.
111 virtual GCC::FileType OutputCode(const std::string &Bytecode,
112 sys::Path &OutFile) {
113 throw std::string("OutputCode not supported by this AbstractInterpreter!");
116 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
117 /// specified filename. This returns the exit code of the program.
119 virtual int ExecuteProgram(const std::string &Bytecode,
120 const std::vector<std::string> &Args,
121 const std::string &InputFile,
122 const std::string &OutputFile,
123 const std::vector<std::string> &GCCArgs =
124 std::vector<std::string>(),
125 const std::vector<std::string> &SharedLibs =
126 std::vector<std::string>(),
127 unsigned Timeout = 0) = 0;
130 //===---------------------------------------------------------------------===//
131 // CBE Implementation of AbstractIntepreter interface
133 class CBE : public AbstractInterpreter {
134 sys::Path LLCPath; // The path to the `llc' executable
135 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
138 CBE(const sys::Path &llcPath, GCC *Gcc,
139 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
141 if (Args) { ToolArgs = *Args; }
143 ~CBE() { delete gcc; }
145 /// compileProgram - Compile the specified program from bytecode to executable
146 /// code. This does not produce any output, it is only used when debugging
147 /// the code generator. If the code generator fails, an exception should be
148 /// thrown, otherwise, this function will just return.
149 virtual void compileProgram(const std::string &Bytecode);
151 virtual int ExecuteProgram(const std::string &Bytecode,
152 const std::vector<std::string> &Args,
153 const std::string &InputFile,
154 const std::string &OutputFile,
155 const std::vector<std::string> &GCCArgs =
156 std::vector<std::string>(),
157 const std::vector<std::string> &SharedLibs =
158 std::vector<std::string>(),
159 unsigned Timeout = 0);
161 /// OutputCode - Compile the specified program from bytecode to code
162 /// understood by the GCC driver (either C or asm). If the code generator
163 /// fails, an exception should be thrown, otherwise, this function returns the
164 /// type of code emitted.
165 virtual GCC::FileType OutputCode(const std::string &Bytecode,
170 //===---------------------------------------------------------------------===//
171 // LLC Implementation of AbstractIntepreter interface
173 class LLC : public AbstractInterpreter {
174 std::string LLCPath; // The path to the LLC executable
175 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
178 LLC(const std::string &llcPath, GCC *Gcc,
179 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
181 if (Args) { ToolArgs = *Args; }
183 ~LLC() { delete gcc; }
185 /// compileProgram - Compile the specified program from bytecode to executable
186 /// code. This does not produce any output, it is only used when debugging
187 /// the code generator. If the code generator fails, an exception should be
188 /// thrown, otherwise, this function will just return.
189 virtual void compileProgram(const std::string &Bytecode);
191 virtual int ExecuteProgram(const std::string &Bytecode,
192 const std::vector<std::string> &Args,
193 const std::string &InputFile,
194 const std::string &OutputFile,
195 const std::vector<std::string> &GCCArgs =
196 std::vector<std::string>(),
197 const std::vector<std::string> &SharedLibs =
198 std::vector<std::string>(),
199 unsigned Timeout = 0);
201 virtual GCC::FileType OutputCode(const std::string &Bytecode,
206 } // End llvm namespace