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