blob: e64d0054f0a71c2796bf43dd694786a935cafdc0 [file] [log] [blame]
Chris Lattnerffac2862006-06-06 22:30:59 +00001//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
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
13// engines.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef BUGPOINT_TOOLRUNNER_H
18#define BUGPOINT_TOOLRUNNER_H
19
20#include "llvm/Support/SystemUtils.h"
21#include <exception>
22#include <vector>
23
24namespace llvm {
25
26class CBE;
27class LLC;
28
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.
32///
33class ToolExecutionError : std::exception {
34 std::string Message;
35public:
36 explicit ToolExecutionError(const std::string &M) : Message(M) {}
37 virtual ~ToolExecutionError() throw();
38 virtual const char* what() const throw() { return Message.c_str(); }
39};
40
41
42//===---------------------------------------------------------------------===//
43// GCC abstraction
44//
45class GCC {
46 sys::Path GCCPath; // The path to the gcc executable
47 GCC(const sys::Path &gccPath) : GCCPath(gccPath) { }
48public:
49 enum FileType { AsmFile, CFile };
50
Chris Lattnercc21fa72006-06-27 20:35:36 +000051 static GCC *create(const std::string &ProgramPath, std::string &Message);
Chris Lattnerffac2862006-06-06 22:30:59 +000052
53 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
54 /// either a .s file, or a .c file, specified by FileType), with the specified
55 /// arguments. Standard input is specified with InputFile, and standard
56 /// Output is captured to the specified OutputFile location. The SharedLibs
57 /// option specifies optional native shared objects that can be loaded into
58 /// the program for execution.
59 ///
60 int ExecuteProgram(const std::string &ProgramFile,
61 const std::vector<std::string> &Args,
62 FileType fileType,
63 const std::string &InputFile,
64 const std::string &OutputFile,
65 const std::vector<std::string> &GCCArgs =
66 std::vector<std::string>(),
67 unsigned Timeout = 0);
68
69 /// MakeSharedObject - This compiles the specified file (which is either a .c
70 /// file or a .s file) into a shared object.
71 ///
72 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattnercc21fa72006-06-27 20:35:36 +000073 std::string &OutputFile,
74 const std::vector<std::string> &ArgsForGCC);
Chris Lattnerffac2862006-06-06 22:30:59 +000075};
76
77
78//===---------------------------------------------------------------------===//
79/// AbstractInterpreter Class - Subclasses of this class are used to execute
80/// LLVM bytecode in a variety of ways. This abstract interface hides this
81/// complexity behind a simple interface.
82///
83class AbstractInterpreter {
84public:
85 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
86 const std::vector<std::string> *Args = 0);
87 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
88 const std::vector<std::string> *Args = 0);
89
90 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
91 std::string &Message,
92 const std::vector<std::string> *Args=0);
93
94 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
95 std::string &Message,
96 const std::vector<std::string> *Args=0);
97
98
99 virtual ~AbstractInterpreter() {}
100
101 /// compileProgram - Compile the specified program from bytecode to executable
102 /// code. This does not produce any output, it is only used when debugging
103 /// the code generator. If the code generator fails, an exception should be
104 /// thrown, otherwise, this function will just return.
105 virtual void compileProgram(const std::string &Bytecode) {}
106
Chris Lattner634bc042006-09-15 21:29:15 +0000107 /// OutputCode - Compile the specified program from bytecode to code
108 /// understood by the GCC driver (either C or asm). If the code generator
109 /// fails, an exception should be thrown, otherwise, this function returns the
110 /// type of code emitted.
111 virtual GCC::FileType OutputCode(const std::string &Bytecode,
112 sys::Path &OutFile) {
113 throw std::string("OutputCode not supported by this AbstractInterpreter!");
114 }
115
Chris Lattnerffac2862006-06-06 22:30:59 +0000116 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
117 /// specified filename. This returns the exit code of the program.
118 ///
119 virtual int ExecuteProgram(const std::string &Bytecode,
120 const std::vector<std::string> &Args,
121 const std::string &InputFile,
122 const std::string &OutputFile,
123 const std::vector<std::string> &GCCArgs =
124 std::vector<std::string>(),
125 const std::vector<std::string> &SharedLibs =
126 std::vector<std::string>(),
127 unsigned Timeout = 0) = 0;
128};
129
130//===---------------------------------------------------------------------===//
131// CBE Implementation of AbstractIntepreter interface
132//
133class CBE : public AbstractInterpreter {
134 sys::Path LLCPath; // The path to the `llc' executable
135 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
136 GCC *gcc;
137public:
138 CBE(const sys::Path &llcPath, GCC *Gcc,
139 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
140 ToolArgs.clear ();
141 if (Args) { ToolArgs = *Args; }
142 }
143 ~CBE() { delete gcc; }
144
145 /// compileProgram - Compile the specified program from bytecode to executable
146 /// code. This does not produce any output, it is only used when debugging
147 /// the code generator. If the code generator fails, an exception should be
148 /// thrown, otherwise, this function will just return.
149 virtual void compileProgram(const std::string &Bytecode);
150
151 virtual int ExecuteProgram(const std::string &Bytecode,
152 const std::vector<std::string> &Args,
153 const std::string &InputFile,
154 const std::string &OutputFile,
155 const std::vector<std::string> &GCCArgs =
156 std::vector<std::string>(),
157 const std::vector<std::string> &SharedLibs =
158 std::vector<std::string>(),
159 unsigned Timeout = 0);
160
Chris Lattner634bc042006-09-15 21:29:15 +0000161 /// OutputCode - Compile the specified program from bytecode to code
162 /// understood by the GCC driver (either C or asm). If the code generator
163 /// fails, an exception should be thrown, otherwise, this function returns the
164 /// type of code emitted.
165 virtual GCC::FileType OutputCode(const std::string &Bytecode,
166 sys::Path &OutFile);
Chris Lattnerffac2862006-06-06 22:30:59 +0000167};
168
169
170//===---------------------------------------------------------------------===//
171// LLC Implementation of AbstractIntepreter interface
172//
173class LLC : public AbstractInterpreter {
174 std::string LLCPath; // The path to the LLC executable
175 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
176 GCC *gcc;
177public:
178 LLC(const std::string &llcPath, GCC *Gcc,
179 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
180 ToolArgs.clear ();
181 if (Args) { ToolArgs = *Args; }
182 }
183 ~LLC() { delete gcc; }
184
185 /// compileProgram - Compile the specified program from bytecode to executable
186 /// code. This does not produce any output, it is only used when debugging
187 /// the code generator. If the code generator fails, an exception should be
188 /// thrown, otherwise, this function will just return.
189 virtual void compileProgram(const std::string &Bytecode);
190
191 virtual int ExecuteProgram(const std::string &Bytecode,
192 const std::vector<std::string> &Args,
193 const std::string &InputFile,
194 const std::string &OutputFile,
195 const std::vector<std::string> &GCCArgs =
196 std::vector<std::string>(),
197 const std::vector<std::string> &SharedLibs =
198 std::vector<std::string>(),
199 unsigned Timeout = 0);
200
Chris Lattner634bc042006-09-15 21:29:15 +0000201 virtual GCC::FileType OutputCode(const std::string &Bytecode,
202 sys::Path &OutFile);
203
Chris Lattnerffac2862006-06-06 22:30:59 +0000204};
205
206} // End llvm namespace
207
208#endif