blob: 9d2903cc643abfb4a227cc39b2616150ad4c2fd1 [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 Lattner7915a1e2003-10-14 21:34:11 +000051 static GCC* create(const std::string &ProgramPath, std::string &Message);
52
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>(),
67 unsigned Timeout = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000068
Chris Lattnereeed9832003-10-14 21:52:52 +000069 /// 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,
Misha Brukman29afb642003-09-29 22:38:57 +000073 std::string &OutputFile);
Misha Brukman29afb642003-09-29 22:38:57 +000074};
75
Misha Brukman29afb642003-09-29 22:38:57 +000076
Chris Lattner7915a1e2003-10-14 21:34:11 +000077//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000078/// AbstractInterpreter Class - Subclasses of this class are used to execute
79/// LLVM bytecode in a variety of ways. This abstract interface hides this
80/// complexity behind a simple interface.
81///
Jeff Cohen83881952005-01-22 16:30:58 +000082class AbstractInterpreter {
83public:
Brian Gaeked11577b2004-05-04 21:09:01 +000084 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
85 const std::vector<std::string> *Args = 0);
86 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
87 const std::vector<std::string> *Args = 0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000088
89 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000090 std::string &Message,
91 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000092
93 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000094 std::string &Message,
95 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000096
Misha Brukman29afb642003-09-29 22:38:57 +000097
98 virtual ~AbstractInterpreter() {}
99
Chris Lattnerf03715c2004-02-18 23:24:29 +0000100 /// compileProgram - Compile the specified program from bytecode to executable
101 /// code. This does not produce any output, it is only used when debugging
102 /// the code generator. If the code generator fails, an exception should be
103 /// thrown, otherwise, this function will just return.
104 virtual void compileProgram(const std::string &Bytecode) {}
105
Misha Brukman29afb642003-09-29 22:38:57 +0000106 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
107 /// specified filename. This returns the exit code of the program.
108 ///
109 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000110 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000111 const std::string &InputFile,
112 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000113 const std::vector<std::string> &GCCArgs =
114 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000115 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000116 std::vector<std::string>(),
117 unsigned Timeout = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000118};
119
120//===---------------------------------------------------------------------===//
121// CBE Implementation of AbstractIntepreter interface
122//
123class CBE : public AbstractInterpreter {
Reid Spencer2418bf92004-12-19 17:59:45 +0000124 sys::Path LLCPath; // The path to the `llc' executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000125 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000126 GCC *gcc;
127public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000128 CBE(const sys::Path &llcPath, GCC *Gcc,
Brian Gaeked11577b2004-05-04 21:09:01 +0000129 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
130 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000131 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000132 }
Misha Brukman29afb642003-09-29 22:38:57 +0000133 ~CBE() { delete gcc; }
134
Chris Lattnerf03715c2004-02-18 23:24:29 +0000135 /// compileProgram - Compile the specified program from bytecode to executable
136 /// code. This does not produce any output, it is only used when debugging
137 /// the code generator. If the code generator fails, an exception should be
138 /// thrown, otherwise, this function will just return.
139 virtual void compileProgram(const std::string &Bytecode);
140
Misha Brukman29afb642003-09-29 22:38:57 +0000141 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000142 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000143 const std::string &InputFile,
144 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000145 const std::vector<std::string> &GCCArgs =
146 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000147 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000148 std::vector<std::string>(),
149 unsigned Timeout = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000150
Chris Lattner8c56be52004-02-18 20:21:57 +0000151 // Sometimes we just want to go half-way and only generate the .c file, not
152 // necessarily compile it with GCC and run the program. This throws an
153 // exception if LLC crashes.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000154 //
Reid Spencer9ac14182004-12-16 23:01:34 +0000155 virtual void OutputC(const std::string &Bytecode, sys::Path& OutputCFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000156};
157
Misha Brukman29afb642003-09-29 22:38:57 +0000158
159//===---------------------------------------------------------------------===//
160// LLC Implementation of AbstractIntepreter interface
161//
162class LLC : public AbstractInterpreter {
163 std::string LLCPath; // The path to the LLC executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000164 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000165 GCC *gcc;
166public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000167 LLC(const std::string &llcPath, GCC *Gcc,
168 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
169 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000170 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000171 }
Misha Brukman29afb642003-09-29 22:38:57 +0000172 ~LLC() { delete gcc; }
173
Chris Lattnerf03715c2004-02-18 23:24:29 +0000174 /// compileProgram - Compile the specified program from bytecode to executable
175 /// code. This does not produce any output, it is only used when debugging
176 /// the code generator. If the code generator fails, an exception should be
177 /// thrown, otherwise, this function will just return.
178 virtual void compileProgram(const std::string &Bytecode);
179
Misha Brukman29afb642003-09-29 22:38:57 +0000180 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000181 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000182 const std::string &InputFile,
183 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000184 const std::vector<std::string> &GCCArgs =
185 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000186 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000187 std::vector<std::string>(),
188 unsigned Timeout = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000189
Chris Lattner7915a1e2003-10-14 21:34:11 +0000190 // Sometimes we just want to go half-way and only generate the .s file,
Chris Lattner8c56be52004-02-18 20:21:57 +0000191 // not necessarily compile it all the way and run the program. This throws
192 // an exception if execution of LLC fails.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000193 //
Reid Spencer9ac14182004-12-16 23:01:34 +0000194 void OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000195};
196
Brian Gaeked0fde302003-11-11 22:41:34 +0000197} // End llvm namespace
198
Misha Brukman29afb642003-09-29 22:38:57 +0000199#endif