Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame^] | 1 | //===- CompilerDriver.h - Compiler Driver -----------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the CompilerDriver class which implements the bulk of the |
| 11 | // LLVM Compiler Driver program (llvmc). |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_TOOLS_LLVMC_COMPILERDRIVER_H |
| 16 | #define LLVM_TOOLS_LLVMC_COMPILERDRIVER_H |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | #include "llvm/System/Program.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | /// This class provides the high level interface to the LLVM Compiler Driver. |
| 24 | /// The driver's purpose is to make it easier for compiler writers and users |
| 25 | /// of LLVM to utilize the compiler toolkits and LLVM toolset by learning only |
| 26 | /// the interface of one program (llvmc). |
| 27 | /// |
| 28 | /// @see llvmc.cpp |
| 29 | /// @brief The interface to the LLVM Compiler Driver. |
| 30 | class CompilerDriver { |
| 31 | /// @name Types |
| 32 | /// @{ |
| 33 | public: |
| 34 | /// @brief A vector of strings, used for argument lists |
| 35 | typedef std::vector<std::string> StringVector; |
| 36 | |
| 37 | /// @brief A vector of sys::Path, used for path lists |
| 38 | typedef std::vector<sys::Path> PathVector; |
| 39 | |
| 40 | /// @brief A table of strings, indexed typically by Phases |
| 41 | typedef std::vector<StringVector> StringTable; |
| 42 | |
| 43 | /// @brief The phases of processing that llvmc understands |
| 44 | enum Phases { |
| 45 | PREPROCESSING, ///< Source language combining, filtering, substitution |
| 46 | TRANSLATION, ///< Translate source -> LLVM bitcode/assembly |
| 47 | OPTIMIZATION, ///< Optimize translation result |
| 48 | ASSEMBLY, ///< Convert program to executable |
| 49 | LINKING, ///< Link bitcode and native code |
| 50 | NUM_PHASES ///< Always last! |
| 51 | }; |
| 52 | |
| 53 | /// @brief The levels of optimization llvmc understands |
| 54 | enum OptimizationLevels { |
| 55 | OPT_FAST_COMPILE, ///< Optimize to make >compile< go faster |
| 56 | OPT_SIMPLE, ///< Standard/simple optimizations |
| 57 | OPT_AGGRESSIVE, ///< Aggressive optimizations |
| 58 | OPT_LINK_TIME, ///< Aggressive + LinkTime optimizations |
| 59 | OPT_AGGRESSIVE_LINK_TIME, ///< Make it go way fast! |
| 60 | OPT_NONE ///< No optimizations. Keep this at the end! |
| 61 | }; |
| 62 | |
| 63 | /// @brief Action specific flags |
| 64 | enum ConfigurationFlags { |
| 65 | REQUIRED_FLAG = 0x0001, ///< Should the action always be run? |
| 66 | PREPROCESSES_FLAG = 0x0002, ///< Does this action preprocess? |
| 67 | TRANSLATES_FLAG = 0x0004, ///< Does this action translate? |
| 68 | OUTPUT_IS_ASM_FLAG = 0x0008, ///< Action produces .ll files? |
| 69 | FLAGS_MASK = 0x000F ///< Union of all flags |
| 70 | }; |
| 71 | |
| 72 | /// This type is the input list to the CompilerDriver. It provides |
| 73 | /// a vector of pathname/filetype pairs. The filetype is used to look up |
| 74 | /// the configuration of the actions to be taken by the driver. |
| 75 | /// @brief The Input Data to the execute method |
| 76 | typedef std::vector<std::pair<sys::Path,std::string> > InputList; |
| 77 | |
| 78 | /// This type is read from configuration files or otherwise provided to |
| 79 | /// the CompilerDriver through a "ConfigDataProvider". It serves as both |
| 80 | /// the template of what to do and the actual Action to be executed. |
| 81 | /// @brief A structure to hold the action data for a given source |
| 82 | /// language. |
| 83 | struct Action { |
| 84 | Action() : flags(0) {} |
| 85 | sys::Path program; ///< The program to execve |
| 86 | StringVector args; ///< Arguments to the program |
| 87 | unsigned flags; ///< Action specific flags |
| 88 | void set(unsigned fl ) { flags |= fl; } |
| 89 | void clear(unsigned fl) { flags &= (FLAGS_MASK ^ fl); } |
| 90 | bool isSet(unsigned fl) { return (flags&fl) != 0; } |
| 91 | }; |
| 92 | |
| 93 | struct ConfigData { |
| 94 | ConfigData(); |
| 95 | std::string version; ///< The version number. |
| 96 | std::string langName; ///< The name of the source language |
| 97 | StringTable opts; ///< The o10n options for each level |
| 98 | StringVector libpaths; ///< The library paths |
| 99 | Action PreProcessor; ///< PreProcessor command line |
| 100 | Action Translator; ///< Translator command line |
| 101 | Action Optimizer; ///< Optimizer command line |
| 102 | Action Assembler; ///< Assembler command line |
| 103 | Action Linker; ///< Linker command line |
| 104 | }; |
| 105 | |
| 106 | /// This pure virtual interface class defines the interface between the |
| 107 | /// CompilerDriver and other software that provides ConfigData objects to |
| 108 | /// it. The CompilerDriver must be configured to use an object of this |
| 109 | /// type so it can obtain the configuration data. |
| 110 | /// @see setConfigDataProvider |
| 111 | /// @brief Configuration Data Provider interface |
| 112 | class ConfigDataProvider { |
| 113 | public: |
| 114 | virtual ~ConfigDataProvider(); |
| 115 | virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0; |
| 116 | virtual void setConfigDir(const sys::Path& dirName) = 0; |
| 117 | }; |
| 118 | |
| 119 | /// These flags control various actions of the compiler driver. They are |
| 120 | /// used by adding the needed flag values together and passing them to the |
| 121 | /// compiler driver's setDriverFlags method. |
| 122 | /// @see setDriverFlags |
| 123 | /// @brief Driver specific flags |
| 124 | enum DriverFlags { |
| 125 | DRY_RUN_FLAG = 0x0001, ///< Do everything but execute actions |
| 126 | VERBOSE_FLAG = 0x0002, ///< Print each action |
| 127 | DEBUG_FLAG = 0x0004, ///< Print debug information |
| 128 | TIME_PASSES_FLAG = 0x0008, ///< Time the passes as they execute |
| 129 | TIME_ACTIONS_FLAG = 0x0010, ///< Time the actions as they execute |
| 130 | SHOW_STATS_FLAG = 0x0020, ///< Show pass statistics |
| 131 | EMIT_NATIVE_FLAG = 0x0040, ///< Emit native code instead of bc |
| 132 | EMIT_RAW_FLAG = 0x0080, ///< Emit raw, unoptimized bitcode |
| 133 | KEEP_TEMPS_FLAG = 0x0100, ///< Don't delete temporary files |
| 134 | STRIP_OUTPUT_FLAG = 0x0200, ///< Strip symbols from linked output |
| 135 | DRIVER_FLAGS_MASK = 0x03FF ///< Union of the above flags |
| 136 | }; |
| 137 | |
| 138 | /// @} |
| 139 | /// @name Constructors |
| 140 | /// @{ |
| 141 | public: |
| 142 | /// @brief Static Constructor |
| 143 | static CompilerDriver* Get(ConfigDataProvider& CDP); |
| 144 | |
| 145 | /// @brief Virtual destructor |
| 146 | virtual ~CompilerDriver(); |
| 147 | |
| 148 | /// @} |
| 149 | /// @name Methods |
| 150 | /// @{ |
| 151 | public: |
| 152 | /// @brief Execute the actions requested for the given input list. |
| 153 | virtual int execute( |
| 154 | const InputList& list, const sys::Path& output, std::string& ErrMsg) =0; |
| 155 | |
| 156 | /// @brief Set the final phase at which compilation terminates |
| 157 | virtual void setFinalPhase(Phases phase) = 0; |
| 158 | |
| 159 | /// @brief Set the optimization level for the compilation |
| 160 | virtual void setOptimization(OptimizationLevels level) = 0; |
| 161 | |
| 162 | /// @brief Set the driver flags. |
| 163 | virtual void setDriverFlags(unsigned flags) = 0; |
| 164 | |
| 165 | /// @brief Set the output machine name. |
| 166 | virtual void setOutputMachine(const std::string& machineName) = 0; |
| 167 | |
| 168 | /// @brief Set the options for a given phase. |
| 169 | virtual void setPhaseArgs(Phases phase, const StringVector& opts) = 0; |
| 170 | |
| 171 | /// @brief Set Library Paths |
| 172 | virtual void setIncludePaths(const StringVector& paths) = 0; |
| 173 | |
| 174 | /// @brief Set Library Paths |
| 175 | virtual void setSymbolDefines(const StringVector& paths) = 0; |
| 176 | |
| 177 | /// @brief Set Library Paths |
| 178 | virtual void setLibraryPaths(const StringVector& paths) = 0; |
| 179 | |
| 180 | /// @brief Add a path to the list of library paths |
| 181 | virtual void addLibraryPath( const sys::Path& libPath ) = 0; |
| 182 | |
| 183 | /// @brief Add a path to the list of paths in which to find tools |
| 184 | virtual void addToolPath( const sys::Path& toolPath) = 0; |
| 185 | |
| 186 | /// @brief Set the list of -f options to be passed through |
| 187 | virtual void setfPassThrough(const StringVector& fOpts) = 0; |
| 188 | |
| 189 | /// @brief Set the list of -M options to be passed through |
| 190 | virtual void setMPassThrough(const StringVector& fOpts) = 0; |
| 191 | |
| 192 | /// @brief Set the list of -W options to be passed through |
| 193 | virtual void setWPassThrough(const StringVector& fOpts) = 0; |
| 194 | |
| 195 | /// @brief Determine where a linkage file is located in the file system |
| 196 | virtual sys::Path GetPathForLinkageItem( |
| 197 | const std::string& link_item, ///< Item to be sought |
| 198 | bool native = false ///< Looking for native? |
| 199 | ) = 0; |
| 200 | |
| 201 | /// @} |
| 202 | }; |
| 203 | } |
| 204 | |
| 205 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |
| 206 | #endif |