blob: 454724ace5ce1e8c525049bafe12edc4579ccd5d [file] [log] [blame]
Chris Lattnerffac2862006-06-06 22:30:59 +00001//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerffac2862006-06-06 22:30:59 +00007//
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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000017#ifndef LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
18#define LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
Chris Lattnerffac2862006-06-06 22:30:59 +000019
Daniel Dunbar8575a602009-08-18 03:35:57 +000020#include "llvm/ADT/Triple.h"
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000021#include "llvm/Support/CommandLine.h"
Nick Lewycky6ba630b2010-04-12 05:08:25 +000022#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000023#include "llvm/Support/Path.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000024#include "llvm/Support/SystemUtils.h"
Chris Lattnerffac2862006-06-06 22:30:59 +000025#include <exception>
26#include <vector>
27
28namespace llvm {
29
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000030extern cl::opt<bool> SaveTemps;
Daniel Dunbar8575a602009-08-18 03:35:57 +000031extern Triple TargetTriple;
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000032
Chris Lattnerffac2862006-06-06 22:30:59 +000033class LLC;
34
Chris Lattnerffac2862006-06-06 22:30:59 +000035//===---------------------------------------------------------------------===//
36// GCC abstraction
37//
38class GCC {
Rafael Espindolae7a629f2013-06-13 16:22:26 +000039 std::string GCCPath; // The path to the gcc executable.
40 std::string RemoteClientPath; // The path to the rsh / ssh executable.
Bill Wendlingbf5d8272009-03-02 23:13:18 +000041 std::vector<std::string> gccArgs; // GCC-specific arguments.
Rafael Espindolae7a629f2013-06-13 16:22:26 +000042 GCC(StringRef gccPath, StringRef RemotePath,
Bill Wendlingbf5d8272009-03-02 23:13:18 +000043 const std::vector<std::string> *GCCArgs)
44 : GCCPath(gccPath), RemoteClientPath(RemotePath) {
45 if (GCCArgs) gccArgs = *GCCArgs;
46 }
Chris Lattnerffac2862006-06-06 22:30:59 +000047public:
Chris Lattnerfd381322010-03-16 06:41:47 +000048 enum FileType { AsmFile, ObjectFile, CFile };
Chris Lattnerffac2862006-06-06 22:30:59 +000049
Dan Gohman46ffffa2009-08-05 20:21:17 +000050 static GCC *create(std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +000051 const std::string &GCCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +000052 const std::vector<std::string> *Args);
Chris Lattnerffac2862006-06-06 22:30:59 +000053
54 /// 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 ///
61 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,
Craig Toppere73658d2014-04-28 04:05:08 +000066 std::string *Error = nullptr,
Chris Lattnerffac2862006-06-06 22:30:59 +000067 const std::vector<std::string> &GCCArgs =
Andrew Trick69a963e2011-02-08 18:07:10 +000068 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +000069 unsigned Timeout = 0,
70 unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +000071
72 /// 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 Lattnercc21fa72006-06-27 20:35:36 +000076 std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +000077 const std::vector<std::string> &ArgsForGCC,
78 std::string &Error);
Chris Lattnerffac2862006-06-06 22:30:59 +000079};
80
81
82//===---------------------------------------------------------------------===//
83/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif0e535c3c2007-07-04 21:55:50 +000084/// LLVM bitcode in a variety of ways. This abstract interface hides this
Chris Lattnerffac2862006-06-06 22:30:59 +000085/// complexity behind a simple interface.
86///
87class AbstractInterpreter {
David Blaikiea379b1812011-12-20 02:50:00 +000088 virtual void anchor();
Chris Lattnerffac2862006-06-06 22:30:59 +000089public:
Dan Gohman46ffffa2009-08-05 20:21:17 +000090 static LLC *createLLC(const char *Argv0, std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +000091 const std::string &GCCBinary,
Craig Toppere73658d2014-04-28 04:05:08 +000092 const std::vector<std::string> *Args = nullptr,
93 const std::vector<std::string> *GCCArgs = nullptr,
Chris Lattnerfd381322010-03-16 06:41:47 +000094 bool UseIntegratedAssembler = false);
Chris Lattnerffac2862006-06-06 22:30:59 +000095
Craig Toppere73658d2014-04-28 04:05:08 +000096 static AbstractInterpreter*
97 createLLI(const char *Argv0, std::string &Message,
98 const std::vector<std::string> *Args = nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +000099
Craig Toppere73658d2014-04-28 04:05:08 +0000100 static AbstractInterpreter*
101 createJIT(const char *Argv0, std::string &Message,
102 const std::vector<std::string> *Args = nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +0000103
Andrew Trick8665d592011-02-08 18:20:48 +0000104 static AbstractInterpreter*
105 createCustomCompiler(std::string &Message,
106 const std::string &CompileCommandLine);
107
108 static AbstractInterpreter*
109 createCustomExecutor(std::string &Message,
110 const std::string &ExecCommandLine);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000111
Chris Lattnerffac2862006-06-06 22:30:59 +0000112
113 virtual ~AbstractInterpreter() {}
114
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000115 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000116 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000117 /// the code generator. It returns false if the code generator fails.
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000118 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
119 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
Chris Lattnerffac2862006-06-06 22:30:59 +0000120
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000121 /// OutputCode - Compile the specified program from bitcode to code
Chris Lattner634bc042006-09-15 21:29:15 +0000122 /// understood by the GCC driver (either C or asm). If the code generator
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000123 /// fails, it sets Error, otherwise, this function returns the type of code
124 /// emitted.
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000125 virtual GCC::FileType OutputCode(const std::string &Bitcode,
Rafael Espindola34889ca2013-06-17 19:21:38 +0000126 std::string &OutFile, std::string &Error,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000127 unsigned Timeout = 0,
128 unsigned MemoryLimit = 0) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000129 Error = "OutputCode not supported by this AbstractInterpreter!";
130 return GCC::AsmFile;
Chris Lattner634bc042006-09-15 21:29:15 +0000131 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000132
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000133 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000134 /// specified filename. This sets RetVal to the exit code of the program or
135 /// returns false if a problem was encountered that prevented execution of
136 /// the program.
Chris Lattnerffac2862006-06-06 22:30:59 +0000137 ///
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000138 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000139 const std::vector<std::string> &Args,
140 const std::string &InputFile,
141 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000142 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000143 const std::vector<std::string> &GCCArgs =
144 std::vector<std::string>(),
145 const std::vector<std::string> &SharedLibs =
146 std::vector<std::string>(),
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000147 unsigned Timeout = 0,
148 unsigned MemoryLimit = 0) = 0;
Chris Lattnerffac2862006-06-06 22:30:59 +0000149};
150
151//===---------------------------------------------------------------------===//
Chris Lattnerffac2862006-06-06 22:30:59 +0000152// LLC Implementation of AbstractIntepreter interface
153//
154class LLC : public AbstractInterpreter {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000155 std::string LLCPath; // The path to the LLC executable.
156 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Chris Lattnerffac2862006-06-06 22:30:59 +0000157 GCC *gcc;
Chris Lattnerfd381322010-03-16 06:41:47 +0000158 bool UseIntegratedAssembler;
Chris Lattnerffac2862006-06-06 22:30:59 +0000159public:
160 LLC(const std::string &llcPath, GCC *Gcc,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000161 const std::vector<std::string> *Args,
Chris Lattnerfd381322010-03-16 06:41:47 +0000162 bool useIntegratedAssembler)
163 : LLCPath(llcPath), gcc(Gcc),
164 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000165 ToolArgs.clear();
166 if (Args) ToolArgs = *Args;
Chris Lattnerffac2862006-06-06 22:30:59 +0000167 }
168 ~LLC() { delete gcc; }
169
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000170 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000171 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000172 /// the code generator. Returns false if the code generator fails.
Craig Toppere56917c2014-03-08 08:27:28 +0000173 void compileProgram(const std::string &Bitcode, std::string *Error,
174 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000175
Craig Toppere56917c2014-03-08 08:27:28 +0000176 int ExecuteProgram(const std::string &Bitcode,
177 const std::vector<std::string> &Args,
178 const std::string &InputFile,
179 const std::string &OutputFile,
180 std::string *Error,
181 const std::vector<std::string> &GCCArgs =
182 std::vector<std::string>(),
183 const std::vector<std::string> &SharedLibs =
184 std::vector<std::string>(),
185 unsigned Timeout = 0,
186 unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000187
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000188 /// OutputCode - Compile the specified program from bitcode to code
189 /// understood by the GCC driver (either C or asm). If the code generator
190 /// fails, it sets Error, otherwise, this function returns the type of code
191 /// emitted.
Craig Toppere56917c2014-03-08 08:27:28 +0000192 GCC::FileType OutputCode(const std::string &Bitcode,
193 std::string &OutFile, std::string &Error,
194 unsigned Timeout = 0,
195 unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000196};
197
198} // End llvm namespace
199
200#endif