blob: dde4ec539cfb0ed526583821ce8afddf02274034 [file] [log] [blame]
Chris Lattnerffac2862006-06-06 22:30:59 +00001//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerffac2862006-06-06 22:30:59 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file exposes an abstraction around a platform C compiler, used to
10// compile C and assembly code. It also exposes an "AbstractIntepreter"
11// interface, which is used to execute code using one of the LLVM execution
12// engines.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000016#ifndef LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
17#define LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
Chris Lattnerffac2862006-06-06 22:30:59 +000018
Daniel Dunbar8575a602009-08-18 03:35:57 +000019#include "llvm/ADT/Triple.h"
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000020#include "llvm/Support/CommandLine.h"
Justin Bogner1c039152016-09-06 17:18:22 +000021#include "llvm/Support/Error.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000022#include "llvm/Support/Path.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000023#include "llvm/Support/SystemUtils.h"
Chris Lattnerffac2862006-06-06 22:30:59 +000024#include <exception>
25#include <vector>
26
27namespace llvm {
28
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000029extern cl::opt<bool> SaveTemps;
Daniel Dunbar8575a602009-08-18 03:35:57 +000030extern Triple TargetTriple;
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000031
Chris Lattnerffac2862006-06-06 22:30:59 +000032class LLC;
33
Chris Lattnerffac2862006-06-06 22:30:59 +000034//===---------------------------------------------------------------------===//
Davide Italianoab256212015-10-14 20:29:54 +000035// CC abstraction
Chris Lattnerffac2862006-06-06 22:30:59 +000036//
Davide Italianoab256212015-10-14 20:29:54 +000037class CC {
Justin Bogner8d0a0812016-09-02 01:21:37 +000038 std::string CCPath; // The path to the cc executable.
39 std::string RemoteClientPath; // The path to the rsh / ssh executable.
Davide Italianoab256212015-10-14 20:29:54 +000040 std::vector<std::string> ccArgs; // CC-specific arguments.
41 CC(StringRef ccPath, StringRef RemotePath,
Justin Bogner8d0a0812016-09-02 01:21:37 +000042 const std::vector<std::string> *CCArgs)
43 : CCPath(ccPath), RemoteClientPath(RemotePath) {
44 if (CCArgs)
45 ccArgs = *CCArgs;
Bill Wendlingbf5d8272009-03-02 23:13:18 +000046 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000047
Chris Lattnerffac2862006-06-06 22:30:59 +000048public:
Chris Lattnerfd381322010-03-16 06:41:47 +000049 enum FileType { AsmFile, ObjectFile, CFile };
Chris Lattnerffac2862006-06-06 22:30:59 +000050
Brian Gesiak5cc89202018-12-10 00:56:13 +000051 static CC *create(const char *Argv0, std::string &Message,
52 const std::string &CCBinary,
Justin Bogner8d0a0812016-09-02 01:21:37 +000053 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 Bogner1c039152016-09-06 17:18:22 +000062 Expected<int> ExecuteProgram(
Justin Bogner8d0a0812016-09-02 01:21:37 +000063 const std::string &ProgramFile, const std::vector<std::string> &Args,
64 FileType fileType, const std::string &InputFile,
Justin Bogner1c039152016-09-06 17:18:22 +000065 const std::string &OutputFile,
Justin Bogner8d0a0812016-09-02 01:21:37 +000066 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 ///
Justin Bogner1c039152016-09-06 17:18:22 +000072 Error MakeSharedObject(const std::string &InputFile, FileType fileType,
73 std::string &OutputFile,
74 const std::vector<std::string> &ArgsForCC);
Chris Lattnerffac2862006-06-06 22:30:59 +000075};
76
Chris Lattnerffac2862006-06-06 22:30:59 +000077//===---------------------------------------------------------------------===//
78/// AbstractInterpreter Class - Subclasses of this class are used to execute
Gabor Greif0e535c3c2007-07-04 21:55:50 +000079/// LLVM bitcode in a variety of ways. This abstract interface hides this
Chris Lattnerffac2862006-06-06 22:30:59 +000080/// complexity behind a simple interface.
81///
82class AbstractInterpreter {
David Blaikiea379b1812011-12-20 02:50:00 +000083 virtual void anchor();
Justin Bogner8d0a0812016-09-02 01:21:37 +000084
Chris Lattnerffac2862006-06-06 22:30:59 +000085public:
Dan Gohman46ffffa2009-08-05 20:21:17 +000086 static LLC *createLLC(const char *Argv0, std::string &Message,
Justin Bogner8d0a0812016-09-02 01:21:37 +000087 const std::string &CCBinary,
Craig Toppere73658d2014-04-28 04:05:08 +000088 const std::vector<std::string> *Args = nullptr,
Davide Italianoab256212015-10-14 20:29:54 +000089 const std::vector<std::string> *CCArgs = nullptr,
Chris Lattnerfd381322010-03-16 06:41:47 +000090 bool UseIntegratedAssembler = false);
Chris Lattnerffac2862006-06-06 22:30:59 +000091
Justin Bogner8d0a0812016-09-02 01:21:37 +000092 static AbstractInterpreter *
Craig Toppere73658d2014-04-28 04:05:08 +000093 createLLI(const char *Argv0, std::string &Message,
94 const std::vector<std::string> *Args = nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +000095
Justin Bogner8d0a0812016-09-02 01:21:37 +000096 static AbstractInterpreter *
Craig Toppere73658d2014-04-28 04:05:08 +000097 createJIT(const char *Argv0, std::string &Message,
98 const std::vector<std::string> *Args = nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +000099
Justin Bogner8d0a0812016-09-02 01:21:37 +0000100 static AbstractInterpreter *
Brian Gesiak5cc89202018-12-10 00:56:13 +0000101 createCustomCompiler(const char *Argv0, std::string &Message,
Andrew Trick8665d592011-02-08 18:20:48 +0000102 const std::string &CompileCommandLine);
103
Justin Bogner8d0a0812016-09-02 01:21:37 +0000104 static AbstractInterpreter *
Brian Gesiak5cc89202018-12-10 00:56:13 +0000105 createCustomExecutor(const char *Argv0, std::string &Message,
Andrew Trick8665d592011-02-08 18:20:48 +0000106 const std::string &ExecCommandLine);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000107
Chris Lattnerffac2862006-06-06 22:30:59 +0000108 virtual ~AbstractInterpreter() {}
109
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000110 /// compileProgram - Compile the specified program from bitcode to executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000111 /// code. This does not produce any output, it is only used when debugging
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000112 /// the code generator. It returns false if the code generator fails.
Justin Bogner1c039152016-09-06 17:18:22 +0000113 virtual Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
114 unsigned MemoryLimit = 0) {
115 return Error::success();
116 }
Chris Lattnerffac2862006-06-06 22:30:59 +0000117
Justin Bogner1c039152016-09-06 17:18:22 +0000118 /// Compile the specified program from bitcode to code understood by the CC
119 /// driver (either C or asm). Returns an error if the code generator fails,,
120 /// otherwise, the type of code emitted.
121 virtual Expected<CC::FileType> OutputCode(const std::string &Bitcode,
122 std::string &OutFile,
123 unsigned Timeout = 0,
124 unsigned MemoryLimit = 0) {
125 return make_error<StringError>(
126 "OutputCode not supported by this AbstractInterpreter!",
127 inconvertibleErrorCode());
Chris Lattner634bc042006-09-15 21:29:15 +0000128 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000129
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000130 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000131 /// specified filename. This sets RetVal to the exit code of the program or
Justin Bogner1c039152016-09-06 17:18:22 +0000132 /// returns an Error if a problem was encountered that prevented execution of
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000133 /// the program.
Chris Lattnerffac2862006-06-06 22:30:59 +0000134 ///
Justin Bogner1c039152016-09-06 17:18:22 +0000135 virtual Expected<int> ExecuteProgram(
Justin Bogner8d0a0812016-09-02 01:21:37 +0000136 const std::string &Bitcode, const std::vector<std::string> &Args,
137 const std::string &InputFile, const std::string &OutputFile,
Justin Bogner8d0a0812016-09-02 01:21:37 +0000138 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.
Justin Bogner1c039152016-09-06 17:18:22 +0000166 Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
167 unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000168
Justin Bogner1c039152016-09-06 17:18:22 +0000169 Expected<int> ExecuteProgram(
Justin Bogner8d0a0812016-09-02 01:21:37 +0000170 const std::string &Bitcode, const std::vector<std::string> &Args,
171 const std::string &InputFile, const std::string &OutputFile,
Justin Bogner8d0a0812016-09-02 01:21:37 +0000172 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
173 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
174 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000175
Justin Bogner1c039152016-09-06 17:18:22 +0000176 Expected<CC::FileType> OutputCode(const std::string &Bitcode,
177 std::string &OutFile, unsigned Timeout = 0,
178 unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000179};
180
Brian Gesiak5cc89202018-12-10 00:56:13 +0000181/// Find the first executable file \ExeName, either in the user's PATH or,
182/// failing that, in the same directory as argv[0]. This allows us to find
183/// another LLVM tool if it is built in the same directory. If no executable is
184/// found, an error is returned.
185ErrorOr<std::string> FindProgramByName(const std::string &ExeName,
186 const char *Argv0, void *MainAddr);
187
Chris Lattnerffac2862006-06-06 22:30:59 +0000188} // End llvm namespace
189
190#endif