blob: ef8551cc669b44942dbdb5c42a3a78d70fa32818 [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
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000017#ifndef LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
18#define LLVM_TOOLS_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"
Justin Bognerd8090ae2016-09-06 17:18:22 +000022#include "llvm/Support/Error.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000023#include "llvm/Support/Path.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000024#include "llvm/Support/SystemUtils.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 LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000034
35//===---------------------------------------------------------------------===//
Davide Italianof7b2acb2015-10-14 20:29:54 +000036// CC abstraction
Misha Brukman29afb642003-09-29 22:38:57 +000037//
Davide Italianof7b2acb2015-10-14 20:29:54 +000038class CC {
Justin Bogner388e8b92016-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 Italianof7b2acb2015-10-14 20:29:54 +000041 std::vector<std::string> ccArgs; // CC-specific arguments.
42 CC(StringRef ccPath, StringRef RemotePath,
Justin Bogner388e8b92016-09-02 01:21:37 +000043 const std::vector<std::string> *CCArgs)
44 : CCPath(ccPath), RemoteClientPath(RemotePath) {
45 if (CCArgs)
46 ccArgs = *CCArgs;
Bill Wendling38efa382009-03-02 23:13:18 +000047 }
Justin Bogner388e8b92016-09-02 01:21:37 +000048
Chris Lattner7915a1e2003-10-14 21:34:11 +000049public:
Chris Lattner50010422010-03-16 06:41:47 +000050 enum FileType { AsmFile, ObjectFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000051
Brian Gesiakdcf592e2018-12-10 00:56:13 +000052 static CC *create(const char *Argv0, std::string &Message,
53 const std::string &CCBinary,
Justin Bogner388e8b92016-09-02 01:21:37 +000054 const std::vector<std::string> *Args);
Chris Lattner7915a1e2003-10-14 21:34:11 +000055
Chris Lattnereeed9832003-10-14 21:52:52 +000056 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
57 /// either a .s file, or a .c file, specified by FileType), with the specified
58 /// arguments. Standard input is specified with InputFile, and standard
59 /// Output is captured to the specified OutputFile location. The SharedLibs
60 /// option specifies optional native shared objects that can be loaded into
61 /// the program for execution.
62 ///
Justin Bognerd8090ae2016-09-06 17:18:22 +000063 Expected<int> ExecuteProgram(
Justin Bogner388e8b92016-09-02 01:21:37 +000064 const std::string &ProgramFile, const std::vector<std::string> &Args,
65 FileType fileType, const std::string &InputFile,
Justin Bognerd8090ae2016-09-06 17:18:22 +000066 const std::string &OutputFile,
Justin Bogner388e8b92016-09-02 01:21:37 +000067 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
68 unsigned Timeout = 0, 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 ///
Justin Bognerd8090ae2016-09-06 17:18:22 +000073 Error MakeSharedObject(const std::string &InputFile, FileType fileType,
74 std::string &OutputFile,
75 const std::vector<std::string> &ArgsForCC);
Misha Brukman29afb642003-09-29 22:38:57 +000076};
77
Chris Lattner7915a1e2003-10-14 21:34:11 +000078//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000079/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif8ff70c22007-07-04 21:55:50 +000080/// LLVM bitcode in a variety of ways. This abstract interface hides this
Misha Brukman29afb642003-09-29 22:38:57 +000081/// complexity behind a simple interface.
82///
Jeff Cohen838819572005-01-22 16:30:58 +000083class AbstractInterpreter {
David Blaikie2d24e2a2011-12-20 02:50:00 +000084 virtual void anchor();
Justin Bogner388e8b92016-09-02 01:21:37 +000085
Jeff Cohen838819572005-01-22 16:30:58 +000086public:
Dan Gohman197f7282009-08-05 20:21:17 +000087 static LLC *createLLC(const char *Argv0, std::string &Message,
Justin Bogner388e8b92016-09-02 01:21:37 +000088 const std::string &CCBinary,
Craig Topperc34a25d2014-04-28 04:05:08 +000089 const std::vector<std::string> *Args = nullptr,
Davide Italianof7b2acb2015-10-14 20:29:54 +000090 const std::vector<std::string> *CCArgs = nullptr,
Chris Lattner50010422010-03-16 06:41:47 +000091 bool UseIntegratedAssembler = false);
Chris Lattner7915a1e2003-10-14 21:34:11 +000092
Justin Bogner388e8b92016-09-02 01:21:37 +000093 static AbstractInterpreter *
Craig Topperc34a25d2014-04-28 04:05:08 +000094 createLLI(const char *Argv0, std::string &Message,
95 const std::vector<std::string> *Args = nullptr);
Chris Lattner7915a1e2003-10-14 21:34:11 +000096
Justin Bogner388e8b92016-09-02 01:21:37 +000097 static AbstractInterpreter *
Craig Topperc34a25d2014-04-28 04:05:08 +000098 createJIT(const char *Argv0, std::string &Message,
99 const std::vector<std::string> *Args = nullptr);
Chris Lattner7915a1e2003-10-14 21:34:11 +0000100
Justin Bogner388e8b92016-09-02 01:21:37 +0000101 static AbstractInterpreter *
Brian Gesiakdcf592e2018-12-10 00:56:13 +0000102 createCustomCompiler(const char *Argv0, std::string &Message,
Andrew Trickf73311b2011-02-08 18:20:48 +0000103 const std::string &CompileCommandLine);
104
Justin Bogner388e8b92016-09-02 01:21:37 +0000105 static AbstractInterpreter *
Brian Gesiakdcf592e2018-12-10 00:56:13 +0000106 createCustomExecutor(const char *Argv0, std::string &Message,
Andrew Trickf73311b2011-02-08 18:20:48 +0000107 const std::string &ExecCommandLine);
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000108
Misha Brukman29afb642003-09-29 22:38:57 +0000109 virtual ~AbstractInterpreter() {}
110
Gabor Greif8ff70c22007-07-04 21:55:50 +0000111 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000112 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000113 /// the code generator. It returns false if the code generator fails.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000114 virtual Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
115 unsigned MemoryLimit = 0) {
116 return Error::success();
117 }
Chris Lattnerf03715c2004-02-18 23:24:29 +0000118
Justin Bognerd8090ae2016-09-06 17:18:22 +0000119 /// Compile the specified program from bitcode to code understood by the CC
120 /// driver (either C or asm). Returns an error if the code generator fails,,
121 /// otherwise, the type of code emitted.
122 virtual Expected<CC::FileType> OutputCode(const std::string &Bitcode,
123 std::string &OutFile,
124 unsigned Timeout = 0,
125 unsigned MemoryLimit = 0) {
126 return make_error<StringError>(
127 "OutputCode not supported by this AbstractInterpreter!",
128 inconvertibleErrorCode());
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000129 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000130
Gabor Greif8ff70c22007-07-04 21:55:50 +0000131 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky22ff7482010-04-12 05:08:25 +0000132 /// specified filename. This sets RetVal to the exit code of the program or
Justin Bognerd8090ae2016-09-06 17:18:22 +0000133 /// returns an Error if a problem was encountered that prevented execution of
Nick Lewycky22ff7482010-04-12 05:08:25 +0000134 /// the program.
Misha Brukman29afb642003-09-29 22:38:57 +0000135 ///
Justin Bognerd8090ae2016-09-06 17:18:22 +0000136 virtual Expected<int> ExecuteProgram(
Justin Bogner388e8b92016-09-02 01:21:37 +0000137 const std::string &Bitcode, const std::vector<std::string> &Args,
138 const std::string &InputFile, const std::string &OutputFile,
Justin Bogner388e8b92016-09-02 01:21:37 +0000139 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
140 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
141 unsigned Timeout = 0, unsigned MemoryLimit = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000142};
143
144//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +0000145// LLC Implementation of AbstractIntepreter interface
146//
147class LLC : public AbstractInterpreter {
Bill Wendling38efa382009-03-02 23:13:18 +0000148 std::string LLCPath; // The path to the LLC executable.
149 std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
Davide Italianof7b2acb2015-10-14 20:29:54 +0000150 CC *cc;
Chris Lattner50010422010-03-16 06:41:47 +0000151 bool UseIntegratedAssembler;
Justin Bogner388e8b92016-09-02 01:21:37 +0000152
Misha Brukman29afb642003-09-29 22:38:57 +0000153public:
Justin Bogner388e8b92016-09-02 01:21:37 +0000154 LLC(const std::string &llcPath, CC *cc, const std::vector<std::string> *Args,
Chris Lattner50010422010-03-16 06:41:47 +0000155 bool useIntegratedAssembler)
Justin Bogner388e8b92016-09-02 01:21:37 +0000156 : LLCPath(llcPath), cc(cc),
157 UseIntegratedAssembler(useIntegratedAssembler) {
Bill Wendling38efa382009-03-02 23:13:18 +0000158 ToolArgs.clear();
Justin Bogner388e8b92016-09-02 01:21:37 +0000159 if (Args)
160 ToolArgs = *Args;
Brian Gaeked11577b2004-05-04 21:09:01 +0000161 }
Davide Italianof7b2acb2015-10-14 20:29:54 +0000162 ~LLC() override { delete cc; }
Misha Brukman29afb642003-09-29 22:38:57 +0000163
Gabor Greif8ff70c22007-07-04 21:55:50 +0000164 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerf03715c2004-02-18 23:24:29 +0000165 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky22ff7482010-04-12 05:08:25 +0000166 /// the code generator. Returns false if the code generator fails.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000167 Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
168 unsigned MemoryLimit = 0) override;
Chris Lattnerf03715c2004-02-18 23:24:29 +0000169
Justin Bognerd8090ae2016-09-06 17:18:22 +0000170 Expected<int> ExecuteProgram(
Justin Bogner388e8b92016-09-02 01:21:37 +0000171 const std::string &Bitcode, const std::vector<std::string> &Args,
172 const std::string &InputFile, const std::string &OutputFile,
Justin Bogner388e8b92016-09-02 01:21:37 +0000173 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;
Misha Brukman29afb642003-09-29 22:38:57 +0000176
Justin Bognerd8090ae2016-09-06 17:18:22 +0000177 Expected<CC::FileType> OutputCode(const std::string &Bitcode,
178 std::string &OutFile, unsigned Timeout = 0,
179 unsigned MemoryLimit = 0) override;
Misha Brukman29afb642003-09-29 22:38:57 +0000180};
181
Brian Gesiakdcf592e2018-12-10 00:56:13 +0000182/// Find the first executable file \ExeName, either in the user's PATH or,
183/// failing that, in the same directory as argv[0]. This allows us to find
184/// another LLVM tool if it is built in the same directory. If no executable is
185/// found, an error is returned.
186ErrorOr<std::string> FindProgramByName(const std::string &ExeName,
187 const char *Argv0, void *MainAddr);
188
Brian Gaeked0fde302003-11-11 22:41:34 +0000189} // End llvm namespace
190
Misha Brukman29afb642003-09-29 22:38:57 +0000191#endif