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/Path.h"
     24 #include "llvm/Support/SystemUtils.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 LLC;
     34 
     35 //===---------------------------------------------------------------------===//
     36 // GCC abstraction
     37 //
     38 class GCC {
     39   std::string GCCPath;                // The path to the gcc executable.
     40   std::string RemoteClientPath;       // The path to the rsh / ssh executable.
     41   std::vector<std::string> gccArgs; // GCC-specific arguments.
     42   GCC(StringRef gccPath, StringRef RemotePath,
     43       const std::vector<std::string> *GCCArgs)
     44     : GCCPath(gccPath), RemoteClientPath(RemotePath) {
     45     if (GCCArgs) gccArgs = *GCCArgs;
     46   }
     47 public:
     48   enum FileType { AsmFile, ObjectFile, CFile };
     49 
     50   static GCC *create(std::string &Message,
     51                      const std::string &GCCBinary,
     52                      const std::vector<std::string> *Args);
     53 
     54   /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
     55   /// either a .s file, or a .c file, specified by FileType), with the specified
     56   /// arguments.  Standard input is specified with InputFile, and standard
     57   /// Output is captured to the specified OutputFile location.  The SharedLibs
     58   /// option specifies optional native shared objects that can be loaded into
     59   /// the program for execution.
     60   ///
     61   int ExecuteProgram(const std::string &ProgramFile,
     62                      const std::vector<std::string> &Args,
     63                      FileType fileType,
     64                      const std::string &InputFile,
     65                      const std::string &OutputFile,
     66                      std::string *Error = 0,
     67                      const std::vector<std::string> &GCCArgs =
     68                          std::vector<std::string>(),
     69                      unsigned Timeout = 0,
     70                      unsigned MemoryLimit = 0);
     71 
     72   /// MakeSharedObject - This compiles the specified file (which is either a .c
     73   /// file or a .s file) into a shared object.
     74   ///
     75   int MakeSharedObject(const std::string &InputFile, FileType fileType,
     76                        std::string &OutputFile,
     77                        const std::vector<std::string> &ArgsForGCC,
     78                        std::string &Error);
     79 };
     80 
     81 
     82 //===---------------------------------------------------------------------===//
     83 /// AbstractInterpreter Class - Subclasses of this class are used to execute
     84 /// LLVM bitcode in a variety of ways.  This abstract interface hides this
     85 /// complexity behind a simple interface.
     86 ///
     87 class AbstractInterpreter {
     88   virtual void anchor();
     89 public:
     90   static LLC *createLLC(const char *Argv0, std::string &Message,
     91                         const std::string              &GCCBinary,
     92                         const std::vector<std::string> *Args = 0,
     93                         const std::vector<std::string> *GCCArgs = 0,
     94                         bool UseIntegratedAssembler = false);
     95 
     96   static AbstractInterpreter* createLLI(const char *Argv0, std::string &Message,
     97                                         const std::vector<std::string> *Args=0);
     98 
     99   static AbstractInterpreter* createJIT(const char *Argv0, std::string &Message,
    100                                         const std::vector<std::string> *Args=0);
    101 
    102   static AbstractInterpreter*
    103   createCustomCompiler(std::string &Message,
    104                        const std::string &CompileCommandLine);
    105 
    106   static AbstractInterpreter*
    107   createCustomExecutor(std::string &Message,
    108                        const std::string &ExecCommandLine);
    109 
    110 
    111   virtual ~AbstractInterpreter() {}
    112 
    113   /// compileProgram - Compile the specified program from bitcode to executable
    114   /// code.  This does not produce any output, it is only used when debugging
    115   /// the code generator.  It returns false if the code generator fails.
    116   virtual void compileProgram(const std::string &Bitcode, std::string *Error,
    117                               unsigned Timeout = 0, unsigned MemoryLimit = 0) {}
    118 
    119   /// OutputCode - Compile the specified program from bitcode to code
    120   /// understood by the GCC driver (either C or asm).  If the code generator
    121   /// fails, it sets Error, otherwise, this function returns the type of code
    122   /// emitted.
    123   virtual GCC::FileType OutputCode(const std::string &Bitcode,
    124                                    std::string &OutFile, std::string &Error,
    125                                    unsigned Timeout = 0,
    126                                    unsigned MemoryLimit = 0) {
    127     Error = "OutputCode not supported by this AbstractInterpreter!";
    128     return GCC::AsmFile;
    129   }
    130 
    131   /// ExecuteProgram - Run the specified bitcode file, emitting output to the
    132   /// specified filename.  This sets RetVal to the exit code of the program or
    133   /// returns false if a problem was encountered that prevented execution of
    134   /// the program.
    135   ///
    136   virtual int ExecuteProgram(const std::string &Bitcode,
    137                              const std::vector<std::string> &Args,
    138                              const std::string &InputFile,
    139                              const std::string &OutputFile,
    140                              std::string *Error,
    141                              const std::vector<std::string> &GCCArgs =
    142                                std::vector<std::string>(),
    143                              const std::vector<std::string> &SharedLibs =
    144                                std::vector<std::string>(),
    145                              unsigned Timeout = 0,
    146                              unsigned MemoryLimit = 0) = 0;
    147 };
    148 
    149 //===---------------------------------------------------------------------===//
    150 // LLC Implementation of AbstractIntepreter interface
    151 //
    152 class LLC : public AbstractInterpreter {
    153   std::string LLCPath;               // The path to the LLC executable.
    154   std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
    155   GCC *gcc;
    156   bool UseIntegratedAssembler;
    157 public:
    158   LLC(const std::string &llcPath, GCC *Gcc,
    159       const std::vector<std::string> *Args,
    160       bool useIntegratedAssembler)
    161     : LLCPath(llcPath), gcc(Gcc),
    162       UseIntegratedAssembler(useIntegratedAssembler) {
    163     ToolArgs.clear();
    164     if (Args) ToolArgs = *Args;
    165   }
    166   ~LLC() { delete gcc; }
    167 
    168   /// compileProgram - Compile the specified program from bitcode to executable
    169   /// code.  This does not produce any output, it is only used when debugging
    170   /// the code generator.  Returns false if the code generator fails.
    171   virtual void compileProgram(const std::string &Bitcode, std::string *Error,
    172                               unsigned Timeout = 0, unsigned MemoryLimit = 0);
    173 
    174   virtual int ExecuteProgram(const std::string &Bitcode,
    175                              const std::vector<std::string> &Args,
    176                              const std::string &InputFile,
    177                              const std::string &OutputFile,
    178                              std::string *Error,
    179                              const std::vector<std::string> &GCCArgs =
    180                                std::vector<std::string>(),
    181                              const std::vector<std::string> &SharedLibs =
    182                                 std::vector<std::string>(),
    183                              unsigned Timeout = 0,
    184                              unsigned MemoryLimit = 0);
    185 
    186   /// OutputCode - Compile the specified program from bitcode to code
    187   /// understood by the GCC driver (either C or asm).  If the code generator
    188   /// fails, it sets Error, otherwise, this function returns the type of code
    189   /// emitted.
    190   virtual GCC::FileType OutputCode(const std::string &Bitcode,
    191                                    std::string &OutFile, std::string &Error,
    192                                    unsigned Timeout = 0,
    193                                    unsigned MemoryLimit = 0);
    194 };
    195 
    196 } // End llvm namespace
    197 
    198 #endif
    199