Path: blob/main/contrib/llvm-project/llvm/tools/bugpoint/ToolRunner.h
35231 views
//===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file exposes an abstraction around a platform C compiler, used to9// compile C and assembly code. It also exposes an "AbstractIntepreter"10// interface, which is used to execute code using one of the LLVM execution11// engines.12//13//===----------------------------------------------------------------------===//1415#ifndef LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H16#define LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H1718#include "llvm/Support/CommandLine.h"19#include "llvm/Support/Error.h"20#include "llvm/Support/Path.h"21#include "llvm/Support/SystemUtils.h"22#include "llvm/TargetParser/Triple.h"23#include <exception>24#include <vector>2526namespace llvm {2728extern cl::opt<bool> SaveTemps;29extern Triple TargetTriple;3031class LLC;3233//===---------------------------------------------------------------------===//34// CC abstraction35//36class CC {37std::string CCPath; // The path to the cc executable.38std::string RemoteClientPath; // The path to the rsh / ssh executable.39std::vector<std::string> ccArgs; // CC-specific arguments.40CC(StringRef ccPath, StringRef RemotePath,41const std::vector<std::string> *CCArgs)42: CCPath(std::string(ccPath)), RemoteClientPath(std::string(RemotePath)) {43if (CCArgs)44ccArgs = *CCArgs;45}4647public:48enum FileType { AsmFile, ObjectFile, CFile };4950static CC *create(const char *Argv0, std::string &Message,51const std::string &CCBinary,52const std::vector<std::string> *Args);5354/// ExecuteProgram - Execute the program specified by "ProgramFile" (which is55/// either a .s file, or a .c file, specified by FileType), with the specified56/// arguments. Standard input is specified with InputFile, and standard57/// Output is captured to the specified OutputFile location. The SharedLibs58/// option specifies optional native shared objects that can be loaded into59/// the program for execution.60///61Expected<int> ExecuteProgram(62const std::string &ProgramFile, const std::vector<std::string> &Args,63FileType fileType, const std::string &InputFile,64const std::string &OutputFile,65const std::vector<std::string> &CCArgs = std::vector<std::string>(),66unsigned Timeout = 0, unsigned MemoryLimit = 0);6768/// MakeSharedObject - This compiles the specified file (which is either a .c69/// file or a .s file) into a shared object.70///71Error MakeSharedObject(const std::string &InputFile, FileType fileType,72std::string &OutputFile,73const std::vector<std::string> &ArgsForCC);74};7576//===---------------------------------------------------------------------===//77/// AbstractInterpreter Class - Subclasses of this class are used to execute78/// LLVM bitcode in a variety of ways. This abstract interface hides this79/// complexity behind a simple interface.80///81class AbstractInterpreter {82virtual void anchor();8384public:85static LLC *createLLC(const char *Argv0, std::string &Message,86const std::string &CCBinary,87const std::vector<std::string> *Args = nullptr,88const std::vector<std::string> *CCArgs = nullptr,89bool UseIntegratedAssembler = false);9091static AbstractInterpreter *92createLLI(const char *Argv0, std::string &Message,93const std::vector<std::string> *Args = nullptr);9495static AbstractInterpreter *96createJIT(const char *Argv0, std::string &Message,97const std::vector<std::string> *Args = nullptr);9899static AbstractInterpreter *100createCustomCompiler(const char *Argv0, std::string &Message,101const std::string &CompileCommandLine);102103static AbstractInterpreter *104createCustomExecutor(const char *Argv0, std::string &Message,105const std::string &ExecCommandLine);106107virtual ~AbstractInterpreter() {}108109/// compileProgram - Compile the specified program from bitcode to executable110/// code. This does not produce any output, it is only used when debugging111/// the code generator. It returns false if the code generator fails.112virtual Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,113unsigned MemoryLimit = 0) {114return Error::success();115}116117/// Compile the specified program from bitcode to code understood by the CC118/// driver (either C or asm). Returns an error if the code generator fails,,119/// otherwise, the type of code emitted.120virtual Expected<CC::FileType> OutputCode(const std::string &Bitcode,121std::string &OutFile,122unsigned Timeout = 0,123unsigned MemoryLimit = 0) {124return make_error<StringError>(125"OutputCode not supported by this AbstractInterpreter!",126inconvertibleErrorCode());127}128129/// ExecuteProgram - Run the specified bitcode file, emitting output to the130/// specified filename. This sets RetVal to the exit code of the program or131/// returns an Error if a problem was encountered that prevented execution of132/// the program.133///134virtual Expected<int> ExecuteProgram(135const std::string &Bitcode, const std::vector<std::string> &Args,136const std::string &InputFile, const std::string &OutputFile,137const std::vector<std::string> &CCArgs = std::vector<std::string>(),138const std::vector<std::string> &SharedLibs = std::vector<std::string>(),139unsigned Timeout = 0, unsigned MemoryLimit = 0) = 0;140};141142//===---------------------------------------------------------------------===//143// LLC Implementation of AbstractIntepreter interface144//145class LLC : public AbstractInterpreter {146std::string LLCPath; // The path to the LLC executable.147std::vector<std::string> ToolArgs; // Extra args to pass to LLC.148CC *cc;149bool UseIntegratedAssembler;150151public:152LLC(const std::string &llcPath, CC *cc, const std::vector<std::string> *Args,153bool useIntegratedAssembler)154: LLCPath(llcPath), cc(cc),155UseIntegratedAssembler(useIntegratedAssembler) {156ToolArgs.clear();157if (Args)158ToolArgs = *Args;159}160~LLC() override { delete cc; }161162/// compileProgram - Compile the specified program from bitcode to executable163/// code. This does not produce any output, it is only used when debugging164/// the code generator. Returns false if the code generator fails.165Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,166unsigned MemoryLimit = 0) override;167168Expected<int> ExecuteProgram(169const std::string &Bitcode, const std::vector<std::string> &Args,170const std::string &InputFile, const std::string &OutputFile,171const std::vector<std::string> &CCArgs = std::vector<std::string>(),172const std::vector<std::string> &SharedLibs = std::vector<std::string>(),173unsigned Timeout = 0, unsigned MemoryLimit = 0) override;174175Expected<CC::FileType> OutputCode(const std::string &Bitcode,176std::string &OutFile, unsigned Timeout = 0,177unsigned MemoryLimit = 0) override;178};179180/// Find the first executable file \ExeName, either in the user's PATH or,181/// failing that, in the same directory as argv[0]. This allows us to find182/// another LLVM tool if it is built in the same directory. If no executable is183/// found, an error is returned.184ErrorOr<std::string> FindProgramByName(const std::string &ExeName,185const char *Argv0, void *MainAddr);186187} // End llvm namespace188189#endif190191192