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 std::string GCCPath; // The path to the gcc executable
48 GCC(const std::string &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 struct AbstractInterpreter {
83 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
84 const std::vector<std::string> *Args = 0);
85 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
86 const std::vector<std::string> *Args = 0);
88 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
90 const std::vector<std::string> *Args=0);
92 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
94 const std::vector<std::string> *Args=0);
97 virtual ~AbstractInterpreter() {}
99 /// compileProgram - Compile the specified program from bytecode to executable
100 /// code. This does not produce any output, it is only used when debugging
101 /// the code generator. If the code generator fails, an exception should be
102 /// thrown, otherwise, this function will just return.
103 virtual void compileProgram(const std::string &Bytecode) {}
105 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
106 /// specified filename. This returns the exit code of the program.
108 virtual int ExecuteProgram(const std::string &Bytecode,
109 const std::vector<std::string> &Args,
110 const std::string &InputFile,
111 const std::string &OutputFile,
112 const std::vector<std::string> &SharedLibs =
113 std::vector<std::string>(),
114 unsigned Timeout = 0) = 0;
117 //===---------------------------------------------------------------------===//
118 // CBE Implementation of AbstractIntepreter interface
120 class CBE : public AbstractInterpreter {
121 std::string LLCPath; // The path to the `llc' executable
122 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
125 CBE(const std::string &llcPath, GCC *Gcc,
126 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
128 if (Args) { ToolArgs = *Args; }
130 ~CBE() { delete gcc; }
132 /// compileProgram - Compile the specified program from bytecode to executable
133 /// code. This does not produce any output, it is only used when debugging
134 /// the code generator. If the code generator fails, an exception should be
135 /// thrown, otherwise, this function will just return.
136 virtual void compileProgram(const std::string &Bytecode);
138 virtual int ExecuteProgram(const std::string &Bytecode,
139 const std::vector<std::string> &Args,
140 const std::string &InputFile,
141 const std::string &OutputFile,
142 const std::vector<std::string> &SharedLibs =
143 std::vector<std::string>(),
144 unsigned Timeout = 0);
146 // Sometimes we just want to go half-way and only generate the .c file, not
147 // necessarily compile it with GCC and run the program. This throws an
148 // exception if LLC crashes.
150 virtual void OutputC(const std::string &Bytecode, sys::Path& OutputCFile);
154 //===---------------------------------------------------------------------===//
155 // LLC Implementation of AbstractIntepreter interface
157 class LLC : public AbstractInterpreter {
158 std::string LLCPath; // The path to the LLC executable
159 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
162 LLC(const std::string &llcPath, GCC *Gcc,
163 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
165 if (Args) { ToolArgs = *Args; }
167 ~LLC() { delete gcc; }
169 /// compileProgram - Compile the specified program from bytecode to executable
170 /// code. This does not produce any output, it is only used when debugging
171 /// the code generator. If the code generator fails, an exception should be
172 /// thrown, otherwise, this function will just return.
173 virtual void compileProgram(const std::string &Bytecode);
175 virtual int ExecuteProgram(const std::string &Bytecode,
176 const std::vector<std::string> &Args,
177 const std::string &InputFile,
178 const std::string &OutputFile,
179 const std::vector<std::string> &SharedLibs =
180 std::vector<std::string>(),
181 unsigned Timeout = 0);
183 // Sometimes we just want to go half-way and only generate the .s file,
184 // not necessarily compile it all the way and run the program. This throws
185 // an exception if execution of LLC fails.
187 void OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile);
190 } // End llvm namespace