blob: 3e5d83d2e1c1c5f270e807b3d9e60251325b5f9b [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//===---------------------------------------------------------------------===//
Davide Italianoab256212015-10-14 20:29:54 +000036// CC abstraction
Chris Lattnerffac2862006-06-06 22:30:59 +000037//
Davide Italianoab256212015-10-14 20:29:54 +000038class CC {
Justin Bogner8d0a0812016-09-02 01:21:37 +000039 std::string CCPath; // The path to the cc executable.
40 std::string RemoteClientPath; // The path to the rsh / ssh executable.
Davide Italianoab256212015-10-14 20:29:54 +000041 std::vector<std::string> ccArgs; // CC-specific arguments.
42 CC(StringRef ccPath, StringRef RemotePath,
Justin Bogner8d0a0812016-09-02 01:21:37 +000043 const std::vector<std::string> *CCArgs)
44 : CCPath(ccPath), RemoteClientPath(RemotePath) {
45 if (CCArgs)
46 ccArgs = *CCArgs;
Bill Wendlingbf5d8272009-03-02 23:13:18 +000047 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000048
Chris Lattnerffac2862006-06-06 22:30:59 +000049public:
Chris Lattnerfd381322010-03-16 06:41:47 +000050 enum FileType { AsmFile, ObjectFile, CFile };
Chris Lattnerffac2862006-06-06 22:30:59 +000051
Justin Bogner8d0a0812016-09-02 01:21:37 +000052 static CC *create(std::string &Message, const std::string &CCBinary,
53 const std::vector<std::string> *Args);
Chris Lattnerffac2862006-06-06 22:30:59 +000054
55 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
56 /// either a .s file, or a .c file, specified by FileType), with the specified
57 /// arguments. Standard input is specified with InputFile, and standard
58 /// Output is captured to the specified OutputFile location. The SharedLibs
59 /// option specifies optional native shared objects that can be loaded into
60 /// the program for execution.
61 ///
Justin Bogner8d0a0812016-09-02 01:21:37 +000062 int ExecuteProgram(
63 const std::string &ProgramFile, const std::vector<std::string> &Args,
64 FileType fileType, const std::string &InputFile,
65 const std::string &OutputFile, std::string *Error = nullptr,
66 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
67 unsigned Timeout = 0, unsigned MemoryLimit = 0);
Chris Lattnerffac2862006-06-06 22:30:59 +000068
69 /// 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,
Chris Lattnercc21fa72006-06-27 20:35:36 +000073 std::string &OutputFile,
Davide Italianoab256212015-10-14 20:29:54 +000074 const std::vector<std::string> &ArgsForCC,
Nick Lewycky6ba630b2010-04-12 05:08:25 +000075 std::string &Error);
Chris Lattnerffac2862006-06-06 22:30:59 +000076};
77
Chris Lattnerffac2862006-06-06 22:30:59 +000078//===---------------------------------------------------------------------===//
79/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif0e535c3c2007-07-04 21:55:50 +000080/// LLVM bitcode in a variety of ways. This abstract interface hides this
Chris Lattnerffac2862006-06-06 22:30:59 +000081/// complexity behind a simple interface.
82///
83class AbstractInterpreter {
David Blaikiea379b1812011-12-20 02:50:00 +000084 virtual void anchor();
Justin Bogner8d0a0812016-09-02 01:21:37 +000085
Chris Lattnerffac2862006-06-06 22:30:59 +000086public:
Dan Gohman46ffffa2009-08-05 20:21:17 +000087 static LLC *createLLC(const char *Argv0, std::string &Message,
Justin Bogner8d0a0812016-09-02 01:21:37 +000088 const std::string &CCBinary,
Craig Toppere73658d2014-04-28 04:05:08 +000089 const std::vector<std::string> *Args = nullptr,
Davide Italianoab256212015-10-14 20:29:54 +000090 const std::vector<std::string> *CCArgs = nullptr,
Chris Lattnerfd381322010-03-16 06:41:47 +000091 bool UseIntegratedAssembler = false);
Chris Lattnerffac2862006-06-06 22:30:59 +000092
Justin Bogner8d0a0812016-09-02 01:21:37 +000093 static AbstractInterpreter *
Craig Toppere73658d2014-04-28 04:05:08 +000094 createLLI(const char *Argv0, std::string &Message,
95 const std::vector<std::string> *Args = nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +000096
Justin Bogner8d0a0812016-09-02 01:21:37 +000097 static AbstractInterpreter *
Craig Toppere73658d2014-04-28 04:05:08 +000098 createJIT(const char *Argv0, std::string &Message,
99 const std::vector<std::string> *Args = nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +0000100
Justin Bogner8d0a0812016-09-02 01:21:37 +0000101 static AbstractInterpreter *
Andrew Trick8665d592011-02-08 18:20:48 +0000102 createCustomCompiler(std::string &Message,
103 const std::string &CompileCommandLine);
104
Justin Bogner8d0a0812016-09-02 01:21:37 +0000105 static AbstractInterpreter *
Andrew Trick8665d592011-02-08 18:20:48 +0000106 createCustomExecutor(std::string &Message,
107 const std::string &ExecCommandLine);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000108
Chris Lattnerffac2862006-06-06 22:30:59 +0000109 virtual ~AbstractInterpreter() {}
110
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000111 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000112 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000113 /// the code generator. It returns false if the code generator fails.
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000114 virtual void compileProgram(const std::string &Bitcode, std::string *Error,
115 unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
Chris Lattnerffac2862006-06-06 22:30:59 +0000116
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000117 /// OutputCode - Compile the specified program from bitcode to code
Davide Italianoab256212015-10-14 20:29:54 +0000118 /// understood by the CC driver (either C or asm). If the code generator
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000119 /// fails, it sets Error, otherwise, this function returns the type of code
120 /// emitted.
Davide Italianoab256212015-10-14 20:29:54 +0000121 virtual CC::FileType OutputCode(const std::string &Bitcode,
Justin Bogner8d0a0812016-09-02 01:21:37 +0000122 std::string &OutFile, std::string &Error,
123 unsigned Timeout = 0,
124 unsigned MemoryLimit = 0) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000125 Error = "OutputCode not supported by this AbstractInterpreter!";
Davide Italianoab256212015-10-14 20:29:54 +0000126 return CC::AsmFile;
Chris Lattner634bc042006-09-15 21:29:15 +0000127 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000128
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000129 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000130 /// specified filename. This sets RetVal to the exit code of the program or
131 /// returns false if a problem was encountered that prevented execution of
132 /// the program.
Chris Lattnerffac2862006-06-06 22:30:59 +0000133 ///
Justin Bogner8d0a0812016-09-02 01:21:37 +0000134 virtual int ExecuteProgram(
135 const std::string &Bitcode, const std::vector<std::string> &Args,
136 const std::string &InputFile, const std::string &OutputFile,
137 std::string *Error,
138 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
139 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
140 unsigned Timeout = 0, unsigned MemoryLimit = 0) = 0;
Chris Lattnerffac2862006-06-06 22:30:59 +0000141};
142
143//===---------------------------------------------------------------------===//
Chris Lattnerffac2862006-06-06 22:30:59 +0000144// LLC Implementation of AbstractIntepreter interface
145//
146class LLC : public AbstractInterpreter {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000147 std::string LLCPath; // The path to the LLC executable.
148 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Davide Italianoab256212015-10-14 20:29:54 +0000149 CC *cc;
Chris Lattnerfd381322010-03-16 06:41:47 +0000150 bool UseIntegratedAssembler;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000151
Chris Lattnerffac2862006-06-06 22:30:59 +0000152public:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000153 LLC(const std::string &llcPath, CC *cc, const std::vector<std::string> *Args,
Chris Lattnerfd381322010-03-16 06:41:47 +0000154 bool useIntegratedAssembler)
Justin Bogner8d0a0812016-09-02 01:21:37 +0000155 : LLCPath(llcPath), cc(cc),
156 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000157 ToolArgs.clear();
Justin Bogner8d0a0812016-09-02 01:21:37 +0000158 if (Args)
159 ToolArgs = *Args;
Chris Lattnerffac2862006-06-06 22:30:59 +0000160 }
Davide Italianoab256212015-10-14 20:29:54 +0000161 ~LLC() override { delete cc; }
Chris Lattnerffac2862006-06-06 22:30:59 +0000162
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000163 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000164 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000165 /// the code generator. Returns false if the code generator fails.
Craig Toppere56917c2014-03-08 08:27:28 +0000166 void compileProgram(const std::string &Bitcode, std::string *Error,
167 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000168
Justin Bogner8d0a0812016-09-02 01:21:37 +0000169 int ExecuteProgram(
170 const std::string &Bitcode, const std::vector<std::string> &Args,
171 const std::string &InputFile, const std::string &OutputFile,
172 std::string *Error,
173 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
174 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
175 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000176
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000177 /// OutputCode - Compile the specified program from bitcode to code
Davide Italianoab256212015-10-14 20:29:54 +0000178 /// understood by the CC driver (either C or asm). If the code generator
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000179 /// fails, it sets Error, otherwise, this function returns the type of code
180 /// emitted.
Justin Bogner8d0a0812016-09-02 01:21:37 +0000181 CC::FileType OutputCode(const std::string &Bitcode, std::string &OutFile,
182 std::string &Error, unsigned Timeout = 0,
183 unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000184};
185
186} // End llvm namespace
187
188#endif