xref: /aosp_15_r20/external/llvm/tools/bugpoint/ToolRunner.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file exposes an abstraction around a platform C compiler, used to
11*9880d681SAndroid Build Coastguard Worker // compile C and assembly code.  It also exposes an "AbstractIntepreter"
12*9880d681SAndroid Build Coastguard Worker // interface, which is used to execute code using one of the LLVM execution
13*9880d681SAndroid Build Coastguard Worker // engines.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
18*9880d681SAndroid Build Coastguard Worker #define LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
19*9880d681SAndroid Build Coastguard Worker 
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Triple.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Path.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/SystemUtils.h"
25*9880d681SAndroid Build Coastguard Worker #include <exception>
26*9880d681SAndroid Build Coastguard Worker #include <vector>
27*9880d681SAndroid Build Coastguard Worker 
28*9880d681SAndroid Build Coastguard Worker namespace llvm {
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker extern cl::opt<bool> SaveTemps;
31*9880d681SAndroid Build Coastguard Worker extern Triple TargetTriple;
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker class LLC;
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
36*9880d681SAndroid Build Coastguard Worker // CC abstraction
37*9880d681SAndroid Build Coastguard Worker //
38*9880d681SAndroid Build Coastguard Worker class CC {
39*9880d681SAndroid Build Coastguard Worker   std::string CCPath;                // The path to the cc executable.
40*9880d681SAndroid Build Coastguard Worker   std::string RemoteClientPath;       // The path to the rsh / ssh executable.
41*9880d681SAndroid Build Coastguard Worker   std::vector<std::string> ccArgs; // CC-specific arguments.
CC(StringRef ccPath,StringRef RemotePath,const std::vector<std::string> * CCArgs)42*9880d681SAndroid Build Coastguard Worker   CC(StringRef ccPath, StringRef RemotePath,
43*9880d681SAndroid Build Coastguard Worker       const std::vector<std::string> *CCArgs)
44*9880d681SAndroid Build Coastguard Worker     : CCPath(ccPath), RemoteClientPath(RemotePath) {
45*9880d681SAndroid Build Coastguard Worker     if (CCArgs) ccArgs = *CCArgs;
46*9880d681SAndroid Build Coastguard Worker   }
47*9880d681SAndroid Build Coastguard Worker public:
48*9880d681SAndroid Build Coastguard Worker   enum FileType { AsmFile, ObjectFile, CFile };
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker   static CC *create(std::string &Message,
51*9880d681SAndroid Build Coastguard Worker                      const std::string &CCBinary,
52*9880d681SAndroid Build Coastguard Worker                      const std::vector<std::string> *Args);
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker   /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
55*9880d681SAndroid Build Coastguard Worker   /// either a .s file, or a .c file, specified by FileType), with the specified
56*9880d681SAndroid Build Coastguard Worker   /// arguments.  Standard input is specified with InputFile, and standard
57*9880d681SAndroid Build Coastguard Worker   /// Output is captured to the specified OutputFile location.  The SharedLibs
58*9880d681SAndroid Build Coastguard Worker   /// option specifies optional native shared objects that can be loaded into
59*9880d681SAndroid Build Coastguard Worker   /// the program for execution.
60*9880d681SAndroid Build Coastguard Worker   ///
61*9880d681SAndroid Build Coastguard Worker   int ExecuteProgram(const std::string &ProgramFile,
62*9880d681SAndroid Build Coastguard Worker                      const std::vector<std::string> &Args,
63*9880d681SAndroid Build Coastguard Worker                      FileType fileType,
64*9880d681SAndroid Build Coastguard Worker                      const std::string &InputFile,
65*9880d681SAndroid Build Coastguard Worker                      const std::string &OutputFile,
66*9880d681SAndroid Build Coastguard Worker                      std::string *Error = nullptr,
67*9880d681SAndroid Build Coastguard Worker                      const std::vector<std::string> &CCArgs =
68*9880d681SAndroid Build Coastguard Worker                          std::vector<std::string>(),
69*9880d681SAndroid Build Coastguard Worker                      unsigned Timeout = 0,
70*9880d681SAndroid Build Coastguard Worker                      unsigned MemoryLimit = 0);
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker   /// MakeSharedObject - This compiles the specified file (which is either a .c
73*9880d681SAndroid Build Coastguard Worker   /// file or a .s file) into a shared object.
74*9880d681SAndroid Build Coastguard Worker   ///
75*9880d681SAndroid Build Coastguard Worker   int MakeSharedObject(const std::string &InputFile, FileType fileType,
76*9880d681SAndroid Build Coastguard Worker                        std::string &OutputFile,
77*9880d681SAndroid Build Coastguard Worker                        const std::vector<std::string> &ArgsForCC,
78*9880d681SAndroid Build Coastguard Worker                        std::string &Error);
79*9880d681SAndroid Build Coastguard Worker };
80*9880d681SAndroid Build Coastguard Worker 
81*9880d681SAndroid Build Coastguard Worker 
82*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
83*9880d681SAndroid Build Coastguard Worker /// AbstractInterpreter Class - Subclasses of this class are used to execute
84*9880d681SAndroid Build Coastguard Worker /// LLVM bitcode in a variety of ways.  This abstract interface hides this
85*9880d681SAndroid Build Coastguard Worker /// complexity behind a simple interface.
86*9880d681SAndroid Build Coastguard Worker ///
87*9880d681SAndroid Build Coastguard Worker class AbstractInterpreter {
88*9880d681SAndroid Build Coastguard Worker   virtual void anchor();
89*9880d681SAndroid Build Coastguard Worker public:
90*9880d681SAndroid Build Coastguard Worker   static LLC *createLLC(const char *Argv0, std::string &Message,
91*9880d681SAndroid Build Coastguard Worker                         const std::string              &CCBinary,
92*9880d681SAndroid Build Coastguard Worker                         const std::vector<std::string> *Args = nullptr,
93*9880d681SAndroid Build Coastguard Worker                         const std::vector<std::string> *CCArgs = nullptr,
94*9880d681SAndroid Build Coastguard Worker                         bool UseIntegratedAssembler = false);
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker   static AbstractInterpreter*
97*9880d681SAndroid Build Coastguard Worker   createLLI(const char *Argv0, std::string &Message,
98*9880d681SAndroid Build Coastguard Worker             const std::vector<std::string> *Args = nullptr);
99*9880d681SAndroid Build Coastguard Worker 
100*9880d681SAndroid Build Coastguard Worker   static AbstractInterpreter*
101*9880d681SAndroid Build Coastguard Worker   createJIT(const char *Argv0, std::string &Message,
102*9880d681SAndroid Build Coastguard Worker             const std::vector<std::string> *Args = nullptr);
103*9880d681SAndroid Build Coastguard Worker 
104*9880d681SAndroid Build Coastguard Worker   static AbstractInterpreter*
105*9880d681SAndroid Build Coastguard Worker   createCustomCompiler(std::string &Message,
106*9880d681SAndroid Build Coastguard Worker                        const std::string &CompileCommandLine);
107*9880d681SAndroid Build Coastguard Worker 
108*9880d681SAndroid Build Coastguard Worker   static AbstractInterpreter*
109*9880d681SAndroid Build Coastguard Worker   createCustomExecutor(std::string &Message,
110*9880d681SAndroid Build Coastguard Worker                        const std::string &ExecCommandLine);
111*9880d681SAndroid Build Coastguard Worker 
112*9880d681SAndroid Build Coastguard Worker 
~AbstractInterpreter()113*9880d681SAndroid Build Coastguard Worker   virtual ~AbstractInterpreter() {}
114*9880d681SAndroid Build Coastguard Worker 
115*9880d681SAndroid Build Coastguard Worker   /// compileProgram - Compile the specified program from bitcode to executable
116*9880d681SAndroid Build Coastguard Worker   /// code.  This does not produce any output, it is only used when debugging
117*9880d681SAndroid Build Coastguard Worker   /// the code generator.  It returns false if the code generator fails.
118*9880d681SAndroid Build Coastguard Worker   virtual void compileProgram(const std::string &Bitcode, std::string *Error,
119*9880d681SAndroid Build Coastguard Worker                               unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker   /// OutputCode - Compile the specified program from bitcode to code
122*9880d681SAndroid Build Coastguard Worker   /// understood by the CC driver (either C or asm).  If the code generator
123*9880d681SAndroid Build Coastguard Worker   /// fails, it sets Error, otherwise, this function returns the type of code
124*9880d681SAndroid Build Coastguard Worker   /// emitted.
125*9880d681SAndroid Build Coastguard Worker   virtual CC::FileType OutputCode(const std::string &Bitcode,
126*9880d681SAndroid Build Coastguard Worker                                    std::string &OutFile, std::string &Error,
127*9880d681SAndroid Build Coastguard Worker                                    unsigned Timeout = 0,
128*9880d681SAndroid Build Coastguard Worker                                    unsigned MemoryLimit = 0) {
129*9880d681SAndroid Build Coastguard Worker     Error = "OutputCode not supported by this AbstractInterpreter!";
130*9880d681SAndroid Build Coastguard Worker     return CC::AsmFile;
131*9880d681SAndroid Build Coastguard Worker   }
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker   /// ExecuteProgram - Run the specified bitcode file, emitting output to the
134*9880d681SAndroid Build Coastguard Worker   /// specified filename.  This sets RetVal to the exit code of the program or
135*9880d681SAndroid Build Coastguard Worker   /// returns false if a problem was encountered that prevented execution of
136*9880d681SAndroid Build Coastguard Worker   /// the program.
137*9880d681SAndroid Build Coastguard Worker   ///
138*9880d681SAndroid Build Coastguard Worker   virtual int ExecuteProgram(const std::string &Bitcode,
139*9880d681SAndroid Build Coastguard Worker                              const std::vector<std::string> &Args,
140*9880d681SAndroid Build Coastguard Worker                              const std::string &InputFile,
141*9880d681SAndroid Build Coastguard Worker                              const std::string &OutputFile,
142*9880d681SAndroid Build Coastguard Worker                              std::string *Error,
143*9880d681SAndroid Build Coastguard Worker                              const std::vector<std::string> &CCArgs =
144*9880d681SAndroid Build Coastguard Worker                                std::vector<std::string>(),
145*9880d681SAndroid Build Coastguard Worker                              const std::vector<std::string> &SharedLibs =
146*9880d681SAndroid Build Coastguard Worker                                std::vector<std::string>(),
147*9880d681SAndroid Build Coastguard Worker                              unsigned Timeout = 0,
148*9880d681SAndroid Build Coastguard Worker                              unsigned MemoryLimit = 0) = 0;
149*9880d681SAndroid Build Coastguard Worker };
150*9880d681SAndroid Build Coastguard Worker 
151*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
152*9880d681SAndroid Build Coastguard Worker // LLC Implementation of AbstractIntepreter interface
153*9880d681SAndroid Build Coastguard Worker //
154*9880d681SAndroid Build Coastguard Worker class LLC : public AbstractInterpreter {
155*9880d681SAndroid Build Coastguard Worker   std::string LLCPath;               // The path to the LLC executable.
156*9880d681SAndroid Build Coastguard Worker   std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
157*9880d681SAndroid Build Coastguard Worker   CC *cc;
158*9880d681SAndroid Build Coastguard Worker   bool UseIntegratedAssembler;
159*9880d681SAndroid Build Coastguard Worker public:
LLC(const std::string & llcPath,CC * cc,const std::vector<std::string> * Args,bool useIntegratedAssembler)160*9880d681SAndroid Build Coastguard Worker   LLC(const std::string &llcPath, CC *cc,
161*9880d681SAndroid Build Coastguard Worker       const std::vector<std::string> *Args,
162*9880d681SAndroid Build Coastguard Worker       bool useIntegratedAssembler)
163*9880d681SAndroid Build Coastguard Worker     : LLCPath(llcPath), cc(cc),
164*9880d681SAndroid Build Coastguard Worker       UseIntegratedAssembler(useIntegratedAssembler) {
165*9880d681SAndroid Build Coastguard Worker     ToolArgs.clear();
166*9880d681SAndroid Build Coastguard Worker     if (Args) ToolArgs = *Args;
167*9880d681SAndroid Build Coastguard Worker   }
~LLC()168*9880d681SAndroid Build Coastguard Worker   ~LLC() override { delete cc; }
169*9880d681SAndroid Build Coastguard Worker 
170*9880d681SAndroid Build Coastguard Worker   /// compileProgram - Compile the specified program from bitcode to executable
171*9880d681SAndroid Build Coastguard Worker   /// code.  This does not produce any output, it is only used when debugging
172*9880d681SAndroid Build Coastguard Worker   /// the code generator.  Returns false if the code generator fails.
173*9880d681SAndroid Build Coastguard Worker   void compileProgram(const std::string &Bitcode, std::string *Error,
174*9880d681SAndroid Build Coastguard Worker                       unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
175*9880d681SAndroid Build Coastguard Worker 
176*9880d681SAndroid Build Coastguard Worker   int ExecuteProgram(const std::string &Bitcode,
177*9880d681SAndroid Build Coastguard Worker                      const std::vector<std::string> &Args,
178*9880d681SAndroid Build Coastguard Worker                      const std::string &InputFile,
179*9880d681SAndroid Build Coastguard Worker                      const std::string &OutputFile,
180*9880d681SAndroid Build Coastguard Worker                      std::string *Error,
181*9880d681SAndroid Build Coastguard Worker                      const std::vector<std::string> &CCArgs =
182*9880d681SAndroid Build Coastguard Worker                        std::vector<std::string>(),
183*9880d681SAndroid Build Coastguard Worker                      const std::vector<std::string> &SharedLibs =
184*9880d681SAndroid Build Coastguard Worker                         std::vector<std::string>(),
185*9880d681SAndroid Build Coastguard Worker                      unsigned Timeout = 0,
186*9880d681SAndroid Build Coastguard Worker                      unsigned MemoryLimit = 0) override;
187*9880d681SAndroid Build Coastguard Worker 
188*9880d681SAndroid Build Coastguard Worker   /// OutputCode - Compile the specified program from bitcode to code
189*9880d681SAndroid Build Coastguard Worker   /// understood by the CC driver (either C or asm).  If the code generator
190*9880d681SAndroid Build Coastguard Worker   /// fails, it sets Error, otherwise, this function returns the type of code
191*9880d681SAndroid Build Coastguard Worker   /// emitted.
192*9880d681SAndroid Build Coastguard Worker   CC::FileType OutputCode(const std::string &Bitcode,
193*9880d681SAndroid Build Coastguard Worker                            std::string &OutFile, std::string &Error,
194*9880d681SAndroid Build Coastguard Worker                            unsigned Timeout = 0,
195*9880d681SAndroid Build Coastguard Worker                            unsigned MemoryLimit = 0) override;
196*9880d681SAndroid Build Coastguard Worker };
197*9880d681SAndroid Build Coastguard Worker 
198*9880d681SAndroid Build Coastguard Worker } // End llvm namespace
199*9880d681SAndroid Build Coastguard Worker 
200*9880d681SAndroid Build Coastguard Worker #endif
201