1 //===-- 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 //===----------------------------------------------------------------------===//
20 #include "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>());
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 static LLC *createLLC(const std::string &ProgramPath, std::string &Message);
86 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
87 std::string &Message);
89 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
90 std::string &Message);
93 virtual ~AbstractInterpreter() {}
95 /// compileProgram - Compile the specified program from bytecode to executable
96 /// code. This does not produce any output, it is only used when debugging
97 /// the code generator. If the code generator fails, an exception should be
98 /// thrown, otherwise, this function will just return.
99 virtual void compileProgram(const std::string &Bytecode) {}
101 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
102 /// specified filename. This returns the exit code of the program.
104 virtual int ExecuteProgram(const std::string &Bytecode,
105 const std::vector<std::string> &Args,
106 const std::string &InputFile,
107 const std::string &OutputFile,
108 const std::vector<std::string> &SharedLibs =
109 std::vector<std::string>()) = 0;
112 //===---------------------------------------------------------------------===//
113 // CBE Implementation of AbstractIntepreter interface
115 class CBE : public AbstractInterpreter {
116 std::string LLCPath; // The path to the `llc' executable
119 CBE(const std::string &llcPath, GCC *Gcc) : LLCPath(llcPath), gcc(Gcc) { }
120 ~CBE() { delete gcc; }
122 /// compileProgram - Compile the specified program from bytecode to executable
123 /// code. This does not produce any output, it is only used when debugging
124 /// the code generator. If the code generator fails, an exception should be
125 /// thrown, otherwise, this function will just return.
126 virtual void compileProgram(const std::string &Bytecode);
128 virtual int ExecuteProgram(const std::string &Bytecode,
129 const std::vector<std::string> &Args,
130 const std::string &InputFile,
131 const std::string &OutputFile,
132 const std::vector<std::string> &SharedLibs =
133 std::vector<std::string>());
135 // Sometimes we just want to go half-way and only generate the .c file, not
136 // necessarily compile it with GCC and run the program. This throws an
137 // exception if LLC crashes.
139 virtual void OutputC(const std::string &Bytecode, std::string &OutputCFile);
143 //===---------------------------------------------------------------------===//
144 // LLC Implementation of AbstractIntepreter interface
146 class LLC : public AbstractInterpreter {
147 std::string LLCPath; // The path to the LLC executable
150 LLC(const std::string &llcPath, GCC *Gcc)
151 : LLCPath(llcPath), gcc(Gcc) { }
152 ~LLC() { delete gcc; }
155 /// compileProgram - Compile the specified program from bytecode to executable
156 /// code. This does not produce any output, it is only used when debugging
157 /// the code generator. If the code generator fails, an exception should be
158 /// thrown, otherwise, this function will just return.
159 virtual void compileProgram(const std::string &Bytecode);
161 virtual int ExecuteProgram(const std::string &Bytecode,
162 const std::vector<std::string> &Args,
163 const std::string &InputFile,
164 const std::string &OutputFile,
165 const std::vector<std::string> &SharedLibs =
166 std::vector<std::string>());
168 // Sometimes we just want to go half-way and only generate the .s file,
169 // not necessarily compile it all the way and run the program. This throws
170 // an exception if execution of LLC fails.
172 void OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
175 } // End llvm namespace