1 //===-- Support/ToolRunner.h ------------------------------------*- C++ -*-===//
3 // This file exposes an abstraction around a platform C compiler, used to
4 // compile C and assembly code. It also exposes an "AbstractIntepreter"
5 // interface, which is used to execute code using one of the LLVM execution
8 //===----------------------------------------------------------------------===//
13 #include "Support/SystemUtils.h"
19 //===---------------------------------------------------------------------===//
23 std::string GCCPath; // The path to the gcc executable
24 GCC(const std::string &gccPath) : GCCPath(gccPath) { }
26 enum FileType { AsmFile, CFile };
28 static GCC* create(const std::string &ProgramPath, std::string &Message);
30 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
31 /// either a .s file, or a .c file, specified by FileType), with the specified
32 /// arguments. Standard input is specified with InputFile, and standard
33 /// Output is captured to the specified OutputFile location. The SharedLibs
34 /// option specifies optional native shared objects that can be loaded into
35 /// the program for execution.
37 int ExecuteProgram(const std::string &ProgramFile,
38 const std::vector<std::string> &Args,
40 const std::string &InputFile,
41 const std::string &OutputFile,
42 const std::vector<std::string> &SharedLibs =
43 std::vector<std::string>());
45 /// MakeSharedObject - This compiles the specified file (which is either a .c
46 /// file or a .s file) into a shared object.
48 int MakeSharedObject(const std::string &InputFile, FileType fileType,
49 std::string &OutputFile);
52 void ProcessFailure(const char **Args);
56 //===---------------------------------------------------------------------===//
57 /// AbstractInterpreter Class - Subclasses of this class are used to execute
58 /// LLVM bytecode in a variety of ways. This abstract interface hides this
59 /// complexity behind a simple interface.
61 struct AbstractInterpreter {
62 static CBE* createCBE(const std::string &ProgramPath, std::string &Message);
63 static LLC *createLLC(const std::string &ProgramPath, std::string &Message);
65 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
66 std::string &Message);
68 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
69 std::string &Message);
72 virtual ~AbstractInterpreter() {}
74 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
75 /// specified filename. This returns the exit code of the program.
77 virtual int ExecuteProgram(const std::string &Bytecode,
78 const std::vector<std::string> &Args,
79 const std::string &InputFile,
80 const std::string &OutputFile,
81 const std::vector<std::string> &SharedLibs =
82 std::vector<std::string>()) = 0;
85 //===---------------------------------------------------------------------===//
86 // CBE Implementation of AbstractIntepreter interface
88 class CBE : public AbstractInterpreter {
89 std::string DISPath; // The path to the `llvm-dis' executable
92 CBE(const std::string &disPath, GCC *Gcc) : DISPath(disPath), gcc(Gcc) { }
93 ~CBE() { delete gcc; }
95 virtual int ExecuteProgram(const std::string &Bytecode,
96 const std::vector<std::string> &Args,
97 const std::string &InputFile,
98 const std::string &OutputFile,
99 const std::vector<std::string> &SharedLibs =
100 std::vector<std::string>());
102 // Sometimes we just want to go half-way and only generate the .c file,
103 // not necessarily compile it with GCC and run the program.
105 virtual int OutputC(const std::string &Bytecode, std::string &OutputCFile);
109 //===---------------------------------------------------------------------===//
110 // LLC Implementation of AbstractIntepreter interface
112 class LLC : public AbstractInterpreter {
113 std::string LLCPath; // The path to the LLC executable
116 LLC(const std::string &llcPath, GCC *Gcc)
117 : LLCPath(llcPath), gcc(Gcc) { }
118 ~LLC() { delete gcc; }
120 virtual int ExecuteProgram(const std::string &Bytecode,
121 const std::vector<std::string> &Args,
122 const std::string &InputFile,
123 const std::string &OutputFile,
124 const std::vector<std::string> &SharedLibs =
125 std::vector<std::string>());
127 // Sometimes we just want to go half-way and only generate the .s file,
128 // not necessarily compile it all the way and run the program.
130 int OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);