Add a file header
[oota-llvm.git] / include / Support / ToolRunner.h
1 //===-- Support/ToolRunner.h ------------------------------------*- C++ -*-===//
2 //
3 // FIXME: document
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef TOOLRUNNER_H
8 #define TOOLRUNNER_H
9
10 #include "Support/CommandLine.h"
11 #include "Support/SystemUtils.h"
12 #include <fstream>
13 #include <iostream>
14 #include <string>
15 #include <vector>
16
17 enum FileType { AsmFile, CFile };
18
19 //===---------------------------------------------------------------------===//
20 // GCC abstraction
21 //
22 // This is not a *real* AbstractInterpreter as it does not accept bytecode
23 // files, but only input acceptable to GCC, i.e. C, C++, and assembly files
24 //
25 class GCC {
26   std::string GCCPath;          // The path to the gcc executable
27 public:
28   GCC(const std::string &gccPath) : GCCPath(gccPath) { }
29   virtual ~GCC() {}
30
31   virtual int ExecuteProgram(const std::string &ProgramFile,
32                              const cl::list<std::string> &Args,
33                              FileType fileType,
34                              const std::string &InputFile,
35                              const std::string &OutputFile,
36                              const std::string &SharedLib = "");
37
38   int MakeSharedObject(const std::string &InputFile,
39                        FileType fileType,
40                        std::string &OutputFile);
41   
42   void ProcessFailure(const char **Args);
43 };
44
45 GCC* createGCCtool(const std::string &ProgramPath,
46                    std::string &Message);
47
48 /// AbstractInterpreter Class - Subclasses of this class are used to execute
49 /// LLVM bytecode in a variety of ways.  This abstract interface hides this
50 /// complexity behind a simple interface.
51 ///
52 struct AbstractInterpreter {
53
54   virtual ~AbstractInterpreter() {}
55
56   /// ExecuteProgram - Run the specified bytecode file, emitting output to the
57   /// specified filename.  This returns the exit code of the program.
58   ///
59   virtual int ExecuteProgram(const std::string &Bytecode,
60                              const cl::list<std::string> &Args,
61                              const std::string &InputFile,
62                              const std::string &OutputFile,
63                              const std::string &SharedLib = "") = 0;
64 };
65
66 //===---------------------------------------------------------------------===//
67 // CBE Implementation of AbstractIntepreter interface
68 //
69 class CBE : public AbstractInterpreter {
70   std::string DISPath;          // The path to the `llvm-dis' executable
71   GCC *gcc;
72 public:
73   CBE(const std::string &disPath, GCC *Gcc) : DISPath(disPath), gcc(Gcc) { }
74   ~CBE() { delete gcc; }
75
76   virtual int ExecuteProgram(const std::string &Bytecode,
77                              const cl::list<std::string> &Args,
78                              const std::string &InputFile,
79                              const std::string &OutputFile,
80                              const std::string &SharedLib = "");
81
82   // Sometimes we just want to go half-way and only generate the C file,
83   // not necessarily compile it with GCC and run the program
84   virtual int OutputC(const std::string &Bytecode,
85                       std::string &OutputCFile);
86
87 };
88
89 CBE* createCBEtool(const std::string &ProgramPath, std::string &Message);
90
91 //===---------------------------------------------------------------------===//
92 // LLC Implementation of AbstractIntepreter interface
93 //
94 class LLC : public AbstractInterpreter {
95   std::string LLCPath;          // The path to the LLC executable
96   GCC *gcc;
97 public:
98   LLC(const std::string &llcPath, GCC *Gcc)
99     : LLCPath(llcPath), gcc(Gcc) { }
100   ~LLC() { delete gcc; }
101
102   virtual int ExecuteProgram(const std::string &Bytecode,
103                              const cl::list<std::string> &Args,
104                              const std::string &InputFile,
105                              const std::string &OutputFile,
106                              const std::string &SharedLib = "");
107
108   int OutputAsm(const std::string &Bytecode,
109                 std::string &OutputAsmFile);
110 };
111
112 LLC* createLLCtool(const std::string &ProgramPath, std::string &Message);
113
114 AbstractInterpreter* createLLItool(const std::string &ProgramPath,
115                                    std::string &Message);
116
117 AbstractInterpreter* createJITtool(const std::string &ProgramPath,
118                                    std::string &Message);
119
120
121 #endif