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"
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 {
36 ToolExecutionError(const std::string &M) : Message(M) {}
37 const std::string getMessage() const { return Message; }
41 //===---------------------------------------------------------------------===//
45 std::string GCCPath; // The path to the gcc executable
46 GCC(const std::string &gccPath) : GCCPath(gccPath) { }
48 enum FileType { AsmFile, CFile };
50 static GCC* create(const std::string &ProgramPath, std::string &Message);
52 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
53 /// either a .s file, or a .c file, specified by FileType), with the specified
54 /// arguments. Standard input is specified with InputFile, and standard
55 /// Output is captured to the specified OutputFile location. The SharedLibs
56 /// option specifies optional native shared objects that can be loaded into
57 /// the program for execution.
59 int ExecuteProgram(const std::string &ProgramFile,
60 const std::vector<std::string> &Args,
62 const std::string &InputFile,
63 const std::string &OutputFile,
64 const std::vector<std::string> &SharedLibs =
65 std::vector<std::string>());
67 /// MakeSharedObject - This compiles the specified file (which is either a .c
68 /// file or a .s file) into a shared object.
70 int MakeSharedObject(const std::string &InputFile, FileType fileType,
71 std::string &OutputFile);
75 //===---------------------------------------------------------------------===//
76 /// AbstractInterpreter Class - Subclasses of this class are used to execute
77 /// LLVM bytecode in a variety of ways. This abstract interface hides this
78 /// complexity behind a simple interface.
80 struct AbstractInterpreter {
81 static CBE* createCBE(const std::string &ProgramPath, std::string &Message);
82 static LLC *createLLC(const std::string &ProgramPath, std::string &Message);
84 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
85 std::string &Message);
87 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
88 std::string &Message);
91 virtual ~AbstractInterpreter() {}
93 /// compileProgram - Compile the specified program from bytecode to executable
94 /// code. This does not produce any output, it is only used when debugging
95 /// the code generator. If the code generator fails, an exception should be
96 /// thrown, otherwise, this function will just return.
97 virtual void compileProgram(const std::string &Bytecode) {}
99 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
100 /// specified filename. This returns the exit code of the program.
102 virtual int ExecuteProgram(const std::string &Bytecode,
103 const std::vector<std::string> &Args,
104 const std::string &InputFile,
105 const std::string &OutputFile,
106 const std::vector<std::string> &SharedLibs =
107 std::vector<std::string>()) = 0;
110 //===---------------------------------------------------------------------===//
111 // CBE Implementation of AbstractIntepreter interface
113 class CBE : public AbstractInterpreter {
114 std::string LLCPath; // The path to the `llc' executable
117 CBE(const std::string &llcPath, GCC *Gcc) : LLCPath(llcPath), gcc(Gcc) { }
118 ~CBE() { delete gcc; }
120 /// compileProgram - Compile the specified program from bytecode to executable
121 /// code. This does not produce any output, it is only used when debugging
122 /// the code generator. If the code generator fails, an exception should be
123 /// thrown, otherwise, this function will just return.
124 virtual void compileProgram(const std::string &Bytecode);
126 virtual int ExecuteProgram(const std::string &Bytecode,
127 const std::vector<std::string> &Args,
128 const std::string &InputFile,
129 const std::string &OutputFile,
130 const std::vector<std::string> &SharedLibs =
131 std::vector<std::string>());
133 // Sometimes we just want to go half-way and only generate the .c file, not
134 // necessarily compile it with GCC and run the program. This throws an
135 // exception if LLC crashes.
137 virtual void OutputC(const std::string &Bytecode, std::string &OutputCFile);
141 //===---------------------------------------------------------------------===//
142 // LLC Implementation of AbstractIntepreter interface
144 class LLC : public AbstractInterpreter {
145 std::string LLCPath; // The path to the LLC executable
148 LLC(const std::string &llcPath, GCC *Gcc)
149 : LLCPath(llcPath), gcc(Gcc) { }
150 ~LLC() { delete gcc; }
153 /// compileProgram - Compile the specified program from bytecode to executable
154 /// code. This does not produce any output, it is only used when debugging
155 /// the code generator. If the code generator fails, an exception should be
156 /// thrown, otherwise, this function will just return.
157 virtual void compileProgram(const std::string &Bytecode);
159 virtual int ExecuteProgram(const std::string &Bytecode,
160 const std::vector<std::string> &Args,
161 const std::string &InputFile,
162 const std::string &OutputFile,
163 const std::vector<std::string> &SharedLibs =
164 std::vector<std::string>());
166 // Sometimes we just want to go half-way and only generate the .s file,
167 // not necessarily compile it all the way and run the program. This throws
168 // an exception if execution of LLC fails.
170 void OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
173 } // End llvm namespace