Home | History | Annotate | Download | only in bugpoint
      1 //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      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 
     17 #ifndef BUGPOINT_TOOLRUNNER_H
     18 #define BUGPOINT_TOOLRUNNER_H
     19 
     20 #include "llvm/ADT/Triple.h"
     21 #include "llvm/Support/CommandLine.h"
     22 #include "llvm/Support/ErrorHandling.h"
     23 #include "llvm/Support/SystemUtils.h"
     24 #include "llvm/Support/Path.h"
     25 #include <exception>
     26 #include <vector>
     27 
     28 namespace llvm {
     29 
     30 extern cl::opt<bool> SaveTemps;
     31 extern Triple TargetTriple;
     32 
     33 class CBE;
     34 class LLC;
     35 
     36 //===---------------------------------------------------------------------===//
     37 // GCC abstraction
     38 //
     39 class GCC {
     40   sys::Path GCCPath;                // The path to the gcc executable.
     41   sys::Path RemoteClientPath;       // The path to the rsh / ssh executable.
     42   std::vector<std::string> gccArgs; // GCC-specific arguments.
     43   GCC(const sys::Path &gccPath, const sys::Path &RemotePath,
     44       const std::vector<std::string> *GCCArgs)
     45     : GCCPath(gccPath), RemoteClientPath(RemotePath) {
     46     if (GCCArgs) gccArgs = *GCCArgs;
     47   }
     48 public:
     49   enum FileType { AsmFile, ObjectFile, CFile };
     50 
     51   static GCC *create(std::string &Message,
     52                      const std::string &GCCBinary,
     53                      const std::vector<std::string> *Args);
     54 
     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   ///
     62   int ExecuteProgram(const std::string &ProgramFile,
     63                      const std::vector<std::string> &Args,
     64                      FileType fileType,
     65                      const std::string &InputFile,
     66                      const std::string &OutputFile,
     67                      std::string *Error = 0,
     68                      const std::vector<std::string> &GCCArgs =
     69                          std::vector<std::string>(),
     70                      unsigned Timeout = 0,
     71                      unsigned MemoryLimit = 0);
     72 
     73   /// MakeSharedObject - This compiles the specified file (which is either a .c
     74   /// file or a .s file) into a shared object.
     75   ///
     76   int MakeSharedObject(const std::string &InputFile, FileType fileType,
     77                        std::string &OutputFile,
     78                        const std::vector<std::string> &ArgsForGCC,
     79                        std::string &Error);
     80 };
     81 
     82 
     83 //===---------------------------------------------------------------------===//
     84 /// AbstractInterpreter Class - Subclasses of this class are used to execute
     85 /// LLVM bitcode in a variety of ways.  This abstract interface hides this
     86 /// complexity behind a simple interface.
     87 ///
     88 class AbstractInterpreter {
     89   virtual void anchor();
     90 public:
     91   static CBE *createCBE(const char *Argv0, std::string &Message,
     92                         const std::string              &GCCBinary,
     93                         const std::vector<std::string> *Args = 0,
     94                         const std::vector<std::string> *GCCArgs = 0);
     95   static LLC *createLLC(const char *Argv0, std::string &Message,
     96                         const std::string              &GCCBinary,
     97                         const std::vector<std::string> *Args = 0,
     98                         const std::vector<std::string> *GCCArgs = 0,
     99                         bool UseIntegratedAssembler = false);
    100 
    101   static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
    102                                         const std::vector<std::string> *Args=0);
    103 
    104   static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
    105                                         const std::vector<std::string> *Args=0);
    106 
    107   static AbstractInterpreter*
    108   createCustomCompiler(std::string &Message,
    109                        const std::string &CompileCommandLine);
    110 
    111   static AbstractInterpreter*
    112   createCustomExecutor(std::string &Message,
    113                        const std::string &ExecCommandLine);
    114 
    115 
    116   virtual ~AbstractInterpreter() {}
    117 
    118   /// compileProgram - Compile the specified program from bitcode to executable
    119   /// code.  This does not produce any output, it is only used when debugging
    120   /// the code generator.  It returns false if the code generator fails.
    121   virtual void compileProgram(const std::string &Bitcode, std::string *Error,
    122                               unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
    123 
    124   /// OutputCode - Compile the specified program from bitcode to code
    125   /// understood by the GCC driver (either C or asm).  If the code generator
    126   /// fails, it sets Error, otherwise, this function returns the type of code
    127   /// emitted.
    128   virtual GCC::FileType OutputCode(const std::string &Bitcode,
    129                                    sys::Path &OutFile, std::string &Error,
    130                                    unsigned Timeout = 0,
    131                                    unsigned MemoryLimit = 0) {
    132     Error = "OutputCode not supported by this AbstractInterpreter!";
    133     return GCC::AsmFile;
    134   }
    135 
    136   /// ExecuteProgram - Run the specified bitcode file, emitting output to the
    137   /// specified filename.  This sets RetVal to the exit code of the program or
    138   /// returns false if a problem was encountered that prevented execution of
    139   /// the program.
    140   ///
    141   virtual int ExecuteProgram(const std::string &Bitcode,
    142                              const std::vector<std::string> &Args,
    143                              const std::string &InputFile,
    144                              const std::string &OutputFile,
    145                              std::string *Error,
    146                              const std::vector<std::string> &GCCArgs =
    147                                std::vector<std::string>(),
    148                              const std::vector<std::string> &SharedLibs =
    149                                std::vector<std::string>(),
    150                              unsigned Timeout = 0,
    151                              unsigned MemoryLimit = 0) = 0;
    152 };
    153 
    154 //===---------------------------------------------------------------------===//
    155 // CBE Implementation of AbstractIntepreter interface
    156 //
    157 class CBE : public AbstractInterpreter {
    158   sys::Path LLCPath;                 // The path to the `llc' executable.
    159   std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
    160   GCC *gcc;
    161 public:
    162   CBE(const sys::Path &llcPath, GCC *Gcc,
    163       const std::vector<std::string> *Args)
    164     : LLCPath(llcPath), gcc(Gcc) {
    165     ToolArgs.clear ();
    166     if (Args) ToolArgs = *Args;
    167   }
    168   ~CBE() { delete gcc; }
    169 
    170   /// compileProgram - Compile the specified program from bitcode to executable
    171   /// code.  This does not produce any output, it is only used when debugging
    172   /// the code generator.  Returns false if the code generator fails.
    173   virtual void compileProgram(const std::string &Bitcode, std::string *Error,
    174                               unsigned Timeout = 0, unsigned MemoryLimit = 0);
    175 
    176   virtual int ExecuteProgram(const std::string &Bitcode,
    177                              const std::vector<std::string> &Args,
    178                              const std::string &InputFile,
    179                              const std::string &OutputFile,
    180                              std::string *Error,
    181                              const std::vector<std::string> &GCCArgs =
    182                                std::vector<std::string>(),
    183                              const std::vector<std::string> &SharedLibs =
    184                                std::vector<std::string>(),
    185                              unsigned Timeout = 0,
    186                              unsigned MemoryLimit = 0);
    187 
    188   /// OutputCode - Compile the specified program from bitcode to code
    189   /// understood by the GCC driver (either C or asm).  If the code generator
    190   /// fails, it sets Error, otherwise, this function returns the type of code
    191   /// emitted.
    192   virtual GCC::FileType OutputCode(const std::string &Bitcode,
    193                                    sys::Path &OutFile, std::string &Error,
    194                                    unsigned Timeout = 0,
    195                                    unsigned MemoryLimit = 0);
    196 };
    197 
    198 
    199 //===---------------------------------------------------------------------===//
    200 // LLC Implementation of AbstractIntepreter interface
    201 //
    202 class LLC : public AbstractInterpreter {
    203   std::string LLCPath;               // The path to the LLC executable.
    204   std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
    205   GCC *gcc;
    206   bool UseIntegratedAssembler;
    207 public:
    208   LLC(const std::string &llcPath, GCC *Gcc,
    209       const std::vector<std::string> *Args,
    210       bool useIntegratedAssembler)
    211     : LLCPath(llcPath), gcc(Gcc),
    212       UseIntegratedAssembler(useIntegratedAssembler) {
    213     ToolArgs.clear();
    214     if (Args) ToolArgs = *Args;
    215   }
    216   ~LLC() { delete gcc; }
    217 
    218   /// compileProgram - Compile the specified program from bitcode to executable
    219   /// code.  This does not produce any output, it is only used when debugging
    220   /// the code generator.  Returns false if the code generator fails.
    221   virtual void compileProgram(const std::string &Bitcode, std::string *Error,
    222                               unsigned Timeout = 0, unsigned MemoryLimit = 0);
    223 
    224   virtual int ExecuteProgram(const std::string &Bitcode,
    225                              const std::vector<std::string> &Args,
    226                              const std::string &InputFile,
    227                              const std::string &OutputFile,
    228                              std::string *Error,
    229                              const std::vector<std::string> &GCCArgs =
    230                                std::vector<std::string>(),
    231                              const std::vector<std::string> &SharedLibs =
    232                                 std::vector<std::string>(),
    233                              unsigned Timeout = 0,
    234                              unsigned MemoryLimit = 0);
    235 
    236   /// OutputCode - Compile the specified program from bitcode to code
    237   /// understood by the GCC driver (either C or asm).  If the code generator
    238   /// fails, it sets Error, otherwise, this function returns the type of code
    239   /// emitted.
    240   virtual GCC::FileType OutputCode(const std::string &Bitcode,
    241                                    sys::Path &OutFile, std::string &Error,
    242                                    unsigned Timeout = 0,
    243                                    unsigned MemoryLimit = 0);
    244 };
    245 
    246 } // End llvm namespace
    247 
    248 #endif
    249