3652ff65ed212d1100e006057a54418aa746c539
[oota-llvm.git] / include / llvm / Support / ToolRunner.h
1 //===-- Support/ToolRunner.h ------------------------------------*- C++ -*-===//
2 //
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
6 // engines.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef TOOLRUNNER_H
11 #define TOOLRUNNER_H
12
13 #include "Support/SystemUtils.h"
14 #include <vector>
15
16 class CBE;
17 class LLC;
18
19 //===---------------------------------------------------------------------===//
20 // GCC abstraction
21 //
22 class GCC {
23   std::string GCCPath;          // The path to the gcc executable
24   GCC(const std::string &gccPath) : GCCPath(gccPath) { }
25 public:
26   enum FileType { AsmFile, CFile };
27
28   static GCC* create(const std::string &ProgramPath, std::string &Message);
29
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.
36   ///
37   int ExecuteProgram(const std::string &ProgramFile,
38                      const std::vector<std::string> &Args,
39                      FileType fileType,
40                      const std::string &InputFile,
41                      const std::string &OutputFile,
42                      const std::vector<std::string> &SharedLibs = 
43                          std::vector<std::string>());
44
45   /// MakeSharedObject - This compiles the specified file (which is either a .c
46   /// file or a .s file) into a shared object.
47   ///
48   int MakeSharedObject(const std::string &InputFile, FileType fileType,
49                        std::string &OutputFile);
50   
51 private:
52   void ProcessFailure(const char **Args);
53 };
54
55
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.
60 ///
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);
64
65   static AbstractInterpreter* createLLI(const std::string &ProgramPath,
66                                         std::string &Message);
67
68   static AbstractInterpreter* createJIT(const std::string &ProgramPath,
69                                         std::string &Message);
70
71
72   virtual ~AbstractInterpreter() {}
73
74   /// ExecuteProgram - Run the specified bytecode file, emitting output to the
75   /// specified filename.  This returns the exit code of the program.
76   ///
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;
83 };
84
85 //===---------------------------------------------------------------------===//
86 // CBE Implementation of AbstractIntepreter interface
87 //
88 class CBE : public AbstractInterpreter {
89   std::string DISPath;          // The path to the `llvm-dis' executable
90   GCC *gcc;
91 public:
92   CBE(const std::string &disPath, GCC *Gcc) : DISPath(disPath), gcc(Gcc) { }
93   ~CBE() { delete gcc; }
94
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>());
101
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.
104   //
105   virtual int OutputC(const std::string &Bytecode, std::string &OutputCFile);
106 };
107
108
109 //===---------------------------------------------------------------------===//
110 // LLC Implementation of AbstractIntepreter interface
111 //
112 class LLC : public AbstractInterpreter {
113   std::string LLCPath;          // The path to the LLC executable
114   GCC *gcc;
115 public:
116   LLC(const std::string &llcPath, GCC *Gcc)
117     : LLCPath(llcPath), gcc(Gcc) { }
118   ~LLC() { delete gcc; }
119
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>());
126
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.
129   //
130   int OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
131 };
132
133 #endif