blob: cba10f214a90c5ff4e8cbdf994e7ece46f01807c [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//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Daniel Dunbarca740962009-08-18 03:35:57 +000020#include "llvm/ADT/Triple.h"
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000021#include "llvm/Support/CommandLine.h"
Nick Lewycky22ff7482010-04-12 05:08:25 +000022#include "llvm/Support/ErrorHandling.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/SystemUtils.h"
Chris Lattnerb683ea42009-08-23 21:36:09 +000024#include "llvm/System/Path.h"
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000025#include <exception>
Misha Brukman29afb642003-09-29 22:38:57 +000026#include <vector>
27
Brian Gaeked0fde302003-11-11 22:41:34 +000028namespace llvm {
29
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000030extern cl::opt<bool> SaveTemps;
Daniel Dunbarca740962009-08-18 03:35:57 +000031extern Triple TargetTriple;
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000032
Chris Lattner7915a1e2003-10-14 21:34:11 +000033class CBE;
34class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000035
36//===---------------------------------------------------------------------===//
37// GCC abstraction
38//
Misha Brukman29afb642003-09-29 22:38:57 +000039class GCC {
Bill Wendling38efa382009-03-02 23:13:18 +000040 sys::Path GCCPath; // The path to the gcc executable.
41 sys::Path RemoteClientPath; // The path to the rsh / ssh executable.
42 std::vector<std::string> gccArgs; // GCC-specific arguments.
43 GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
44 const std::vector<std::string> *GCCArgs)
45 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
46 if (GCCArgs) gccArgs = *GCCArgs;
47 }
Chris Lattner7915a1e2003-10-14 21:34:11 +000048public:
Chris Lattner50010422010-03-16 06:41:47 +000049 enum FileType { AsmFile, ObjectFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000050
Dan Gohman197f7282009-08-05 20:21:17 +000051 static GCC *create(std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +000052 const std::vector<std::string> *Args);
Chris Lattner7915a1e2003-10-14 21:34:11 +000053
Chris Lattnereeed9832003-10-14 21:52:52 +000054 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
55 /// either a .s file, or a .c file, specified by FileType), with the specified
56 /// arguments. Standard input is specified with InputFile, and standard
57 /// Output is captured to the specified OutputFile location. The SharedLibs
58 /// option specifies optional native shared objects that can be loaded into
59 /// the program for execution.
60 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000061 int ExecuteProgram(const std::string &ProgramFile,
62 const std::vector<std::string> &Args,
63 FileType fileType,
64 const std::string &InputFile,
65 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +000066 std::string *Error = 0,
Reid Spencer51ab5c82006-06-06 00:00:42 +000067 const std::vector<std::string> &GCCArgs =
68 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000069 unsigned Timeout = 0,
70 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000071
Chris Lattnereeed9832003-10-14 21:52:52 +000072 /// MakeSharedObject - This compiles the specified file (which is either a .c
73 /// file or a .s file) into a shared object.
74 ///
75 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +000076 std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +000077 const std::vector<std::string> &ArgsForGCC,
78 std::string &Error);
Misha Brukman29afb642003-09-29 22:38:57 +000079};
80
Misha Brukman29afb642003-09-29 22:38:57 +000081
Chris Lattner7915a1e2003-10-14 21:34:11 +000082//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000083/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif8ff70c22007-07-04 21:55:50 +000084/// LLVM bitcode in a variety of ways. This abstract interface hides this
Misha Brukman29afb642003-09-29 22:38:57 +000085/// complexity behind a simple interface.
86///
Jeff Cohen83881952005-01-22 16:30:58 +000087class AbstractInterpreter {
88public:
Dan Gohman197f7282009-08-05 20:21:17 +000089 static CBE *createCBE(const char *Argv0, std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +000090 const std::vector<std::string> *Args = 0,
91 const std::vector<std::string> *GCCArgs = 0);
Dan Gohman197f7282009-08-05 20:21:17 +000092 static LLC *createLLC(const char *Argv0, std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +000093 const std::vector<std::string> *Args = 0,
Chris Lattner50010422010-03-16 06:41:47 +000094 const std::vector<std::string> *GCCArgs = 0,
95 bool UseIntegratedAssembler = false);
Chris Lattner7915a1e2003-10-14 21:34:11 +000096
Dan Gohman197f7282009-08-05 20:21:17 +000097 static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +000098 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000099
Dan Gohman197f7282009-08-05 20:21:17 +0000100 static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
Brian Gaeked11577b2004-05-04 21:09:01 +0000101 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000102
Dan Gohman197f7282009-08-05 20:21:17 +0000103 static AbstractInterpreter* createCustom(std::string &Message,
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000104 const std::string &ExecCommandLine);
105
Misha Brukman29afb642003-09-29 22:38:57 +0000106
107 virtual ~AbstractInterpreter() {}
108
Gabor Greif8ff70c22007-07-04 21:55:50 +0000109 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000110 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000111 /// the code generator. It returns false if the code generator fails.
112 virtual void compileProgram(const std::string &Bitcode, std::string *Error) {}
Chris Lattnerf03715c2004-02-18 23:24:29 +0000113
Gabor Greif8ff70c22007-07-04 21:55:50 +0000114 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000115 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000116 /// fails, it sets Error, otherwise, this function returns the type of code
117 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000118 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000119 sys::Path &OutFile, std::string &Error) {
120 Error = "OutputCode not supported by this AbstractInterpreter!";
121 return GCC::AsmFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000122 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000123
Gabor Greif8ff70c22007-07-04 21:55:50 +0000124 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky22ff7482010-04-12 05:08:25 +0000125 /// specified filename. This sets RetVal to the exit code of the program or
126 /// returns false if a problem was encountered that prevented execution of
127 /// the program.
Misha Brukman29afb642003-09-29 22:38:57 +0000128 ///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000129 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000130 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000131 const std::string &InputFile,
132 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000133 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000134 const std::vector<std::string> &GCCArgs =
135 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000136 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000137 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000138 unsigned Timeout = 0,
139 unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000140};
141
142//===---------------------------------------------------------------------===//
143// CBE Implementation of AbstractIntepreter interface
144//
145class CBE : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000146 sys::Path LLCPath; // The path to the `llc' executable.
147 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Misha Brukman29afb642003-09-29 22:38:57 +0000148 GCC *gcc;
149public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000150 CBE(const sys::Path &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000151 const std::vector<std::string> *Args)
152 : LLCPath(llcPath), gcc(Gcc) {
Brian Gaeked11577b2004-05-04 21:09:01 +0000153 ToolArgs.clear ();
Bill Wendling38efa382009-03-02 23:13:18 +0000154 if (Args) ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000155 }
Misha Brukman29afb642003-09-29 22:38:57 +0000156 ~CBE() { delete gcc; }
157
Gabor Greif8ff70c22007-07-04 21:55:50 +0000158 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000159 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000160 /// the code generator. Returns false if the code generator fails.
161 virtual void compileProgram(const std::string &Bitcode, std::string *Error);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000162
Gabor Greif8ff70c22007-07-04 21:55:50 +0000163 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000164 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000165 const std::string &InputFile,
166 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000167 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000168 const std::vector<std::string> &GCCArgs =
169 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000170 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000171 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000172 unsigned Timeout = 0,
173 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000174
Gabor Greif8ff70c22007-07-04 21:55:50 +0000175 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000176 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky22ff7482010-04-12 05:08:25 +0000177 /// fails, it sets Error, otherwise, this function returns the type of code
178 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000179 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000180 sys::Path &OutFile, std::string &Error);
Misha Brukman29afb642003-09-29 22:38:57 +0000181};
182
Misha Brukman29afb642003-09-29 22:38:57 +0000183
184//===---------------------------------------------------------------------===//
185// LLC Implementation of AbstractIntepreter interface
186//
187class LLC : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000188 std::string LLCPath; // The path to the LLC executable.
189 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
190 std::vector<std::string> gccArgs; // Extra args to pass to GCC.
Misha Brukman29afb642003-09-29 22:38:57 +0000191 GCC *gcc;
Chris Lattner50010422010-03-16 06:41:47 +0000192 bool UseIntegratedAssembler;
Misha Brukman29afb642003-09-29 22:38:57 +0000193public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000194 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendling38efa382009-03-02 23:13:18 +0000195 const std::vector<std::string> *Args,
Chris Lattner50010422010-03-16 06:41:47 +0000196 const std::vector<std::string> *GCCArgs,
197 bool useIntegratedAssembler)
198 : LLCPath(llcPath), gcc(Gcc),
199 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendling38efa382009-03-02 23:13:18 +0000200 ToolArgs.clear();
201 if (Args) ToolArgs = *Args;
202 if (GCCArgs) gccArgs = *GCCArgs;
Brian Gaeked11577b2004-05-04 21:09:01 +0000203 }
Misha Brukman29afb642003-09-29 22:38:57 +0000204 ~LLC() { delete gcc; }
205
Gabor Greif8ff70c22007-07-04 21:55:50 +0000206 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000207 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000208 /// the code generator. Returns false if the code generator fails.
209 virtual void compileProgram(const std::string &Bitcode, std::string *Error);
Chris Lattnerf03715c2004-02-18 23:24:29 +0000210
Gabor Greif8ff70c22007-07-04 21:55:50 +0000211 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000212 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000213 const std::string &InputFile,
214 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000215 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000216 const std::vector<std::string> &GCCArgs =
217 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000218 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000219 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000220 unsigned Timeout = 0,
221 unsigned MemoryLimit = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000222
Nick Lewycky22ff7482010-04-12 05:08:25 +0000223 /// OutputCode - Compile the specified program from bitcode to code
224 /// understood by the GCC driver (either C or asm). If the code generator
225 /// fails, it sets Error, otherwise, this function returns the type of code
226 /// emitted.
Gabor Greif8ff70c22007-07-04 21:55:50 +0000227 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000228 sys::Path &OutFile, std::string &Error);
Misha Brukman29afb642003-09-29 22:38:57 +0000229};
230
Brian Gaeked0fde302003-11-11 22:41:34 +0000231} // End llvm namespace
232
Misha Brukman29afb642003-09-29 22:38:57 +0000233#endif