blob: 1ebccd860a0a166c3027962572ab167cc0f70685 [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>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +000067 unsigned Timeout = 0,
68 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +000069
70 /// MakeSharedObject - This compiles the specified file (which is either a .c
71 /// file or a .s file) into a shared object.
72 ///
73 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattnercc21fa72006-06-27 20:35:36 +000074 std::string &OutputFile,
75 const std::vector<std::string> &ArgsForGCC);
Chris Lattnerffac2862006-06-06 22:30:59 +000076};
77
78
79//===---------------------------------------------------------------------===//
80/// AbstractInterpreter Class - Subclasses of this class are used to execute
81/// LLVM bytecode in a variety of ways. This abstract interface hides this
82/// complexity behind a simple interface.
83///
84class AbstractInterpreter {
85public:
86 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
87 const std::vector<std::string> *Args = 0);
88 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
89 const std::vector<std::string> *Args = 0);
90
91 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
92 std::string &Message,
93 const std::vector<std::string> *Args=0);
94
95 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
96 std::string &Message,
97 const std::vector<std::string> *Args=0);
98
99
100 virtual ~AbstractInterpreter() {}
101
102 /// compileProgram - Compile the specified program from bytecode to executable
103 /// code. This does not produce any output, it is only used when debugging
104 /// the code generator. If the code generator fails, an exception should be
105 /// thrown, otherwise, this function will just return.
106 virtual void compileProgram(const std::string &Bytecode) {}
107
Chris Lattner634bc042006-09-15 21:29:15 +0000108 /// OutputCode - Compile the specified program from bytecode to code
109 /// understood by the GCC driver (either C or asm). If the code generator
110 /// fails, an exception should be thrown, otherwise, this function returns the
111 /// type of code emitted.
112 virtual GCC::FileType OutputCode(const std::string &Bytecode,
113 sys::Path &OutFile) {
114 throw std::string("OutputCode not supported by this AbstractInterpreter!");
115 }
116
Chris Lattnerffac2862006-06-06 22:30:59 +0000117 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
118 /// specified filename. This returns the exit code of the program.
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> &GCCArgs =
125 std::vector<std::string>(),
126 const std::vector<std::string> &SharedLibs =
127 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000128 unsigned Timeout = 0,
129 unsigned MemoryLimit = 0) = 0;
Chris Lattnerffac2862006-06-06 22:30:59 +0000130};
131
132//===---------------------------------------------------------------------===//
133// CBE Implementation of AbstractIntepreter interface
134//
135class CBE : public AbstractInterpreter {
136 sys::Path LLCPath; // The path to the `llc' executable
137 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
138 GCC *gcc;
139public:
140 CBE(const sys::Path &llcPath, GCC *Gcc,
141 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
142 ToolArgs.clear ();
143 if (Args) { ToolArgs = *Args; }
144 }
145 ~CBE() { delete gcc; }
146
147 /// compileProgram - Compile the specified program from bytecode to executable
148 /// code. This does not produce any output, it is only used when debugging
149 /// the code generator. If the code generator fails, an exception should be
150 /// thrown, otherwise, this function will just return.
151 virtual void compileProgram(const std::string &Bytecode);
152
153 virtual int ExecuteProgram(const std::string &Bytecode,
154 const std::vector<std::string> &Args,
155 const std::string &InputFile,
156 const std::string &OutputFile,
157 const std::vector<std::string> &GCCArgs =
158 std::vector<std::string>(),
159 const std::vector<std::string> &SharedLibs =
160 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000161 unsigned Timeout = 0,
162 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000163
Chris Lattner634bc042006-09-15 21:29:15 +0000164 /// OutputCode - Compile the specified program from bytecode to code
165 /// understood by the GCC driver (either C or asm). If the code generator
166 /// fails, an exception should be thrown, otherwise, this function returns the
167 /// type of code emitted.
168 virtual GCC::FileType OutputCode(const std::string &Bytecode,
169 sys::Path &OutFile);
Chris Lattnerffac2862006-06-06 22:30:59 +0000170};
171
172
173//===---------------------------------------------------------------------===//
174// LLC Implementation of AbstractIntepreter interface
175//
176class LLC : public AbstractInterpreter {
177 std::string LLCPath; // The path to the LLC executable
178 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
179 GCC *gcc;
180public:
181 LLC(const std::string &llcPath, GCC *Gcc,
182 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
183 ToolArgs.clear ();
184 if (Args) { ToolArgs = *Args; }
185 }
186 ~LLC() { delete gcc; }
187
188 /// compileProgram - Compile the specified program from bytecode to executable
189 /// code. This does not produce any output, it is only used when debugging
190 /// the code generator. If the code generator fails, an exception should be
191 /// thrown, otherwise, this function will just return.
192 virtual void compileProgram(const std::string &Bytecode);
193
194 virtual int ExecuteProgram(const std::string &Bytecode,
195 const std::vector<std::string> &Args,
196 const std::string &InputFile,
197 const std::string &OutputFile,
198 const std::vector<std::string> &GCCArgs =
199 std::vector<std::string>(),
200 const std::vector<std::string> &SharedLibs =
201 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000202 unsigned Timeout = 0,
203 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +0000204
Chris Lattner634bc042006-09-15 21:29:15 +0000205 virtual GCC::FileType OutputCode(const std::string &Bytecode,
206 sys::Path &OutFile);
207
Chris Lattnerffac2862006-06-06 22:30:59 +0000208};
209
210} // End llvm namespace
211
212#endif