push bytecode decompressor out through APIs. Now the bytecode reader
[oota-llvm.git] / tools / llvmc / CompilerDriver.h
index da2a8f1a31717e48a52180ecadf6ee556a7e84fe..02ec0e9f524390d0860eae8b655b1b8ab20a2c45 100644 (file)
@@ -1,45 +1,53 @@
 //===- CompilerDriver.h - Compiler Driver -----------------------*- C++ -*-===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by Reid Spencer and is distributed under the 
+// This file was developed by Reid Spencer and is distributed under the
 // University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+//
 //===----------------------------------------------------------------------===//
 //
 // This file declares the CompilerDriver class which implements the bulk of the
 // LLVM Compiler Driver program (llvmc).
 //
-//===------------------------------------------------------------------------===
+//===----------------------------------------------------------------------===//
+
 #ifndef LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
 #define LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
 
-#include "llvm/System/Path.h"
 #include <string>
 #include <vector>
+#include "llvm/System/Program.h"
 
 namespace llvm {
   /// This class provides the high level interface to the LLVM Compiler Driver.
   /// The driver's purpose is to make it easier for compiler writers and users
   /// of LLVM to utilize the compiler toolkits and LLVM toolset by learning only
   /// the interface of one program (llvmc).
-  /// 
+  ///
   /// @see llvmc.cpp
   /// @brief The interface to the LLVM Compiler Driver.
   class CompilerDriver {
     /// @name Types
     /// @{
     public:
-      /// @brief A vector of strings, commonly used
+      /// @brief A vector of strings, used for argument lists
       typedef std::vector<std::string> StringVector;
 
+      /// @brief A vector of sys::Path, used for path lists
+      typedef std::vector<sys::Path> PathVector;
+
+      /// @brief A table of strings, indexed typically by Phases
+      typedef std::vector<StringVector> StringTable;
+
       /// @brief The phases of processing that llvmc understands
       enum Phases {
         PREPROCESSING, ///< Source language combining, filtering, substitution
         TRANSLATION,   ///< Translate source -> LLVM bytecode/assembly
-        OPTIMIZATION,  ///< Optimize translation result 
-        LINKING,       ///< Link bytecode and native code
+        OPTIMIZATION,  ///< Optimize translation result
         ASSEMBLY,      ///< Convert program to executable
+        LINKING,       ///< Link bytecode and native code
+        NUM_PHASES     ///< Always last!
       };
 
       /// @brief The levels of optimization llvmc understands
@@ -55,15 +63,14 @@ namespace llvm {
       /// @brief Action specific flags
       enum ConfigurationFlags {
         REQUIRED_FLAG        = 0x0001, ///< Should the action always be run?
-        GROKS_DASH_O_FLAG    = 0x0002, ///< Understands the -On options?
-        PREPROCESSES_FLAG    = 0x0004, ///< Does this action preprocess?
-        OPTIMIZES_FLAG       = 0x0008, ///< Does this action optimize?
-        GROKS_O10N_FLAG      = 0x0010, ///< Understands optimization options?
-        FLAGS_MASK           = 0x001F, ///< Union of all flags
+        PREPROCESSES_FLAG    = 0x0002, ///< Does this action preprocess?
+        TRANSLATES_FLAG      = 0x0004, ///< Does this action translate?
+        OUTPUT_IS_ASM_FLAG   = 0x0008, ///< Action produces .ll files?
+        FLAGS_MASK           = 0x000F  ///< Union of all flags
       };
 
       /// This type is the input list to the CompilerDriver. It provides
-      /// a vector of filename/filetype pairs. The filetype is used to look up
+      /// a vector of pathname/filetype pairs. The filetype is used to look up
       /// the configuration of the actions to be taken by the driver.
       /// @brief The Input Data to the execute method
       typedef std::vector<std::pair<sys::Path,std::string> > InputList;
@@ -74,160 +81,124 @@ namespace llvm {
       /// @brief A structure to hold the action data for a given source
       /// language.
       struct Action {
-        Action() : inputAt(0) , outputAt(0), flags(0) {}
-        sys::Path program;     ///< The program to execve
-        StringVector args;     ///< Arguments to the program
-        size_t inputAt;        ///< Argument index to insert input file
-        size_t outputAt;       ///< Argument index to insert output file
-        unsigned flags;        ///< Action specific flags
+        Action() : flags(0) {}
+        sys::Path program; ///< The program to execve
+        StringVector args; ///< Arguments to the program
+        unsigned flags;    ///< Action specific flags
         void set(unsigned fl ) { flags |= fl; }
         void clear(unsigned fl) { flags &= (FLAGS_MASK ^ fl); }
-        bool isSet(unsigned fl) { return flags&fl != 0; }
+        bool isSet(unsigned fl) { return (flags&fl) != 0; }
       };
 
       struct ConfigData {
-        std::string langName;           ///< The name of the source language 
-        std::vector<StringVector> opts; ///< The o10n options for each level
-        Action PreProcessor;            ///< PreProcessor command line
-        Action Translator;              ///< Translator command line
-        Action Optimizer;               ///< Optimizer command line
-        Action Assembler;               ///< Assembler command line
-        Action Linker;                  ///< Linker command line
+        ConfigData();
+        std::string version;    ///< The version number.
+        std::string langName;   ///< The name of the source language
+        StringTable opts;       ///< The o10n options for each level
+        StringVector libpaths;  ///< The library paths
+        Action PreProcessor;    ///< PreProcessor command line
+        Action Translator;      ///< Translator command line
+        Action Optimizer;       ///< Optimizer command line
+        Action Assembler;       ///< Assembler command line
+        Action Linker;          ///< Linker command line
       };
 
       /// This pure virtual interface class defines the interface between the
       /// CompilerDriver and other software that provides ConfigData objects to
       /// it. The CompilerDriver must be configured to use an object of this
-      /// type so it can obtain the configuration data. 
+      /// type so it can obtain the configuration data.
       /// @see setConfigDataProvider
       /// @brief Configuration Data Provider interface
       class ConfigDataProvider {
       public:
+        virtual ~ConfigDataProvider();
         virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
-        virtual void setConfigDir(const std::string& dirName) = 0;
+        virtual void setConfigDir(const sys::Path& dirName) = 0;
+      };
+
+      /// These flags control various actions of the compiler driver. They are
+      /// used by adding the needed flag values together and passing them to the
+      /// compiler driver's setDriverFlags method.
+      /// @see setDriverFlags
+      /// @brief Driver specific flags
+      enum DriverFlags {
+        DRY_RUN_FLAG         = 0x0001, ///< Do everything but execute actions
+        VERBOSE_FLAG         = 0x0002, ///< Print each action
+        DEBUG_FLAG           = 0x0004, ///< Print debug information
+        TIME_PASSES_FLAG     = 0x0008, ///< Time the passes as they execute
+        TIME_ACTIONS_FLAG    = 0x0010, ///< Time the actions as they execute
+        SHOW_STATS_FLAG      = 0x0020, ///< Show pass statistics
+        EMIT_NATIVE_FLAG     = 0x0040, ///< Emit native code instead of bc
+        EMIT_RAW_FLAG        = 0x0080, ///< Emit raw, unoptimized bytecode
+        KEEP_TEMPS_FLAG      = 0x0100, ///< Don't delete temporary files
+        STRIP_OUTPUT_FLAG    = 0x0200, ///< Strip symbols from linked output
+        DRIVER_FLAGS_MASK    = 0x03FF  ///< Union of the above flags
       };
 
     /// @}
     /// @name Constructors
     /// @{
     public:
-      CompilerDriver(ConfigDataProvider& cdp );
+      /// @brief Static Constructor
+      static CompilerDriver* Get(ConfigDataProvider& CDP);
+
+      /// @brief Virtual destructor
       virtual ~CompilerDriver();
 
     /// @}
     /// @name Methods
     /// @{
     public:
-      /// @brief Handle an error
-      virtual void error(const std::string& errmsg);
-
       /// @brief Execute the actions requested for the given input list.
-      virtual int execute(const InputList& list, const sys::Path& output);
+      virtual int execute(
+        const InputList& list, const sys::Path& output, std::string& ErrMsg) =0;
 
-    /// @}
-    /// @name Mutators
-    /// @{
-    public:
       /// @brief Set the final phase at which compilation terminates
-      void setFinalPhase( Phases phase ) { finalPhase = phase; }
+      virtual void setFinalPhase(Phases phase) = 0;
 
       /// @brief Set the optimization level for the compilation
-      void setOptimization( OptimizationLevels level ) { optLevel = level; }
+      virtual void setOptimization(OptimizationLevels level) = 0;
 
-      /// @brief Prevent the CompilerDriver from taking any actions
-      void setDryRun( bool TF ) { isDryRun = TF; }
+      /// @brief Set the driver flags.
+      virtual void setDriverFlags(unsigned flags) = 0;
 
-      /// @brief Cause the CompilerDriver to print to stderr all the
-      /// actions it is taking.
-      void setVerbose( bool TF ) { isVerbose = TF; }
+      /// @brief Set the output machine name.
+      virtual void setOutputMachine(const std::string& machineName) = 0;
 
-      /// @brief Cause the CompilerDriver to print to stderr very verbose
-      /// information that might be useful in debugging the driver's actions
-      void setDebug( bool TF ) { isDebug = TF; }
+      /// @brief Set the options for a given phase.
+      virtual void setPhaseArgs(Phases phase, const StringVector& opts) = 0;
 
-      /// @brief Cause the CompilerDriver to print to stderr the 
-      /// execution time of each action taken.
-      void setTimeActions( bool TF ) { timeActions = TF; }
+      /// @brief Set Library Paths
+      virtual void setIncludePaths(const StringVector& paths) = 0;
 
-      /// @brief Indicate that native code is to be generated instead
-      /// of LLVM bytecode.
-      void setEmitNativeCode( bool TF ) { emitNativeCode = TF; }
+      /// @brief Set Library Paths
+      virtual void setSymbolDefines(const StringVector& paths) = 0;
 
-      /// @brief Indicate that raw, unoptimized code is to be generated.
-      void setEmitRawCode(bool TF ) { emitRawCode = TF; }
+      /// @brief Set Library Paths
+      virtual void setLibraryPaths(const StringVector& paths) = 0;
 
-      /// @brief Set the output machine name.
-      void setOutputMachine( const std::string& machineName ) {
-        machine = machineName;
-      }
-
-      /// @brief Set Preprocessor specific options
-      void setPreprocessorOptions(const std::vector<std::string>& opts) {
-        PreprocessorOptions = opts;
-      }
-
-      /// @brief Set Translator specific options
-      void setTranslatorOptions(const std::vector<std::string>& opts) {
-        TranslatorOptions = opts;
-      }
-
-      /// @brief Set Optimizer specific options
-      void setOptimizerOptions(const std::vector<std::string>& opts) {
-        OptimizerOptions = opts;
-      }
-
-      /// @brief Set Assembler specific options
-      void setAssemblerOptions(const std::vector<std::string>& opts) {
-        AssemblerOptions = opts;
-      }
-
-      /// @brief Set Linker specific options
-      void setLinkerOptions(const std::vector<std::string>& opts) {
-        LinkerOptions = opts;
-      }
+      /// @brief Add a path to the list of library paths
+      virtual void addLibraryPath( const sys::Path& libPath )  = 0;
 
-      /// @brief Set Library Paths
-      void setLibraryPaths(const std::vector<std::string>& paths) {
-        LibraryPaths = paths;
-      }
+      /// @brief Add a path to the list of paths in which to find tools
+      virtual void addToolPath( const sys::Path& toolPath) = 0;
 
-      /// @brief Set the list of library paths to be searched for
-      /// libraries.
-      void addLibraryPath( const std::string& libPath ) {
-        LibraryPaths.push_back(libPath);
-      }
+      /// @brief Set the list of -f options to be passed through
+      virtual void setfPassThrough(const StringVector& fOpts) = 0;
 
-    /// @}
-    /// @name Functions
-    /// @{
-    private:
-      Action* GetAction(ConfigData* cd, const std::string& input, 
-                       const std::string& output, Phases phase );
-      void DoAction(Action* a);
+      /// @brief Set the list of -M options to be passed through
+      virtual void setMPassThrough(const StringVector& fOpts) = 0;
 
-    /// @}
-    /// @name Data
-    /// @{
-    private:
-      ConfigDataProvider* cdp;      ///< Where we get configuration data from
-      Phases finalPhase;            ///< The final phase of compilation
-      OptimizationLevels optLevel;  ///< The optimization level to apply
-      bool isDryRun;                ///< Prevent actions ?
-      bool isVerbose;               ///< Print actions?
-      bool isDebug;                 ///< Print lotsa debug info?
-      bool timeActions;             ///< Time the actions executed ?
-      bool emitRawCode;             ///< Emit Raw (unoptimized) code?
-      bool emitNativeCode;          ///< Emit native code instead of bytecode?
-      std::string machine;          ///< Target machine name
-      std::vector<std::string> LibraryPaths;
-      std::vector<std::string> PreprocessorOptions; 
-      std::vector<std::string> TranslatorOptions;
-      std::vector<std::string> OptimizerOptions;
-      std::vector<std::string> AssemblerOptions;
-      std::vector<std::string> LinkerOptions;
+      /// @brief Set the list of -W options to be passed through
+      virtual void setWPassThrough(const StringVector& fOpts) = 0;
 
-    /// @}
+      /// @brief Determine where a linkage file is located in the file system
+      virtual sys::Path GetPathForLinkageItem(
+        const std::string& link_item, ///< Item to be sought
+        bool native = false           ///< Looking for native?
+      ) = 0;
 
+    /// @}
   };
 }