blob: 1ebccd860a0a166c3027962572ab167cc0f70685 [file] [log] [blame]
Chris Lattnerf1b20d82006-06-06 22:30:59 +00001//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
Misha Brukman63b3afa2005-04-21 20:48:15 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// 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.
Misha Brukman63b3afa2005-04-21 20:48:15 +00007//
John Criswell6fbcc262003-10-20 20:19:47 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf4744492003-09-30 18:28:53 +00009//
Chris Lattner7915a1e2003-10-14 21:34:11 +000010// 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.
Chris Lattnerf4744492003-09-30 18:28:53 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerf1b20d82006-06-06 22:30:59 +000017#ifndef BUGPOINT_TOOLRUNNER_H
18#define BUGPOINT_TOOLRUNNER_H
Misha Brukman29afb642003-09-29 22:38:57 +000019
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/SystemUtils.h"
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000021#include <exception>
Misha Brukman29afb642003-09-29 22:38:57 +000022#include <vector>
23
Brian Gaeked0fde302003-11-11 22:41:34 +000024namespace llvm {
25
Chris Lattner7915a1e2003-10-14 21:34:11 +000026class CBE;
27class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000028
Chris Lattner8c56be52004-02-18 20:21:57 +000029/// 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///
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000033class ToolExecutionError : std::exception {
Chris Lattner8c56be52004-02-18 20:21:57 +000034 std::string Message;
35public:
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000036 explicit ToolExecutionError(const std::string &M) : Message(M) {}
37 virtual ~ToolExecutionError() throw();
38 virtual const char* what() const throw() { return Message.c_str(); }
Chris Lattner8c56be52004-02-18 20:21:57 +000039};
40
41
Misha Brukman29afb642003-09-29 22:38:57 +000042//===---------------------------------------------------------------------===//
43// GCC abstraction
44//
Misha Brukman29afb642003-09-29 22:38:57 +000045class GCC {
Reid Spencer2418bf92004-12-19 17:59:45 +000046 sys::Path GCCPath; // The path to the gcc executable
47 GCC(const sys::Path &gccPath) : GCCPath(gccPath) { }
Chris Lattner7915a1e2003-10-14 21:34:11 +000048public:
49 enum FileType { AsmFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000050
Chris Lattner130e2a32006-06-27 20:35:36 +000051 static GCC *create(const std::string &ProgramPath, std::string &Message);
Chris Lattner7915a1e2003-10-14 21:34:11 +000052
Chris Lattnereeed9832003-10-14 21:52:52 +000053 /// 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 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000060 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,
Reid Spencer51ab5c82006-06-06 00:00:42 +000065 const std::vector<std::string> &GCCArgs =
66 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000067 unsigned Timeout = 0,
68 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000069
Chris Lattnereeed9832003-10-14 21:52:52 +000070 /// 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 Lattner130e2a32006-06-27 20:35:36 +000074 std::string &OutputFile,
75 const std::vector<std::string> &ArgsForGCC);
Misha Brukman29afb642003-09-29 22:38:57 +000076};
77
Misha Brukman29afb642003-09-29 22:38:57 +000078
Chris Lattner7915a1e2003-10-14 21:34:11 +000079//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000080/// 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///
Jeff Cohen83881952005-01-22 16:30:58 +000084class AbstractInterpreter {
85public:
Brian Gaeked11577b2004-05-04 21:09:01 +000086 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);
Chris Lattner7915a1e2003-10-14 21:34:11 +000090
91 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000092 std::string &Message,
93 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000094
95 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000096 std::string &Message,
97 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000098
Misha Brukman29afb642003-09-29 22:38:57 +000099
100 virtual ~AbstractInterpreter() {}
101
Chris Lattnerf03715c2004-02-18 23:24:29 +0000102 /// 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 Lattnerc600f3c2006-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
Misha Brukman29afb642003-09-29 22:38:57 +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,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000121 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000122 const std::string &InputFile,
123 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000124 const std::vector<std::string> &GCCArgs =
125 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000126 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000127 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000128 unsigned Timeout = 0,
129 unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000130};
131
132//===---------------------------------------------------------------------===//
133// CBE Implementation of AbstractIntepreter interface
134//
135class CBE : public AbstractInterpreter {
Reid Spencer2418bf92004-12-19 17:59:45 +0000136 sys::Path LLCPath; // The path to the `llc' executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000137 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000138 GCC *gcc;
139public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000140 CBE(const sys::Path &llcPath, GCC *Gcc,
Brian Gaeked11577b2004-05-04 21:09:01 +0000141 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
142 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000143 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000144 }
Misha Brukman29afb642003-09-29 22:38:57 +0000145 ~CBE() { delete gcc; }
146
Chris Lattnerf03715c2004-02-18 23:24:29 +0000147 /// 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
Misha Brukman29afb642003-09-29 22:38:57 +0000153 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000154 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000155 const std::string &InputFile,
156 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000157 const std::vector<std::string> &GCCArgs =
158 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000159 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000160 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000161 unsigned Timeout = 0,
162 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000163
Chris Lattnerc600f3c2006-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);
Misha Brukman29afb642003-09-29 22:38:57 +0000170};
171
Misha Brukman29afb642003-09-29 22:38:57 +0000172
173//===---------------------------------------------------------------------===//
174// LLC Implementation of AbstractIntepreter interface
175//
176class LLC : public AbstractInterpreter {
177 std::string LLCPath; // The path to the LLC executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000178 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000179 GCC *gcc;
180public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000181 LLC(const std::string &llcPath, GCC *Gcc,
182 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
183 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000184 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000185 }
Misha Brukman29afb642003-09-29 22:38:57 +0000186 ~LLC() { delete gcc; }
187
Chris Lattnerf03715c2004-02-18 23:24:29 +0000188 /// 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
Misha Brukman29afb642003-09-29 22:38:57 +0000194 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000195 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000196 const std::string &InputFile,
197 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000198 const std::vector<std::string> &GCCArgs =
199 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000200 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000201 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000202 unsigned Timeout = 0,
203 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000204
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000205 virtual GCC::FileType OutputCode(const std::string &Bytecode,
206 sys::Path &OutFile);
207
Misha Brukman29afb642003-09-29 22:38:57 +0000208};
209
Brian Gaeked0fde302003-11-11 22:41:34 +0000210} // End llvm namespace
211
Misha Brukman29afb642003-09-29 22:38:57 +0000212#endif