1 //===-- llvm/Support/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 LLVM_SUPPORT_TOOLRUNNER_H
18 #define LLVM_SUPPORT_TOOLRUNNER_H
20 #include "llvm/Support/SystemUtils.h"
30 /// ToolExecutionError - An instance of this class is thrown by the
31 /// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
32 /// crashes) which prevents execution of the program.
34 class ToolExecutionError : std::exception {
37 explicit ToolExecutionError(const std::string &M) : Message(M) {}
38 virtual ~ToolExecutionError() throw();
39 virtual const char* what() const throw() { return Message.c_str(); }
43 //===---------------------------------------------------------------------===//
47 sys::Path GCCPath; // The path to the gcc executable
48 GCC(const sys::Path &gccPath) : GCCPath(gccPath) { }
50 enum FileType { AsmFile, CFile };
52 static GCC* create(const std::string &ProgramPath, std::string &Message);
54 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
55 /// either a .s file, or a .c file, specified by FileType), with the specified
56 /// arguments. Standard input is specified with InputFile, and standard
57 /// Output is captured to the specified OutputFile location. The SharedLibs
58 /// option specifies optional native shared objects that can be loaded into
59 /// the program for execution.
61 int ExecuteProgram(const std::string &ProgramFile,
62 const std::vector<std::string> &Args,
64 const std::string &InputFile,
65 const std::string &OutputFile,
66 const std::vector<std::string> &SharedLibs =
67 std::vector<std::string>(), 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);
77 //===---------------------------------------------------------------------===//
78 /// AbstractInterpreter Class - Subclasses of this class are used to execute
79 /// LLVM bytecode in a variety of ways. This abstract interface hides this
80 /// complexity behind a simple interface.
82 class AbstractInterpreter {
84 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
85 const std::vector<std::string> *Args = 0);
86 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
87 const std::vector<std::string> *Args = 0);
89 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
91 const std::vector<std::string> *Args=0);
93 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
95 const std::vector<std::string> *Args=0);
98 virtual ~AbstractInterpreter() {}
100 /// compileProgram - Compile the specified program from bytecode to executable
101 /// code. This does not produce any output, it is only used when debugging
102 /// the code generator. If the code generator fails, an exception should be
103 /// thrown, otherwise, this function will just return.
104 virtual void compileProgram(const std::string &Bytecode) {}
106 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
107 /// specified filename. This returns the exit code of the program.
109 virtual int ExecuteProgram(const std::string &Bytecode,
110 const std::vector<std::string> &Args,
111 const std::string &InputFile,
112 const std::string &OutputFile,
113 const std::vector<std::string> &SharedLibs =
114 std::vector<std::string>(),
115 unsigned Timeout = 0) = 0;
118 //===---------------------------------------------------------------------===//
119 // CBE Implementation of AbstractIntepreter interface
121 class CBE : public AbstractInterpreter {
122 sys::Path LLCPath; // The path to the `llc' executable
123 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
126 CBE(const sys::Path &llcPath, GCC *Gcc,
127 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
129 if (Args) { ToolArgs = *Args; }
131 ~CBE() { delete gcc; }
133 /// compileProgram - Compile the specified program from bytecode to executable
134 /// code. This does not produce any output, it is only used when debugging
135 /// the code generator. If the code generator fails, an exception should be
136 /// thrown, otherwise, this function will just return.
137 virtual void compileProgram(const std::string &Bytecode);
139 virtual int ExecuteProgram(const std::string &Bytecode,
140 const std::vector<std::string> &Args,
141 const std::string &InputFile,
142 const std::string &OutputFile,
143 const std::vector<std::string> &SharedLibs =
144 std::vector<std::string>(),
145 unsigned Timeout = 0);
147 // Sometimes we just want to go half-way and only generate the .c file, not
148 // necessarily compile it with GCC and run the program. This throws an
149 // exception if LLC crashes.
151 virtual void OutputC(const std::string &Bytecode, sys::Path& OutputCFile);
155 //===---------------------------------------------------------------------===//
156 // LLC Implementation of AbstractIntepreter interface
158 class LLC : public AbstractInterpreter {
159 std::string LLCPath; // The path to the LLC executable
160 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
163 LLC(const std::string &llcPath, GCC *Gcc,
164 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
166 if (Args) { ToolArgs = *Args; }
168 ~LLC() { delete gcc; }
170 /// compileProgram - Compile the specified program from bytecode to executable
171 /// code. This does not produce any output, it is only used when debugging
172 /// the code generator. If the code generator fails, an exception should be
173 /// thrown, otherwise, this function will just return.
174 virtual void compileProgram(const std::string &Bytecode);
176 virtual int ExecuteProgram(const std::string &Bytecode,
177 const std::vector<std::string> &Args,
178 const std::string &InputFile,
179 const std::string &OutputFile,
180 const std::vector<std::string> &SharedLibs =
181 std::vector<std::string>(),
182 unsigned Timeout = 0);
184 // Sometimes we just want to go half-way and only generate the .s file,
185 // not necessarily compile it all the way and run the program. This throws
186 // an exception if execution of LLC fails.
188 void OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile);
191 } // End llvm namespace